Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Make "Show Node in Tree" button more user friendly

kobewi773730db7e63
diff --git a/editor/docks/scene_tree_dock.cpp b/editor/docks/scene_tree_dock.cpp
index 1ed07329be7..6088b8bf503 100644
--- a/editor/docks/scene_tree_dock.cpp
+++ b/editor/docks/scene_tree_dock.cpp
@@ -74,6 +74,7 @@
#include "scene/main/scene_tree.h"
#include "scene/property_utils.h"
#include "scene/resources/packed_scene.h"
+#include "scene/resources/style_box_flat.h"
#include "servers/display/display_server.h"
void SceneTreeDock::_nodes_drag_begin() {
@@ -298,6 +299,33 @@ void SceneTreeDock::_scene_tree_gui_input(Ref<InputEvent> p_event) {
}
}
+void SceneTreeDock::_scene_tree_draw() {
+ if (highlighted_item.is_null()) {
+ return;
+ }
+ Tree *tree = scene_tree->get_scene_tree();
+ TreeItem *item = ObjectDB::get_instance<TreeItem>(highlighted_item);
+ if (!item || item->get_tree() != tree) {
+ return;
+ }
+ Rect2 rect = tree->get_item_rect(item);
+ constexpr float shrink = 4;
+ rect.position.x += EDSCALE_RND(shrink);
+ rect.size.x -= EDSCALE_RND(shrink * 2);
+ theme_cache.item_highlight->set_border_color(Color(theme_cache.item_highlight->get_border_color(), MIN(highlight_timer, 1.0)));
+ tree->draw_style_box(theme_cache.item_highlight, rect);
+}
+
+void SceneTreeDock::_scene_tree_item_selected() {
+ if (highlighted_item.is_null()) {
+ return;
+ }
+ TreeItem *item = ObjectDB::get_instance<TreeItem>(highlighted_item);
+ if (item && scene_tree->get_scene_tree()->get_selected() == item) {
+ _cancel_highlight();
+ }
+}
+
void SceneTreeDock::instantiate(const String &p_file) {
Vector<String> scenes;
scenes.push_back(p_file);
@@ -1825,6 +1853,14 @@ void SceneTreeDock::_notification(int p_what) {
}
menu_subresources->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
+
+ if (theme_cache.item_highlight.is_null()) {
+ theme_cache.item_highlight.instantiate();
+ theme_cache.item_highlight->set_draw_center(false);
+ theme_cache.item_highlight->set_border_width_all(EDSCALE_RND(2));
+ theme_cache.item_highlight->set_corner_radius_all(8);
+ }
+ theme_cache.item_highlight->set_border_color(get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
} break;
case NOTIFICATION_DRAG_END: {
@@ -1863,6 +1899,19 @@ void SceneTreeDock::_notification(int p_what) {
scene_tree->set_selected(node_edited);
}
} break;
+
+ case NOTIFICATION_INTERNAL_PROCESS: {
+ TreeItem *item = ObjectDB::get_instance<TreeItem>(highlighted_item);
+ if (!item) {
+ _cancel_highlight();
+ }
+ highlight_timer -= get_process_delta_time();
+ if (highlight_timer <= 0.0) {
+ _cancel_highlight();
+ } else if (highlight_timer < 1.0) {
+ scene_tree->get_scene_tree()->queue_redraw();
+ }
+ } break;
}
}
@@ -3034,6 +3083,15 @@ void SceneTreeDock::_queue_update_script_button() {
callable_mp(this, &SceneTreeDock::_update_script_button).call_deferred();
}
+void SceneTreeDock::_cancel_highlight() {
+ if (highlighted_item.is_null()) {
+ return;
+ }
+ highlighted_item = ObjectID();
+ scene_tree->get_scene_tree()->queue_redraw();
+ set_process_internal(false);
+}
+
void SceneTreeDock::_selection_changed() {
int selection_size = editor_selection->get_selection().size();
if (selection_size > 1) {
@@ -3499,6 +3557,25 @@ void SceneTreeDock::set_selected(Node *p_node, bool p_emit_selected) {
scene_tree->set_selected(p_node, p_emit_selected);
}
+void SceneTreeDock::highlight_node(Node *p_node) {
+ TreeItem *item = scene_tree->get_node_item(p_node);
+ ERR_FAIL_NULL(item);
+
+ TreeItem *parent = item->get_parent();
+ while (parent) {
+ parent->set_collapsed(false);
+ parent = parent->get_parent();
+ }
+
+ Tree *tree = scene_tree->get_scene_tree();
+ tree->scroll_to_item(item);
+ tree->queue_redraw();
+ highlighted_item = item->get_instance_id();
+
+ highlight_timer = 3.5;
+ set_process_internal(true);
+}
+
void SceneTreeDock::_new_scene_from(const String &p_file) {
const List<Node *> selection = editor_selection->get_top_selected_node_list();
@@ -5146,9 +5223,12 @@ SceneTreeDock::SceneTreeDock(Node *p_scene_root, EditorSelection *p_editor_selec
scene_tree->connect("files_dropped", callable_mp(this, &SceneTreeDock::_files_dropped));
scene_tree->connect("script_dropped", callable_mp(this, &SceneTreeDock::_script_dropped));
scene_tree->connect("nodes_dragged", callable_mp(this, &SceneTreeDock::_nodes_drag_begin));
- scene_tree->get_scene_tree()->get_vscroll_bar()->connect("value_changed", callable_mp(this, &SceneTreeDock::_reset_hovering_timer).unbind(1));
+ scene_tree->get_scene_tree()->get_vscroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &SceneTreeDock::_reset_hovering_timer).unbind(1));
+ scene_tree->get_scene_tree()->get_vscroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &SceneTreeDock::_cancel_highlight).unbind(1));
scene_tree->get_scene_tree()->connect(SceneStringName(gui_input), callable_mp(this, &SceneTreeDock::_scene_tree_gui_input));
+ scene_tree->get_scene_tree()->connect(SceneStringName(draw), callable_mp(this, &SceneTreeDock::_scene_tree_draw));
+ scene_tree->get_scene_tree()->connect("cell_selected", callable_mp(this, &SceneTreeDock::_scene_tree_item_selected));
scene_tree->get_scene_tree()->connect("item_icon_double_clicked", callable_mp(this, &SceneTreeDock::_focus_node));
editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeDock::_selection_changed));
diff --git a/editor/docks/scene_tree_dock.h b/editor/docks/scene_tree_dock.h
index ab48b577f1a..26194021d80 100644
--- a/editor/docks/scene_tree_dock.h
+++ b/editor/docks/scene_tree_dock.h
@@ -45,6 +45,7 @@ class ReparentDialog;
class Shader;
class ShaderCreateDialog;
class ShaderMaterial;
+class StyleBoxFlat;
class TextureRect;
class VBoxContainer;
@@ -100,6 +101,10 @@ class SceneTreeDock : public EditorDock {
EDIT_SUBRESOURCE_BASE = 100
};
+ struct ThemeCache {
+ Ref<StyleBoxFlat> item_highlight;
+ } theme_cache;
+
Vector<ObjectID> subresources;
int current_option = 0;
@@ -245,9 +250,14 @@ class SceneTreeDock : public EditorDock {
Object *edited_object_at_drag_start = nullptr;
bool scene_tree_drag_active = false;
+ ObjectID highlighted_item;
+ float highlight_timer = 0;
+
virtual void input(const Ref<InputEvent> &p_event) override;
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
void _scene_tree_gui_input(Ref<InputEvent> p_event);
+ void _scene_tree_draw();
+ void _scene_tree_item_selected();
void _new_scene_from(const String &p_file);
void _set_node_owner_recursive(Node *p_node, Node *p_owner, const HashMap<const Node *, Node *> &p_inverse_duplimap);
@@ -257,6 +267,7 @@ class SceneTreeDock : public EditorDock {
void _selection_changed();
void _update_script_button();
void _queue_update_script_button();
+ void _cancel_highlight();
void _fill_path_renames(Vector<StringName> base_path, Vector<StringName> new_base_path, Node *p_node, HashMap<Node *, NodePath> *p_renames);
bool _has_tracks_to_delete(Node *p_node, List<Node *> &p_to_delete) const;
@@ -335,6 +346,7 @@ public:
void clear_previous_node_selection();
void set_selection(const Vector<Node *> &p_nodes);
void set_selected(Node *p_node, bool p_emit_selected = false);
+ void highlight_node(Node *p_node);
void fill_path_renames(Node *p_node, Node *p_new_parent, HashMap<Node *, NodePath> *p_renames);
void perform_node_renames(Node *p_base, HashMap<Node *, NodePath> *p_renames, HashMap<Ref<Animation>, HashSet<int>> *r_rem_anims = nullptr, LocalVector<Pair<StringName, StringName>> *r_folded_group_renames = nullptr);
void perform_node_replace(Node *p_base, Node *p_node, Node *p_by_node);
diff --git a/editor/inspector/editor_properties.cpp b/editor/inspector/editor_properties.cpp
index 573ddbdbfd9..e07459b755a 100644
--- a/editor/inspector/editor_properties.cpp
+++ b/editor/inspector/editor_properties.cpp
@@ -37,6 +37,7 @@
#include "core/object/callable_mp.h"
#include "core/object/class_db.h"
#include "core/string/translation_server.h"
+#include "editor/docks/editor_dock_manager.h"
#include "editor/docks/inspector_dock.h"
#include "editor/docks/scene_tree_dock.h"
#include "editor/editor_node.h"
@@ -3089,15 +3090,37 @@ void EditorPropertyNodePath::_assign_draw() {
}
}
-void EditorPropertyNodePath::_update_menu() {
+void EditorPropertyNodePath::_popup_menu(const Vector2i &p_pos) {
+ if (!menu) {
+ menu = memnew(PopupMenu);
+ menu->add_item(TTRC("Clear"), ACTION_CLEAR);
+ menu->add_item(TTRC("Copy as Text"), ACTION_COPY);
+ menu->add_item(TTRC("Edit"), ACTION_EDIT);
+ menu->add_item(TTRC("Show Node in Tree"), ACTION_SELECT);
+ menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyNodePath::_menu_option));
+ add_child(menu);
+ notification(NOTIFICATION_THEME_CHANGED); // Update menu icons.
+ }
+
const NodePath &np = _get_node_path();
- menu->get_popup()->set_item_disabled(ACTION_CLEAR, np.is_empty() || is_read_only());
- menu->get_popup()->set_item_disabled(ACTION_COPY, np.is_empty());
- menu->get_popup()->set_item_disabled(ACTION_EDIT, is_read_only());
+ menu->set_item_disabled(ACTION_CLEAR, np.is_empty() || is_read_only());
+ menu->set_item_disabled(ACTION_COPY, np.is_empty());
+ menu->set_item_disabled(ACTION_EDIT, is_read_only());
Node *edited_node = Object::cast_to<Node>(get_edited_object());
- menu->get_popup()->set_item_disabled(ACTION_SELECT, !edited_node || !edited_node->has_node(np));
+ menu->set_item_disabled(ACTION_SELECT, !edited_node || !edited_node->has_node(np));
+
+ menu->reset_size();
+
+ if (p_pos.x >= 0) {
+ menu->set_position(p_pos);
+ } else {
+ int ms = menu->get_contents_minimum_size().width;
+ Vector2 popup_pos = menu_button->get_screen_rect().get_end() - Vector2(ms, 0);
+ menu->set_position(popup_pos);
+ }
+ menu->popup();
}
void EditorPropertyNodePath::_menu_option(int p_idx) {
@@ -3133,7 +3156,8 @@ void EditorPropertyNodePath::_menu_option(int p_idx) {
Node *target_node = edited_node->get_node_or_null(np);
ERR_FAIL_NULL(target_node);
- SceneTreeDock::get_singleton()->set_selected(target_node);
+ SceneTreeDock::get_singleton()->make_visible();
+ callable_mp(SceneTreeDock::get_singleton(), &SceneTreeDock::highlight_node).call_deferred(target_node);
} break;
}
}
@@ -3169,6 +3193,14 @@ const NodePath EditorPropertyNodePath::_get_node_path() const {
}
}
+void EditorPropertyNodePath::_button_input(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> mb = p_event;
+
+ if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
+ _popup_menu(assign->get_screen_position() + mb->get_position());
+ }
+}
+
bool EditorPropertyNodePath::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
return !is_read_only() && is_drop_valid(p_data);
}
@@ -3269,11 +3301,13 @@ void EditorPropertyNodePath::setup(const Vector<StringName> &p_valid_types, bool
void EditorPropertyNodePath::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
- menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
- menu->get_popup()->set_item_icon(ACTION_CLEAR, get_editor_theme_icon(SNAME("Clear")));
- menu->get_popup()->set_item_icon(ACTION_COPY, get_editor_theme_icon(SNAME("ActionCopy")));
- menu->get_popup()->set_item_icon(ACTION_EDIT, get_editor_theme_icon(SNAME("Edit")));
- menu->get_popup()->set_item_icon(ACTION_SELECT, get_editor_theme_icon(SNAME("ExternalLink")));
+ menu_button->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
+ if (menu) {
+ menu->set_item_icon(ACTION_CLEAR, get_editor_theme_icon(SNAME("Clear")));
+ menu->set_item_icon(ACTION_COPY, get_editor_theme_icon(SNAME("ActionCopy")));
+ menu->set_item_icon(ACTION_EDIT, get_editor_theme_icon(SNAME("Edit")));
+ menu->set_item_icon(ACTION_SELECT, get_editor_theme_icon(SNAME("ExternalLink")));
+ }
// Use a constant width for the icon to avoid sizing issues or blurry icons.
assign->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
@@ -3354,19 +3388,14 @@ EditorPropertyNodePath::EditorPropertyNodePath() {
assign->set_expand_icon(true);
assign->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyNodePath::_node_assign));
assign->connect(SceneStringName(draw), callable_mp(this, &EditorPropertyNodePath::_assign_draw));
+ assign->connect(SceneStringName(gui_input), callable_mp(this, &EditorPropertyNodePath::_button_input));
SET_DRAG_FORWARDING_CD(assign, EditorPropertyNodePath);
hbc->add_child(assign);
- menu = memnew(MenuButton);
- menu->set_flat(true);
- menu->connect(SNAME("about_to_popup"), callable_mp(this, &EditorPropertyNodePath::_update_menu));
- hbc->add_child(menu);
-
- menu->get_popup()->add_item(TTR("Clear"), ACTION_CLEAR);
- menu->get_popup()->add_item(TTR("Copy as Text"), ACTION_COPY);
- menu->get_popup()->add_item(TTR("Edit"), ACTION_EDIT);
- menu->get_popup()->add_item(TTR("Show Node in Tree"), ACTION_SELECT);
- menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyNodePath::_menu_option));
+ menu_button = memnew(Button);
+ menu_button->set_button_mask(MouseButtonMask::LEFT | MouseButtonMask::RIGHT);
+ menu_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyNodePath::_popup_menu).bind(Vector2i(-1, -1)));
+ hbc->add_child(menu_button);
edit = memnew(LineEdit);
edit->set_accessibility_name(TTRC("Node Path"));
diff --git a/editor/inspector/editor_properties.h b/editor/inspector/editor_properties.h
index 7c891659f8e..2f30725c8b1 100644
--- a/editor/inspector/editor_properties.h
+++ b/editor/inspector/editor_properties.h
@@ -41,7 +41,6 @@ class EditorLocaleDialog;
class EditorResourcePicker;
class EditorSpinSlider;
class EditorVariantTypePopupMenu;
-class MenuButton;
class SceneTreeDialog;
class TextEdit;
class TextureButton;
@@ -694,7 +693,8 @@ class EditorPropertyNodePath : public EditorProperty {
};
Button *assign = nullptr;
- MenuButton *menu = nullptr;
+ Button *menu_button = nullptr;
+ PopupMenu *menu = nullptr;
LineEdit *edit = nullptr;
SceneTreeDialog *scene_tree = nullptr;
@@ -707,11 +707,12 @@ class EditorPropertyNodePath : public EditorProperty {
void _node_assign();
void _assign_draw();
Node *get_base_node();
- void _update_menu();
+ void _popup_menu(const Vector2i &p_pos);
void _menu_option(int p_idx);
void _accept_text();
void _text_submitted(const String &p_text);
const NodePath _get_node_path() const;
+ void _button_input(const Ref<InputEvent> &p_event);
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
diff --git a/editor/scene/scene_tree_editor.cpp b/editor/scene/scene_tree_editor.cpp
index 53072a508d3..eba53a61a92 100644
--- a/editor/scene/scene_tree_editor.cpp
+++ b/editor/scene/scene_tree_editor.cpp
@@ -1885,6 +1885,14 @@ void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeEditor::_selection_changed));
}
+TreeItem *SceneTreeEditor::get_node_item(Node *p_node) const {
+ const CachedNode *node = node_cache.cache.getptr(p_node);
+ if (node) {
+ return node->item;
+ }
+ return nullptr;
+}
+
void SceneTreeEditor::_update_selection(TreeItem *item) {
ERR_FAIL_NULL(item);
diff --git a/editor/scene/scene_tree_editor.h b/editor/scene/scene_tree_editor.h
index 92991a5b358..3981c2ad77e 100644
--- a/editor/scene/scene_tree_editor.h
+++ b/editor/scene/scene_tree_editor.h
@@ -257,6 +257,7 @@ public:
Node *get_selected();
void set_can_rename(bool p_can_rename) { can_rename = p_can_rename; }
void set_editor_selection(EditorSelection *p_selection);
+ TreeItem *get_node_item(Node *p_node) const;
void set_show_enabled_subscene(bool p_show) { show_enabled_subscene = p_show; }
void set_valid_types(const Vector<StringName> &p_valid);