Files
Dwinzo_dev/app/src/components/ui/Tools.tsx

482 lines
15 KiB
TypeScript
Raw Normal View History

2025-03-25 11:47:41 +05:30
import React, { useEffect, useRef, useState } from "react";
import {
AsileIcon,
CommentIcon,
CursorIcon,
2025-03-29 12:57:16 +05:30
DeleteIcon,
2025-03-25 11:47:41 +05:30
FloorIcon,
FreeMoveIcon,
2025-03-29 12:57:16 +05:30
MeasureToolIcon,
2025-03-25 11:47:41 +05:30
PenIcon,
PlayIcon,
SaveTemplateIcon,
WallIcon,
ZoneIcon,
} from "../icons/ExportToolsIcons";
import { ArrowIcon, TickIcon } from "../icons/ExportCommonIcons";
import useModuleStore, { useThreeDStore } from "../../store/useModuleStore";
2025-03-25 11:47:41 +05:30
import { handleSaveTemplate } from "../../modules/visualization/handleSaveTemplate";
import { usePlayButtonStore } from "../../store/usePlayButtonStore";
import useTemplateStore from "../../store/useTemplateStore";
import { useSelectedZoneStore } from "../../store/useZoneStore";
2025-03-26 11:30:17 +05:30
import {
useActiveTool,
2025-03-26 11:30:17 +05:30
useAddAction,
useDeleteTool,
useDeletePointOrLine,
useMovePoint,
useRefTextUpdate,
2025-03-26 11:30:17 +05:30
useSelectedWallItem,
useSocketStore,
2025-03-26 11:30:17 +05:30
useToggleView,
useToolMode,
useTransformMode,
useActiveSubTool,
2025-03-26 11:30:17 +05:30
} from "../../store/store";
2025-03-29 12:57:16 +05:30
import useToggleStore from "../../store/useUIToggleStore";
2025-04-02 19:12:14 +05:30
import {
use3DWidget,
useDroppedObjectsStore,
2025-04-02 19:12:14 +05:30
useFloatingWidget,
} from "../../store/useDroppedObjectsStore";
2025-03-25 11:47:41 +05:30
const Tools: React.FC = () => {
const { templates } = useTemplateStore();
const { activeSubTool, setActiveSubTool } = useActiveSubTool();
const { toggleThreeD, setToggleThreeD } = useThreeDStore();
const { setToggleUI } = useToggleStore();
2025-03-25 11:47:41 +05:30
const dropdownRef = useRef<HTMLDivElement>(null);
const [openDrop, setOpenDrop] = useState(false);
const { visualizationSocket } = useSocketStore();
2025-03-25 11:47:41 +05:30
const { activeModule } = useModuleStore();
const { isPlaying, setIsPlaying } = usePlayButtonStore();
const { addTemplate } = useTemplateStore();
const { selectedZone } = useSelectedZoneStore();
2025-04-02 19:12:14 +05:30
const { floatingWidget } = useFloatingWidget();
2025-04-02 19:12:14 +05:30
const { widgets3D } = use3DWidget();
2025-03-25 11:47:41 +05:30
const zones = useDroppedObjectsStore((state) => state.zones);
// wall options
const { toggleView, setToggleView } = useToggleView();
const { setDeleteTool } = useDeleteTool();
const { setAddAction } = useAddAction();
const { setSelectedWallItem } = useSelectedWallItem();
const { transformMode, setTransformMode } = useTransformMode();
const { deletePointOrLine, setDeletePointOrLine } = useDeletePointOrLine();
const { movePoint, setMovePoint } = useMovePoint();
const { toolMode, setToolMode } = useToolMode();
const { activeTool, setActiveTool } = useActiveTool();
const { refTextupdate, setRefTextUpdate } = useRefTextUpdate();
2025-03-25 11:47:41 +05:30
// Reset activeTool whenever activeModule changes
2025-03-29 12:57:16 +05:30
useEffect(() => {
setToggleUI(
localStorage.getItem("navBarUi")
? localStorage.getItem("navBarUi") === "true"
: true
);
2025-03-29 12:57:16 +05:30
}, []);
2025-04-02 19:12:14 +05:30
useEffect(() => {}, [activeModule]);
2025-03-25 11:47:41 +05:30
useEffect(() => {
setActiveTool(activeSubTool);
setActiveSubTool(activeSubTool);
}, [activeModule]);
const toggleSwitch = () => {
if (toggleThreeD) {
setSelectedWallItem(null);
setDeleteTool(false);
setAddAction(null);
setToggleView(true);
// localStorage.setItem("navBarUi", JSON.stringify(!toggleThreeD));
2025-03-26 11:30:17 +05:30
} else {
setToggleView(false);
}
setToggleUI(
localStorage.getItem("navBarUi")
? localStorage.getItem("navBarUi") === "true"
: true
);
setToggleThreeD(!toggleThreeD);
setActiveSubTool("cursor");
setActiveTool("cursor");
};
2025-03-25 11:47:41 +05:30
useEffect(() => {
const handleOutsideClick = (event: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node)
) {
setOpenDrop(false); // Close the dropdown
}
};
document.addEventListener("mousedown", handleOutsideClick);
return () => {
document.removeEventListener("mousedown", handleOutsideClick);
};
}, []);
2025-03-29 12:57:16 +05:30
useEffect(() => {
if (!toggleThreeD) {
setToggleUI(false);
}
}, [toggleThreeD]);
2025-03-25 11:47:41 +05:30
useEffect(() => {
setToolMode(null);
setDeleteTool(false);
setAddAction(null);
setTransformMode(null);
setMovePoint(false);
setDeletePointOrLine(false);
setRefTextUpdate((prevUpdate) => prevUpdate - 1);
switch (activeTool) {
case "Move":
if (toggleView) {
setMovePoint(true);
} else {
setTransformMode("translate");
}
break;
case "Rotate":
if (!toggleView) {
setTransformMode("rotate");
}
break;
case "Scale":
if (!toggleView) {
setTransformMode("scale");
}
break;
case "draw-wall":
if (toggleView) {
setToolMode("Wall");
}
break;
2025-03-29 12:57:16 +05:30
case "draw-aisle":
if (toggleView) {
setToolMode("Aisle");
}
break;
2025-03-29 12:57:16 +05:30
case "draw-zone":
if (toggleView) {
setToolMode("Zone");
}
break;
2025-03-29 12:57:16 +05:30
case "draw-floor":
if (toggleView) {
setToolMode("Floor");
}
break;
2025-03-29 12:57:16 +05:30
case "measure":
setToolMode("MeasurementScale");
break;
case "Add pillar":
if (!toggleView) {
setAddAction("pillar");
}
break;
2025-03-29 12:57:16 +05:30
case "delete":
if (toggleView) {
setDeletePointOrLine(true);
} else {
setDeleteTool(true);
}
break;
default:
break;
}
setActiveTool(activeTool);
}, [activeTool]);
2025-03-25 11:47:41 +05:30
return (
2025-03-26 11:30:17 +05:30
<>
{!isPlaying ? (
<>
<div className="tools-container">
<div className="drop-down-icons">
<div className="activeDropicon">
{activeSubTool == "cursor" && (
2025-03-26 10:03:43 +05:30
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "cursor" ? "active" : ""
}`}
2025-03-26 10:03:43 +05:30
onClick={() => {
setActiveTool("cursor");
}}
>
2025-03-26 11:30:17 +05:30
<CursorIcon isActive={activeTool === "cursor"} />
2025-03-26 10:03:43 +05:30
</div>
2025-03-26 11:30:17 +05:30
)}
{activeSubTool == "free-hand" && (
2025-03-26 10:03:43 +05:30
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "free-hand" ? "active" : ""
}`}
2025-03-26 10:03:43 +05:30
onClick={() => {
setActiveTool("free-hand");
}}
>
2025-03-26 11:30:17 +05:30
<FreeMoveIcon isActive={activeTool === "free-hand"} />
2025-03-25 11:47:41 +05:30
</div>
2025-03-26 11:30:17 +05:30
)}
2025-03-29 12:57:16 +05:30
{activeSubTool == "delete" && (
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "delete" ? "active" : ""
}`}
2025-03-29 12:57:16 +05:30
onClick={() => {
setActiveTool("delete");
}}
>
<DeleteIcon isActive={activeTool === "delete"} />
</div>
)}
2025-03-26 11:30:17 +05:30
{activeModule !== "visualization" && (
<div
className="drop-down-option-button"
ref={dropdownRef}
onClick={() => {
setOpenDrop(!openDrop);
}}
>
<ArrowIcon />
{openDrop && (
<div className="drop-down-container">
<div
className="option-list"
onClick={() => {
setOpenDrop(false);
setActiveTool("cursor");
setActiveSubTool("cursor");
}}
>
<div className="active-option">
{activeSubTool === "cursor" && <TickIcon />}
</div>
<CursorIcon isActive={false} />
<div className="option">Cursor</div>
</div>
<div
className="option-list"
onClick={() => {
setOpenDrop(false);
setActiveTool("free-hand");
setActiveSubTool("free-hand");
}}
>
<div className="active-option">
{activeSubTool === "free-hand" && <TickIcon />}
</div>
<FreeMoveIcon isActive={false} />
<div className="option">Free Hand</div>
</div>
2025-03-29 12:57:16 +05:30
<div
className="option-list"
onClick={() => {
setOpenDrop(false);
setActiveTool("delete");
setActiveSubTool("delete");
}}
>
<div className="active-option">
{activeSubTool === "delete" && <TickIcon />}
</div>
<DeleteIcon isActive={false} />
<div className="option">Delete</div>
</div>
2025-03-26 11:30:17 +05:30
</div>
)}
</div>
)}
</div>
2025-03-25 11:47:41 +05:30
</div>
2025-03-26 11:30:17 +05:30
{!toggleThreeD && activeModule === "builder" && (
<>
<div className="split"></div>
<div className="draw-tools">
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "draw-wall" ? "active" : ""
}`}
2025-03-26 11:30:17 +05:30
onClick={() => {
setActiveTool("draw-wall");
}}
2025-03-29 12:57:16 +05:30
title="Wall"
2025-03-26 11:30:17 +05:30
>
<WallIcon isActive={activeTool === "draw-wall"} />
</div>
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "draw-zone" ? "active" : ""
}`}
2025-03-26 11:30:17 +05:30
onClick={() => {
setActiveTool("draw-zone");
}}
2025-03-29 12:57:16 +05:30
title="Zone"
2025-03-26 11:30:17 +05:30
>
<ZoneIcon isActive={activeTool === "draw-zone"} />
</div>
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "draw-aisle" ? "active" : ""
}`}
2025-03-26 11:30:17 +05:30
onClick={() => {
setActiveTool("draw-aisle");
}}
2025-03-29 12:57:16 +05:30
title="Aisle"
2025-03-26 11:30:17 +05:30
>
<AsileIcon isActive={activeTool === "draw-aisle"} />
</div>
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "draw-floor" ? "active" : ""
}`}
2025-03-26 11:30:17 +05:30
onClick={() => {
setActiveTool("draw-floor");
}}
2025-03-29 12:57:16 +05:30
title="Floor"
2025-03-26 11:30:17 +05:30
>
<FloorIcon isActive={activeTool === "draw-floor"} />
</div>
</div>
</>
)}
2025-03-29 12:57:16 +05:30
{activeModule === "builder" && (
<>
<div className="split"></div>
<div className="draw-tools">
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "measure" ? "active" : ""
}`}
2025-03-29 12:57:16 +05:30
onClick={() => {
setActiveTool("measure");
}}
title="Measure"
>
<MeasureToolIcon isActive={activeTool === "measure"} />
</div>
</div>
</>
)}
2025-03-26 11:30:17 +05:30
{activeModule === "simulation" && (
<>
<div className="split"></div>
<div className="draw-tools">
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "pen" ? "active" : ""
}`}
2025-03-26 11:30:17 +05:30
onClick={() => {
setActiveTool("pen");
}}
>
<PenIcon isActive={activeTool === "pen"} />
</div>
</div>
</>
)}
{activeModule === "visualization" && (
<>
<div className="split"></div>
<div className="draw-tools">
<div
className={`tool-button`}
onClick={() => {
2025-03-26 11:30:17 +05:30
handleSaveTemplate({
addTemplate,
floatingWidget,
widgets3D,
2025-03-26 11:30:17 +05:30
selectedZone,
templates,
visualizationSocket,
});
}}
2025-03-26 11:30:17 +05:30
>
<SaveTemplateIcon isActive={false} />
</div>
</div>
</>
)}
<div className="split"></div>
<div className="general-options">
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "comment" ? "active" : ""
}`}
2025-03-26 11:30:17 +05:30
onClick={() => {
setActiveTool("comment");
}}
>
<CommentIcon isActive={activeTool === "comment"} />
</div>
2025-03-29 12:57:16 +05:30
{toggleThreeD && (
<div
2025-04-02 19:12:14 +05:30
className={`tool-button ${
activeTool === "play" ? "active" : ""
}`}
2025-03-29 12:57:16 +05:30
onClick={() => {
setIsPlaying(!isPlaying);
}}
>
<PlayIcon isActive={activeTool === "play"} />
</div>
)}
2025-03-25 11:47:41 +05:30
</div>
{activeModule === "builder" && (
<>
<div className="split"></div>
<div
2025-04-02 19:12:14 +05:30
className={`toggle-threed-button${
toggleThreeD ? " toggled" : ""
}`}
onClick={toggleSwitch}
>
2025-04-02 19:12:14 +05:30
<div
className={`toggle-option${!toggleThreeD ? " active" : ""}`}
>
2d
</div>
2025-04-02 19:12:14 +05:30
<div
className={`toggle-option${toggleThreeD ? " active" : ""}`}
>
3d
</div>
</div>
</>
)}
2025-03-25 11:47:41 +05:30
</div>
</>
2025-03-26 11:30:17 +05:30
) : (
2025-03-27 10:55:44 +05:30
<>
{activeModule !== "simulation" && (
<div className="exitPlay" onClick={() => setIsPlaying(false)}>
X
</div>
)}
</>
2025-03-26 11:30:17 +05:30
)}
</>
2025-03-25 11:47:41 +05:30
);
};
export default Tools;