From 9cd8c0f18d495ffd638ffb2c4ebbdbfc2a080eef Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Tue, 28 Jan 2025 21:36:34 +0000 Subject: [PATCH] feat: set coordinates dialog --- src/components/dialogs/SetCoords.tsx | 92 ++++++++++++++++++++++++++++ src/components/menubar/ViewMenu.tsx | 2 + 2 files changed, 94 insertions(+) create mode 100644 src/components/dialogs/SetCoords.tsx diff --git a/src/components/dialogs/SetCoords.tsx b/src/components/dialogs/SetCoords.tsx new file mode 100644 index 0000000..ba9819d --- /dev/null +++ b/src/components/dialogs/SetCoords.tsx @@ -0,0 +1,92 @@ +import { useContext, useEffect, useState } from "react"; + +import { CanvasContext } from "@/context/Canvas"; + +import { DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; + +function SetCoords({ close }: DialogProps) { + const { stageSize, coords, scale, setCoords } = useContext(CanvasContext); + + const [newTilemapCoords, setNewTilemapCoords] = useState(coords); + + const onSubmit = () => { + // Get center of screen + const halfWidth = stageSize.width / 2; + const halfHeight = stageSize.height / 2; + + // Calculate coordinate offset to show the center of the block in the middle of the screen + const offsetX = halfWidth - (16 / 2) * scale; + const offsetY = halfHeight - (16 / 2) * scale; + + // Multiply by -16 to reverse the direction + setCoords({ + x: newTilemapCoords.x * -16 * scale + offsetX, + y: newTilemapCoords.y * -16 * scale + offsetY, + }); + + close(); + }; + + // Sets the current coordinates when the dialog is first opened + useEffect(() => { + // Get center of screen + const halfWidth = stageSize.width / 2; + const halfHeight = stageSize.height / 2; + + // Calculate coordinate offset + const offsetX = halfWidth + 16 / 2; + const offsetY = halfHeight + 16 / 2; + + setNewTilemapCoords({ + x: Math.floor((coords.x - offsetX) / -16 / scale), + y: Math.floor((coords.y - offsetY) / -16 / scale), + }); + }, []); + + return ( + + + Set Coordinates + Set your coordinates to a particular position + + +
+
+ + setNewTilemapCoords((prev) => ({ ...prev, x: parseInt(e.target.value) }))} + /> +
+ +
+ + setNewTilemapCoords((prev) => ({ ...prev, y: parseInt(e.target.value) }))} + /> +
+
+ + + + + +
+ ); +} + +export default SetCoords; diff --git a/src/components/menubar/ViewMenu.tsx b/src/components/menubar/ViewMenu.tsx index 918babc..1d768a5 100644 --- a/src/components/menubar/ViewMenu.tsx +++ b/src/components/menubar/ViewMenu.tsx @@ -18,9 +18,11 @@ function ViewMenu() { View + openDialog("SetCoords")}>Set Coordinates openDialog("SetScale")}>Set Scale Center Canvas + Grid