From df016ddf745fbd7c90c4dc84dc4f0c4c4543ec8e Mon Sep 17 00:00:00 2001 From: trafficlunar Date: Fri, 7 Feb 2025 17:10:09 +0000 Subject: [PATCH] refactor: add affixes to variable names in selection context --- src/components/canvas/Canvas.tsx | 7 +----- src/components/canvas/SelectionBar.tsx | 10 ++++---- src/components/menubar/EditMenu.tsx | 2 +- src/components/menubar/SelectMenu.tsx | 2 +- src/components/tool-settings/Replace.tsx | 2 +- src/context/Selection.tsx | 29 ++++++++++++++++-------- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/components/canvas/Canvas.tsx b/src/components/canvas/Canvas.tsx index 491b92f..ac44343 100644 --- a/src/components/canvas/Canvas.tsx +++ b/src/components/canvas/Canvas.tsx @@ -31,12 +31,7 @@ PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST; function Canvas() { const { stageSize, canvasSize, blocks, coords, scale, version, setStageSize, setBlocks, setCoords, setScale } = useContext(CanvasContext); - const { - coords: selectionCoords, - layerBlocks: selectionLayerBlocks, - setCoords: setSelectionCoords, - setLayerBlocks: setSelectionLayerBlocks, - } = useContext(SelectionContext); + const { selectionCoords, selectionLayerBlocks, setSelectionCoords, setSelectionLayerBlocks } = useContext(SelectionContext); const { settings } = useContext(SettingsContext); const { missingTexture } = useContext(TexturesContext); const { isDark } = useContext(ThemeContext); diff --git a/src/components/canvas/SelectionBar.tsx b/src/components/canvas/SelectionBar.tsx index 5a60c1c..79ea1a8 100644 --- a/src/components/canvas/SelectionBar.tsx +++ b/src/components/canvas/SelectionBar.tsx @@ -10,13 +10,13 @@ import { Button } from "@/components/ui/button"; function SelectionBar() { const { blocks, setBlocks } = useContext(CanvasContext); - const { layerBlocks, setLayerBlocks } = useContext(SelectionContext); + const { selectionLayerBlocks, setSelectionLayerBlocks } = useContext(SelectionContext); const [isVisible, setIsVisible] = useState(false); useEffect(() => { - setIsVisible(layerBlocks.length !== 0); - }, [layerBlocks]); + setIsVisible(selectionLayerBlocks.length !== 0); + }, [selectionLayerBlocks]); return (
{/* todo: place back blocks removed */} - Confirm selection? -
diff --git a/src/components/menubar/EditMenu.tsx b/src/components/menubar/EditMenu.tsx index db0d16b..62d852b 100644 --- a/src/components/menubar/EditMenu.tsx +++ b/src/components/menubar/EditMenu.tsx @@ -10,7 +10,7 @@ import { MenubarContent, MenubarItem, MenubarMenu, MenubarSeparator, MenubarShor function EditMenu() { const { blocks, setBlocks } = useContext(CanvasContext); - const { coords: selectionCoords, setCoords: setSelectionCoords, setLayerBlocks: setSelectionLayerBlocks } = useContext(SelectionContext); + const { selectionCoords, setSelectionCoords, setSelectionLayerBlocks } = useContext(SelectionContext); const { setTool } = useContext(ToolContext); const cut = () => { diff --git a/src/components/menubar/SelectMenu.tsx b/src/components/menubar/SelectMenu.tsx index 6968d30..2aa2ade 100644 --- a/src/components/menubar/SelectMenu.tsx +++ b/src/components/menubar/SelectMenu.tsx @@ -9,7 +9,7 @@ import * as selection from "@/utils/selection"; function SelectMenu() { const { canvasSize } = useContext(CanvasContext); - const { coords: selectionCoords, setCoords: setSelectionCoords } = useContext(SelectionContext); + const { selectionCoords, setSelectionCoords } = useContext(SelectionContext); // Add every block within the canvas size to the temporary array const selectAll = () => { diff --git a/src/components/tool-settings/Replace.tsx b/src/components/tool-settings/Replace.tsx index 4ed48fb..54835fa 100644 --- a/src/components/tool-settings/Replace.tsx +++ b/src/components/tool-settings/Replace.tsx @@ -14,7 +14,7 @@ import * as selection from "@/utils/selection"; function Replace() { const { version, setBlocks } = useContext(CanvasContext); - const { coords: selectionCoords } = useContext(SelectionContext); + const { selectionCoords } = useContext(SelectionContext); const { selectedBlock, tool, setTool } = useContext(ToolContext); const { missingTexture } = useContext(TexturesContext); diff --git a/src/context/Selection.tsx b/src/context/Selection.tsx index 0feeab4..8f088f3 100644 --- a/src/context/Selection.tsx +++ b/src/context/Selection.tsx @@ -1,10 +1,11 @@ import { createContext, ReactNode, useState } from "react"; interface Context { - coords: CoordinateArray; - layerBlocks: Block[]; - setCoords: React.Dispatch>; - setLayerBlocks: React.Dispatch>; + selectionCoords: CoordinateArray; + selectionLayerBlocks: Block[]; + setSelectionCoords: React.Dispatch>; + setSelectionLayerBlocks: React.Dispatch>; + isInSelection: (x: number, y: number) => boolean; } interface Props { @@ -14,16 +15,24 @@ interface Props { export const SelectionContext = createContext({} as Context); export const SelectionProvider = ({ children }: Props) => { - const [coords, setCoords] = useState([]); - const [layerBlocks, setLayerBlocks] = useState([]); + const [selectionCoords, setSelectionCoords] = useState([]); + const [selectionLayerBlocks, setSelectionLayerBlocks] = useState([]); + + const isInSelection = (x: number, y: number): boolean => { + if (selectionCoords.length !== 0) { + return selectionCoords.some(([x2, y2]) => x2 === x && y2 === y); + } + return true; + }; return ( {children}