From a10e60985d0a66ab4ea105e8b5755bad806c4f5a Mon Sep 17 00:00:00 2001 From: kobewi Date: Thu, 27 Jun 2024 18:41:42 +0200 Subject: [PATCH] Prevent selecting when a CanvasItem is selected --- editor/plugins/canvas_item_editor_plugin.cpp | 32 ++++++++++++++------ editor/plugins/canvas_item_editor_plugin.h | 2 +- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index ee5d90b1f9b6f8..4294b5479e1724 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -780,19 +780,24 @@ bool CanvasItemEditor::_select_click_on_item(CanvasItem *item, Point2 p_click_po return still_selected; } -List CanvasItemEditor::_get_edited_canvas_items(bool retrieve_locked, bool remove_canvas_item_if_parent_in_selection) const { +List CanvasItemEditor::_get_edited_canvas_items(bool p_retrieve_locked, bool p_remove_canvas_item_if_parent_in_selection, bool *r_has_locked_items) const { List selection; for (const KeyValue &E : editor_selection->get_selection()) { CanvasItem *ci = Object::cast_to(E.key); - if (ci && ci->is_visible_in_tree() && ci->get_viewport() == EditorNode::get_singleton()->get_scene_root() && (retrieve_locked || !_is_node_locked(ci))) { - CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data(ci); - if (se) { - selection.push_back(ci); + if (ci) { + if (ci->is_visible_in_tree() && ci->get_viewport() == EditorNode::get_singleton()->get_scene_root() && (p_retrieve_locked || !_is_node_locked(ci))) { + CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data(ci); + if (se) { + selection.push_back(ci); + } + } else if (r_has_locked_items) { + // CanvasItem is selected, but can't be interacted with. + *r_has_locked_items = true; } } } - if (remove_canvas_item_if_parent_in_selection) { + if (p_remove_canvas_item_if_parent_in_selection) { List filtered_selection; for (CanvasItem *E : selection) { if (!selection.find(E->get_parent())) { @@ -1454,7 +1459,8 @@ bool CanvasItemEditor::_gui_input_rotate(const Ref &p_event) { if (drag_type == DRAG_NONE) { if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) { if ((b->is_command_or_control_pressed() && !b->is_alt_pressed() && tool == TOOL_SELECT) || tool == TOOL_ROTATE) { - List selection = _get_edited_canvas_items(); + bool has_locked_items = false; + List selection = _get_edited_canvas_items(false, true, &has_locked_items); // Remove not movable nodes for (CanvasItem *E : selection) { @@ -1477,6 +1483,8 @@ bool CanvasItemEditor::_gui_input_rotate(const Ref &p_event) { } _save_canvas_item_state(drag_selection); return true; + } else { + return has_locked_items; } } } @@ -1912,7 +1920,8 @@ bool CanvasItemEditor::_gui_input_scale(const Ref &p_event) { // Drag resize handles if (drag_type == DRAG_NONE) { if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && ((b->is_alt_pressed() && b->is_command_or_control_pressed()) || tool == TOOL_SCALE)) { - List selection = _get_edited_canvas_items(); + bool has_locked_items = false; + List selection = _get_edited_canvas_items(false, true, &has_locked_items); if (selection.size() == 1) { CanvasItem *ci = selection.front()->get(); @@ -1941,6 +1950,8 @@ bool CanvasItemEditor::_gui_input_scale(const Ref &p_event) { _save_canvas_item_state(drag_selection); return true; } + } else { + return has_locked_items; } } } @@ -2051,7 +2062,8 @@ bool CanvasItemEditor::_gui_input_move(const Ref &p_event) { //Start moving the nodes if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) { if ((b->is_alt_pressed() && !b->is_command_or_control_pressed()) || tool == TOOL_MOVE) { - List selection = _get_edited_canvas_items(); + bool has_locked_items = false; + List selection = _get_edited_canvas_items(false, true, &has_locked_items); if (selection.size() > 0) { drag_selection.clear(); @@ -2084,6 +2096,8 @@ bool CanvasItemEditor::_gui_input_move(const Ref &p_event) { _save_canvas_item_state(drag_selection); return true; + } else { + return has_locked_items; } } } diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h index a9de5e9a0b12b1..9befbec964a701 100644 --- a/editor/plugins/canvas_item_editor_plugin.h +++ b/editor/plugins/canvas_item_editor_plugin.h @@ -430,7 +430,7 @@ class CanvasItemEditor : public VBoxContainer { ThemePreviewMode theme_preview = THEME_PREVIEW_PROJECT; void _switch_theme_preview(int p_mode); - List _get_edited_canvas_items(bool retrieve_locked = false, bool remove_canvas_item_if_parent_in_selection = true) const; + List _get_edited_canvas_items(bool p_retrieve_locked = false, bool p_remove_canvas_item_if_parent_in_selection = true, bool *r_has_locked_items = nullptr) const; Rect2 _get_encompassing_rect_from_list(const List &p_list); void _expand_encompassing_rect_using_children(Rect2 &r_rect, const Node *p_node, bool &r_first, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D(), bool include_locked_nodes = true); Rect2 _get_encompassing_rect(const Node *p_node);