2020-04-28 22:06:20 +10:00
|
|
|
import React, { useState, Fragment, useEffect, useRef } from "react";
|
2020-04-27 17:29:46 +10:00
|
|
|
import { IconButton, Flex, Box } from "theme-ui";
|
|
|
|
|
|
|
|
|
|
import RadioIconButton from "./controls/RadioIconButton";
|
|
|
|
|
import Divider from "./controls/Divider";
|
2020-04-18 23:31:40 +10:00
|
|
|
|
2020-04-23 21:54:58 +10:00
|
|
|
import SelectMapButton from "./SelectMapButton";
|
2020-04-27 17:29:46 +10:00
|
|
|
|
|
|
|
|
import FogToolSettings from "./controls/FogToolSettings";
|
|
|
|
|
import BrushToolSettings from "./controls/BrushToolSettings";
|
|
|
|
|
import ShapeToolSettings from "./controls/ShapeToolSettings";
|
|
|
|
|
import EraseToolSettings from "./controls/EraseToolSettings";
|
|
|
|
|
|
2020-04-23 10:09:12 +10:00
|
|
|
import PanToolIcon from "../../icons/PanToolIcon";
|
2020-04-27 17:29:46 +10:00
|
|
|
import FogToolIcon from "../../icons/FogToolIcon";
|
2020-04-23 10:09:12 +10:00
|
|
|
import BrushToolIcon from "../../icons/BrushToolIcon";
|
2020-04-27 17:29:46 +10:00
|
|
|
import ShapeToolIcon from "../../icons/ShapeToolIcon";
|
2020-04-23 10:09:12 +10:00
|
|
|
import EraseToolIcon from "../../icons/EraseToolIcon";
|
|
|
|
|
import UndoIcon from "../../icons/UndoIcon";
|
|
|
|
|
import RedoIcon from "../../icons/RedoIcon";
|
2020-04-27 17:29:46 +10:00
|
|
|
import ExpandMoreIcon from "../../icons/ExpandMoreIcon";
|
2020-04-20 11:56:56 +10:00
|
|
|
|
2020-04-27 17:29:46 +10:00
|
|
|
function MapContols({
|
2020-04-19 00:24:06 +10:00
|
|
|
onMapChange,
|
2020-04-23 21:54:58 +10:00
|
|
|
onMapStateChange,
|
|
|
|
|
currentMap,
|
2020-04-27 17:29:46 +10:00
|
|
|
selectedToolId,
|
|
|
|
|
onSelectedToolChange,
|
|
|
|
|
toolSettings,
|
|
|
|
|
onToolSettingChange,
|
2020-04-29 20:55:52 +10:00
|
|
|
onToolAction,
|
2020-04-27 17:29:46 +10:00
|
|
|
disabledControls,
|
2020-04-19 00:24:06 +10:00
|
|
|
onUndo,
|
|
|
|
|
onRedo,
|
|
|
|
|
}) {
|
2020-04-19 13:33:31 +10:00
|
|
|
const [isExpanded, setIsExpanded] = useState(false);
|
|
|
|
|
|
2020-04-27 17:29:46 +10:00
|
|
|
const toolsById = {
|
|
|
|
|
pan: {
|
|
|
|
|
id: "pan",
|
|
|
|
|
icon: <PanToolIcon />,
|
|
|
|
|
title: "Pan Tool",
|
|
|
|
|
},
|
|
|
|
|
fog: {
|
|
|
|
|
id: "fog",
|
|
|
|
|
icon: <FogToolIcon />,
|
|
|
|
|
title: "Fog Tool",
|
|
|
|
|
SettingsComponent: FogToolSettings,
|
|
|
|
|
},
|
|
|
|
|
brush: {
|
|
|
|
|
id: "brush",
|
|
|
|
|
icon: <BrushToolIcon />,
|
|
|
|
|
title: "Brush Tool",
|
|
|
|
|
SettingsComponent: BrushToolSettings,
|
|
|
|
|
},
|
|
|
|
|
shape: {
|
|
|
|
|
id: "shape",
|
|
|
|
|
icon: <ShapeToolIcon />,
|
|
|
|
|
title: "Shape Tool",
|
|
|
|
|
SettingsComponent: ShapeToolSettings,
|
|
|
|
|
},
|
|
|
|
|
erase: {
|
|
|
|
|
id: "erase",
|
|
|
|
|
icon: <EraseToolIcon />,
|
|
|
|
|
title: "Erase tool",
|
|
|
|
|
SettingsComponent: EraseToolSettings,
|
|
|
|
|
},
|
2020-04-20 11:56:56 +10:00
|
|
|
};
|
2020-04-27 17:29:46 +10:00
|
|
|
const tools = ["pan", "fog", "brush", "shape", "erase"];
|
2020-04-20 17:25:40 +10:00
|
|
|
|
2020-04-29 21:32:23 +10:00
|
|
|
const sections = [
|
|
|
|
|
{
|
2020-04-26 14:34:27 +10:00
|
|
|
id: "map",
|
|
|
|
|
component: (
|
|
|
|
|
<SelectMapButton
|
|
|
|
|
onMapChange={onMapChange}
|
|
|
|
|
onMapStateChange={onMapStateChange}
|
|
|
|
|
currentMap={currentMap}
|
|
|
|
|
/>
|
|
|
|
|
),
|
2020-04-29 21:32:23 +10:00
|
|
|
},
|
|
|
|
|
{
|
2020-04-26 14:34:27 +10:00
|
|
|
id: "drawing",
|
2020-04-27 17:29:46 +10:00
|
|
|
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-29 21:32:23 +10:00
|
|
|
},
|
|
|
|
|
{
|
2020-04-27 17:29:46 +10:00
|
|
|
id: "history",
|
2020-04-26 14:34:27 +10:00
|
|
|
component: (
|
|
|
|
|
<>
|
2020-04-20 11:56:56 +10:00
|
|
|
<IconButton
|
2020-04-27 17:29:46 +10:00
|
|
|
onClick={onUndo}
|
|
|
|
|
disabled={disabledControls.includes("undo")}
|
2020-04-20 11:56:56 +10:00
|
|
|
>
|
|
|
|
|
<UndoIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
<IconButton
|
2020-04-27 17:29:46 +10:00
|
|
|
onClick={onRedo}
|
|
|
|
|
disabled={disabledControls.includes("redo")}
|
2020-04-20 11:56:56 +10:00
|
|
|
>
|
|
|
|
|
<RedoIcon />
|
|
|
|
|
</IconButton>
|
2020-04-26 14:34:27 +10:00
|
|
|
</>
|
|
|
|
|
),
|
2020-04-29 21:32:23 +10:00
|
|
|
},
|
|
|
|
|
];
|
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}
|
2020-04-27 17:29:46 +10:00
|
|
|
{index !== sections.length - 1 && <Divider />}
|
2020-04-26 18:33:28 +10:00
|
|
|
</Fragment>
|
2020-04-26 14:34:27 +10:00
|
|
|
))}
|
2020-04-20 11:56:56 +10:00
|
|
|
</Box>
|
2020-04-26 14:34:27 +10:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 22:06:20 +10:00
|
|
|
const controlsRef = useRef();
|
|
|
|
|
const settingsRef = useRef();
|
|
|
|
|
|
2020-04-27 17:29:46 +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}
|
2020-04-28 22:06:20 +10:00
|
|
|
ref={settingsRef}
|
2020-04-27 17:29:46 +10:00
|
|
|
>
|
|
|
|
|
<Settings
|
|
|
|
|
settings={toolSettings[selectedToolId]}
|
|
|
|
|
onSettingChange={(change) =>
|
|
|
|
|
onToolSettingChange(selectedToolId, change)
|
|
|
|
|
}
|
2020-04-29 20:55:52 +10:00
|
|
|
onToolAction={onToolAction}
|
2020-04-27 17:29:46 +10:00
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 21:12:57 +10:00
|
|
|
// Move back to pan tool if selected tool becomes disabled
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (disabledControls.includes(selectedToolId)) {
|
|
|
|
|
onSelectedToolChange("pan");
|
|
|
|
|
}
|
|
|
|
|
}, [disabledControls]);
|
|
|
|
|
|
2020-04-28 22:06:20 +10:00
|
|
|
// Stop map drawing from happening when selecting controls
|
|
|
|
|
// Not using react events as they seem to trigger after dom events
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
function stopPropagation(e) {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
const controls = controlsRef.current;
|
|
|
|
|
if (controls) {
|
|
|
|
|
controls.addEventListener("mousedown", stopPropagation);
|
|
|
|
|
controls.addEventListener("touchstart", stopPropagation);
|
|
|
|
|
}
|
|
|
|
|
const settings = settingsRef.current;
|
|
|
|
|
if (settings) {
|
|
|
|
|
settings.addEventListener("mousedown", stopPropagation);
|
|
|
|
|
settings.addEventListener("touchstart", stopPropagation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (controls) {
|
|
|
|
|
controls.removeEventListener("mousedown", stopPropagation);
|
|
|
|
|
controls.removeEventListener("touchstart", stopPropagation);
|
|
|
|
|
}
|
|
|
|
|
if (settings) {
|
|
|
|
|
settings.removeEventListener("mousedown", stopPropagation);
|
|
|
|
|
settings.removeEventListener("touchstart", stopPropagation);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2020-04-26 14:34:27 +10:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Flex
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
top: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
}}
|
|
|
|
|
mx={1}
|
2020-04-28 22:06:20 +10:00
|
|
|
ref={controlsRef}
|
2020-04-26 14:34:27 +10:00
|
|
|
>
|
|
|
|
|
{controls}
|
2020-04-20 11:56:56 +10:00
|
|
|
</Flex>
|
2020-04-27 17:29:46 +10:00
|
|
|
{getToolSettings()}
|
2020-04-20 11:56:56 +10:00
|
|
|
</>
|
2020-04-18 23:31:40 +10:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-27 17:29:46 +10:00
|
|
|
export default MapContols;
|