fix: better implementation of ae9f8500

This commit is contained in:
trafficlunar 2025-01-17 11:34:32 +00:00
parent 47e7a1d737
commit 3c40a51d42

View file

@ -87,39 +87,28 @@ function Canvas() {
}, [dragging, holdingAlt, tool, setCssCursor]);
const onToolUse = useCallback(() => {
// If selection box is there
if (!(selectionBoxBounds.maxX == selectionBoxBounds.minX && selectionBoxBounds.maxY == selectionBoxBounds.maxY)) {
// If the mouse is not in the selection, return
if (
!(
mouseCoords.x >= selectionBoxBounds.minX &&
mouseCoords.x <= selectionBoxBounds.maxX - 1 &&
mouseCoords.y >= selectionBoxBounds.minY &&
mouseCoords.y <= selectionBoxBounds.maxY - 1
)
) {
return;
}
}
// Radius calculation - if odd number cursor is in center, if even cursor is in top-left corner
// Calculate the top-left position of the radius
const getRadiusPosition = (): Position => {
const halfSize = Math.floor(radius / 2);
const x = mouseCoords.x - (radius % 2 === 0 ? 0 : halfSize);
const y = mouseCoords.y - (radius % 2 === 0 ? 0 : halfSize);
return { x, y };
};
// Check if a block is within the selection bounds
const isInSelection = (x: number, y: number) => {
return x >= selectionBoxBounds.minX && x < selectionBoxBounds.maxX && y >= selectionBoxBounds.minY && y < selectionBoxBounds.maxY;
};
const eraseTool = () => {
// Fixes Infinity and NaN errors
// Fixes Infinity and NaN errors when no blocks are present
if (blocks.length == 1) return;
const radiusPosition = getRadiusPosition();
const updated = blocks.filter((block) => {
const withinSquare =
const withinRadius =
block.x >= radiusPosition.x && block.x < radiusPosition.x + radius && block.y >= radiusPosition.y && block.y < radiusPosition.y + radius;
return !withinSquare;
return !withinRadius || !isInSelection(block.x, block.y);
});
setBlocks(updated);
@ -140,6 +129,8 @@ function Canvas() {
const tileX = radiusPosition.x + x;
const tileY = radiusPosition.y + y;
// Only add blocks within the selection
if (isInSelection(tileX, tileY)) {
radiusBlocks.push({
name: selectedBlock,
x: tileX,
@ -147,6 +138,7 @@ function Canvas() {
});
}
}
}
const mergedBlocks = blocks.filter((block) => {
return !radiusBlocks.some((newBlock) => block.x === newBlock.x && block.y === newBlock.y);