From 752b8375abf19caa172b361279178edb690ec0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Wed, 24 Nov 2021 09:14:27 +0100 Subject: [PATCH] Non-square tokens are now fully supported on square grids (fixes #116) --- src/util.js | 60 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/src/util.js b/src/util.js index 2cbdd5d..21a1d18 100644 --- a/src/util.js +++ b/src/util.js @@ -10,33 +10,61 @@ export function getSnapPointForToken(x, y, token) { if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) { return new PIXI.Point(x, y); } - if (canvas.grid.isHex && game.modules.get("hex-size-support")?.active && CONFIG.hexSizeSupport.getAltSnappingFlag(token)) { - if (token.document.getFlag("hex-size-support", "borderSize") % 2 === 0) { - const snapPoint = CONFIG.hexSizeSupport.findVertexSnapPoint(x, y, token, canvas.grid.grid) - return new PIXI.Point(snapPoint.x, snapPoint.y) + if (canvas.grid.isHex) { + if (game.modules.get("hex-size-support")?.active && CONFIG.hexSizeSupport.getAltSnappingFlag(token)) { + if (token.document.getFlag("hex-size-support", "borderSize") % 2 === 0) { + const snapPoint = CONFIG.hexSizeSupport.findVertexSnapPoint(x, y, token, canvas.grid.grid) + return new PIXI.Point(snapPoint.x, snapPoint.y) + } + else { + return new PIXI.Point(...canvas.grid.getCenter(x, y)) + } } else { - return new PIXI.Point(...canvas.grid.getCenter(x, y)) + return new PIXI.Point(...canvas.grid.getCenter(x, y)); } } + + const [topLeftX, topLeftY] = canvas.grid.getTopLeft(x, y); + let cellX, cellY; + if (token.data.width % 2 === 0) + cellX = x - canvas.grid.h / 2; + else + cellX = x; + if (token.data.height % 2 === 0) + cellY = y - canvas.grid.h / 2; + else + cellY = y; + const [centerX, centerY] = canvas.grid.getCenter(cellX, cellY); + let snapX, snapY; // Tiny tokens can snap to the cells corners - if (!canvas.grid.isHex && token.data.width <= 0.5) { - const [topLeftX, topLeftY] = canvas.grid.getTopLeft(x, y); + if (token.data.width <= 0.5) { const offsetX = x - topLeftX; - const offsetY = y - topLeftY; const subGridWidth = Math.floor(canvas.grid.w / 2); - const subGridHeight = Math.floor(canvas.grid.h / 2); const subGridPosX = Math.floor(offsetX / subGridWidth); - const subGridPosY = Math.floor(offsetY / subGridHeight); - return new PIXI.Point(topLeftX + (subGridPosX + 0.5) * subGridWidth, topLeftY + (subGridPosY + 0.5) * subGridHeight); + snapX = topLeftX + (subGridPosX + 0.5) * subGridWidth; } - // Hex tokens, tokens with odd multipliers (1x1, 3x3, ...) and tokens smaller than 1x1 but bigger than 0.5x0.5 snap to the center of the grid cell - if (canvas.grid.isHex || Math.round(token.data.width) % 2 === 1 || token.data.width < 1) { - return new PIXI.Point(...canvas.grid.getCenter(x, y)) + // Tokens with odd multipliers (1x1, 3x3, ...) and tokens smaller than 1x1 but bigger than 0.5x0.5 snap to the center of the grid cell + else if (Math.round(token.data.width) % 2 === 1 || token.data.width < 1) { + snapX = centerX; } // All remaining tokens (those with even or fractional multipliers on square grids) snap to the intersection points of the grid - const [snappedX, snappedY] = canvas.grid.getCenter(x - canvas.grid.w / 2, y - canvas.grid.h / 2) - return new PIXI.Point(snappedX + canvas.grid.w / 2, snappedY + canvas.grid.h / 2) + else { + snapX = centerX + canvas.grid.w / 2; + } + if (token.data.height <= 0.5) { + const offsetY = y - topLeftY; + const subGridHeight = Math.floor(canvas.grid.h / 2); + const subGridPosY = Math.floor(offsetY / subGridHeight); + snapY = topLeftY + (subGridPosY + 0.5) * subGridHeight; + } + else if (Math.round(token.data.height) % 2 === 1 || token.data.height < 1) { + snapY = centerY; + } + else { + snapY = centerY + canvas.grid.h / 2; + } + return new PIXI.Point(snapX, snapY); } export function getSnapPointForMeasuredTemplate(x, y) {