diff --git a/src/foundry_fixes.js b/src/foundry_fixes.js index 876d7dc..2dc2676 100644 --- a/src/foundry_fixes.js +++ b/src/foundry_fixes.js @@ -1,31 +1,26 @@ // Wrapper to fix a FoundryVTT bug that causes the return values of canvas.grid.grid.getPixelsFromGridPosition to be ordered inconsistently -// https://gitlab.com/foundrynet/foundryvtt/-/issues/4705 +// This code could be phased out. The bug that caused the creation of these functions is now fixed, so this is only a wrapper function now export function getPixelsFromGridPosition(xGrid, yGrid) { - if (canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS) { - return canvas.grid.grid.getPixelsFromGridPosition(yGrid, xGrid); - } - return canvas.grid.grid.getPixelsFromGridPosition(xGrid, yGrid); + let coord = getPixelsFromGridPositionObj({x: xGrid, y: yGrid}); + return [coord.x, coord.y]; } -// Wrapper to fix a FoundryVTT bug that causes the return values of canvas.grid.grid.getPixelsFromGridPosition to be ordered inconsistently -// https://gitlab.com/foundrynet/foundryvtt/-/issues/4705 +// This code could be phased out. The bug that caused the creation of these functions is now fixed, so this is only a wrapper function now export function getGridPositionFromPixels(xPixel, yPixel) { - const [x, y] = canvas.grid.grid.getGridPositionFromPixels(xPixel, yPixel); - if (canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS) return [y, x]; - return [x, y]; + let coord = getGridPositionFromPixelsObj({x: xPixel, y: yPixel}); + return [coord.x, coord.y]; } +// This code could be phased out. The bug that caused the creation of these functions is now fixed, so this is only a wrapper function now export function getGridPositionFromPixelsObj(o) { - const r = {}; - [r.x, r.y] = getGridPositionFromPixels(o.x, o.y); - return r; + const coord = canvas.grid.getOffset(o); + return {x: coord.j, y: coord.i}; } +// This code could be phased out. The bug that caused the creation of these functions is now fixed, so this is only a wrapper function now export function getPixelsFromGridPositionObj(o) { - const r = {}; - [r.x, r.y] = getPixelsFromGridPosition(o.x, o.y); - return r; + return canvas.grid.getTopLeftPoint({j: o.x, i: o.y}); } export function getCenterFromGridPositionObj(o) { diff --git a/src/hex_support.js b/src/hex_support.js index 33451d1..7749c9d 100644 --- a/src/hex_support.js +++ b/src/hex_support.js @@ -9,11 +9,11 @@ * - Instead of taking a grid parameter, get the grid value from the globas canvas */ export function findVertexSnapPoint(x, y, altOrientationFlag) { - const grid = canvas.grid.grid; + const grid = canvas.grid; if (grid.columnar) { - return findSnapPointCols(x, y, grid.h, grid.w, altOrientationFlag); + return findSnapPointCols(x, y, grid.sizeY, grid.sizeX, altOrientationFlag); } else { - return findSnapPointRows(x, y, grid.h, grid.w, altOrientationFlag); + return findSnapPointRows(x, y, grid.sizeY, grid.sizeX, altOrientationFlag); } } diff --git a/src/main.js b/src/main.js index bbb985c..0a361dc 100644 --- a/src/main.js +++ b/src/main.js @@ -251,7 +251,7 @@ function applyGridlessSnapping(event) { return {ray}; }); origin = segments.pop().ray.A; - waypointDistance = canvas.grid.measureDistances(segments).reduce((a, b) => a + b); + waypointDistance = canvas.grid.measurePath(segments).reduce((a, b) => a + b); origin = {x: origin.x, y: origin.y}; } diff --git a/src/util.js b/src/util.js index afb6fa8..7034a93 100644 --- a/src/util.js +++ b/src/util.js @@ -42,15 +42,15 @@ export function getEntityCenter(token) { const size = getHexTokenSize(token); if (size % 2 === 0) { let offset; - if (canvas.grid.grid.columnar) { - offset = canvas.grid.grid.w - canvas.grid.grid.h; + if (canvas.grid.columnar) { + offset = canvas.grid.sizeX - canvas.grid.sizeY; } else { - offset = canvas.grid.grid.h - canvas.grid.grid.w; + offset = canvas.grid.sizeY - canvas.grid.sizeX; } if (getAltOrientationFlagForToken(token, size)) { offset *= -1; } - if (canvas.grid.grid.columnar) { + if (canvas.grid.columnar) { center.x -= offset; return center; } else { @@ -89,27 +89,27 @@ export function getSnapPointForToken(x, y, token) { } const [topLeftX, topLeftY] = canvas.grid.getTopLeft(x, y); - let cellX, cellY; - if (token.document.width % 2 === 0) cellX = x - canvas.grid.h / 2; - else cellX = x; - if (token.document.height % 2 === 0) cellY = y - canvas.grid.h / 2; - else cellY = y; - const [centerX, centerY] = canvas.grid.getCenter(cellX, cellY); + let cell = {}; + if (token.document.width % 2 === 0) cell.x = x - canvas.grid.sizeY / 2; + else cell.x = x; + if (token.document.height % 2 === 0) cell.y = y - canvas.grid.sizeY / 2; + else cell.y = y; + const center = canvas.grid.getCenterPoint(cell); let snapX, snapY; // Tiny tokens can snap to the cells corners if (token.document.width <= 0.5) { const offsetX = x - topLeftX; - const subGridWidth = Math.floor(canvas.grid.w / 2); + const subGridWidth = Math.floor(canvas.grid.sizeX / 2); const subGridPosX = Math.floor(offsetX / subGridWidth); snapX = topLeftX + (subGridPosX + 0.5) * subGridWidth; } // 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.document.width) % 2 === 1 || token.document.width < 1) { - snapX = centerX; + snapX = center.x; } // All remaining tokens (those with even or fractional multipliers on square grids) snap to the intersection points of the grid else { - snapX = centerX + canvas.grid.w / 2; + snapX = center.x + canvas.grid.sizeX / 2; } if (token.document.height <= 0.5) { const offsetY = y - topLeftY; @@ -117,9 +117,9 @@ export function getSnapPointForToken(x, y, token) { const subGridPosY = Math.floor(offsetY / subGridHeight); snapY = topLeftY + (subGridPosY + 0.5) * subGridHeight; } else if (Math.round(token.document.height) % 2 === 1 || token.document.height < 1) { - snapY = centerY; + snapY = center.y; } else { - snapY = centerY + canvas.grid.h / 2; + snapY = center.y + canvas.grid.sizeY / 2; } return {x: snapX, y: snapY}; } @@ -147,7 +147,7 @@ export function highlightTokenShape(position, shape, color, alpha) { const area = getAreaFromPositionAndShape(position, shape); for (const space of area) { const [x, y] = getPixelsFromGridPosition(space.x, space.y); - canvas.grid.grid.highlightGridPosition(layer, {x, y, color, alpha: 0.25 * alpha}); + canvas.grid.highlightGridPosition(layer, {x, y, color, alpha: 0.25 * alpha}); } } @@ -258,22 +258,22 @@ export function applyTokenSizeOffset(waypoints, token) { const isAltOrientation = getAltOrientationFlagForToken(token, getHexTokenSize(token)); if (canvas.grid.grid.columnar) { if (tokenSize.w % 2 === 0) { - waypointOffset.x = canvas.grid.w / 2; + waypointOffset.x = canvas.grid.sizeX / 2; if (isAltOrientation) waypointOffset.x *= -1; } } else { if (tokenSize.h % 2 === 0) { - waypointOffset.y = canvas.grid.h / 2; + waypointOffset.y = canvas.grid.sizeY / 2; if (isAltOrientation) waypointOffset.y *= -1; } } // If hex size support isn't active leave the waypoints like they are } else { if (tokenSize.w % 2 === 0) { - waypointOffset.x = canvas.grid.w / 2; + waypointOffset.x = canvas.grid.sizeX / 2; } if (tokenSize.h % 2 === 0) { - waypointOffset.y = canvas.grid.h / 2; + waypointOffset.y = canvas.grid.sizeY / 2; } }