Compare commits

...

18 Commits

Author SHA1 Message Date
Manuel Vögele 43f26088b5 Release v1.6.1 2021-04-30 10:52:00 +02:00
Manuel Vögele 39a0787c79 Add documentation about what needs to be awaited in SpeedProvider.onMovementHistoryUpdate 2021-04-30 10:26:37 +02:00
Manuel Vögele 3be898e49c First move the token, then store it's path. This ensures that getMovedDistance always returns a valid result. 2021-04-30 09:53:53 +02:00
Manuel Vögele 817662bf30 Send a proper token to onMovementHistoryUpdate 2021-04-30 09:51:05 +02:00
Manuel Vögele 56527ccf75 Add a hook that's called when the movement history is updated to the API 2021-04-30 00:10:45 +02:00
Manuel Vögele 6bf8083f7a Add a function for resetting the movement history to the public api 2021-04-30 00:00:15 +02:00
Manuel Vögele b5ea1f2284 Release v1.6.0 2021-04-29 14:04:14 +02:00
Manuel Vögele 6d9870bedb Add reference to the github issue in changelog 2021-04-29 13:24:33 +02:00
Manuel Vögele afbe5f9473 Don't animate tokens when they are dropped with the alt key being pressed (resolves #3) 2021-04-29 13:18:35 +02:00
Manuel Vögele bae0e43b7b Document 70b166d8 in changelog 2021-04-29 12:46:25 +02:00
Manuel Vögele 1acc012d65 Cache the output of SpeedProvider.getRanges during a drag to increase speed provider performance 2021-04-29 12:40:17 +02:00
Manuel Vögele 4671e6de51 Only allow users to update the movement history for tokens they own 2021-04-29 00:20:24 +02:00
Manuel Vögele f0ef109658 Track the movement of all tokens that are moved simultaneously in one batch (huge performance bump if many tokens are moved) 2021-04-29 00:03:05 +02:00
Manuel Vögele f0d1ef9d48 There might not actually be a dragged token when trying to end the measurement 2021-04-28 23:25:22 +02:00
Manuel Vögele d04ea9b0b7 Remove leftover lines from a refactor that didn't do anything 2021-04-28 12:11:58 +02:00
Manuel Vögele ba8ab9d473 After moving a token only end the measurement if no new token is being dragged 2021-04-27 21:17:28 +02:00
Manuel Vögele 2691720090 Update all moved tokens at once (provides a huge performance bump) 2021-04-27 20:28:44 +02:00
Manuel Vögele 70b166d844 Add a context menu entry that the gm can use to reset the movement history 2021-04-26 11:09:56 +02:00
10 changed files with 137 additions and 42 deletions
+19
View File
@@ -1,3 +1,22 @@
## 1.6.1
### API
- Added `onMovementHistoryUpdate` callback to Speed Providers, that allows them to perform game systems specific improvements to the movement history
- Added `dragRuler.resetMovementHistory` that clears the stored movement history for a token.
## 1.6.0
### Performance
- Greatly increased the performance when playing on huge maps and when moving many tokens at once.
- Huge performance improvements for speed providers. (Technical details: `getRanges` is now being called way less frequently)
### New features
- GMs now have an option to reset the movement history for individual tokens in the right click menu of the combat tracker
- When releasing a dragged token while pressing Alt the token will be moved to the target location without an animation ([#3](https://github.com/manuelVo/foundryvtt-drag-ruler/issues/3))
### Bugfixes
- When starting to drag a new token while the previous one is still moving the ruler won't dissappear anymore when the previous token arrives at it's destination.
## 1.5.4
### Bugfixes
- Fixed a bug that prevented tokens from being moved when their movement history collides with a wall. ([#61](https://github.com/manuelVo/foundryvtt-drag-ruler/issues/61))
+1
View File
@@ -12,6 +12,7 @@
"text": "Drag Ruler benötigt das socketlib-Modul, um korrekt zu funktionieren. Bitte aktiviere das socketlib-Modul in dieser Welt."
}
},
"resetMovementHistory": "Bewegungsverlauf zurücksetzen",
"genericSpeedProvider": {
"settings": {
"dashMultiplier": {
+1
View File
@@ -12,6 +12,7 @@
"text": "Drag Ruler requires the socketlib module to work properly. Please activate the socketlib module in this world."
}
},
"resetMovementHistory": "Reset Movement History",
"genericSpeedProvider": {
"settings": {
"dashMultiplier": {
+2 -2
View File
@@ -2,7 +2,7 @@
"name": "drag-ruler",
"title": "Drag Ruler",
"description": "When dragging a token displays a ruler showing how far you've moved that token.",
"version": "1.5.4",
"version": "1.6.1",
"minimumCoreVersion" : "0.7.9",
"compatibleCoreVersion" : "0.7.9",
"authors": [
@@ -49,7 +49,7 @@
],
"socket": true,
"url": "https://github.com/manuelVo/foundryvtt-drag-ruler",
"download": "https://github.com/manuelVo/foundryvtt-drag-ruler/archive/v1.5.4.zip",
"download": "https://github.com/manuelVo/foundryvtt-drag-ruler/archive/v1.6.1.zip",
"manifest": "https://raw.githubusercontent.com/manuelVo/foundryvtt-drag-ruler/master/module.json",
"readme": "https://github.com/manuelVo/foundryvtt-drag-ruler/blob/master/README.md",
"changelog": "https://github.com/manuelVo/foundryvtt-drag-ruler/blob/master/CHANGELOG.md",
+36 -19
View File
@@ -33,45 +33,62 @@ export async function moveTokens(draggedToken, selectedTokens) {
// Execute the movement path.
// Transform each center-to-center ray into a top-left to top-left ray using the prior token offsets.
this._state = Ruler.STATES.MOVING;
await Promise.all(selectedTokens.map(token => {
// Return the promise so we can wait for it outside the loop
const offset = calculateTokenOffset(token, draggedToken)
return animateToken.call(this, token, rays, offset, wasPaused)
}))
await animateTokens.call(this, selectedTokens, draggedToken, rays, wasPaused);
// Once all animations are complete we can clear the ruler
if (this.draggedToken?.id === draggedToken.id)
this._endMeasurement();
}
// This is a modified version code extracted from Ruler.moveToken from foundry 0.7.9
async function animateToken(token, rays, tokenOffset, wasPaused) {
const offsetRays = rays.filter(r => !r.isPrevious).map(ray => applyOffsetToRay(ray, tokenOffset));
trackRays(token, offsetRays);
async function animateTokens(tokens, draggedToken, draggedRays, wasPaused) {
const newRays = draggedRays.filter(r => !r.isPrevious);
const tokenAnimationData = tokens.map(token => {
const tokenOffset = calculateTokenOffset(token, draggedToken);
const offsetRays = newRays.map(ray => applyOffsetToRay(ray, tokenOffset));
// Determine offset relative to the Token top-left.
// This is important so we can position the token relative to the ruler origin for non-1x1 tokens.
const firstWaypoint = this.waypoints.find(w => !w.isPrevious);
const origin = [firstWaypoint.x + tokenOffset.x, firstWaypoint.y + tokenOffset.y];
let dx, dy
let dx, dy;
if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) {
dx = token.data.x - origin[0]
dy = token.data.y - origin[1]
dx = token.data.x - origin[0];
dy = token.data.y - origin[1];
}
else {
dx = token.data.x - origin[0]
dy = token.data.y - origin[1]
dx = token.data.x - origin[0];
dy = token.data.y - origin[1];
}
return {token, rays: offsetRays, dx, dy};
});
for (const {token, rays} of tokenAnimationData) {
token._noAnimate = true;
for (let r of offsetRays) {
if (!wasPaused && game.paused) break;
const dest = [r.B.x, r.B.y];
const path = new Ray({ x: token.x, y: token.y }, { x: dest[0] + dx, y: dest[1] + dy });
await token.update(path.B);
await token.animateMovement(path);
}
const animate = !game.keyboard.isDown("Alt");
const startWaypoint = animate ? 0 : tokenAnimationData[0].rays.length - 1;
for (let i = startWaypoint;i < tokenAnimationData[0].rays.length; i++) {
if (!wasPaused && game.paused) break;
const tokenPaths = tokenAnimationData.map(({token, rays, dx, dy}) => {
const ray = rays[i];
const dest = [ray.B.x, ray.B.y];
const path = new Ray({x: token.x, y: token.y}, {x: dest[0] + dx, y: dest[1] + dy});
return {token, path};
});
const updates = tokenPaths.map(({token, path}) => {
return {x: path.B.x, y: path.B.y, _id: token.id};
});
await draggedToken.scene.updateEmbeddedEntity(draggedToken.constructor.embeddedName, updates, {animate});
if (animate)
await Promise.all(tokenPaths.map(({token, path}) => token.animateMovement(path)));
}
for (const {token} of tokenAnimationData) {
token._noAnimate = false;
}
trackRays(tokenAnimationData.map(({token}) => token), tokenAnimationData.map(({rays}) => rays));
}
function calculateTokenOffset(tokenA, tokenB) {
return {x: tokenA.data.x - tokenB.data.x, y: tokenA.data.y - tokenB.data.y}
+14 -2
View File
@@ -5,7 +5,7 @@ import {checkDependencies, getHexSizeSupportTokenGridCenter} from "./compatibili
import {moveTokens, onMouseMove} from "./foundry_imports.js"
import {performMigrations} from "./migration.js"
import {DragRulerRuler} from "./ruler.js";
import {getMovementHistory} from "./movement_tracking.js";
import {getMovementHistory, resetMovementHistory} from "./movement_tracking.js";
import {registerSettings, settingsKey} from "./settings.js"
import {SpeedProvider} from "./speed_provider.js"
@@ -22,6 +22,7 @@ Hooks.once("init", () => {
getMovedDistanceFromToken,
registerModule,
registerSystem,
resetMovementHistory,
}
})
@@ -42,6 +43,15 @@ Hooks.on("canvasReady", () => {
})
})
Hooks.on("getCombatTrackerEntryContext", function (html, menu) {
const entry = {
name: "drag-ruler.resetMovementHistory",
icon: '<i class="fas fa-undo-alt"></i>',
callback: li => resetMovementHistory(ui.combat.combat, li.data('combatant-id')),
};
menu.splice(1, 0, entry);
});
function hookTokenDragHandlers() {
const originalDragLeftStartHandler = Token.prototype._onDragLeftStart
Token.prototype._onDragLeftStart = function(event) {
@@ -176,7 +186,9 @@ export function getColorForDistance(startDistance, subDistance=0) {
return this.color
}
const distance = startDistance + subDistance
const ranges = getRangesFromSpeedProvider(this.draggedToken)
if (!this.dragRulerRanges)
this.dragRulerRanges = getRangesFromSpeedProvider(this.draggedToken)
const ranges = this.dragRulerRanges;
if (ranges.length === 0)
return this.color
const currentRange = ranges.reduce((minRange, currentRange) => {
+23 -4
View File
@@ -1,6 +1,6 @@
import {measureDistances} from "./compatibility.js";
import {updateCombatantDragRulerFlags} from "./socket.js";
import {getTokenShape} from "./util.js";
import {getTokenShape, zip} from "./util.js";
function initTrackingFlag(combatant) {
const initialFlag = {passedWaypoints: [], trackedRound: 0};
@@ -23,13 +23,21 @@ function getInitializedCombatant(token, combat) {
return combatant;
}
export async function trackRays(token, rays) {
// Only track movement if the current token is participating in the active combat
export async function trackRays(tokens, tokenRays) {
const combat = game.combat;
if (!combat)
return;
if (!combat.started)
return;
if (!(tokens instanceof Array)) {
tokens = [tokens];
tokenRays = [tokenRays];
}
const updates = Array.from(zip(tokens, tokenRays)).map(([token, rays]) => calculateUpdate(combat, token, rays)).filter(Boolean);
await updateCombatantDragRulerFlags(combat, updates);
}
function calculateUpdate(combat, token, rays) {
const combatant = getInitializedCombatant(token, combat);
if (!combatant)
return;
@@ -55,7 +63,7 @@ export async function trackRays(token, rays) {
waypoints.push(ray.A);
}
}
await updateCombatantDragRulerFlags(combat, combatant, dragRulerFlags);
return {_id: combatant._id, dragRulerFlags};
}
export function getMovementHistory(token) {
@@ -72,3 +80,14 @@ export function getMovementHistory(token) {
return [];
return dragRulerFlags.passedWaypoints ?? [];
}
export async function resetMovementHistory(combat, combatantId) {
const combatant = combat.getCombatant(combatantId);
const dragRulerFlags = combatant.flags.dragRuler;
if (!dragRulerFlags)
return;
dragRulerFlags.passedWaypoints = undefined;
dragRulerFlags.trackedRound = undefined;
dragRulerFlags.rulerState = undefined;
await updateCombatantDragRulerFlags(combat, [{_id: combatantId, dragRulerFlags}]);
}
+1
View File
@@ -14,6 +14,7 @@ export class DragRulerRuler extends Ruler {
super.clear();
this.previousWaypoints = [];
this.previousLabels.removeChildren().forEach(c => c.destroy());
this.dragRulerRanges = undefined;
}
async moveToken(event) {
+22 -5
View File
@@ -1,3 +1,5 @@
import {currentSpeedProvider} from "./api.js";
let socket;
Hooks.once("socketlib.ready", () => {
@@ -5,13 +7,28 @@ Hooks.once("socketlib.ready", () => {
socket.register("updateCombatantDragRulerFlags", _socketUpdateCombatantDragRulerFlags);
});
export async function updateCombatantDragRulerFlags(combat, combatant, flags) {
export function updateCombatantDragRulerFlags(combat, updates) {
const combatId = combat.id;
const combatantId = combatant._id;
return socket.executeAsGM(_socketUpdateCombatantDragRulerFlags, combatId, combatantId, flags);
// TODO Check if canvas.tokens.get is still neccessary in future foundry versions
return socket.executeAsGM(_socketUpdateCombatantDragRulerFlags, combatId, updates)
.then(() => currentSpeedProvider.onMovementHistoryUpdate(updates.map(update => canvas.tokens.get(combat.getCombatant(update._id).token._id))));
}
async function _socketUpdateCombatantDragRulerFlags(combatId, combatantId, flags) {
async function _socketUpdateCombatantDragRulerFlags(combatId, updates) {
const user = game.users.get(this.socketdata.userId);
const combat = game.combats.get(combatId);
await combat.updateEmbeddedEntity("Combatant", {_id: combatantId, flags: {dragRuler: flags}}, {diff: false});
const requestedUpdates = updates.length;
updates = updates.filter(update => {
const actor = combat.getCombatant(update._id).actor;
if (!actor)
return false;
return actor.hasPerm(user, "OWNER");
});
if (updates.length !== requestedUpdates) {
console.warn(`Some of the movement history updates requested by user '${game.users.get(this.socketdata.userId).name}' were not performed because the user lacks owner permissions for those tokens`);
}
updates = updates.map(update => {
return {_id: update._id, flags: {dragRuler: update.dragRulerFlags}};
});
await combat.updateEmbeddedEntity("Combatant", updates, {diff: false});
}
+8
View File
@@ -84,6 +84,14 @@ export class SpeedProvider {
return true
}
/**
* This hook is being called after Drag Ruler has updated the movement history for one or more tokens.
* It'll receive an array of tokens that have been updated.
* If your speed provider is storing any additional values that are relevant for the movement history, this function should
* await until those updates have completed inside foundry.
*/
async onMovementHistoryUpdate(tokens) {}
/**
* Returns the value that is currently set for the setting registered with the provided settingId.
*