refactor: move selection util script into selection context

This commit is contained in:
trafficlunar 2025-02-08 13:17:37 +00:00
parent 4687330f02
commit a80119c011
6 changed files with 22 additions and 38 deletions

View file

@ -25,8 +25,6 @@ import { usePaintBucketTool } from "@/hooks/tools/paint-bucket";
import { useEyedropperTool } from "@/hooks/tools/eyedropper";
import { useZoomTool } from "@/hooks/tools/zoom";
import * as selection from "@/utils/selection";
import Blocks from "./Blocks";
import Cursor from "./Cursor";
import Selection from "./Selection";
@ -43,7 +41,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 { addHistory, undo, redo } = useContext(HistoryContext);
const { selectionCoords, selectionLayerBlocks, setSelectionCoords, setSelectionLayerBlocks } = useContext(SelectionContext);
const { selectionCoords, selectionLayerBlocks, setSelectionCoords, setSelectionLayerBlocks, confirmSelection } = useContext(SelectionContext);
const { settings } = useContext(SettingsContext);
const { missingTexture } = useContext(TexturesContext);
const { isDark } = useContext(ThemeContext);
@ -250,7 +248,7 @@ function Canvas() {
setSelectionLayerBlocks([]);
break;
case "Enter":
selection.confirm(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks);
confirmSelection();
break;
case " ": // Space
setDragging(true);

View file

@ -1,16 +1,12 @@
import { useContext, useEffect, useState } from "react";
import { CheckIcon, XIcon } from "lucide-react";
import { CanvasContext } from "@/context/Canvas";
import { SelectionContext } from "@/context/Selection";
import * as selection from "@/utils/selection";
import { Button } from "@/components/ui/button";
function SelectionBar() {
const { blocks, setBlocks } = useContext(CanvasContext);
const { selectionLayerBlocks, setSelectionLayerBlocks } = useContext(SelectionContext);
const { selectionLayerBlocks, setSelectionLayerBlocks, confirmSelection } = useContext(SelectionContext);
const [isVisible, setIsVisible] = useState(false);
@ -29,7 +25,7 @@ function SelectionBar() {
<XIcon />
</Button>
<span className="mx-2 text-[0.85rem]">Confirm selection?</span>
<Button variant="ghost" className="w-8 h-8" onClick={() => selection.confirm(blocks, selectionLayerBlocks, setBlocks, setSelectionLayerBlocks)}>
<Button variant="ghost" className="w-8 h-8" onClick={confirmSelection}>
<CheckIcon />
</Button>
</div>

View file

@ -5,11 +5,9 @@ import { SelectionContext } from "@/context/Selection";
import { MenubarContent, MenubarItem, MenubarMenu, MenubarShortcut, MenubarTrigger } from "@/components/ui/menubar";
import { CanvasContext } from "@/context/Canvas";
import * as selection from "@/utils/selection";
function SelectMenu() {
const { canvasSize } = useContext(CanvasContext);
const { selectionCoords, setSelectionCoords } = useContext(SelectionContext);
const { setSelectionCoords, isInSelection } = useContext(SelectionContext);
// Add every block within the canvas size to the temporary array
const selectAll = () => {
@ -30,7 +28,7 @@ function SelectMenu() {
for (let x = canvasSize.minX; x < canvasSize.maxX; x++) {
for (let y = canvasSize.minY; y < canvasSize.maxY; y++) {
if (!selection.isIn(selectionCoords, x, y)) newSelection.push([x, y]);
if (!isInSelection(x, y)) newSelection.push([x, y]);
}
}

View file

@ -10,11 +10,10 @@ import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
import { useTextures } from "@/hooks/useTextures";
import * as selection from "@/utils/selection";
function Replace() {
const { version, setBlocks } = useContext(CanvasContext);
const { selectionCoords } = useContext(SelectionContext);
const { isInSelection } = useContext(SelectionContext);
const { selectedBlock, tool, setTool } = useContext(ToolContext);
const { missingTexture } = useContext(TexturesContext);
@ -36,7 +35,7 @@ function Replace() {
setBlocks((prev) =>
prev
.map((block) => {
if (selection.isIn(selectionCoords, block.x, block.y)) {
if (isInSelection(block.x, block.y)) {
if (block.name === block1) {
// If block2 is air, return null
// If not, change the block name