From 19c238f2f0bd775885819d3c0e1f26691a4233cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 14 Jan 2022 15:06:54 +0100 Subject: [PATCH] Add a keybinding for the terrain ruler toggle --- lang/de.json | 1 + lang/en.json | 1 + src/main.js | 24 ++++++++++++++---------- src/settings.js | 23 +++++++++++++++++++++++ 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/lang/de.json b/lang/de.json index 47575a5..a4ec9d9 100644 --- a/lang/de.json +++ b/lang/de.json @@ -1,5 +1,6 @@ { "terrain-ruler": { + "keybinding": "Terrain Ruler umschalten", "terrainRuler": "Messe die Entfernung mit schwierigem Gelände", "settings": { "defaultToggleState": { diff --git a/lang/en.json b/lang/en.json index 5c058ee..292da55 100644 --- a/lang/en.json +++ b/lang/en.json @@ -1,5 +1,6 @@ { "terrain-ruler": { + "keybinding": "Toggle Terrain Ruler", "terrainRuler": "Measure Distance with Difficult Terrain", "settings": { "defaultToggleState": { diff --git a/src/main.js b/src/main.js index bbbba9a..8f5f07c 100644 --- a/src/main.js +++ b/src/main.js @@ -8,7 +8,7 @@ patchRulerMeasure() CONFIG.debug.terrainRuler = false -let terrainRulerTool +export let terrainRulerTool; Hooks.once("init", () => { registerSettings(); @@ -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); @@ -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); } @@ -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); } diff --git a/src/settings.js b/src/settings.js index bdfe211..a94c0f7 100644 --- a/src/settings.js +++ b/src/settings.js @@ -1,3 +1,5 @@ +import { terrainRulerTool, updateTerrainRulerState } from "./main.js"; + export const settingsKey = "terrain-ruler"; export const DefaultToggleState = Object.freeze({ @@ -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() { @@ -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); + } + } +}