fix: air blocks not working with magic wand tool
This commit is contained in:
parent
c8edd54f4f
commit
59d69152dc
1 changed files with 16 additions and 13 deletions
|
|
@ -341,15 +341,17 @@ function Canvas() {
|
||||||
const visited = new Set<string>();
|
const visited = new Set<string>();
|
||||||
const result: CoordinateArray = [];
|
const result: CoordinateArray = [];
|
||||||
const startBlock = blocks.find((block) => block.x === mouseCoords.x && block.y === mouseCoords.y);
|
const startBlock = blocks.find((block) => block.x === mouseCoords.x && block.y === mouseCoords.y);
|
||||||
|
const startName = startBlock ? startBlock.name : "air";
|
||||||
|
|
||||||
// Return if the block is not found
|
function depthFirstSearch(x: number, y: number) {
|
||||||
if (!startBlock) return result;
|
const key = `${x},${y}`;
|
||||||
|
|
||||||
function depthFirstSearch(block: Block) {
|
|
||||||
const key = `${block.x},${block.y}`;
|
|
||||||
if (visited.has(key)) return;
|
if (visited.has(key)) return;
|
||||||
visited.add(key);
|
visited.add(key);
|
||||||
result.push([block.x, block.y]);
|
|
||||||
|
const withinCanvas = x >= canvasSize.minX && x < canvasSize.maxX && y >= canvasSize.minY && y < canvasSize.maxY;
|
||||||
|
if (!withinCanvas) return;
|
||||||
|
|
||||||
|
result.push([x, y]);
|
||||||
|
|
||||||
// Directions for adjacent blocks (up, down, left, right)
|
// Directions for adjacent blocks (up, down, left, right)
|
||||||
const directions = [
|
const directions = [
|
||||||
|
|
@ -360,17 +362,18 @@ function Canvas() {
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const { dx, dy } of directions) {
|
for (const { dx, dy } of directions) {
|
||||||
const newX = block.x + dx;
|
const newX = x + dx;
|
||||||
const newY = block.y + dy;
|
const newY = y + dy;
|
||||||
const adjacentBlock = blocks.find((b) => b.x === newX && b.y === newY && b.name === block.name);
|
const adjacentBlock = blocks.find((b) => b.x === newX && b.y === newY);
|
||||||
|
const adjacentName = adjacentBlock ? adjacentBlock.name : "air";
|
||||||
|
|
||||||
if (adjacentBlock) {
|
if (adjacentName === startName) {
|
||||||
depthFirstSearch({ ...block, x: newX, y: newY });
|
depthFirstSearch(newX, newY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
depthFirstSearch({ name: startBlock.name, x: mouseCoords.x, y: mouseCoords.y });
|
depthFirstSearch(mouseCoords.x, mouseCoords.y);
|
||||||
setSelectionCoords((prev) => {
|
setSelectionCoords((prev) => {
|
||||||
if (holdingAltRef.current) {
|
if (holdingAltRef.current) {
|
||||||
// If holding alt, remove new magic wand selection
|
// If holding alt, remove new magic wand selection
|
||||||
|
|
@ -402,7 +405,7 @@ function Canvas() {
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}, [tool, holdingAltRef, scale, mouseCoords, blocks, setSelectionCoords, setSelectedBlock, zoom]);
|
}, [tool, holdingAltRef, scale, mouseCoords, blocks, canvasSize, setSelectionCoords, setSelectedBlock, zoom]);
|
||||||
|
|
||||||
const onKeyDown = useCallback(
|
const onKeyDown = useCallback(
|
||||||
async (e: KeyboardEvent) => {
|
async (e: KeyboardEvent) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue