diff --git a/src/App.tsx b/src/App.tsx index b419aa8..da88232 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,3 +1,4 @@ +import { SettingsProvider } from "./context/SettingsContext"; import { TexturesProvider } from "./context/TexturesContext"; import { ToolProvider } from "./context/ToolContext"; @@ -7,15 +8,17 @@ import Canvas from "./components/Canvas"; function App() { return ( - - -
- - - -
-
-
+ + + +
+ + + +
+
+
+
); } diff --git a/src/components/Canvas.tsx b/src/components/Canvas.tsx index 0cdd634..c3ad2c2 100644 --- a/src/components/Canvas.tsx +++ b/src/components/Canvas.tsx @@ -2,10 +2,12 @@ import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } import { Container, Stage } from "@pixi/react"; import * as PIXI from "pixi.js"; +import { SettingsContext } from "@/context/SettingsContext"; import { ToolContext } from "@/context/ToolContext"; import { TexturesContext } from "@/context/TexturesContext"; import Blocks from "./Blocks"; +import Grid from "./Grid"; import Cursor from "./Cursor"; import CursorInformation from "./information/Cursor"; import CanvasInformation from "./information/Canvas"; @@ -14,11 +16,12 @@ import CanvasInformation from "./information/Canvas"; PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST; function Canvas() { + const { settings } = useContext(SettingsContext); const { tool, selectedBlock } = useContext(ToolContext); const textures = useContext(TexturesContext); const stageContainerRef = useRef(null); - const [stageSize, setStageSize] = useState({ width: 0, height: 0 }); + const [stageSize, setStageSize] = useState({ width: 0, height: 0 }); const [coords, setCoords] = useState({ x: 0, y: 0 }); const [scale, setScale] = useState(1); @@ -136,6 +139,12 @@ function Canvas() { + + {settings.grid && ( + + + + )} diff --git a/src/components/Grid.tsx b/src/components/Grid.tsx new file mode 100644 index 0000000..e94b4a1 --- /dev/null +++ b/src/components/Grid.tsx @@ -0,0 +1,32 @@ +import { Graphics } from "@pixi/react"; + +interface Props { + stageSize: Dimension; + coords: Position; + scale: number; +} + +function Grid({ stageSize, coords, scale }: Props) { + return ( + { + g.clear(); + g.lineStyle(1, 0xffffff, 0.1); + + const tileSize = 16 * scale; + + for (let x = coords.x % tileSize; x < stageSize.width; x += tileSize) { + g.moveTo(x, 0); + g.lineTo(x, stageSize.height); + } + + for (let y = coords.y % tileSize; y < stageSize.height; y += tileSize) { + g.moveTo(0, y); + g.lineTo(stageSize.width, y); + } + }} + /> + ); +} + +export default Grid; diff --git a/src/components/information/Canvas.tsx b/src/components/information/Canvas.tsx index 0b07bdc..ae7d8d0 100644 --- a/src/components/information/Canvas.tsx +++ b/src/components/information/Canvas.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useMemo, useState } from "react"; +import React, { useMemo } from "react"; import { Slider } from "@/components/ui/slider"; interface Props { @@ -22,8 +22,8 @@ function CanvasInformation({ scale, setScale, blocks }: Props) { }); return { - x: maxX - minX, - y: maxY - minY, + width: maxX - minX, + height: maxY - minY, }; }, [blocks]); @@ -36,8 +36,8 @@ function CanvasInformation({ scale, setScale, blocks }: Props) {
{Math.floor(scale * 100)}%
- W: {canvasSize.x} - H: {canvasSize.y} + W: {canvasSize.width} + H: {canvasSize.height}
diff --git a/src/context/SettingsContext.tsx b/src/context/SettingsContext.tsx new file mode 100644 index 0000000..54a637e --- /dev/null +++ b/src/context/SettingsContext.tsx @@ -0,0 +1,24 @@ +import { createContext, ReactNode, useState } from "react"; + +interface Props { + children: ReactNode; +} + +const defaultSettings: Settings = { + grid: false, +}; + +export const SettingsContext = createContext({ + settings: defaultSettings, + setSetting: (key: K, value: Settings[K]) => {}, +}); + +export const SettingsProvider = ({ children }: Props) => { + const [settings, setSettings] = useState(defaultSettings); + + const setSetting = (key: K, value: Settings[K]) => { + setSettings((prev) => ({ ...prev, [key]: value })); + }; + + return {children}; +}; diff --git a/src/types.d.ts b/src/types.d.ts index 09da3a9..d9d5b8e 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,12 +1,21 @@ type Theme = "dark" | "light" | "system"; interface Position { - x: number; - y: number + x: number; + y: number; +} + +interface Dimension { + width: number; + height: number; } interface Block extends Position { - name: string; + name: string; } -type Tool = "hand" | "pencil" | "eraser"; \ No newline at end of file +type Tool = "hand" | "pencil" | "eraser"; + +interface Settings { + grid: boolean; +}