Skip to content

Commit

Permalink
Add a keybinding for the terrain ruler toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelVo committed Jan 14, 2022
1 parent c01d9fb commit 19c238f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions lang/de.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"terrain-ruler": {
"keybinding": "Terrain Ruler umschalten",
"terrainRuler": "Messe die Entfernung mit schwierigem Gelände",
"settings": {
"defaultToggleState": {
Expand Down
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"terrain-ruler": {
"keybinding": "Toggle Terrain Ruler",
"terrainRuler": "Measure Distance with Difficult Terrain",
"settings": {
"defaultToggleState": {
Expand Down
24 changes: 14 additions & 10 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ patchRulerMeasure()

CONFIG.debug.terrainRuler = false

let terrainRulerTool
export let terrainRulerTool;

Hooks.once("init", () => {
registerSettings();
Expand Down Expand Up @@ -46,7 +46,7 @@ Hooks.on("getSceneControlButtons", controls => {
tokenControls.splice(tokenControls.findIndex(tool => tool.name === "ruler") + 1, 0, terrainRulerTool)
})

function updateTerrainRulerState(newState) {
export function updateTerrainRulerState(newState) {
terrainRuler.active = newState;
if (game.settings.get(settingsKey, "defaultToggleState") === DefaultToggleState.REMEMBER)
game.settings.set(settingsKey, "lastToggleState", newState);
Expand All @@ -65,15 +65,18 @@ function onDragLeftStart(wrapped, event) {
const layer = this.activeLayer;
const isRuler = game.activeTool === "ruler";
const isCtrlRuler = game.keyboard.isModifierActive(KeyboardManager.MODIFIER_KEYS.CONTROL) && (layer.name === "TokenLayer");
if (terrainRuler.active && (isRuler || isCtrlRuler)) {
// Show Terrain
if (game.settings.get("enhanced-terrain-layer", "show-on-drag"))
canvas.terrain.visible = true;

// Start measuring
if (isRuler || isCtrlRuler) {
const ruler = this.controls.ruler;
ruler.isTerrainRuler = true;
return ruler._onDragStart(event);
ruler.terrainRulerIsCandidate = true;
if (terrainRuler.active) {
// Show Terrain
if (game.settings.get("enhanced-terrain-layer", "show-on-drag"))
canvas.terrain.visible = true;

// Start measuring
ruler.isTerrainRuler = true;
return ruler._onDragStart(event);
}
}
return wrapped(event);
}
Expand All @@ -83,6 +86,7 @@ function endMeasurement(wrapped, event) {
canvas.terrain.visible = (canvas.terrain.showterrain || ui.controls.activeControl == "terrain");

this.isTerrainRuler = false;
this.terrainRulerIsCandidate = false;
return wrapped(event);
}

Expand Down
23 changes: 23 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { terrainRulerTool, updateTerrainRulerState } from "./main.js";

export const settingsKey = "terrain-ruler";

export const DefaultToggleState = Object.freeze({
Expand Down Expand Up @@ -31,6 +33,12 @@ export function registerSettings() {
type: Boolean,
default: true,
});

game.keybindings.register(settingsKey, "toggleTerrainRuler", {
name: "terrain-ruler.keybinding",
onDown: toggleTerrainRuler,
precedence: -1,
});
}

export function getDefaultToggleState() {
Expand All @@ -43,3 +51,18 @@ export function getDefaultToggleState() {
return game.settings.get(settingsKey, "lastToggleState");
}
}

function toggleTerrainRuler() {
const newState = !terrainRuler.active;
terrainRulerTool.active = newState;
ui.controls.render();
updateTerrainRulerState(newState);
const ruler = canvas.controls.ruler;
if (ruler.terrainRulerIsCandidate) {
ruler.isTerrainRuler = newState;
if (ruler._state === Ruler.STATES.MEASURING) {
const mousePosition = canvas.app.renderer.plugins.interaction.mouse.getLocalPosition(canvas.tokens);
ruler.measure(mousePosition);
}
}
}

0 comments on commit 19c238f

Please sign in to comment.