2024-04-19 14:34:07 +03:00
|
|
|
import { Polygon } from "@/types/canvas";
|
|
|
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";
|
|
|
|
|
import { MdOutlineRestartAlt, MdUndo } from "react-icons/md";
|
|
|
|
|
import { Button } from "../ui/button";
|
2025-02-11 19:08:28 +03:00
|
|
|
import { TbPolygon, TbPolygonOff } from "react-icons/tb";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2025-03-16 18:36:20 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-04-19 14:34:07 +03:00
|
|
|
|
|
|
|
|
type PolygonEditControlsProps = {
|
|
|
|
|
polygons: Polygon[];
|
|
|
|
|
setPolygons: React.Dispatch<React.SetStateAction<Polygon[]>>;
|
|
|
|
|
activePolygonIndex: number | undefined;
|
2025-02-11 19:08:28 +03:00
|
|
|
snapPoints: boolean;
|
|
|
|
|
setSnapPoints: React.Dispatch<React.SetStateAction<boolean>>;
|
2024-04-19 14:34:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function PolygonEditControls({
|
|
|
|
|
polygons,
|
|
|
|
|
setPolygons,
|
|
|
|
|
activePolygonIndex,
|
2025-02-11 19:08:28 +03:00
|
|
|
snapPoints,
|
|
|
|
|
setSnapPoints,
|
2024-04-19 14:34:07 +03:00
|
|
|
}: PolygonEditControlsProps) {
|
2025-03-16 18:36:20 +03:00
|
|
|
const { t } = useTranslation(["views/settings"]);
|
2024-04-19 14:34:07 +03:00
|
|
|
const undo = () => {
|
|
|
|
|
if (activePolygonIndex === undefined || !polygons) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updatedPolygons = [...polygons];
|
|
|
|
|
const activePolygon = updatedPolygons[activePolygonIndex];
|
2024-04-19 16:59:28 +03:00
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
activePolygon.points.length > 0 &&
|
|
|
|
|
activePolygon.pointsOrder &&
|
|
|
|
|
activePolygon.pointsOrder.length > 0
|
|
|
|
|
) {
|
|
|
|
|
const lastPointOrderIndex = activePolygon.pointsOrder.indexOf(
|
|
|
|
|
Math.max(...activePolygon.pointsOrder),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
updatedPolygons[activePolygonIndex] = {
|
|
|
|
|
...activePolygon,
|
|
|
|
|
points: [
|
|
|
|
|
...activePolygon.points.slice(0, lastPointOrderIndex),
|
|
|
|
|
...activePolygon.points.slice(lastPointOrderIndex + 1),
|
|
|
|
|
],
|
|
|
|
|
pointsOrder: [
|
|
|
|
|
...activePolygon.pointsOrder.slice(0, lastPointOrderIndex),
|
|
|
|
|
...activePolygon.pointsOrder.slice(lastPointOrderIndex + 1),
|
|
|
|
|
],
|
2024-05-04 17:37:35 +03:00
|
|
|
isFinished: activePolygon.isFinished && activePolygon.points.length > 3,
|
2024-04-19 16:59:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setPolygons(updatedPolygons);
|
|
|
|
|
}
|
2024-04-19 14:34:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const reset = () => {
|
|
|
|
|
if (activePolygonIndex === undefined || !polygons) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updatedPolygons = [...polygons];
|
|
|
|
|
const activePolygon = updatedPolygons[activePolygonIndex];
|
|
|
|
|
updatedPolygons[activePolygonIndex] = {
|
|
|
|
|
...activePolygon,
|
|
|
|
|
points: [],
|
|
|
|
|
isFinished: false,
|
|
|
|
|
};
|
|
|
|
|
setPolygons(updatedPolygons);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (activePolygonIndex === undefined || !polygons) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-row justify-center gap-2">
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="default"
|
2024-05-14 18:06:44 +03:00
|
|
|
className="size-6 rounded-md p-1"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("masksAndZones.form.polygonDrawing.removeLastPoint")}
|
2024-04-19 14:34:07 +03:00
|
|
|
disabled={!polygons[activePolygonIndex].points.length}
|
|
|
|
|
onClick={undo}
|
|
|
|
|
>
|
|
|
|
|
<MdUndo className="text-secondary-foreground" />
|
|
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
2025-03-16 18:36:20 +03:00
|
|
|
<TooltipContent>
|
|
|
|
|
{t("masksAndZones.form.polygonDrawing.removeLastPoint")}
|
|
|
|
|
</TooltipContent>
|
2024-04-19 14:34:07 +03:00
|
|
|
</Tooltip>
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="default"
|
2024-05-14 18:06:44 +03:00
|
|
|
className="size-6 rounded-md p-1"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("masksAndZones.form.polygonDrawing.reset.label")}
|
2024-04-19 14:34:07 +03:00
|
|
|
disabled={!polygons[activePolygonIndex].points.length}
|
|
|
|
|
onClick={reset}
|
|
|
|
|
>
|
|
|
|
|
<MdOutlineRestartAlt className="text-secondary-foreground" />
|
|
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
2025-03-16 18:36:20 +03:00
|
|
|
<TooltipContent>{t("button.reset", { ns: "common" })}</TooltipContent>
|
2024-04-19 14:34:07 +03:00
|
|
|
</Tooltip>
|
2025-02-11 19:08:28 +03:00
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant={snapPoints ? "select" : "default"}
|
|
|
|
|
className={cn("size-6 rounded-md p-1")}
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("masksAndZones.form.polygonDrawing.snapPoints.true")}
|
2025-02-11 19:08:28 +03:00
|
|
|
onClick={() => setSnapPoints((prev) => !prev)}
|
|
|
|
|
>
|
|
|
|
|
{snapPoints ? (
|
|
|
|
|
<TbPolygon className="text-primary" />
|
|
|
|
|
) : (
|
|
|
|
|
<TbPolygonOff className="text-secondary-foreground" />
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent>
|
2025-03-16 18:36:20 +03:00
|
|
|
{snapPoints
|
|
|
|
|
? t("masksAndZones.form.polygonDrawing.snapPoints.false")
|
|
|
|
|
: t("masksAndZones.form.polygonDrawing.snapPoints.true")}
|
2025-02-11 19:08:28 +03:00
|
|
|
</TooltipContent>
|
|
|
|
|
</Tooltip>
|
2024-04-19 14:34:07 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|