Skip to content

Commit

Permalink
Merge branch 'feature/unsnapped-tokens' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Jan 10, 2024
2 parents dee6828 + b72d457 commit 20e9ce0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.7.8
More tweaks to how token origin and destination are set when dragging so that the token movement follows the position of the cloned dragged token. Revisits issue #30.
Fix issue where token dragging cannot move to the adjacent space. Closes issue #32.

# 0.7.7
Allow GM to move tokens using the ruler regardless of obstacle. Closes issue #27.
Cancel the ruler when canceling the drag. Closes issue #28.
Expand Down
6 changes: 4 additions & 2 deletions scripts/BaseGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ function _getRulerDestination(wrapped, ray, offset, token) {
if ( canvas.controls.ruler._unsnap ) return ray.B.add(offset);

// We are moving from the token center, so add back 1/2 width/height to offset.
offset.x += canvas.scene.dimensions.size * 0.5;
offset.y += canvas.scene.dimensions.size * 0.5;
if ( !canvas.controls.ruler._unsnappedOrigin ) {
offset.x += canvas.scene.dimensions.size * 0.5;
offset.y += canvas.scene.dimensions.size * 0.5;
}
return wrapped(ray, offset, token);
}

Expand Down
16 changes: 13 additions & 3 deletions scripts/Ruler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* globals
canvas,
duplicate,
CONST,
game,
ui
*/
Expand All @@ -26,6 +26,8 @@ import {
modifiedMoveDistance
} from "./segments.js";

import { tokenIsSnapped } from "./util.js";

/**
* Modified Ruler
* Measure elevation change at each waypoint and destination.
Expand Down Expand Up @@ -83,6 +85,7 @@ function toJSON(wrapper) {
const obj = wrapper();
obj._userElevationIncrements = this._userElevationIncrements;
obj._unsnap = this._unsnap;
obj._unsnappedOrigin = this._unsnappedOrigin;
return obj;
}

Expand All @@ -96,6 +99,7 @@ function update(wrapper, data) {
const triggerMeasure = this._userElevationIncrements !== data._userElevationIncrements;
this._userElevationIncrements = data._userElevationIncrements;
this._unsnap = data._unsnap;
this._unsnappedOrigin = data._unsnappedOrigin;
wrapper(data);

if ( triggerMeasure ) {
Expand All @@ -114,6 +118,14 @@ function _addWaypoint(wrapper, point) {

// If shift was held, use the precise point.
if ( this._unsnap ) this.waypoints.at(-1).copyFrom(point);
else if ( this.waypoints.length === 1 ) {
// Move the waypoint to find unsnapped token.
const oldWaypoint = duplicate(this.waypoints[0]);
this.waypoints[0].copyFrom(point);
const token = this._getMovementToken();
if ( token && !tokenIsSnapped(token) ) this._unsnappedOrigin = true;
else this.waypoints[0].copyFrom(oldWaypoint);
}

// Elevate the waypoint.
addWaypointElevationIncrements(this, point);
Expand Down Expand Up @@ -183,8 +195,6 @@ function _computeDistance(wrapped, gridSpaces) {
for ( const segment of this.segments ) {
segment.moveDistance = modifiedMoveDistance(segment.distance, segment.ray, token);
totalMoveDistance += segment.moveDistance;
// const { A, B } = segment.ray;
//console.debug(`${A.x},${A.y},${A.z} --> ${B.x},${B.y},${B.z}: distance ${segment.distance}, move ${segment.moveDistance}`);
}
this.totalMoveDistance = totalMoveDistance;
}
Expand Down
10 changes: 10 additions & 0 deletions scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,14 @@ export function * iterateGridUnderLine(origin, destination, { reverse = false }
}
}

/**
* Determine if a token is currently snapped to the grid.
* @param {Token} token
* @returns {boolean}
*/
export function tokenIsSnapped(token) {
const { x, y } = token.document;
const [snappedX, snappedY] = canvas.grid.grid.getTopLeft(x, y);
return snappedX.almostEqual(x) && snappedY.almostEqual(y);
}

0 comments on commit 20e9ce0

Please sign in to comment.