Skip to content

Commit

Permalink
Add setting to allow the default state of the terrain ruler toggle (r…
Browse files Browse the repository at this point in the history
…esolves #24)
  • Loading branch information
manuelVo committed Jan 14, 2022
1 parent e406c0f commit c01d9fb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lang/de.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{
"terrain-ruler": {
"terrainRuler": "Messe die Entfernung mit schwierigem Gelände"
"terrainRuler": "Messe die Entfernung mit schwierigem Gelände",
"settings": {
"defaultToggleState": {
"name": "Schalterzustand beim Start",
"hint": "Definiert welchen Zustand der Terrain Ruler Schalter beim Start von Foundry haben soll",
"choices": {
"on": "An",
"off": "Aus",
"remember": "Letzten Zustand merken"
}
}
}
}
}
13 changes: 12 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{
"terrain-ruler": {
"terrainRuler": "Measure Distance with Difficult Terrain"
"terrainRuler": "Measure Distance with Difficult Terrain",
"settings": {
"defaultToggleState": {
"name": "Default Toggle State",
"hint": "The state of the terrian ruler toggle after joining the game",
"choices": {
"on": "On",
"off": "Off",
"remember": "Remember last state"
}
}
}
}
}
12 changes: 10 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {libWrapper} from "../lib/libwrapper_shim.js";
import {getPixelsFromGridPosition} from "./foundry_fixes.js"
import {measureDistances, getCostEnhancedTerrainlayer} from "./measure.js"
import {DefaultToggleState, getDefaultToggleState, registerSettings, settingsKey} from "./settings.js";

// Patch the function as early as possible to decrease the chance of anyone having hooked it already
patchRulerMeasure()
Expand All @@ -10,9 +11,10 @@ CONFIG.debug.terrainRuler = false
let terrainRulerTool

Hooks.once("init", () => {
registerSettings();
hookFunctions()
window.terrainRuler = {
active: true,
active: getDefaultToggleState(),
measureDistances,
};
Object.defineProperty(game, "terrainRuler", {
Expand All @@ -36,14 +38,20 @@ Hooks.on("getSceneControlButtons", controls => {
icon: "fas fa-hiking",
toggle: true,
active: terrainRuler?.active,
onClick: toggled => terrainRuler.active = toggled,
onClick: updateTerrainRulerState,
visible: true,
}
}
const tokenControls = controls.find(group => group.name === "token").tools
tokenControls.splice(tokenControls.findIndex(tool => tool.name === "ruler") + 1, 0, terrainRulerTool)
})

function updateTerrainRulerState(newState) {
terrainRuler.active = newState;
if (game.settings.get(settingsKey, "defaultToggleState") === DefaultToggleState.REMEMBER)
game.settings.set(settingsKey, "lastToggleState", newState);
}

function hookFunctions() {
libWrapper.register("terrain-ruler", "Canvas.prototype._onDragLeftStart", onDragLeftStart, "MIXED");
libWrapper.register("terrain-ruler", "Ruler.prototype._endMeasurement", endMeasurement, "WRAPPER");
Expand Down
45 changes: 45 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export const settingsKey = "terrain-ruler";

export const DefaultToggleState = Object.freeze({
ON: 0,
OFF: 1,
REMEMBER: 2,
});

export function registerSettings() {
game.settings.register(settingsKey, "defaultToggleState", {
name: "terrain-ruler.settings.defaultToggleState.name",
hint: "terrain-ruler.settings.defaultToggleState.hint",
scope: "client",
config: true,
type: Number,
default: DefaultToggleState.ON,
choices: {
0: "terrain-ruler.settings.defaultToggleState.choices.on",
1: "terrain-ruler.settings.defaultToggleState.choices.off",
2: "terrain-ruler.settings.defaultToggleState.choices.remember",
},
onChange: (value) => {
if (value === DefaultToggleState.REMEMBER)
game.settings.set(settingsKey, "lastToggleState", terrainRuler.active);
},
});

game.settings.register(settingsKey, "lastToggleState", {
scope: "client",
config: false,
type: Boolean,
default: true,
});
}

export function getDefaultToggleState() {
switch (game.settings.get(settingsKey, "defaultToggleState")) {
case DefaultToggleState.ON:
return true;
case DefaultToggleState.OFF:
return false;
case DefaultToggleState.REMEMBER:
return game.settings.get(settingsKey, "lastToggleState");
}
}

0 comments on commit c01d9fb

Please sign in to comment.