Skip to content

Commit

Permalink
Redo elevationAtLocation to handle force-to-ground
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Apr 5, 2024
1 parent b0ffe0b commit c74f92c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
1 change: 1 addition & 0 deletions scripts/Ruler.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ function segmentGridHalfIntersection(gridCoords, a, b) {
*/
function _onDragStart(wrapped, event) {
Settings.FORCE_TO_GROUND = false;
this._userElevationIncrements = 0;
this._unsnap = event.shiftKey || canvas.scene.grid.type === CONST.GRID_TYPES.GRIDLESS;
return wrapped(event);
}
Expand Down
4 changes: 3 additions & 1 deletion scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ export class Settings extends ModuleSettingsAbstract {
{ key: "KeyG" }
],
onDown: _context => {
if ( !canvas.controls.ruler.active ) return;
const ruler = canvas.controls.ruler;
if ( !ruler.active ) return;
this.FORCE_TO_GROUND = !this.FORCE_TO_GROUND;
ruler.measure(ruler.destination, { force: true });
ui.notifications.info(`Ruler measure to ground ${this.FORCE_TO_GROUND ? "enabled" : "disabled"}.`);
},
precedence: CONST.KEYBINDING_PRECEDENCE.NORMAL
Expand Down
57 changes: 42 additions & 15 deletions scripts/terrain_elevation.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ elevationAtLocation -- all ruler types
Used by ruler to get elevation at waypoints and at the end of the ruler.
*/

import { MODULES_ACTIVE } from "./const.js";
import { MODULES_ACTIVE, MOVEMENT_TYPES } from "./const.js";
import { Settings } from "./settings.js";
import { Point3d } from "./geometry/3d/Point3d.js";

Expand Down Expand Up @@ -114,23 +114,50 @@ export function elevationAtLocation(location, token) {
token ??= this._getMovementToken();
const isTokenRuler = Settings.get(Settings.KEYS.TOKEN_RULER.ENABLED)
&& ui.controls.activeControl === "token"
&& ui.controls.activeTool === "select";
&& ui.controls.activeTool === "select"
&& token;

// 1. If forcing to ground, always use the terrain elevation.

/* If not forcing to ground:
2. No move token
--> Default: Use origin elevation
--> If hovering over a token, use that token's elevation
3. Move token, normal ruler
--> Default: Use move token elevation
--> If hovering over a token, use that token's elevation
4. Token ruler
--> Default: Use move token elevation
--> If token is walking, use terrain elevation
*/
const terrainElevationFn = () => this.constructor.terrainElevationAtLocation(location, {
movementToken: token,
startingElevation: this.originElevation
});

// If at the token, use the token's elevation.
if ( token && location.almostEqual(token.center) ) return token.elevationE;

// If normal ruler and not prioritizing the token elevation, use elevation of other tokens at this point.
if ( !isTokenRuler ) {
const maxTokenE = retrieveVisibleTokens()
.filter(t => t.constrainedTokenBorder.contains(location.x, location.y))
.reduce((e, t) => Math.max(t.elevationE, e), Number.NEGATIVE_INFINITY);
if ( isFinite(maxTokenE) ) return maxTokenE;
// #1 Forcing to ground
if ( Settings.FORCE_TO_GROUND ) return terrainElevationFn();

// #4 token ruler
if ( isTokenRuler ) {
if ( token.movementType === MOVEMENT_TYPES.WALK ) return terrainElevationFn();
return token.elevationE;
}

// Use the terrain at this point.
return this.constructor.terrainElevationAtLocation(location, {
movementToken: token,
startingElevation: this.originElevation });
// If at the token, use the token's elevation.
// if ( token && location.almostEqual(token.center) ) return token.elevationE;

// Check for other tokens at destination and use that elevation.
const maxTokenE = retrieveVisibleTokens()
.filter(t => t.constrainedTokenBorder.contains(location.x, location.y))
.reduce((e, t) => Math.max(t.elevationE, e), Number.NEGATIVE_INFINITY);
if ( isFinite(maxTokenE) ) return maxTokenE; // #2 or #3

// #3 move token
if ( token ) return token.elevationE;

// #2 no move token
return this.originElevation;
}

// ----- NOTE: HELPER FUNCTIONS ----- //
Expand Down

0 comments on commit c74f92c

Please sign in to comment.