From 8b69cb2f65d5dadadb5f372d217109d72916680d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Mon, 8 Feb 2021 11:24:48 +0100 Subject: [PATCH] Allow swapping of right click and spacebar behavior (resolves #4) --- CHANGELOG.md | 4 ++++ lang/en.json | 4 ++++ src/main.js | 40 ++++++++++++++++++++++++++++++++-------- src/settings.js | 9 +++++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fc0d50..eb6e2d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## In development +### New features +- Right click and spacebar can now be swapped, allowing to place waypoints with right click and removing them with spacebar + ## v1.1.1 ### Bugfixes - Fixed a bug where tokens wouldn't be moved to the corect end position on gridless maps diff --git a/lang/en.json b/lang/en.json index f36bc72..a3c0243 100644 --- a/lang/en.json +++ b/lang/en.json @@ -21,6 +21,10 @@ "native": "Drag Ruler", "system": "System" } + }, + "swapSpacebarRightClick": { + "name": "Swap spacebar and right click", + "hint": "Swaps the functions of spacebar and right click during dragging. If enabled right click will place waypoints and spacebar will delete them" } } } diff --git a/src/main.js b/src/main.js index 0e48c92..0c960e6 100644 --- a/src/main.js +++ b/src/main.js @@ -124,30 +124,54 @@ function onTokenDragLeftDrop(event) { } function onTokenDragLeftCancel(event) { + // This function is invoked by right clicking if (!canvas.controls.ruler.isDragRuler) return false if (canvas.controls.ruler._state === Ruler.STATES.MEASURING) { - if (canvas.controls.ruler.waypoints.length > 1) { - canvas.controls.ruler._removeWaypoint(canvas.app.renderer.plugins.interaction.mouse.getLocalPosition(canvas.tokens), {snap: !event.shiftKey}) - game.user.broadcastActivity({ruler: canvas.controls.ruler}) - event.preventDefault() + if (!game.settings.get(settingsKey, "swapSpacebarRightClick")) { + if (canvas.controls.ruler.waypoints.length > 1) + event.preventDefault() + deleteWaypoint() } else { - canvas.controls.ruler._endMeasurement() - canvas.controls.ruler.draggedToken = null - return false + event.preventDefault() + canvas.controls.ruler._addWaypoint(canvas.controls.ruler.destination) } } return true } function onRulerMoveToken(event) { + // This function is invoked by left clicking if (!this.isDragRuler) return false - this._addWaypoint(this.destination) + if (!game.settings.get(settingsKey, "swapSpacebarRightClick")) + this._addWaypoint(this.destination) + else + deleteWaypoint() return true } +function deleteWaypoint() { + if (canvas.controls.ruler.waypoints.length > 1) { + canvas.controls.ruler._removeWaypoint(canvas.app.renderer.plugins.interaction.mouse.getLocalPosition(canvas.tokens), /* TODO What is this? */{snap: !event.shiftKey}) + game.user.broadcastActivity({ruler: canvas.controls.ruler}) + } + else { + const token = canvas.controls.ruler.draggedToken + canvas.controls.ruler._endMeasurement() + canvas.controls.ruler.draggedToken = null + + // Deactivate the drag workflow in mouse + token.mouseInteractionManager._deactivateDragEvents(); + token.mouseInteractionManager.state = token.mouseInteractionManager.states.HOVER; + + // This will cancel the current drag operation + // Pass in a fake event that hopefully is enough to allow other modules to function + token._onDragLeftCancel({preventDefault: () => {return}}) + } +} + function strInsertAfter(haystack, needle, strToInsert) { const pos = haystack.indexOf(needle) + needle.length return haystack.slice(0, pos) + strToInsert + haystack.slice(pos) diff --git a/src/settings.js b/src/settings.js index cfa1fbe..e8be3e1 100644 --- a/src/settings.js +++ b/src/settings.js @@ -4,6 +4,15 @@ import {getDefaultDashMultiplier, getDefaultSpeedAttribute} from "./systems.js" export const settingsKey = "drag-ruler"; export function registerSettings() { + game.settings.register(settingsKey, "swapSpacebarRightClick", { + name: "drag-ruler.settings.swapSpacebarRightClick.name", + hint: "drag-ruler.settings.swapSpacebarRightClick.hint", + scope: "client", + config: true, + type: Boolean, + default: false, + }) + game.settings.register(settingsKey, "alwaysShowSpeedForPCs", { name: "drag-ruler.settings.alwaysShowSpeedForPCs.name", hint: "drag-ruler.settings.alwaysShowSpeedForPCs.hint",