If a measurement is being skipped because of the ruler's rate limiting, schedule the measurement for later to ensure the ruler sticks to the token

This commit is contained in:
Manuel Vögele
2021-05-20 12:00:37 +02:00
parent 732802c579
commit 3cbe41e2be
3 changed files with 26 additions and 4 deletions
+5
View File
@@ -1,3 +1,8 @@
## In development
### Bugfixes
- Fixed a bug that could cause the ruler to not end up at the token's center (especially if the token is being moved very quickly and then stopped abruptly)
## 1.7.1
### Bugfixes
- Fixed a bug that prevented players from moving their tokens ([#74](https://github.com/manuelVo/foundryvtt-drag-ruler/issues/74))
+21 -3
View File
@@ -16,6 +16,9 @@ export async function moveEntities(draggedEntity, selectedEntities) {
if (!this.visible || !this.destination) return false;
if (!draggedEntity) return;
// Wait until all scheduled measurements are done
await this.deferredMeasurementPromise;
// Get the movement rays and check collision along each Ray
// These rays are center-to-center for the purposes of collision checking
const rays = this.constructor.dragRulerGetRaysFromWaypoints(this.waypoints, this.destination);
@@ -103,8 +106,6 @@ export function onMouseMove(event) {
if (this._state === Ruler.STATES.MOVING) return;
// Extract event data
const mt = event._measureTime || 0;
const originalEvent = event.data.originalEvent;
const destination = {x: event.data.destination.x + this.rulerOffset.x, y: event.data.destination.y + this.rulerOffset.y}
// Hide any existing Token HUD
@@ -112,10 +113,27 @@ export function onMouseMove(event) {
delete event.data.hudState;
// Draw measurement updates
if (Date.now() - mt > 50) {
scheduleMeasurement.call(this, destination, event);
}
function scheduleMeasurement(destination, event) {
const measurementInterval = 50;
const mt = event._measureTime || 0;
const originalEvent = event.data.originalEvent;
if (Date.now() - mt > measurementInterval) {
this.measure(destination, {snap: !originalEvent.shiftKey});
event._measureTime = Date.now();
this._state = Ruler.STATES.MEASURING;
window.clearTimeout(this.deferredMeasurementTimeout);
this.deferredMeasurementTimeout = undefined;
this.deferredMeasurementResolve?.();
}
else {
this.deferredMeasurementData = {destination, event};
if (!this.deferredMeasurementTimeout) {
this.deferredMeasurementPromise = new Promise((resolve, reject) => this.deferredMeasurementResolve = resolve);
this.deferredMeasurementTimeout = window.setTimeout(() => scheduleMeasurement.call(this, this.deferredMeasurementData.destination, this.deferredMeasurementData.event), measurementInterval);
}
}
}
-1
View File
@@ -154,7 +154,6 @@ function onEntityDragLeftDrop(event) {
const ruler = canvas.controls.ruler
if (!ruler.isDragRuler)
return false
onMouseMove.call(ruler, event);
// When we're dragging a measured template no token will ever be selected,
// resulting in only the dragged template to be moved as would be expected
const selectedTokens = canvas.tokens.controlled