import React, { useState, Fragment, useEffect, useRef } from "react";
import { IconButton, Flex, Box } from "theme-ui";
import RadioIconButton from "./controls/RadioIconButton";
import Divider from "./controls/Divider";
import SelectMapButton from "./SelectMapButton";
import FogToolSettings from "./controls/FogToolSettings";
import BrushToolSettings from "./controls/BrushToolSettings";
import ShapeToolSettings from "./controls/ShapeToolSettings";
import EraseToolSettings from "./controls/EraseToolSettings";
import PanToolIcon from "../../icons/PanToolIcon";
import FogToolIcon from "../../icons/FogToolIcon";
import BrushToolIcon from "../../icons/BrushToolIcon";
import ShapeToolIcon from "../../icons/ShapeToolIcon";
import EraseToolIcon from "../../icons/EraseToolIcon";
import UndoIcon from "../../icons/UndoIcon";
import RedoIcon from "../../icons/RedoIcon";
import ExpandMoreIcon from "../../icons/ExpandMoreIcon";
function MapContols({
onMapChange,
onMapStateChange,
currentMap,
selectedToolId,
onSelectedToolChange,
toolSettings,
onToolSettingChange,
onToolAction,
disabledControls,
onUndo,
onRedo,
}) {
const [isExpanded, setIsExpanded] = useState(false);
const toolsById = {
pan: {
id: "pan",
icon: ,
title: "Pan Tool",
},
fog: {
id: "fog",
icon: ,
title: "Fog Tool",
SettingsComponent: FogToolSettings,
},
brush: {
id: "brush",
icon: ,
title: "Brush Tool",
SettingsComponent: BrushToolSettings,
},
shape: {
id: "shape",
icon: ,
title: "Shape Tool",
SettingsComponent: ShapeToolSettings,
},
erase: {
id: "erase",
icon: ,
title: "Erase tool",
SettingsComponent: EraseToolSettings,
},
};
const tools = ["pan", "fog", "brush", "shape", "erase"];
const sections = [];
if (!disabledControls.includes("map")) {
sections.push({
id: "map",
component: (
),
});
}
if (!disabledControls.includes("drawing")) {
sections.push({
id: "drawing",
component: tools.map((tool) => (
onSelectedToolChange(tool)}
isSelected={selectedToolId === tool}
disabled={disabledControls.includes(tool)}
>
{toolsById[tool].icon}
)),
});
sections.push({
id: "history",
component: (
<>
>
),
});
}
let controls = null;
if (sections.length === 1 && sections[0].id === "map") {
controls = (
{sections[0].component}
);
} else if (sections.length > 0) {
controls = (
<>
setIsExpanded(!isExpanded)}
sx={{
transform: `rotate(${isExpanded ? "0" : "180deg"})`,
display: "block",
backgroundColor: "overlay",
borderRadius: "50%",
}}
m={2}
>
{sections.map((section, index) => (
{section.component}
{index !== sections.length - 1 && }
))}
>
);
}
const controlsRef = useRef();
const settingsRef = useRef();
function getToolSettings() {
const Settings = toolsById[selectedToolId].SettingsComponent;
if (Settings) {
return (
onToolSettingChange(selectedToolId, change)
}
onToolAction={onToolAction}
/>
);
} else {
return null;
}
}
// Move back to pan tool if selected tool becomes disabled
useEffect(() => {
if (disabledControls.includes(selectedToolId)) {
onSelectedToolChange("pan");
}
}, [disabledControls]);
// 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);
}
};
});
return (
<>
{controls}
{getToolSettings()}
>
);
}
export default MapContols;