Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing compat issues with Foundry v12 #25

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions js/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ class Cache {
export class GriddedCache extends Cache {
reset() {
super.reset();
if (canvas.grid.isHex && canvas.grid.grid.columnar) {
this.gridWidth = Math.ceil(canvas.dimensions.width / ((3 / 4) * canvas.grid.w));
if (canvas.grid.isHexagonal && canvas.grid.columnar) {
this.gridWidth = Math.ceil(canvas.dimensions.width / ((3 / 4) * canvas.grid.sizeX));
} else {
this.gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.w);
this.gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.sizeX);
}
if (canvas.grid.isHex && !canvas.grid.grid.columnar) {
this.gridHeight = Math.ceil(canvas.dimensions.height / ((3 / 4) * canvas.grid.h));
if (canvas.grid.isHexagonal && !canvas.grid.columnar) {
this.gridHeight = Math.ceil(canvas.dimensions.height / ((3 / 4) * canvas.grid.sizeY));
} else {
this.gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.h);
this.gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.sizeY);
}
}

static getSnapPointIndexForTokenData(tokenData) {
if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) return 0;
if (canvas.grid.isHex) {
if (canvas.grid.isHexagonal) {
if (tokenData.hexSizeSupport?.altSnappingFlag) {
return tokenData.hexSizeSupport.borderSize % 2;
} else {
Expand All @@ -95,9 +95,8 @@ export class GriddedCache extends Cache {
let node = graph[pos.y][pos.x];
if (!node) {
const neighbors = [];
for (const neighborPos of canvas.grid.grid.getNeighbors(pos.y, pos.x).map(([y, x]) => {
return {x, y};
})) {
for (const neighborPos of canvas.grid.getAdjacentOffsets({x: pos.x, y: pos.y})
.map(a => { return {x: (a.i - 1) + pos.x, y: (a.j - 1) + pos.y} })) {
if (
neighborPos.x < 0 ||
neighborPos.y < 0 ||
Expand Down Expand Up @@ -219,7 +218,7 @@ export function stepCollidesWithWall(from, to, tokenData, adjustPos = false) {
adjustedStart = stepStart;
}
adjustedStart.t = adjustedStart.b = tokenData.elevation;
const source = new VisionSource({});
const source = new foundry.canvas.sources.PointVisionSource({});
return CONFIG.Canvas.polygonBackends.move.testCollision(adjustedStart, stepEnd, {
mode: "any",
type: "move",
Expand Down
12 changes: 7 additions & 5 deletions js/foundry_fixes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// 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.getPixelsFromGridPosition to be ordered inconsistently

// https://gitlab.com/foundrynet/foundryvtt/-/issues/4705
export function getPixelsFromGridPosition(xGrid, yGrid) {
const coord = canvas.grid.getTopLeftPoint({i: xGrid, j: yGrid});
if (canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS) {
return canvas.grid.grid.getPixelsFromGridPosition(yGrid, xGrid);
return [coord.y, coord.x];
}
return canvas.grid.grid.getPixelsFromGridPosition(xGrid, yGrid);
// return canvas.grid.getPixelsFromGridPosition(xGrid, 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
// Wrapper to fix a FoundryVTT bug that causes the return values of canvas.grid.getPixelsFromGridPosition to be ordered inconsistently
// https://gitlab.com/foundrynet/foundryvtt/-/issues/4705
export function getGridPositionFromPixels(xPixel, yPixel) {
const [x, y] = canvas.grid.grid.getGridPositionFromPixels(xPixel, yPixel);
const [x, y] = canvas.grid.getGridPositionFromPixels(xPixel, yPixel);
if (canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS) return [y, x];
return [x, y];
}
Expand Down
4 changes: 2 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ function initializePathfinder(from, to, options) {
? token.losHeight
: token.document.elevation;
}
if (canvas.grid.isHex) {
if (canvas.grid.isHexagonal) {
tokenData.size = getHexTokenSize(token);
tokenData.altOrientation = getAltOrientationFlagForToken(token, tokenData.size);
}
} else {
tokenData = {width: 1, height: 1};
elevation = elevation ?? 0;
if (canvas.grid.isHex) {
if (canvas.grid.isHexagonal) {
tokenData.size = 1;
tokenData.altOrientation = false;
}
Expand Down
4 changes: 2 additions & 2 deletions js/pathfinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class GriddedPathfinder {
node => node.estimated,
);
this.previousNodes = new Set();
this.gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.w);
this.gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.h);
this.gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.sizeX);
this.gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.sizeY);
this.startNode = cache.getInitializedNode(
this.startPos,
this.sizeIndex,
Expand Down
24 changes: 12 additions & 12 deletions js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getSnapPointForTokenData(x, y, tokenData) {
if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) {
return new PIXI.Point(x, y);
}
if (canvas.grid.isHex) {
if (canvas.grid.isHexagonal) {
if (tokenData.hexSizeSupport?.altSnappingFlag) {
if (tokenData.hexSizeSupport.borderSize % 2 === 0) {
const snapPoint = findVertexSnapPoint(x, y, tokenData.hexSizeSupport.altOrientationFlag);
Expand All @@ -24,18 +24,18 @@ function getSnapPointForTokenData(x, y, tokenData) {
}
}

const [topLeftX, topLeftY] = canvas.grid.getTopLeft(x, y);
const {x: topLeftX, y: topLeftY} = canvas.grid.getTopLeftPoint({x: x, y: y});
let cellX, cellY;
if (tokenData.width % 2 === 0) cellX = x - canvas.grid.h / 2;
if (tokenData.width % 2 === 0) cellX = x - canvas.grid.sizeY / 2;
else cellX = x;
if (tokenData.height % 2 === 0) cellY = y - canvas.grid.h / 2;
if (tokenData.height % 2 === 0) cellY = y - canvas.grid.sizeY / 2;
else cellY = y;
const [centerX, centerY] = canvas.grid.getCenter(cellX, cellY);
const {x: centerX, y: centerY} = canvas.grid.getCenterPoint({x: cellX, y: cellY});
let snapX, snapY;
// Tiny tokens can snap to the cells corners
if (tokenData.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;
}
Expand All @@ -45,17 +45,17 @@ function getSnapPointForTokenData(x, y, tokenData) {
}
// 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 = centerX + canvas.grid.sizeX / 2;
}
if (tokenData.height <= 0.5) {
const offsetY = y - topLeftY;
const subGridHeight = Math.floor(canvas.grid.h / 2);
const subGridHeight = Math.floor(canvas.grid.sizeY / 2);
const subGridPosY = Math.floor(offsetY / subGridHeight);
snapY = topLeftY + (subGridPosY + 0.5) * subGridHeight;
} else if (Math.round(tokenData.height) % 2 === 1 || tokenData.height < 1) {
snapY = centerY;
} else {
snapY = centerY + canvas.grid.h / 2;
snapY = centerY + canvas.grid.sizeY / 2;
}
return new PIXI.Point(snapX, snapY);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ export function getAreaFromPositionAndShape(position, shape) {
return shape.map(space => {
let x = position.x + space.x;
let y = position.y + space.y;
if (canvas.grid.isHex) {
if (canvas.grid.isHexagonal) {
let shiftedRow;
if (canvas.grid.grid.even) shiftedRow = 1;
else shiftedRow = 0;
Expand All @@ -164,7 +164,7 @@ export function getAreaFromPositionAndShape(position, shape) {

export function buildOffset(startPos, endPos) {
const offset = {x: endPos.x - startPos.x, y: endPos.y - startPos.y};
if (canvas.grid.isHex) {
if (canvas.grid.isHexagonal) {
if (canvas.grid.grid.columnar) {
offset.adjustmentNeeded = (startPos.x - endPos.x) % 2 !== 0;
offset.originEven = startPos.x % 2 === 0;
Expand All @@ -178,7 +178,7 @@ export function buildOffset(startPos, endPos) {

export function applyOffset(origin, offset) {
const pos = {x: origin.x + offset.x, y: origin.y + offset.y};
if (canvas.grid.isHex && offset.adjustmentNeeded) {
if (canvas.grid.isHexagonal && offset.adjustmentNeeded) {
if (canvas.grid.grid.columnar) {
if ((origin.x % 2 === 0) !== offset.originEven) {
if (offset.originEven === canvas.grid.grid.even) {
Expand Down