From f119107a165955d1a48436eea835baef64dd87b7 Mon Sep 17 00:00:00 2001 From: JannisX11 Date: Fri, 10 Nov 2023 17:12:04 +0100 Subject: [PATCH] Avoid keybind conflicts between tools --- js/interface/actions.js | 35 ++++++++++++++++++++++------------- js/interface/keyboard.js | 7 +++++++ js/texturing/painter.js | 3 +-- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/js/interface/actions.js b/js/interface/actions.js index 7db2f769..ce5fee67 100644 --- a/js/interface/actions.js +++ b/js/interface/actions.js @@ -428,10 +428,8 @@ class Tool extends Action { this.allowed_view_modes = data.allowed_view_modes || null; this.tool_settings = {}; - if (this.condition == undefined) { - this.condition = function() { - return !scope.modes || scope.modes.includes(Modes.id); - } + if (this.condition == undefined && this.modes instanceof Array) { + this.condition = {modes: this.modes}; } this.onCanvasClick = data.onCanvasClick; this.onTextureEditorClick = data.onTextureEditorClick; @@ -440,6 +438,7 @@ class Tool extends Action { this.node.onclick = () => { scope.select(); } + Tool.all.push(this); } select() { if (this === Toolbox.selected) return; @@ -495,19 +494,28 @@ class Tool extends Action { if (BARS.condition(this.condition, this)) { this.select() return true; - } else if (this.modes) { - for (var i = 0; i < this.modes.length; i++) { - var mode = Modes.options[this.modes[i]] - if (mode && Condition(mode.condition)) { - mode.select() - this.select() - return true; - } - } + } else if (this.modes && event instanceof KeyboardEvent == false) { + return this.switchModeAndSelect(); } return false; } + switchModeAndSelect() { + for (var i = 0; i < this.modes.length; i++) { + var mode = Modes.options[this.modes[i]] + if (mode && Condition(mode.condition)) { + mode.select() + this.select() + return true; + } + } + } + delete() { + super.delete(); + Tool.all.remove(this); + } } +Tool.all = []; +Tool.selected = null; class Toggle extends Action { constructor(id, data) { super(id, data); @@ -1808,6 +1816,7 @@ const BARS = { selectElements: true, cursor: 'copy', modes: ['edit'], + condition: {modes: ['edit']}, keybind: new Keybind({key: 'x'}), onCanvasClick(data) { Vertexsnap.canvasClick(data) diff --git a/js/interface/keyboard.js b/js/interface/keyboard.js index bdc29c69..4cf36d22 100644 --- a/js/interface/keyboard.js +++ b/js/interface/keyboard.js @@ -795,6 +795,13 @@ addEventListeners(document, 'keydown mousedown', function(e) { } } }) + if (!used && !open_dialog) { + for (let tool of Tool.all) { + if (tool.keybind && typeof tool.trigger === 'function' && tool.keybind.isTriggered(e)) { + if (tool.switchModeAndSelect(e)) break; + } + } + } } // Menu if (open_menu) { diff --git a/js/texturing/painter.js b/js/texturing/painter.js index 4d90692e..d7fd16f0 100644 --- a/js/texturing/painter.js +++ b/js/texturing/painter.js @@ -2463,7 +2463,6 @@ BARS.defineActions(function() { paintTool: true, allowed_view_modes: ['textured'], modes: ['paint'], - condition: {modes: ['paint']}, keybind: new Keybind({key: 'm'}), side_menu: new Menu('selection_tool', () => { let modes = { @@ -2521,7 +2520,7 @@ BARS.defineActions(function() { paintTool: true, allowed_view_modes: ['textured'], modes: ['paint'], - condition: {modes: ['paint']}, + keybind: new Keybind({key: 'v'}), onCanvasClick(data) { if (data && data.element) { Blockbench.showQuickMessage('message.copy_paste_tool_viewport')