Rename Ruler.draggedToken to Ruler.draggedEntity in preparation of measurement template suppport

This commit is contained in:
Manuel Vögele
2021-05-12 00:31:00 +02:00
parent a413d44795
commit fd81833583
3 changed files with 34 additions and 34 deletions
+15 -15
View File
@@ -7,21 +7,21 @@ import {recalculate} from "./socket.js";
import {applyTokenSizeOffset, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js"; import {applyTokenSizeOffset, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js";
// This is a modified version of Ruler.moveToken from foundry 0.7.9 // This is a modified version of Ruler.moveToken from foundry 0.7.9
export async function moveTokens(draggedToken, selectedTokens) { export async function moveTokens(draggedEntity, selectedTokens) {
let wasPaused = game.paused; let wasPaused = game.paused;
if (wasPaused && !game.user.isGM) { if (wasPaused && !game.user.isGM) {
ui.notifications.warn(game.i18n.localize("GAME.PausedWarning")); ui.notifications.warn(game.i18n.localize("GAME.PausedWarning"));
return false; return false;
} }
if (!this.visible || !this.destination) return false; if (!this.visible || !this.destination) return false;
if (!draggedToken) return; if (!draggedEntity) return;
// Get the movement rays and check collision along each Ray // Get the movement rays and check collision along each Ray
// These rays are center-to-center for the purposes of collision checking // These rays are center-to-center for the purposes of collision checking
const rays = this.constructor.dragRulerGetRaysFromWaypoints(this.waypoints, this.destination); const rays = this.constructor.dragRulerGetRaysFromWaypoints(this.waypoints, this.destination);
if (!game.user.isGM) { if (!game.user.isGM) {
const hasCollision = selectedTokens.some(token => { const hasCollision = selectedTokens.some(token => {
const offset = calculateTokenOffset(token, draggedToken) const offset = calculateTokenOffset(token, draggedEntity);
const offsetRays = rays.filter(ray => !ray.isPrevious).map(ray => applyOffsetToRay(ray, offset)) const offsetRays = rays.filter(ray => !ray.isPrevious).map(ray => applyOffsetToRay(ray, offset))
return offsetRays.some(r => canvas.walls.checkCollision(r)); return offsetRays.some(r => canvas.walls.checkCollision(r));
}) })
@@ -35,18 +35,18 @@ export async function moveTokens(draggedToken, selectedTokens) {
// Execute the movement path. // Execute the movement path.
// Transform each center-to-center ray into a top-left to top-left ray using the prior token offsets. // Transform each center-to-center ray into a top-left to top-left ray using the prior token offsets.
this._state = Ruler.STATES.MOVING; this._state = Ruler.STATES.MOVING;
await animateTokens.call(this, selectedTokens, draggedToken, rays, wasPaused); await animateTokens.call(this, selectedTokens, draggedEntity, rays, wasPaused);
// Once all animations are complete we can clear the ruler // Once all animations are complete we can clear the ruler
if (this.draggedToken?.id === draggedToken.id) if (this.draggedEntity?.id === draggedEntity.id)
this._endMeasurement(); this._endMeasurement();
} }
// This is a modified version code extracted from Ruler.moveToken from foundry 0.7.9 // This is a modified version code extracted from Ruler.moveToken from foundry 0.7.9
async function animateTokens(tokens, draggedToken, draggedRays, wasPaused) { async function animateTokens(tokens, draggedEntity, draggedRays, wasPaused) {
const newRays = draggedRays.filter(r => !r.isPrevious); const newRays = draggedRays.filter(r => !r.isPrevious);
const tokenAnimationData = tokens.map(token => { const tokenAnimationData = tokens.map(token => {
const tokenOffset = calculateTokenOffset(token, draggedToken); const tokenOffset = calculateTokenOffset(token, draggedEntity);
const offsetRays = newRays.map(ray => applyOffsetToRay(ray, tokenOffset)); const offsetRays = newRays.map(ray => applyOffsetToRay(ray, tokenOffset));
// Determine offset relative to the Token top-left. // Determine offset relative to the Token top-left.
@@ -79,7 +79,7 @@ async function animateTokens(tokens, draggedToken, draggedRays, wasPaused) {
const updates = tokenPaths.map(({token, path}) => { const updates = tokenPaths.map(({token, path}) => {
return {x: path.B.x, y: path.B.y, _id: token.id}; return {x: path.B.x, y: path.B.y, _id: token.id};
}); });
await draggedToken.scene.updateEmbeddedEntity(draggedToken.constructor.embeddedName, updates, {animate}); await draggedEntity.scene.updateEmbeddedEntity(draggedEntity.constructor.embeddedName, updates, {animate});
if (animate) if (animate)
await Promise.all(tokenPaths.map(({token, path}) => token.animateMovement(path))); await Promise.all(tokenPaths.map(({token, path}) => token.animateMovement(path)));
} }
@@ -119,17 +119,17 @@ export function onMouseMove(event) {
// This is a modified version of Ruler.measure form foundry 0.7.9 // This is a modified version of Ruler.measure form foundry 0.7.9
export function measure(destination, {gridSpaces=true, snap=false} = {}) { export function measure(destination, {gridSpaces=true, snap=false} = {}) {
if (this.isDragRuler && !this.draggedToken.isVisible) if (this.isDragRuler && !this.draggedEntity.isVisible)
return [] return []
if (snap) if (snap)
destination = getSnapPointForToken(destination.x, destination.y, this.draggedToken) destination = getSnapPointForToken(destination.x, destination.y, this.draggedEntity);
const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && (!game.modules.get("TerrainLayer")?.active || canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS); const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && (!game.modules.get("TerrainLayer")?.active || canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS);
const waypoints = this.waypoints.concat([destination]); const waypoints = this.waypoints.concat([destination]);
// Move the waypoints to the center of the grid if a size is used that measures from edge to edge // Move the waypoints to the center of the grid if a size is used that measures from edge to edge
const centeredWaypoints = applyTokenSizeOffset(waypoints, this.draggedToken) const centeredWaypoints = applyTokenSizeOffset(waypoints, this.draggedEntity);
// Foundries native ruler requires the waypoints to sit in the dead center of the square to work properly // Foundries native ruler requires the waypoints to sit in the dead center of the square to work properly
if (!terrainRulerAvailable) if (!terrainRulerAvailable)
centeredWaypoints.forEach(w => [w.x, w.y] = canvas.grid.getCenter(w.x, w.y)); centeredWaypoints.forEach(w => [w.x, w.y] = canvas.grid.getCenter(w.x, w.y));
@@ -162,10 +162,10 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) {
} }
const shape = getTokenShape(this.draggedToken) const shape = getTokenShape(this.draggedEntity)
// Compute measured distance // Compute measured distance
const distances = measureDistances(centeredSegments, this.draggedToken, shape, {gridSpaces}); const distances = measureDistances(centeredSegments, this.draggedEntity, shape, {gridSpaces});
let totalDistance = 0; let totalDistance = 0;
for (let [i, d] of distances.entries()) { for (let [i, d] of distances.entries()) {
@@ -258,7 +258,7 @@ export function highlightMeasurementNative(ray, startDistance, tokenShape=[{x: 0
let [xg, yg] = canvas.grid.grid.getPixelsFromGridPosition(x1, y1); let [xg, yg] = canvas.grid.grid.getPixelsFromGridPosition(x1, y1);
const subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xg, y: yg})}], {gridSpaces: true})[0] const subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xg, y: yg})}], {gridSpaces: true})[0]
const color = dragRuler.getColorForDistance.call(this, startDistance, subDistance) const color = dragRuler.getColorForDistance.call(this, startDistance, subDistance)
const snapPoint = getSnapPointForToken(...canvas.grid.getTopLeft(x, y), this.draggedToken); const snapPoint = getSnapPointForToken(...canvas.grid.getTopLeft(x, y), this.draggedEntity);
const [snapX, snapY] = getGridPositionFromPixels(snapPoint.x + 1, snapPoint.y + 1); const [snapX, snapY] = getGridPositionFromPixels(snapPoint.x + 1, snapPoint.y + 1);
prior = [x1, y1]; prior = [x1, y1];
@@ -271,7 +271,7 @@ export function highlightMeasurementNative(ray, startDistance, tokenShape=[{x: 0
let [xgh, ygh] = canvas.grid.grid.getPixelsFromGridPosition(x1h, y1h); let [xgh, ygh] = canvas.grid.grid.getPixelsFromGridPosition(x1h, y1h);
const subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xgh, y: ygh})}], {gridSpaces: true})[0] const subDistance = canvas.grid.measureDistances([{ray: new Ray(ray.A, {x: xgh, y: ygh})}], {gridSpaces: true})[0]
const color = dragRuler.getColorForDistance.call(this, startDistance, subDistance) const color = dragRuler.getColorForDistance.call(this, startDistance, subDistance)
const snapPoint = getSnapPointForToken(...canvas.grid.getTopLeft(x, y), this.draggedToken); const snapPoint = getSnapPointForToken(...canvas.grid.getTopLeft(x, y), this.draggedEntity);
const [snapX, snapY] = getGridPositionFromPixels(snapPoint.x + 1, snapPoint.y + 1); const [snapX, snapY] = getGridPositionFromPixels(snapPoint.x + 1, snapPoint.y + 1);
highlightTokenShape.call(this, {x: snapX, y: snapY}, tokenShape, color, alpha); highlightTokenShape.call(this, {x: snapX, y: snapY}, tokenShape, color, alpha);
} }
+9 -9
View File
@@ -36,10 +36,10 @@ Hooks.once("ready", () => {
Hooks.on("canvasReady", () => { Hooks.on("canvasReady", () => {
canvas.controls.rulers.children.forEach(ruler => { canvas.controls.rulers.children.forEach(ruler => {
ruler.draggedToken = null ruler.draggedEntity = null;
Object.defineProperty(ruler, "isDragRuler", { Object.defineProperty(ruler, "isDragRuler", {
get: function isDragRuler() { get: function isDragRuler() {
return Boolean(this.draggedToken) // If draggedToken is set this is a drag ruler return Boolean(this.draggedEntity); // If draggedEntity is set this is a drag ruler
} }
}) })
}) })
@@ -128,7 +128,7 @@ function onTokenLeftDragStart(event) {
if (!currentSpeedProvider.usesRuler(this)) if (!currentSpeedProvider.usesRuler(this))
return return
const ruler = canvas.controls.ruler const ruler = canvas.controls.ruler
ruler.draggedToken = this ruler.draggedEntity = this;
let tokenCenter let tokenCenter
if (canvas.grid.isHex && game.modules.get("hex-size-support")?.active && CONFIG.hexSizeSupport.getAltSnappingFlag(this)) if (canvas.grid.isHex && game.modules.get("hex-size-support")?.active && CONFIG.hexSizeSupport.getAltSnappingFlag(this))
tokenCenter = getHexSizeSupportTokenGridCenter(this) tokenCenter = getHexSizeSupportTokenGridCenter(this)
@@ -156,9 +156,9 @@ function onTokenDragLeftDrop(event) {
const selectedTokens = canvas.tokens.controlled const selectedTokens = canvas.tokens.controlled
// This can happen if the user presses ESC during drag (maybe there are other ways too) // This can happen if the user presses ESC during drag (maybe there are other ways too)
if (selectedTokens.length === 0) if (selectedTokens.length === 0)
selectedTokens.push(ruler.draggedToken); selectedTokens.push(ruler.draggedEntity);
ruler._state = Ruler.STATES.MOVING ruler._state = Ruler.STATES.MOVING
moveTokens.call(ruler, ruler.draggedToken, selectedTokens) moveTokens.call(ruler, ruler.draggedEntity, selectedTokens);
return true return true
} }
@@ -183,18 +183,18 @@ function onTokenDragLeftCancel(event) {
export function getColorForDistance(startDistance, subDistance=0) { export function getColorForDistance(startDistance, subDistance=0) {
if (!this.isDragRuler) if (!this.isDragRuler)
return this.color return this.color
if (!this.draggedToken.actor) { if (!this.draggedEntity.actor) {
return this.color; return this.color;
} }
// Don't apply colors if the current user doesn't have at least observer permissions // Don't apply colors if the current user doesn't have at least observer permissions
if (this.draggedToken.actor.permission < 2) { if (this.draggedEntity.actor.permission < 2) {
// If this is a pc and alwaysShowSpeedForPCs is enabled we show the color anyway // If this is a pc and alwaysShowSpeedForPCs is enabled we show the color anyway
if (!(this.draggedToken.actor.data.type === "character" && game.settings.get(settingsKey, "alwaysShowSpeedForPCs"))) if (!(this.draggedEntity.actor.data.type === "character" && game.settings.get(settingsKey, "alwaysShowSpeedForPCs")))
return this.color return this.color
} }
const distance = startDistance + subDistance const distance = startDistance + subDistance
if (!this.dragRulerRanges) if (!this.dragRulerRanges)
this.dragRulerRanges = getRangesFromSpeedProvider(this.draggedToken) this.dragRulerRanges = getRangesFromSpeedProvider(this.draggedEntity);
const ranges = this.dragRulerRanges; const ranges = this.dragRulerRanges;
if (ranges.length === 0) if (ranges.length === 0)
return this.color return this.color
+10 -10
View File
@@ -33,18 +33,18 @@ export class DragRulerRuler extends Ruler {
toJSON() { toJSON() {
const json = super.toJSON(); const json = super.toJSON();
if (this.draggedToken) if (this.draggedEntity)
json["draggedToken"] = this.draggedToken.data._id; json["draggedEntity"] = this.draggedEntity.data._id;
return json; return json;
} }
update(data) { update(data) {
// Don't show a GMs drag ruler to non GM players // Don't show a GMs drag ruler to non GM players
if (data.draggedToken && this.user.isGM && !game.user.isGM && !game.settings.get(settingsKey, "showGMRulerToPlayers")) if (data.draggedEntity && this.user.isGM && !game.user.isGM && !game.settings.get(settingsKey, "showGMRulerToPlayers"))
return; return;
if (data.draggedToken) { if (data.draggedEntity) {
this.draggedToken = canvas.tokens.get(data.draggedToken); this.draggedEntity = canvas.tokens.get(data.draggedEntity);
} }
super.update(data); super.update(data);
} }
@@ -60,13 +60,13 @@ export class DragRulerRuler extends Ruler {
_endMeasurement() { _endMeasurement() {
super._endMeasurement(); super._endMeasurement();
this.draggedToken = null; this.draggedEntity = null;
} }
// The functions below aren't present in the orignal Ruler class and are added by Drag Ruler // The functions below aren't present in the orignal Ruler class and are added by Drag Ruler
dragRulerAddWaypoint(point, snap=true) { dragRulerAddWaypoint(point, snap=true) {
if (snap) if (snap)
point = getSnapPointForToken(point.x, point.y, this.draggedToken); point = getSnapPointForToken(point.x, point.y, this.draggedEntity);
this.waypoints.push(new PIXI.Point(point.x, point.y)); this.waypoints.push(new PIXI.Point(point.x, point.y));
this.labels.addChild(new PreciseText("", CONFIG.canvasTextStyle)); this.labels.addChild(new PreciseText("", CONFIG.canvasTextStyle));
} }
@@ -93,7 +93,7 @@ export class DragRulerRuler extends Ruler {
game.user.broadcastActivity({ruler: this}); game.user.broadcastActivity({ruler: this});
} }
else { else {
const token = this.draggedToken; const token = this.draggedEntity;
this._endMeasurement(); this._endMeasurement();
// Deactivate the drag workflow in mouse // Deactivate the drag workflow in mouse
@@ -109,12 +109,12 @@ export class DragRulerRuler extends Ruler {
async dragRulerRecalculate(tokenIds) { async dragRulerRecalculate(tokenIds) {
if (this._state !== Ruler.STATES.MEASURING) if (this._state !== Ruler.STATES.MEASURING)
return; return;
if (tokenIds && !tokenIds.includes(this.draggedToken.id)) if (tokenIds && !tokenIds.includes(this.draggedEntity.id))
return; return;
const waypoints = this.waypoints.filter(waypoint => !waypoint.isPrevious); const waypoints = this.waypoints.filter(waypoint => !waypoint.isPrevious);
this.dragRulerClearWaypoints(); this.dragRulerClearWaypoints();
if (game.settings.get(settingsKey, "enableMovementHistory")) if (game.settings.get(settingsKey, "enableMovementHistory"))
this.dragRulerAddWaypointHistory(getMovementHistory(this.draggedToken)); this.dragRulerAddWaypointHistory(getMovementHistory(this.draggedEntity));
for (const waypoint of waypoints) { for (const waypoint of waypoints) {
this.dragRulerAddWaypoint(waypoint, false); this.dragRulerAddWaypoint(waypoint, false);
} }