refactor: add affixes to variable names in selection context

This commit is contained in:
trafficlunar 2025-02-07 17:10:09 +00:00
parent 634b6c24cb
commit df016ddf74
6 changed files with 28 additions and 24 deletions

View file

@ -31,12 +31,7 @@ PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
function Canvas() { function Canvas() {
const { stageSize, canvasSize, blocks, coords, scale, version, setStageSize, setBlocks, setCoords, setScale } = useContext(CanvasContext); const { stageSize, canvasSize, blocks, coords, scale, version, setStageSize, setBlocks, setCoords, setScale } = useContext(CanvasContext);
const { const { selectionCoords, selectionLayerBlocks, setSelectionCoords, setSelectionLayerBlocks } = useContext(SelectionContext);
coords: selectionCoords,
layerBlocks: selectionLayerBlocks,
setCoords: setSelectionCoords,
setLayerBlocks: setSelectionLayerBlocks,
} = useContext(SelectionContext);
const { settings } = useContext(SettingsContext); const { settings } = useContext(SettingsContext);
const { missingTexture } = useContext(TexturesContext); const { missingTexture } = useContext(TexturesContext);
const { isDark } = useContext(ThemeContext); const { isDark } = useContext(ThemeContext);

View file

@ -10,13 +10,13 @@ import { Button } from "@/components/ui/button";
function SelectionBar() { function SelectionBar() {
const { blocks, setBlocks } = useContext(CanvasContext); const { blocks, setBlocks } = useContext(CanvasContext);
const { layerBlocks, setLayerBlocks } = useContext(SelectionContext); const { selectionLayerBlocks, setSelectionLayerBlocks } = useContext(SelectionContext);
const [isVisible, setIsVisible] = useState(false); const [isVisible, setIsVisible] = useState(false);
useEffect(() => { useEffect(() => {
setIsVisible(layerBlocks.length !== 0); setIsVisible(selectionLayerBlocks.length !== 0);
}, [layerBlocks]); }, [selectionLayerBlocks]);
return ( return (
<div <div
@ -25,11 +25,11 @@ function SelectionBar() {
`} `}
> >
{/* todo: place back blocks removed */} {/* todo: place back blocks removed */}
<Button variant="ghost" className="w-8 h-8" onClick={() => setLayerBlocks([])}> <Button variant="ghost" className="w-8 h-8" onClick={() => setSelectionLayerBlocks([])}>
<XIcon /> <XIcon />
</Button> </Button>
<span className="mx-2 text-[0.85rem]">Confirm selection?</span> <span className="mx-2 text-[0.85rem]">Confirm selection?</span>
<Button variant="ghost" className="w-8 h-8" onClick={() => selection.confirm(blocks, layerBlocks, setBlocks, setLayerBlocks)}> <Button variant="ghost" className="w-8 h-8" onClick={() => selection.confirm(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks)}>
<CheckIcon /> <CheckIcon />
</Button> </Button>
</div> </div>

View file

@ -10,7 +10,7 @@ import { MenubarContent, MenubarItem, MenubarMenu, MenubarSeparator, MenubarShor
function EditMenu() { function EditMenu() {
const { blocks, setBlocks } = useContext(CanvasContext); 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 { setTool } = useContext(ToolContext);
const cut = () => { const cut = () => {

View file

@ -9,7 +9,7 @@ import * as selection from "@/utils/selection";
function SelectMenu() { function SelectMenu() {
const { canvasSize } = useContext(CanvasContext); 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 // Add every block within the canvas size to the temporary array
const selectAll = () => { const selectAll = () => {

View file

@ -14,7 +14,7 @@ import * as selection from "@/utils/selection";
function Replace() { function Replace() {
const { version, setBlocks } = useContext(CanvasContext); const { version, setBlocks } = useContext(CanvasContext);
const { coords: selectionCoords } = useContext(SelectionContext); const { selectionCoords } = useContext(SelectionContext);
const { selectedBlock, tool, setTool } = useContext(ToolContext); const { selectedBlock, tool, setTool } = useContext(ToolContext);
const { missingTexture } = useContext(TexturesContext); const { missingTexture } = useContext(TexturesContext);

View file

@ -1,10 +1,11 @@
import { createContext, ReactNode, useState } from "react"; import { createContext, ReactNode, useState } from "react";
interface Context { interface Context {
coords: CoordinateArray; selectionCoords: CoordinateArray;
layerBlocks: Block[]; selectionLayerBlocks: Block[];
setCoords: React.Dispatch<React.SetStateAction<CoordinateArray>>; setSelectionCoords: React.Dispatch<React.SetStateAction<CoordinateArray>>;
setLayerBlocks: React.Dispatch<React.SetStateAction<Block[]>>; setSelectionLayerBlocks: React.Dispatch<React.SetStateAction<Block[]>>;
isInSelection: (x: number, y: number) => boolean;
} }
interface Props { interface Props {
@ -14,16 +15,24 @@ interface Props {
export const SelectionContext = createContext<Context>({} as Context); export const SelectionContext = createContext<Context>({} as Context);
export const SelectionProvider = ({ children }: Props) => { export const SelectionProvider = ({ children }: Props) => {
const [coords, setCoords] = useState<CoordinateArray>([]); const [selectionCoords, setSelectionCoords] = useState<CoordinateArray>([]);
const [layerBlocks, setLayerBlocks] = useState<Block[]>([]); const [selectionLayerBlocks, setSelectionLayerBlocks] = useState<Block[]>([]);
const isInSelection = (x: number, y: number): boolean => {
if (selectionCoords.length !== 0) {
return selectionCoords.some(([x2, y2]) => x2 === x && y2 === y);
}
return true;
};
return ( return (
<SelectionContext.Provider <SelectionContext.Provider
value={{ value={{
coords, selectionCoords,
layerBlocks, selectionLayerBlocks,
setCoords, setSelectionCoords,
setLayerBlocks, setSelectionLayerBlocks,
isInSelection,
}} }}
> >
{children} {children}