From 81d603ad0ef0ee40890a60d32271d61ab88dafad Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Sun, 7 Jun 2020 15:14:18 +0200 Subject: [PATCH] Replace all flags with one value when holding Cmd in the layers editor This behavior is inspired by Blender (except it's the other way around to preserve the current default behavior). Trying to enable a single enabled value with Cmd held will invert the current flags, which makes enabling all flags but one faster. --- editor/editor_properties.cpp | 40 +++++++++++++++++++++++------------- editor/editor_properties.h | 6 +++--- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index d6af7107a353..ee020223e6cb 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -809,7 +809,7 @@ EditorPropertyFlags::EditorPropertyFlags() { void EditorPropertyLayersGrid::_rename_pressed(int p_menu) { // Show rename popup for active layer. - if (renamed_layer_index == -1) { + if (renamed_layer_index == INT32_MAX) { return; } String name = names[renamed_layer_index]; @@ -902,8 +902,8 @@ void EditorPropertyLayersGrid::_update_hovered(const Vector2 &p_position) { } // Remove highlight when no square is hovered. - if (hovered_index != -1) { - hovered_index = -1; + if (hovered_index != INT32_MAX) { + hovered_index = INT32_MAX; queue_redraw(); } } @@ -913,20 +913,32 @@ void EditorPropertyLayersGrid::_on_hover_exit() { expand_hovered = false; queue_redraw(); } - if (hovered_index != -1) { - hovered_index = -1; + if (hovered_index != INT32_MAX) { + hovered_index = INT32_MAX; queue_redraw(); } } -void EditorPropertyLayersGrid::_update_flag() { - if (hovered_index >= 0) { +void EditorPropertyLayersGrid::_update_flag(bool p_replace) { + if (hovered_index != INT32_MAX) { // Toggle the flag. // We base our choice on the hovered flag, so that it always matches the hovered flag. - if (value & (1 << hovered_index)) { - value &= ~(1 << hovered_index); + if (p_replace) { + // Replace all flags with the hovered flag ("solo mode"), + // instead of toggling the hovered flags while preserving other flags' state. + if (value == uint32_t(1 << hovered_index)) { + // If the flag is already enabled, enable all other items and disable the current flag. + // This allows for quicker toggling. + value = INT32_MAX - (1 << hovered_index); + } else { + value = 1 << hovered_index; + } } else { - value |= (1 << hovered_index); + if (value & (1 << hovered_index)) { + value &= ~(1 << hovered_index); + } else { + value |= (1 << hovered_index); + } } emit_signal(SNAME("flag_changed"), value); @@ -951,10 +963,10 @@ void EditorPropertyLayersGrid::gui_input(const Ref &p_ev) { const Ref mb = p_ev; if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) { _update_hovered(mb->get_position()); - _update_flag(); + _update_flag(mb->is_command_or_control_pressed()); } if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { - if (hovered_index >= 0) { + if (hovered_index != INT32_MAX) { renamed_layer_index = hovered_index; layer_rename->set_position(get_screen_position() + mb->get_position()); layer_rename->reset_size(); @@ -987,7 +999,7 @@ void EditorPropertyLayersGrid::_notification(int p_what) { const int vofs = (grid_size.height - h) / 2; - int layer_index = 0; + uint32_t layer_index = 0; Point2 arrow_pos; @@ -1242,7 +1254,7 @@ void EditorPropertyLayers::_button_pressed() { } void EditorPropertyLayers::_menu_pressed(int p_menu) { - if (p_menu == grid->layer_count) { + if (uint32_t(p_menu) == grid->layer_count) { ProjectSettingsEditor::get_singleton()->popup_project_settings(true); ProjectSettingsEditor::get_singleton()->set_general_page(basename); } else { diff --git a/editor/editor_properties.h b/editor/editor_properties.h index 33e325bd42c0..281ab039869e 100644 --- a/editor/editor_properties.h +++ b/editor/editor_properties.h @@ -264,7 +264,7 @@ class EditorPropertyLayersGrid : public Control { bool expand_hovered = false; bool expanded = false; int expansion_rows = 0; - int hovered_index = -1; + uint32_t hovered_index = INT32_MAX; // Nothing is hovered. bool read_only = false; int renamed_layer_index = -1; PopupMenu *layer_rename = nullptr; @@ -275,7 +275,7 @@ class EditorPropertyLayersGrid : public Control { void _rename_operation_confirm(); void _update_hovered(const Vector2 &p_position); void _on_hover_exit(); - void _update_flag(); + void _update_flag(bool p_replace); Size2 get_grid_size() const; protected: @@ -285,7 +285,7 @@ class EditorPropertyLayersGrid : public Control { public: uint32_t value = 0; int layer_group_size = 0; - int layer_count = 0; + uint32_t layer_count = 0; Vector names; Vector tooltips;