Files
grungnet/src/components/map/MapControls.js

183 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-05-08 15:09:40 +10:00
import React, { useState, Fragment } from "react";
import { IconButton, Flex, Box } from "theme-ui";
import RadioIconButton from "./controls/RadioIconButton";
2020-05-26 14:47:37 +10:00
import Divider from "../Divider";
import SelectMapButton from "./SelectMapButton";
import FogToolSettings from "./controls/FogToolSettings";
2020-06-21 11:01:03 +10:00
import DrawingToolSettings from "./controls/DrawingToolSettings";
import PanToolIcon from "../../icons/PanToolIcon";
import FogToolIcon from "../../icons/FogToolIcon";
import BrushToolIcon from "../../icons/BrushToolIcon";
import ExpandMoreIcon from "../../icons/ExpandMoreIcon";
function MapContols({
2020-04-19 00:24:06 +10:00
onMapChange,
onMapStateChange,
currentMap,
currentMapState,
selectedToolId,
onSelectedToolChange,
toolSettings,
onToolSettingChange,
2020-04-29 20:55:52 +10:00
onToolAction,
disabledControls,
disabledSettings,
2020-04-19 00:24:06 +10:00
}) {
const [isExpanded, setIsExpanded] = useState(false);
const toolsById = {
pan: {
id: "pan",
icon: <PanToolIcon />,
title: "Pan Tool",
},
fog: {
id: "fog",
icon: <FogToolIcon />,
title: "Fog Tool",
SettingsComponent: FogToolSettings,
},
2020-06-21 11:01:03 +10:00
drawing: {
id: "drawing",
icon: <BrushToolIcon />,
title: "Drawing Tool",
2020-06-21 11:01:03 +10:00
SettingsComponent: DrawingToolSettings,
},
};
2020-06-21 11:01:03 +10:00
const tools = ["pan", "fog", "drawing"];
const sections = [
{
2020-04-26 14:34:27 +10:00
id: "map",
component: (
<SelectMapButton
onMapChange={onMapChange}
onMapStateChange={onMapStateChange}
currentMap={currentMap}
currentMapState={currentMapState}
2020-04-26 14:34:27 +10:00
/>
),
},
{
2020-04-26 14:34:27 +10:00
id: "drawing",
component: tools.map((tool) => (
<RadioIconButton
key={tool}
title={toolsById[tool].title}
onClick={() => onSelectedToolChange(tool)}
isSelected={selectedToolId === tool}
disabled={disabledControls.includes(tool)}
>
{toolsById[tool].icon}
</RadioIconButton>
)),
},
];
2020-04-26 14:34:27 +10:00
let controls = null;
if (sections.length === 1 && sections[0].id === "map") {
controls = (
<Box
sx={{
display: "block",
backgroundColor: "overlay",
borderRadius: "4px",
}}
m={2}
>
{sections[0].component}
</Box>
);
} else if (sections.length > 0) {
controls = (
<>
<IconButton
aria-label={isExpanded ? "Hide Map Controls" : "Show Map Controls"}
title={isExpanded ? "Hide Map Controls" : "Show Map Controls"}
onClick={() => setIsExpanded(!isExpanded)}
sx={{
transform: `rotate(${isExpanded ? "0" : "180deg"})`,
display: "block",
backgroundColor: "overlay",
borderRadius: "50%",
}}
m={2}
>
<ExpandMoreIcon />
</IconButton>
<Box
sx={{
flexDirection: "column",
alignItems: "center",
display: isExpanded ? "flex" : "none",
backgroundColor: "overlay",
borderRadius: "4px",
}}
p={2}
>
{sections.map((section, index) => (
2020-04-26 18:33:28 +10:00
<Fragment key={section.id}>
2020-04-26 14:34:27 +10:00
{section.component}
{index !== sections.length - 1 && <Divider />}
2020-04-26 18:33:28 +10:00
</Fragment>
2020-04-26 14:34:27 +10:00
))}
</Box>
2020-04-26 14:34:27 +10:00
</>
);
}
function getToolSettings() {
const Settings = toolsById[selectedToolId].SettingsComponent;
if (Settings) {
return (
<Box
sx={{
position: "absolute",
top: "4px",
left: "50%",
transform: "translateX(-50%)",
backgroundColor: "overlay",
borderRadius: "4px",
}}
p={1}
>
<Settings
settings={toolSettings[selectedToolId]}
onSettingChange={(change) =>
onToolSettingChange(selectedToolId, change)
}
2020-04-29 20:55:52 +10:00
onToolAction={onToolAction}
disabledActions={disabledSettings[selectedToolId]}
/>
</Box>
);
} else {
return null;
}
}
2020-04-26 14:34:27 +10:00
return (
<>
<Flex
sx={{
position: "absolute",
top: 0,
right: 0,
flexDirection: "column",
alignItems: "center",
}}
mx={1}
>
{controls}
</Flex>
{getToolSettings()}
</>
);
}
export default MapContols;