import React, { useEffect, useRef, useState } from "react"; import { Unit } from "../../unit/unit"; import { ContextActionSet } from "../../unit/contextactionset"; import { OlStateButton } from "../components/olstatebutton"; import { getApp } from "../../olympusapp"; import { ContextAction } from "../../unit/contextaction"; import { CONTEXT_ACTION } from "../../constants/constants"; import { FaInfoCircle } from "react-icons/fa"; import { FaChevronLeft, FaChevronRight } from "react-icons/fa6"; export function UnitMouseControlBar(props: {}) { const [open, setOpen] = useState(false); const [contextActionsSet, setContextActionsSet] = useState(new ContextActionSet()); const [activeContextAction, setActiveContextAction] = useState(null as null | ContextAction); const [scrolledLeft, setScrolledLeft] = useState(true); const [scrolledRight, setScrolledRight] = useState(false); /* Initialize the "scroll" position of the element */ var scrollRef = useRef(null); useEffect(() => { if (scrollRef.current) onScroll(scrollRef.current); }); useEffect(() => { /* When a unit is selected, open the menu */ document.addEventListener("unitsSelection", (ev: CustomEventInit) => { setOpen(true); updateData(); setActiveContextAction(null); }); /* When a unit is deselected, refresh the view */ document.addEventListener("unitDeselection", (ev: CustomEventInit) => { window.setTimeout(() => updateData(), 200); }); /* When all units are deselected clean the view */ document.addEventListener("clearSelection", () => { setOpen(false); updateData(); }); /* Deselect the context action when exiting state */ document.addEventListener("mapStateChanged", (ev) => { setOpen((ev as CustomEvent).detail === CONTEXT_ACTION); }); }, []); /* Update the current values of the shown data */ function updateData() { var newContextActionSet = new ContextActionSet(); getApp() .getUnitsManager() .getSelectedUnits() .forEach((unit: Unit) => { unit.appendContextActions(newContextActionSet); }); setContextActionsSet(newContextActionSet); return newContextActionSet; } function onScroll(el) { const sl = el.scrollLeft; const sr = el.scrollWidth - el.scrollLeft - el.clientWidth; sl < 1 && !scrolledLeft && setScrolledLeft(true); sl > 1 && scrolledLeft && setScrolledLeft(false); sr < 1 && !scrolledRight && setScrolledRight(true); sr > 1 && scrolledRight && setScrolledRight(false); } let colors = [null, "white", "green", "purple", "blue", "red"]; let reorderedActions: ContextAction[] = []; colors.forEach((color) => { Object.values(contextActionsSet.getContextActions()).forEach((contextAction: ContextAction) => { if (color === null && contextAction.getOptions().buttonColor === undefined) reorderedActions.push(contextAction); else if (color === contextAction.getOptions().buttonColor) reorderedActions.push(contextAction); }); }); return ( <> {open && Object.keys(contextActionsSet.getContextActions()).length > 0 && ( <>
{!scrolledLeft && ( )}
onScroll(ev.target)} ref={scrollRef}> {reorderedActions.map((contextAction: ContextAction) => { return ( { if (contextAction.getOptions().executeImmediately) { setActiveContextAction(null); contextAction.executeCallback(null, null); } else { if (activeContextAction !== contextAction) { setActiveContextAction(contextAction); getApp().getMap().setState(CONTEXT_ACTION, { contextAction: contextAction, defaultContextAction: contextActionsSet.getDefaultContextAction(), }); } else { setActiveContextAction(null); getApp().getMap().setState(CONTEXT_ACTION, { contextAction: null, defaultContextAction: contextActionsSet.getDefaultContextAction(), }); } } }} /> ); })}
{!scrolledRight && ( )}
{activeContextAction && (
)} )} ); }