From 6a31048dbc15e9139a49b6fe22301789528c8279 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Mon, 26 Feb 2024 15:27:31 +0000 Subject: [PATCH] Tilemap editor - prevent changing tool when mouse buttons pressed Changing tool when painting prevented the corresponding commit of undo action when the mouse button was released. This led to undo actions getting out of sync and the undo system breaking the editor. This PR simply prevents changing tool while mouse buttons are pressed, and prevents the above scenario. --- editor/plugins/tile_map_editor_plugin.cpp | 15 +++++++++++++++ editor/plugins/tile_map_editor_plugin.h | 1 + 2 files changed, 16 insertions(+) diff --git a/editor/plugins/tile_map_editor_plugin.cpp b/editor/plugins/tile_map_editor_plugin.cpp index d8ddfc05468..872a0caeb53 100644 --- a/editor/plugins/tile_map_editor_plugin.cpp +++ b/editor/plugins/tile_map_editor_plugin.cpp @@ -150,6 +150,13 @@ void TileMapEditor::_update_button_tool() { } void TileMapEditor::_button_tool_select(int p_tool) { + if (_mouse_buttons_pressed) { + // Disallow changing tool when drawing, + // to prevent undo actions getting messed up + // and out of sync. + return; + } + tool = (Tool)p_tool; _update_button_tool(); switch (tool) { @@ -1160,6 +1167,14 @@ bool TileMapEditor::forward_gui_input(const Ref &p_event) { Ref mb = p_event; if (mb.is_valid()) { + // Keep track internally of which mouse buttons are pressed + // so we can disallow changing tool. + if (mb->is_pressed()) { + _mouse_buttons_pressed |= mb->get_button_index(); + } else { + _mouse_buttons_pressed &= ~mb->get_button_index(); + } + if (mb->get_button_index() == BUTTON_LEFT) { if (mb->is_pressed()) { if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) { diff --git a/editor/plugins/tile_map_editor_plugin.h b/editor/plugins/tile_map_editor_plugin.h index 824fcfc9ce0..07f69244af9 100644 --- a/editor/plugins/tile_map_editor_plugin.h +++ b/editor/plugins/tile_map_editor_plugin.h @@ -105,6 +105,7 @@ class TileMapEditor : public VBoxContainer { Tool tool; Tool last_tool; + uint32_t _mouse_buttons_pressed = 0; bool selection_active; bool mouse_over;