fix out-of-bounds placing

This commit is contained in:
HF 2021-01-27 12:37:43 +01:00
parent c617d8f92b
commit 97737a16a1
2 changed files with 33 additions and 11 deletions

View File

@ -182,17 +182,23 @@ class PixelPlainterControls {
if (scale < 3) return;
const curColor = renderer.getColorIndexOfPixel(...cell);
if (selectedColor !== curColor) {
const { canvasSize } = state.canvas;
const [i, j] = getChunkOfPixel(canvasSize, ...cell);
const offset = getOffsetOfPixel(canvasSize, ...cell);
tryPlacePixel(
store,
i, j, offset,
selectedColor,
curColor,
);
if (selectedColor === curColor) {
return;
}
const { canvasSize } = state.canvas;
const [x, y] = cell;
const maxCoords = canvasSize / 2;
if (x < -maxCoords || x >= maxCoords || y < -maxCoords || y >= maxCoords) {
return;
}
const [i, j] = getChunkOfPixel(canvasSize, x, y);
const offset = getOffsetOfPixel(canvasSize, x, y);
tryPlacePixel(
store,
i, j, offset,
selectedColor,
curColor,
);
}
static getMultiTouchDistance(event: TouchEvent) {
@ -377,7 +383,21 @@ class PixelPlainterControls {
this.viewport,
[clientX, clientY],
);
if (!hover || hover[0] !== screenCoor[0] || hover[1] !== screenCoor[1]) {
const [x, y] = screenCoor;
/* out of bounds check */
const { canvasSize } = state.canvas;
const maxCoords = canvasSize / 2;
if (x < -maxCoords || x >= maxCoords
|| y < -maxCoords || y >= maxCoords
) {
if (hover) {
store.dispatch(unsetHover(screenCoor));
}
return;
}
if (!hover || hover[0] !== x || hover[1] !== y) {
store.dispatch(setHover(screenCoor));
}
if (this.holdPainting && !this.coolDownDelta) {

View File

@ -66,10 +66,12 @@ export async function drawByOffsets(
*/
if (i >= canvasSize / tileSize) {
// x out of bounds
// (we don't have to check for <0 becaue it is received as uint)
throw new Error(2);
}
if (j >= canvasSize / tileSize) {
// y out of bounds
// (we don't have to check for <0 becaue it is received as uint)
throw new Error(3);
}
if (canvas.req !== -1) {