Fix some deprecation warnings

This commit is contained in:
Manuel Vögele
2024-07-27 23:01:20 +02:00
parent a7d06eaed3
commit 6da3e65301
4 changed files with 35 additions and 40 deletions
+11 -16
View File
@@ -1,31 +1,26 @@
// Wrapper to fix a FoundryVTT bug that causes the return values of canvas.grid.grid.getPixelsFromGridPosition to be ordered inconsistently // 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) { export function getPixelsFromGridPosition(xGrid, yGrid) {
if (canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS) { let coord = getPixelsFromGridPositionObj({x: xGrid, y: yGrid});
return canvas.grid.grid.getPixelsFromGridPosition(yGrid, xGrid); return [coord.x, coord.y];
}
return canvas.grid.grid.getPixelsFromGridPosition(xGrid, yGrid);
} }
// Wrapper to fix a FoundryVTT bug that causes the return values of canvas.grid.grid.getPixelsFromGridPosition to be ordered inconsistently // 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
// https://gitlab.com/foundrynet/foundryvtt/-/issues/4705
export function getGridPositionFromPixels(xPixel, yPixel) { export function getGridPositionFromPixels(xPixel, yPixel) {
const [x, y] = canvas.grid.grid.getGridPositionFromPixels(xPixel, yPixel); let coord = getGridPositionFromPixelsObj({x: xPixel, y: yPixel});
if (canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS) return [y, x]; return [coord.x, coord.y];
return [x, 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) { export function getGridPositionFromPixelsObj(o) {
const r = {}; const coord = canvas.grid.getOffset(o);
[r.x, r.y] = getGridPositionFromPixels(o.x, o.y); return {x: coord.j, y: coord.i};
return r;
} }
// 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) { export function getPixelsFromGridPositionObj(o) {
const r = {}; return canvas.grid.getTopLeftPoint({j: o.x, i: o.y});
[r.x, r.y] = getPixelsFromGridPosition(o.x, o.y);
return r;
} }
export function getCenterFromGridPositionObj(o) { export function getCenterFromGridPositionObj(o) {
+3 -3
View File
@@ -9,11 +9,11 @@
* - Instead of taking a grid parameter, get the grid value from the globas canvas * - Instead of taking a grid parameter, get the grid value from the globas canvas
*/ */
export function findVertexSnapPoint(x, y, altOrientationFlag) { export function findVertexSnapPoint(x, y, altOrientationFlag) {
const grid = canvas.grid.grid; const grid = canvas.grid;
if (grid.columnar) { if (grid.columnar) {
return findSnapPointCols(x, y, grid.h, grid.w, altOrientationFlag); return findSnapPointCols(x, y, grid.sizeY, grid.sizeX, altOrientationFlag);
} else { } else {
return findSnapPointRows(x, y, grid.h, grid.w, altOrientationFlag); return findSnapPointRows(x, y, grid.sizeY, grid.sizeX, altOrientationFlag);
} }
} }
+1 -1
View File
@@ -251,7 +251,7 @@ function applyGridlessSnapping(event) {
return {ray}; return {ray};
}); });
origin = segments.pop().ray.A; 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}; origin = {x: origin.x, y: origin.y};
} }
+20 -20
View File
@@ -42,15 +42,15 @@ export function getEntityCenter(token) {
const size = getHexTokenSize(token); const size = getHexTokenSize(token);
if (size % 2 === 0) { if (size % 2 === 0) {
let offset; let offset;
if (canvas.grid.grid.columnar) { if (canvas.grid.columnar) {
offset = canvas.grid.grid.w - canvas.grid.grid.h; offset = canvas.grid.sizeX - canvas.grid.sizeY;
} else { } else {
offset = canvas.grid.grid.h - canvas.grid.grid.w; offset = canvas.grid.sizeY - canvas.grid.sizeX;
} }
if (getAltOrientationFlagForToken(token, size)) { if (getAltOrientationFlagForToken(token, size)) {
offset *= -1; offset *= -1;
} }
if (canvas.grid.grid.columnar) { if (canvas.grid.columnar) {
center.x -= offset; center.x -= offset;
return center; return center;
} else { } else {
@@ -89,27 +89,27 @@ export function getSnapPointForToken(x, y, token) {
} }
const [topLeftX, topLeftY] = canvas.grid.getTopLeft(x, y); const [topLeftX, topLeftY] = canvas.grid.getTopLeft(x, y);
let cellX, cellY; let cell = {};
if (token.document.width % 2 === 0) cellX = x - canvas.grid.h / 2; if (token.document.width % 2 === 0) cell.x = x - canvas.grid.sizeY / 2;
else cellX = x; else cell.x = x;
if (token.document.height % 2 === 0) cellY = y - canvas.grid.h / 2; if (token.document.height % 2 === 0) cell.y = y - canvas.grid.sizeY / 2;
else cellY = y; else cell.y = y;
const [centerX, centerY] = canvas.grid.getCenter(cellX, cellY); const center = canvas.grid.getCenterPoint(cell);
let snapX, snapY; let snapX, snapY;
// Tiny tokens can snap to the cells corners // Tiny tokens can snap to the cells corners
if (token.document.width <= 0.5) { if (token.document.width <= 0.5) {
const offsetX = x - topLeftX; 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); const subGridPosX = Math.floor(offsetX / subGridWidth);
snapX = topLeftX + (subGridPosX + 0.5) * 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 // 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) { 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 // All remaining tokens (those with even or fractional multipliers on square grids) snap to the intersection points of the grid
else { else {
snapX = centerX + canvas.grid.w / 2; snapX = center.x + canvas.grid.sizeX / 2;
} }
if (token.document.height <= 0.5) { if (token.document.height <= 0.5) {
const offsetY = y - topLeftY; const offsetY = y - topLeftY;
@@ -117,9 +117,9 @@ export function getSnapPointForToken(x, y, token) {
const subGridPosY = Math.floor(offsetY / subGridHeight); const subGridPosY = Math.floor(offsetY / subGridHeight);
snapY = topLeftY + (subGridPosY + 0.5) * subGridHeight; snapY = topLeftY + (subGridPosY + 0.5) * subGridHeight;
} else if (Math.round(token.document.height) % 2 === 1 || token.document.height < 1) { } else if (Math.round(token.document.height) % 2 === 1 || token.document.height < 1) {
snapY = centerY; snapY = center.y;
} else { } else {
snapY = centerY + canvas.grid.h / 2; snapY = center.y + canvas.grid.sizeY / 2;
} }
return {x: snapX, y: snapY}; return {x: snapX, y: snapY};
} }
@@ -147,7 +147,7 @@ export function highlightTokenShape(position, shape, color, alpha) {
const area = getAreaFromPositionAndShape(position, shape); const area = getAreaFromPositionAndShape(position, shape);
for (const space of area) { for (const space of area) {
const [x, y] = getPixelsFromGridPosition(space.x, space.y); 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)); const isAltOrientation = getAltOrientationFlagForToken(token, getHexTokenSize(token));
if (canvas.grid.grid.columnar) { if (canvas.grid.grid.columnar) {
if (tokenSize.w % 2 === 0) { if (tokenSize.w % 2 === 0) {
waypointOffset.x = canvas.grid.w / 2; waypointOffset.x = canvas.grid.sizeX / 2;
if (isAltOrientation) waypointOffset.x *= -1; if (isAltOrientation) waypointOffset.x *= -1;
} }
} else { } else {
if (tokenSize.h % 2 === 0) { if (tokenSize.h % 2 === 0) {
waypointOffset.y = canvas.grid.h / 2; waypointOffset.y = canvas.grid.sizeY / 2;
if (isAltOrientation) waypointOffset.y *= -1; if (isAltOrientation) waypointOffset.y *= -1;
} }
} }
// If hex size support isn't active leave the waypoints like they are // If hex size support isn't active leave the waypoints like they are
} else { } else {
if (tokenSize.w % 2 === 0) { if (tokenSize.w % 2 === 0) {
waypointOffset.x = canvas.grid.w / 2; waypointOffset.x = canvas.grid.sizeX / 2;
} }
if (tokenSize.h % 2 === 0) { if (tokenSize.h % 2 === 0) {
waypointOffset.y = canvas.grid.h / 2; waypointOffset.y = canvas.grid.sizeY / 2;
} }
} }