diff --git a/web/src/components/settings/PolygonCanvas.tsx b/web/src/components/settings/PolygonCanvas.tsx index 74f228f1e..2497e82bc 100644 --- a/web/src/components/settings/PolygonCanvas.tsx +++ b/web/src/components/settings/PolygonCanvas.tsx @@ -67,8 +67,8 @@ export function PolygonCanvas({ return distance < 15; }; - const handleMouseDown = (e: KonvaEventObject) => { - if (!activePolygonIndex || !polygons) { + const handleMouseDown = (e: KonvaEventObject) => { + if (activePolygonIndex == null || !polygons) { return; } @@ -100,50 +100,63 @@ export function PolygonCanvas({ // } }; - const handleMouseOverStartPoint = (e: KonvaEventObject) => { - if (activePolygonIndex !== null && polygons) { - const activePolygon = polygons[activePolygonIndex]; - if (!activePolygon.isFinished && activePolygon.points.length >= 3) { - e.currentTarget.scale({ x: 2, y: 2 }); - } + const handleMouseOverStartPoint = ( + e: KonvaEventObject, + ) => { + if (activePolygonIndex == null || !polygons) { + return; + } + + const activePolygon = polygons[activePolygonIndex]; + if (!activePolygon.isFinished && activePolygon.points.length >= 3) { + e.currentTarget.scale({ x: 2, y: 2 }); } }; - const handleMouseOutStartPoint = (e: KonvaEventObject) => { + const handleMouseOutStartPoint = ( + e: KonvaEventObject, + ) => { e.currentTarget.scale({ x: 1, y: 1 }); - if (activePolygonIndex !== null && polygons) { - const activePolygon = polygons[activePolygonIndex]; - if ( - (!activePolygon.isFinished && activePolygon.points.length >= 3) || - activePolygon.isFinished - ) { - e.currentTarget.scale({ x: 1, y: 1 }); - } + + if (activePolygonIndex == null || !polygons) { + return; + } + + const activePolygon = polygons[activePolygonIndex]; + if ( + (!activePolygon.isFinished && activePolygon.points.length >= 3) || + activePolygon.isFinished + ) { + e.currentTarget.scale({ x: 1, y: 1 }); } }; - const handlePointDragMove = (e: KonvaEventObject) => { - if (activePolygonIndex !== null && polygons) { - const updatedPolygons = [...polygons]; - const activePolygon = updatedPolygons[activePolygonIndex]; - const stage = e.target.getStage(); - if (stage) { - const index = e.target.index - 1; - const pos = [e.target._lastPos!.x, e.target._lastPos!.y]; - if (pos[0] < 0) pos[0] = 0; - if (pos[1] < 0) pos[1] = 0; - if (pos[0] > stage.width()) pos[0] = stage.width(); - if (pos[1] > stage.height()) pos[1] = stage.height(); - updatedPolygons[activePolygonIndex] = { - ...activePolygon, - points: [ - ...activePolygon.points.slice(0, index), - pos, - ...activePolygon.points.slice(index + 1), - ], - }; - setPolygons(updatedPolygons); - } + const handlePointDragMove = ( + e: KonvaEventObject, + ) => { + if (activePolygonIndex == null || !polygons) { + return; + } + + const updatedPolygons = [...polygons]; + const activePolygon = updatedPolygons[activePolygonIndex]; + const stage = e.target.getStage(); + if (stage) { + const index = e.target.index - 1; + const pos = [e.target._lastPos!.x, e.target._lastPos!.y]; + if (pos[0] < 0) pos[0] = 0; + if (pos[1] < 0) pos[1] = 0; + if (pos[0] > stage.width()) pos[0] = stage.width(); + if (pos[1] > stage.height()) pos[1] = stage.height(); + updatedPolygons[activePolygonIndex] = { + ...activePolygon, + points: [ + ...activePolygon.points.slice(0, index), + pos, + ...activePolygon.points.slice(index + 1), + ], + }; + setPolygons(updatedPolygons); } }; @@ -151,7 +164,7 @@ export function PolygonCanvas({ return points.reduce((acc, point) => [...acc, ...point], []); }; - const handleGroupDragEnd = (e: KonvaEventObject) => { + const handleGroupDragEnd = (e: KonvaEventObject) => { if (activePolygonIndex !== null && e.target.name() === "polygon") { const updatedPolygons = [...polygons]; const activePolygon = updatedPolygons[activePolygonIndex]; @@ -174,6 +187,7 @@ export function PolygonCanvas({ width={width} height={height} onMouseDown={handleMouseDown} + onTouchStart={handleMouseDown} > ) => void; - handleGroupDragEnd: (e: KonvaEventObject) => void; - handleMouseOverStartPoint: (e: KonvaEventObject) => void; - handleMouseOutStartPoint: (e: KonvaEventObject) => void; + handlePointDragMove: (e: KonvaEventObject) => void; + handleGroupDragEnd: (e: KonvaEventObject) => void; + handleMouseOverStartPoint: ( + e: KonvaEventObject, + ) => void; + handleMouseOutStartPoint: ( + e: KonvaEventObject, + ) => void; }; export default function PolygonDrawer({ @@ -33,13 +37,17 @@ export default function PolygonDrawer({ const [minMaxX, setMinMaxX] = useState([0, 0]); const [minMaxY, setMinMaxY] = useState([0, 0]); - const handleGroupMouseOver = (e: Konva.KonvaEventObject) => { + const handleGroupMouseOver = ( + e: Konva.KonvaEventObject, + ) => { if (!isFinished) return; e.target.getStage()!.container().style.cursor = "move"; setStage(e.target.getStage()!); }; - const handleGroupMouseOut = (e: Konva.KonvaEventObject) => { + const handleGroupMouseOut = ( + e: Konva.KonvaEventObject, + ) => { if (!e.target) return; e.target.getStage()!.container().style.cursor = "default"; }; @@ -87,6 +95,7 @@ export default function PolygonDrawer({ onDragEnd={isActive ? handleGroupDragEnd : undefined} dragBoundFunc={isActive ? groupDragBound : undefined} onMouseOver={isActive ? handleGroupMouseOver : undefined} + onTouchStart={isActive ? handleGroupMouseOver : undefined} onMouseOut={isActive ? handleGroupMouseOut : undefined} > (); - // console.log("zone name", zoneName); - // console.log(cameraConfig.zones[zoneName].objects); cameraConfig.objects.track.forEach((label) => { if (!ATTRIBUTES.includes(label)) { @@ -55,9 +52,13 @@ export function ZoneObjectSelector({ } }); - cameraConfig.zones[zoneName].objects.forEach((label) => { - labels.add(label); - }); + if (cameraConfig.zones[zoneName]) { + cameraConfig.zones[zoneName].objects.forEach((label) => { + if (!ATTRIBUTES.includes(label)) { + labels.add(label); + } + }); + } return [...labels].sort() || []; }, [cameraConfig, zoneName]); @@ -166,7 +167,7 @@ export function ZoneControls({ updatedPolygons[activePolygonIndex] = { points: [], isFinished: false, - name: "new", + name: updatedPolygons[activePolygonIndex].name, camera: camera, color: updatedPolygons[activePolygonIndex].color ?? [220, 0, 0], }; @@ -210,29 +211,31 @@ export function ZoneControls({ }} > + {isMobile && } New Zone - Enter a label for your zone. Do not include spaces, and don't use - the name of a camera. + Enter a unique label for your zone. Do not include spaces, and + don't use the name of a camera. <> { setInvalidName( Object.keys(config.cameras).includes(e.target.value) || - e.target.value.includes(" "), + e.target.value.includes(" ") || + polygons + .map((item) => item.name) + .includes(e.target.value), ); setZoneName(e.target.value); }} /> {invalidName && ( -
- Zone name is not valid. -
+
Invalid zone name.
)}