feat: press key "delete" to remove blocks in selection

This commit is contained in:
trafficlunar 2025-01-17 22:03:36 +00:00
parent fdcd73c748
commit 5b8a859f60

View file

@ -37,13 +37,13 @@ function Canvas() {
const [mousePosition, setMousePosition] = useState<Position>({ x: 0, y: 0 }); const [mousePosition, setMousePosition] = useState<Position>({ x: 0, y: 0 });
const [mouseCoords, setMouseCoords] = useState<Position>({ x: 0, y: 0 }); const [mouseCoords, setMouseCoords] = useState<Position>({ x: 0, y: 0 });
const [dragging, setDragging] = useState(false);
const mouseMovement = useRef<Position>(); const mouseMovement = useRef<Position>();
const [dragging, setDragging] = useState(false);
const [holdingAlt, setHoldingAlt] = useState(false); const [holdingAlt, setHoldingAlt] = useState(false);
const [oldTool, setOldTool] = useState<Tool>("hand"); const [oldTool, setOldTool] = useState<Tool>("hand");
const [selectionBoxBounds, setSelectionBoxBounds] = useState<BoundingBox>({ minX: 0, minY: 0, maxX: 0, maxY: 0 }); const [selectionBoxBounds, setSelectionBoxBounds] = useState<BoundingBox>({ minX: 0, minY: 0, maxX: 0, maxY: 0 });
const selectionBoxBoundsRef = useRef<BoundingBox>();
const visibleArea = useMemo(() => { const visibleArea = useMemo(() => {
const blockSize = 16 * scale; const blockSize = 16 * scale;
@ -121,31 +121,25 @@ function Canvas() {
if (!mouseMovement.current) return; if (!mouseMovement.current) return;
const { x: movementX, y: movementY } = mouseMovement.current; const { x: movementX, y: movementY } = mouseMovement.current;
setSelectionBoxBounds((prev) => ({ setSelectionBoxBounds((prev) => {
const newBounds = {
minX: prev.minX + movementX, minX: prev.minX + movementX,
minY: prev.minY + movementY, minY: prev.minY + movementY,
maxX: prev.maxX + movementX, maxX: prev.maxX + movementX,
maxY: prev.maxY + movementY, maxY: prev.maxY + movementY,
})); };
selectionBoxBoundsRef.current = newBounds;
return newBounds;
});
setBlocks((prev) => { setBlocks((prev) => {
const airBlocks: Block[] = []; return prev.map((block) => {
for (let x = selectionBoxBounds.minX; x < selectionBoxBounds.maxX; x++) {
for (let y = selectionBoxBounds.minY; y < selectionBoxBounds.maxY; y++) {
const existingBlock = prev.find((block) => block.x === x && block.y === y);
if (existingBlock) continue;
airBlocks.push({ name: "air", x, y });
}
}
return [...prev, ...airBlocks].map((block) => {
if ( if (
block.x >= selectionBoxBounds.minX && block.x >= selectionBoxBounds.minX &&
block.x <= selectionBoxBounds.maxX - 1 && block.x < selectionBoxBounds.maxX &&
block.y >= selectionBoxBounds.minY && block.y >= selectionBoxBounds.minY &&
block.y <= selectionBoxBounds.maxY - 1 block.y < selectionBoxBounds.maxY
) { ) {
return { return {
...block, ...block,
@ -156,6 +150,7 @@ function Canvas() {
return block; return block;
}); });
}); });
break; break;
} }
case "pencil": { case "pencil": {
@ -232,11 +227,16 @@ function Canvas() {
})); }));
break; break;
case "rectangle-select": case "rectangle-select":
setSelectionBoxBounds((prev) => ({ setSelectionBoxBounds((prev) => {
const newBounds = {
...prev, ...prev,
maxX: mouseCoords.x + 1, maxX: mouseCoords.x + 1,
maxY: mouseCoords.y + 1, maxY: mouseCoords.y + 1,
})); };
selectionBoxBoundsRef.current = newBounds;
return newBounds;
});
break; break;
} }
@ -252,12 +252,15 @@ function Canvas() {
updateCssCursor(); updateCssCursor();
if (tool == "rectangle-select") { if (tool == "rectangle-select") {
setSelectionBoxBounds({ const newBounds = {
minX: mouseCoords.x, minX: mouseCoords.x,
minY: mouseCoords.y, minY: mouseCoords.y,
maxX: mouseCoords.x, maxX: mouseCoords.x,
maxY: mouseCoords.y, maxY: mouseCoords.y,
}); };
selectionBoxBoundsRef.current = newBounds;
setSelectionBoxBounds(newBounds);
} }
}, [onToolUse, updateCssCursor, tool, setSelectionBoxBounds, mouseCoords]); }, [onToolUse, updateCssCursor, tool, setSelectionBoxBounds, mouseCoords]);
@ -296,6 +299,8 @@ function Canvas() {
}, [tool, holdingAlt, scale, mouseCoords, blocks, setSelectedBlock, zoom]); }, [tool, holdingAlt, scale, mouseCoords, blocks, setSelectedBlock, zoom]);
const onKeyDown = (e: KeyboardEvent) => { const onKeyDown = (e: KeyboardEvent) => {
console.log(e.key);
switch (e.key) { switch (e.key) {
case " ": // Space case " ": // Space
setDragging(true); setDragging(true);
@ -328,6 +333,13 @@ function Canvas() {
setHoldingAlt(true); setHoldingAlt(true);
setCssCursor("zoom-out"); setCssCursor("zoom-out");
break; break;
case "Delete": {
if (!selectionBoxBoundsRef.current) return;
const bounds = selectionBoxBoundsRef.current;
setBlocks((prev) => prev.filter((b) => !(b.x >= bounds.minX && b.x < bounds.maxX && b.y >= bounds.minY && b.y < bounds.maxY)));
break;
}
} }
}; };