Path: blob/master/editor/debugger/editor_debugger_tree.cpp
9896 views
/**************************************************************************/1/* editor_debugger_tree.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_debugger_tree.h"3132#include "editor/debugger/editor_debugger_node.h"33#include "editor/docks/scene_tree_dock.h"34#include "editor/editor_node.h"35#include "editor/editor_string_names.h"36#include "editor/gui/editor_file_dialog.h"37#include "editor/gui/editor_toaster.h"38#include "editor/settings/editor_settings.h"39#include "scene/debugger/scene_debugger.h"40#include "scene/gui/texture_rect.h"41#include "scene/resources/packed_scene.h"42#include "servers/display_server.h"4344EditorDebuggerTree::EditorDebuggerTree() {45set_v_size_flags(SIZE_EXPAND_FILL);46set_allow_rmb_select(true);47set_select_mode(SELECT_MULTI);4849// Popup50item_menu = memnew(PopupMenu);51item_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorDebuggerTree::_item_menu_id_pressed));52add_child(item_menu);5354// File Dialog55file_dialog = memnew(EditorFileDialog);56file_dialog->connect("file_selected", callable_mp(this, &EditorDebuggerTree::_file_selected));57add_child(file_dialog);5859accept = memnew(AcceptDialog);60add_child(accept);61}6263void EditorDebuggerTree::_notification(int p_what) {64switch (p_what) {65case NOTIFICATION_POSTINITIALIZE: {66set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);6768connect("multi_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_selection_changed));69connect("nothing_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_nothing_selected));70connect("item_collapsed", callable_mp(this, &EditorDebuggerTree::_scene_tree_folded));71connect("item_mouse_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_rmb_selected));72} break;7374case NOTIFICATION_ENTER_TREE: {75update_icon_max_width();76} break;77}78}7980void EditorDebuggerTree::_bind_methods() {81ADD_SIGNAL(MethodInfo("objects_selected", PropertyInfo(Variant::ARRAY, "object_ids"), PropertyInfo(Variant::INT, "debugger")));82ADD_SIGNAL(MethodInfo("selection_cleared", PropertyInfo(Variant::INT, "debugger")));83ADD_SIGNAL(MethodInfo("save_node", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "filename"), PropertyInfo(Variant::INT, "debugger")));84ADD_SIGNAL(MethodInfo("open"));85}8687void EditorDebuggerTree::_scene_tree_selection_changed(TreeItem *p_item, int p_column, bool p_selected) {88if (updating_scene_tree || !p_item) {89return;90}9192uint64_t id = uint64_t(p_item->get_metadata(0));93if (p_selected) {94if (inspected_object_ids.size() == (int)EDITOR_GET("debugger/max_node_selection")) {95selection_surpassed_limit = true;96p_item->deselect(0);97} else if (!inspected_object_ids.has(id)) {98inspected_object_ids.append(id);99}100} else if (inspected_object_ids.has(id)) {101inspected_object_ids.erase(id);102}103104if (!notify_selection_queued) {105callable_mp(this, &EditorDebuggerTree::_notify_selection_changed).call_deferred();106notify_selection_queued = true;107}108}109110void EditorDebuggerTree::_scene_tree_nothing_selected() {111deselect_all();112inspected_object_ids.clear();113emit_signal(SNAME("selection_cleared"), debugger_id);114}115116void EditorDebuggerTree::_notify_selection_changed() {117notify_selection_queued = false;118119if (inspected_object_ids.is_empty()) {120emit_signal(SNAME("selection_cleared"), debugger_id);121} else {122emit_signal(SNAME("objects_selected"), inspected_object_ids.duplicate(), debugger_id);123}124125if (selection_surpassed_limit) {126selection_surpassed_limit = false;127EditorToaster::get_singleton()->popup_str(vformat(TTR("Some remote nodes were not selected, as the configured maximum selection is %d. This can be changed at \"debugger/max_node_selection\" in the Editor Settings."), EDITOR_GET("debugger/max_node_selection")), EditorToaster::SEVERITY_WARNING);128}129}130131void EditorDebuggerTree::_scene_tree_folded(Object *p_obj) {132if (updating_scene_tree) {133return;134}135TreeItem *item = Object::cast_to<TreeItem>(p_obj);136137if (!item) {138return;139}140141ObjectID id = ObjectID(uint64_t(item->get_metadata(0)));142if (unfold_cache.has(id)) {143unfold_cache.erase(id);144} else {145unfold_cache.insert(id);146}147}148149void EditorDebuggerTree::_scene_tree_rmb_selected(const Vector2 &p_position, MouseButton p_button) {150if (p_button != MouseButton::RIGHT) {151return;152}153154TreeItem *item = get_item_at_position(p_position);155if (!item) {156return;157}158159item->select(0);160161item_menu->clear();162item_menu->add_icon_item(get_editor_theme_icon(SNAME("CreateNewSceneFrom")), TTR("Save Branch as Scene..."), ITEM_MENU_SAVE_REMOTE_NODE);163item_menu->add_icon_item(get_editor_theme_icon(SNAME("CopyNodePath")), TTR("Copy Node Path"), ITEM_MENU_COPY_NODE_PATH);164item_menu->add_icon_item(get_editor_theme_icon(SNAME("Collapse")), TTR("Expand/Collapse Branch"), ITEM_MENU_EXPAND_COLLAPSE);165item_menu->set_position(get_screen_position() + get_local_mouse_position());166item_menu->reset_size();167item_menu->popup();168}169170/// Populates inspect_scene_tree given data in nodes as a flat list, encoded depth first.171///172/// Given a nodes array like [R,A,B,C,D,E] the following Tree will be generated, assuming173/// filter is an empty String, R and A child count are 2, B is 1 and C, D and E are 0.174///175/// R176/// |-A177/// | |-B178/// | | |-C179/// | |180/// | |-D181/// |182/// |-E183///184void EditorDebuggerTree::update_scene_tree(const SceneDebuggerTree *p_tree, int p_debugger) {185set_hide_root(false);186187updating_scene_tree = true;188const String last_path = get_selected_path();189const String filter = SceneTreeDock::get_singleton()->get_filter();190LocalVector<TreeItem *> select_items;191bool hide_filtered_out_parents = EDITOR_GET("docks/scene_tree/hide_filtered_out_parents");192193bool should_scroll = scrolling_to_item || filter != last_filter;194scrolling_to_item = false;195TreeItem *scroll_item = nullptr;196TypedArray<uint64_t> ids_present;197198// Nodes are in a flatten list, depth first. Use a stack of parents, avoid recursion.199List<ParentItem> parents;200for (const SceneDebuggerTree::RemoteNode &node : p_tree->nodes) {201TreeItem *parent = nullptr;202Pair<TreeItem *, TreeItem *> move_from_to;203if (parents.size()) { // Find last parent.204ParentItem &p = parents.front()->get();205parent = p.tree_item;206if (!(--p.child_count)) { // If no child left, remove it.207parents.pop_front();208209if (hide_filtered_out_parents && !filter.is_subsequence_ofn(parent->get_text(0))) {210if (parent == get_root()) {211set_hide_root(true);212} else {213move_from_to.first = parent;214// Find the closest ancestor that matches the filter.215for (const ParentItem p2 : parents) {216move_from_to.second = p2.tree_item;217if (p2.matches_filter || move_from_to.second == get_root()) {218break;219}220}221222if (!move_from_to.second) {223move_from_to.second = get_root();224}225}226}227}228}229230// Add this node.231TreeItem *item = create_item(parent);232item->set_text(0, node.name);233if (node.scene_file_path.is_empty()) {234item->set_tooltip_text(0, node.name + "\n" + TTR("Type:") + " " + node.type_name);235} else {236item->set_tooltip_text(0, node.name + "\n" + TTR("Instance:") + " " + node.scene_file_path + "\n" + TTR("Type:") + " " + node.type_name);237}238Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(node.type_name, "");239if (icon.is_valid()) {240item->set_icon(0, icon);241}242item->set_metadata(0, node.id);243244String current_path;245if (parent) {246current_path += (String)parent->get_meta("node_path");247248// Set current item as collapsed if necessary (root is never collapsed).249if (!unfold_cache.has(node.id)) {250item->set_collapsed(true);251}252}253item->set_meta("node_path", current_path + "/" + item->get_text(0));254255// Select previously selected nodes.256if (debugger_id == p_debugger) { // Can use remote id.257if (inspected_object_ids.has(uint64_t(node.id))) {258ids_present.append(node.id);259260if (selection_uncollapse_all) {261selection_uncollapse_all = false;262263// Temporarily set to `false`, to allow caching the unfolds.264updating_scene_tree = false;265item->uncollapse_tree();266updating_scene_tree = true;267}268269select_items.push_back(item);270if (should_scroll) {271scroll_item = item;272}273}274} else if (last_path == (String)item->get_meta("node_path")) { // Must use path.275updating_scene_tree = false; // Force emission of new selections.276select_items.push_back(item);277if (should_scroll) {278scroll_item = item;279}280updating_scene_tree = true;281}282283// Add buttons.284const Color remote_button_color = Color(1, 1, 1, 0.8);285if (!node.scene_file_path.is_empty()) {286String node_scene_file_path = node.scene_file_path;287Ref<Texture2D> button_icon = get_editor_theme_icon(SNAME("InstanceOptions"));288String tooltip = vformat(TTR("This node has been instantiated from a PackedScene file:\n%s\nClick to open the original file in the Editor."), node_scene_file_path);289290item->set_meta("scene_file_path", node_scene_file_path);291item->add_button(0, button_icon, BUTTON_SUBSCENE, false, tooltip);292item->set_button_color(0, item->get_button_count(0) - 1, remote_button_color);293}294295if (node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_HAS_VISIBLE_METHOD) {296bool node_visible = node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_VISIBLE;297bool node_visible_in_tree = node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_VISIBLE_IN_TREE;298Ref<Texture2D> button_icon = get_editor_theme_icon(node_visible ? SNAME("GuiVisibilityVisible") : SNAME("GuiVisibilityHidden"));299String tooltip = TTR("Toggle Visibility");300301item->set_meta("visible", node_visible);302item->add_button(0, button_icon, BUTTON_VISIBILITY, false, tooltip);303if (ClassDB::is_parent_class(node.type_name, "CanvasItem") || ClassDB::is_parent_class(node.type_name, "Node3D")) {304item->set_button_color(0, item->get_button_count(0) - 1, node_visible_in_tree ? remote_button_color : Color(1, 1, 1, 0.6));305} else {306item->set_button_color(0, item->get_button_count(0) - 1, remote_button_color);307}308}309310// Add in front of the parents stack if children are expected.311if (node.child_count) {312parents.push_front(ParentItem(item, node.child_count, filter.is_subsequence_ofn(item->get_text(0))));313} else {314// Apply filters.315while (parent) {316const bool had_siblings = item->get_prev() || item->get_next();317if (filter.is_subsequence_ofn(item->get_text(0))) {318break; // Filter matches, must survive.319}320321if (select_items.has(item) || scroll_item == item) {322select_items.resize(select_items.size() - 1);323scroll_item = nullptr;324}325parent->remove_child(item);326memdelete(item);327328if (had_siblings) {329break; // Parent must survive.330}331332item = parent;333parent = item->get_parent();334// Check if parent expects more children.335for (ParentItem &pair : parents) {336if (pair.tree_item == item) {337parent = nullptr;338break; // Might have more children.339}340}341}342}343344// Move all children to the ancestor that matches the filter, if picked.345if (move_from_to.first) {346TreeItem *from = move_from_to.first;347TypedArray<TreeItem> children = from->get_children();348if (!children.is_empty()) {349for (Variant &c : children) {350TreeItem *ti = Object::cast_to<TreeItem>(c);351from->remove_child(ti);352move_from_to.second->add_child(ti);353}354355from->get_parent()->remove_child(from);356memdelete(from);357if (select_items.has(from) || scroll_item == from) {358select_items.erase(from);359scroll_item = nullptr;360}361}362}363}364365inspected_object_ids = ids_present;366367debugger_id = p_debugger; // Needed by hook, could be avoided if every debugger had its own tree.368369for (TreeItem *item : select_items) {370item->select(0);371}372if (scroll_item) {373scroll_to_item(scroll_item, false);374}375376last_filter = filter;377updating_scene_tree = false;378}379380void EditorDebuggerTree::select_nodes(const TypedArray<int64_t> &p_ids) {381// Manually select, as the tree control may be out-of-date for some reason (e.g. not shown yet).382selection_uncollapse_all = true;383inspected_object_ids = p_ids;384scrolling_to_item = true;385386if (!updating_scene_tree) {387// Request a tree refresh.388EditorDebuggerNode::get_singleton()->request_remote_tree();389}390// Set the value immediately, so no update flooding happens and causes a crash.391updating_scene_tree = true;392}393394void EditorDebuggerTree::clear_selection() {395inspected_object_ids.clear();396397if (!updating_scene_tree) {398// Request a tree refresh.399EditorDebuggerNode::get_singleton()->request_remote_tree();400}401// Set the value immediately, so no update flooding happens and causes a crash.402updating_scene_tree = true;403}404405Variant EditorDebuggerTree::get_drag_data(const Point2 &p_point) {406if (get_button_id_at_position(p_point) != -1) {407return Variant();408}409410TreeItem *selected = get_selected();411if (!selected) {412return Variant();413}414415String path = selected->get_text(0);416const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));417418HBoxContainer *hb = memnew(HBoxContainer);419TextureRect *tf = memnew(TextureRect);420tf->set_texture(selected->get_icon(0));421tf->set_custom_minimum_size(Size2(icon_size, icon_size));422tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);423tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);424hb->add_child(tf);425Label *label = memnew(Label(path));426hb->add_child(label);427set_drag_preview(hb);428429if (!selected->get_parent() || !selected->get_parent()->get_parent()) {430path = ".";431} else {432while (selected->get_parent()->get_parent() != get_root()) {433selected = selected->get_parent();434path = selected->get_text(0) + "/" + path;435}436}437438return vformat("\"%s\"", path);439}440441void EditorDebuggerTree::update_icon_max_width() {442add_theme_constant_override("icon_max_width", get_theme_constant("class_icon_size", EditorStringName(Editor)));443}444445String EditorDebuggerTree::get_selected_path() {446if (!get_selected()) {447return "";448}449return get_selected()->get_meta("node_path");450}451452void EditorDebuggerTree::_item_menu_id_pressed(int p_option) {453switch (p_option) {454case ITEM_MENU_SAVE_REMOTE_NODE: {455file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);456file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);457458List<String> extensions;459Ref<PackedScene> sd = memnew(PackedScene);460ResourceSaver::get_recognized_extensions(sd, &extensions);461file_dialog->clear_filters();462for (const String &extension : extensions) {463file_dialog->add_filter("*." + extension, extension.to_upper());464}465466String filename = get_selected_path().get_file() + "." + extensions.front()->get().to_lower();467file_dialog->set_current_path(filename);468file_dialog->popup_file_dialog();469} break;470case ITEM_MENU_COPY_NODE_PATH: {471String text = get_selected_path();472if (text.is_empty()) {473return;474} else if (text == "/root") {475text = ".";476} else {477text = text.replace("/root/", "");478int slash = text.find_char('/');479if (slash < 0) {480text = ".";481} else {482text = text.substr(slash + 1);483}484}485DisplayServer::get_singleton()->clipboard_set(text);486} break;487case ITEM_MENU_EXPAND_COLLAPSE: {488TreeItem *s_item = get_selected();489490if (!s_item) {491s_item = get_root();492if (!s_item) {493break;494}495}496497bool collapsed = s_item->is_any_collapsed();498s_item->set_collapsed_recursive(!collapsed);499500ensure_cursor_is_visible();501}502}503}504505void EditorDebuggerTree::_file_selected(const String &p_file) {506if (inspected_object_ids.size() != 1) {507accept->set_text(vformat(TTR("Saving the branch as a scene requires selecting only one node, but you have selected %d nodes."), inspected_object_ids.size()));508accept->popup_centered();509return;510}511512emit_signal(SNAME("save_node"), inspected_object_ids[0], p_file, debugger_id);513}514515516