Skip to content

Commit

Permalink
Merge branch 'release/0.7.8' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Jan 10, 2024
2 parents 3791add + 20e9ce0 commit e092010
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 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
41 changes: 17 additions & 24 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 @@ -112,26 +116,17 @@ function update(wrapper, data) {
function _addWaypoint(wrapper, point) {
wrapper(point);

// If moving a token, start the origin at the token center.
if ( this.waypoints.length === 1 ) {
// Temporarily replace the waypoint with the point so we can detect the token properly.
const snappedWaypoint = duplicate(this.waypoints[0]);
// 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();

// Use grid center, not token center, so measurements in grid space work.
// For snapped 1x1 or 3x3 tokens, this is token center. For snapped 2x2, it is top left.
if ( token ) {
const c = token.center;
const newCoords = canvas.grid.grid.getCenter(c.x, c.y);
this.waypoints[0].copyFrom({ x: newCoords[0], y: newCoords[1] });
}
else this.waypoints[0].copyFrom(snappedWaypoint);
if ( token && !tokenIsSnapped(token) ) this._unsnappedOrigin = true;
else this.waypoints[0].copyFrom(oldWaypoint);
}

// Otherwise if shift was held, use the precise point.
else if ( this._unsnap ) this.waypoints.at(-1).copyFrom(point);

// Elevate the waypoint.
addWaypointElevationIncrements(this, point);
}
Expand Down Expand Up @@ -200,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 All @@ -216,7 +209,7 @@ function _computeDistance(wrapped, gridSpaces) {
* @see {Canvas._onDragLeftStart}
*/
function _onDragStart(wrapped, event) {
this._unsnap = event.shiftKey;
this._unsnap = event.shiftKey || canvas.scene.grid.type === CONST.GRID_TYPES.GRIDLESS;
return wrapped(event);
}

Expand All @@ -227,7 +220,7 @@ function _onDragStart(wrapped, event) {
* @see {Canvas._onDragLeftStart}
*/
function _onClickLeft(wrapped, event) {
this._unsnap = event.shiftKey;
this._unsnap = event.shiftKey || canvas.scene.grid.type === CONST.GRID_TYPES.GRIDLESS;
return wrapped(event);
}

Expand All @@ -238,7 +231,7 @@ function _onClickLeft(wrapped, event) {
* @see {Canvas._onClickRight}
*/
function _onClickRight(wrapped, event) {
this._unsnap = event.shiftKey;
this._unsnap = event.shiftKey || canvas.scene.grid.type === CONST.GRID_TYPES.GRIDLESS;
return wrapped(event);
}

Expand All @@ -249,7 +242,7 @@ function _onClickRight(wrapped, event) {
* @see {Canvas._onDragLeftMove}
*/
function _onMouseMove(wrapped, event) {
this._unsnap = event.shiftKey;
this._unsnap = event.shiftKey || canvas.scene.grid.type === CONST.GRID_TYPES.GRIDLESS;
return wrapped(event);
}

Expand All @@ -260,7 +253,7 @@ function _onMouseMove(wrapped, event) {
* @see {Canvas._onDragLeftDrop}
*/
function _onMouseUp(wrapped, event) {
this._unsnap = event.shiftKey;
this._unsnap = event.shiftKey || canvas.scene.grid.type === CONST.GRID_TYPES.GRIDLESS;
return wrapped(event);
}

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 e092010

Please sign in to comment.