scene_tree_editor.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. /**************************************************************************/
  2. /* scene_tree_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "scene_tree_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/object/script_language.h"
  33. #include "editor/editor_dock_manager.h"
  34. #include "editor/editor_file_system.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/node_dock.h"
  40. #include "editor/plugins/animation_player_editor_plugin.h"
  41. #include "editor/plugins/canvas_item_editor_plugin.h"
  42. #include "editor/plugins/script_editor_plugin.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/gui/flow_container.h"
  45. #include "scene/gui/label.h"
  46. #include "scene/gui/texture_rect.h"
  47. #include "scene/main/window.h"
  48. #include "scene/resources/packed_scene.h"
  49. Node *SceneTreeEditor::get_scene_node() const {
  50. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  51. return get_tree()->get_edited_scene_root();
  52. }
  53. void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  54. if (p_button != MouseButton::LEFT) {
  55. return;
  56. }
  57. if (connect_to_script_mode) {
  58. return; //don't do anything in this mode
  59. }
  60. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  61. ERR_FAIL_NULL(item);
  62. NodePath np = item->get_metadata(0);
  63. Node *n = get_node(np);
  64. ERR_FAIL_NULL(n);
  65. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  66. if (p_id == BUTTON_SUBSCENE) {
  67. if (n == get_scene_node()) {
  68. if (n && n->get_scene_inherited_state().is_valid()) {
  69. emit_signal(SNAME("open"), n->get_scene_inherited_state()->get_path());
  70. }
  71. } else {
  72. emit_signal(SNAME("open"), n->get_scene_file_path());
  73. }
  74. } else if (p_id == BUTTON_SCRIPT) {
  75. Ref<Script> script_typed = n->get_script();
  76. if (!script_typed.is_null()) {
  77. emit_signal(SNAME("open_script"), script_typed);
  78. }
  79. } else if (p_id == BUTTON_VISIBILITY) {
  80. undo_redo->create_action(TTR("Toggle Visible"));
  81. _toggle_visible(n);
  82. List<Node *> selection = editor_selection->get_selected_node_list();
  83. if (selection.size() > 1 && selection.find(n) != nullptr) {
  84. for (Node *nv : selection) {
  85. ERR_FAIL_NULL(nv);
  86. if (nv == n) {
  87. continue;
  88. }
  89. _toggle_visible(nv);
  90. }
  91. }
  92. undo_redo->commit_action();
  93. } else if (p_id == BUTTON_LOCK) {
  94. undo_redo->create_action(TTR("Unlock Node"));
  95. undo_redo->add_do_method(n, "remove_meta", "_edit_lock_");
  96. undo_redo->add_undo_method(n, "set_meta", "_edit_lock_", true);
  97. undo_redo->add_do_method(this, "_update_tree");
  98. undo_redo->add_undo_method(this, "_update_tree");
  99. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  100. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  101. undo_redo->commit_action();
  102. } else if (p_id == BUTTON_PIN) {
  103. if (n->is_class("AnimationMixer")) {
  104. AnimationPlayerEditor::get_singleton()->unpin();
  105. _update_tree();
  106. }
  107. } else if (p_id == BUTTON_GROUP) {
  108. undo_redo->create_action(TTR("Ungroup Children"));
  109. if (n->is_class("CanvasItem") || n->is_class("Node3D")) {
  110. undo_redo->add_do_method(n, "remove_meta", "_edit_group_");
  111. undo_redo->add_undo_method(n, "set_meta", "_edit_group_", true);
  112. undo_redo->add_do_method(this, "_update_tree");
  113. undo_redo->add_undo_method(this, "_update_tree");
  114. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  115. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  116. }
  117. undo_redo->commit_action();
  118. } else if (p_id == BUTTON_WARNING) {
  119. const PackedStringArray warnings = n->get_configuration_warnings();
  120. if (warnings.is_empty()) {
  121. return;
  122. }
  123. // Improve looks on tooltip, extra spacing on non-bullet point newlines.
  124. const String bullet_point = U"• ";
  125. String all_warnings;
  126. for (const String &w : warnings) {
  127. all_warnings += "\n" + bullet_point + w;
  128. }
  129. // Limit the line width while keeping some padding.
  130. // It is not efficient, but it does not have to be.
  131. const PackedInt32Array boundaries = TS->string_get_word_breaks(all_warnings, "", 80);
  132. PackedStringArray lines;
  133. for (int i = 0; i < boundaries.size(); i += 2) {
  134. const int start = boundaries[i];
  135. const int end = boundaries[i + 1];
  136. const String line = all_warnings.substr(start, end - start);
  137. lines.append(line);
  138. }
  139. all_warnings = String("\n").join(lines).indent(" ").replace(U" •", U"\n•").substr(2); // We don't want the first two newlines.
  140. warning->set_text(all_warnings);
  141. warning->popup_centered();
  142. } else if (p_id == BUTTON_SIGNALS) {
  143. editor_selection->clear();
  144. editor_selection->add_node(n);
  145. set_selected(n);
  146. EditorDockManager::get_singleton()->focus_dock(NodeDock::get_singleton());
  147. NodeDock::get_singleton()->show_connections();
  148. } else if (p_id == BUTTON_GROUPS) {
  149. editor_selection->clear();
  150. editor_selection->add_node(n);
  151. set_selected(n);
  152. EditorDockManager::get_singleton()->focus_dock(NodeDock::get_singleton());
  153. NodeDock::get_singleton()->show_groups();
  154. } else if (p_id == BUTTON_UNIQUE) {
  155. bool ask_before_revoking_unique_name = EDITOR_GET("docks/scene_tree/ask_before_revoking_unique_name");
  156. revoke_node = n;
  157. if (ask_before_revoking_unique_name) {
  158. String msg = vformat(TTR("Revoke unique name for node \"%s\"?"), n->get_name());
  159. ask_before_revoke_checkbox->set_pressed(false);
  160. revoke_dialog_label->set_text(msg);
  161. revoke_dialog->reset_size();
  162. revoke_dialog->popup_centered();
  163. } else {
  164. _revoke_unique_name();
  165. }
  166. }
  167. }
  168. void SceneTreeEditor::_update_ask_before_revoking_unique_name() {
  169. if (ask_before_revoke_checkbox->is_pressed()) {
  170. EditorSettings::get_singleton()->set("docks/scene_tree/ask_before_revoking_unique_name", false);
  171. ask_before_revoke_checkbox->set_pressed(false);
  172. }
  173. _revoke_unique_name();
  174. }
  175. void SceneTreeEditor::_revoke_unique_name() {
  176. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  177. undo_redo->create_action(TTR("Disable Scene Unique Name"));
  178. undo_redo->add_do_method(revoke_node, "set_unique_name_in_owner", false);
  179. undo_redo->add_undo_method(revoke_node, "set_unique_name_in_owner", true);
  180. undo_redo->add_do_method(this, "_update_tree");
  181. undo_redo->add_undo_method(this, "_update_tree");
  182. undo_redo->commit_action();
  183. }
  184. void SceneTreeEditor::_toggle_visible(Node *p_node) {
  185. if (p_node->has_method("is_visible") && p_node->has_method("set_visible")) {
  186. bool v = bool(p_node->call("is_visible"));
  187. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  188. undo_redo->add_do_method(p_node, "set_visible", !v);
  189. undo_redo->add_undo_method(p_node, "set_visible", v);
  190. }
  191. }
  192. void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {
  193. if (!p_node) {
  194. return;
  195. }
  196. // only owned nodes are editable, since nodes can create their own (manually owned) child nodes,
  197. // which the editor needs not to know about.
  198. bool part_of_subscene = false;
  199. if (!display_foreign && p_node->get_owner() != get_scene_node() && p_node != get_scene_node()) {
  200. if ((show_enabled_subscene || can_open_instance) && p_node->get_owner() && (get_scene_node()->is_editable_instance(p_node->get_owner()))) {
  201. part_of_subscene = true;
  202. //allow
  203. } else {
  204. return;
  205. }
  206. } else {
  207. part_of_subscene = p_node != get_scene_node() && get_scene_node()->get_scene_inherited_state().is_valid() && get_scene_node()->get_scene_inherited_state()->find_node_by_path(get_scene_node()->get_path_to(p_node)) >= 0;
  208. }
  209. TreeItem *item = tree->create_item(p_parent);
  210. item->set_text(0, p_node->get_name());
  211. item->set_text_overrun_behavior(0, TextServer::OVERRUN_NO_TRIMMING);
  212. if (can_rename && !part_of_subscene) {
  213. item->set_editable(0, true);
  214. }
  215. item->set_selectable(0, true);
  216. if (can_rename) {
  217. bool collapsed = p_node->is_displayed_folded();
  218. if (collapsed) {
  219. item->set_collapsed(true);
  220. }
  221. }
  222. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(p_node, "Node");
  223. item->set_icon(0, icon);
  224. item->set_metadata(0, p_node->get_path());
  225. if (connecting_signal) {
  226. // Add script icons for all scripted nodes.
  227. Ref<Script> scr = p_node->get_script();
  228. if (scr.is_valid()) {
  229. item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT);
  230. if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr) {
  231. // Disable button on custom scripts (pure visual cue).
  232. item->set_button_disabled(0, item->get_button_count(0) - 1, true);
  233. }
  234. }
  235. }
  236. if (connect_to_script_mode) {
  237. Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  238. Ref<Script> scr = p_node->get_script();
  239. bool has_custom_script = scr.is_valid() && EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr;
  240. if (scr.is_null() || has_custom_script) {
  241. _set_item_custom_color(item, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  242. item->set_selectable(0, false);
  243. accent.a *= 0.7;
  244. }
  245. if (marked.has(p_node)) {
  246. String node_name = p_node->get_name();
  247. if (connecting_signal) {
  248. node_name += " " + TTR("(Connecting From)");
  249. }
  250. item->set_text(0, node_name);
  251. _set_item_custom_color(item, accent);
  252. }
  253. } else if (part_of_subscene) {
  254. if (valid_types.size() == 0) {
  255. _set_item_custom_color(item, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  256. }
  257. } else if (marked.has(p_node)) {
  258. String node_name = p_node->get_name();
  259. if (connecting_signal) {
  260. node_name += " " + TTR("(Connecting From)");
  261. }
  262. item->set_text(0, node_name);
  263. item->set_selectable(0, marked_selectable);
  264. _set_item_custom_color(item, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
  265. } else if (!p_node->can_process()) {
  266. _set_item_custom_color(item, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  267. } else if (!marked_selectable && !marked_children_selectable) {
  268. Node *node = p_node;
  269. while (node) {
  270. if (marked.has(node)) {
  271. item->set_selectable(0, false);
  272. _set_item_custom_color(item, get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  273. break;
  274. }
  275. node = node->get_parent();
  276. }
  277. }
  278. if (can_rename) { //should be can edit..
  279. const PackedStringArray warnings = p_node->get_configuration_warnings();
  280. const int num_warnings = warnings.size();
  281. if (num_warnings > 0) {
  282. StringName warning_icon;
  283. if (num_warnings == 1) {
  284. warning_icon = SNAME("NodeWarning");
  285. } else if (num_warnings <= 3) {
  286. warning_icon = vformat("NodeWarnings%d", num_warnings);
  287. } else {
  288. warning_icon = SNAME("NodeWarnings4Plus");
  289. }
  290. // Improve looks on tooltip, extra spacing on non-bullet point newlines.
  291. const String bullet_point = U"• ";
  292. String all_warnings;
  293. for (const String &w : warnings) {
  294. all_warnings += "\n\n" + bullet_point + w.replace("\n", "\n ");
  295. }
  296. if (num_warnings == 1) {
  297. all_warnings.remove_at(0); // With only one warning, two newlines do not look great.
  298. }
  299. item->add_button(0, get_editor_theme_icon(warning_icon), BUTTON_WARNING, false, TTR("Node configuration warning:") + all_warnings);
  300. }
  301. if (p_node->is_unique_name_in_owner()) {
  302. const bool disabled = p_node->get_owner() != EditorNode::get_singleton()->get_edited_scene();
  303. String button_text = vformat(TTR("This node can be accessed from anywhere within the scene it belongs to by using the '%s' prefix in the node path."), UNIQUE_NODE_PREFIX);
  304. if (!disabled) {
  305. button_text += "\n" + TTR("Click to disable this.");
  306. }
  307. item->add_button(0, get_editor_theme_icon(SNAME("SceneUniqueName")), BUTTON_UNIQUE, disabled, button_text);
  308. }
  309. int num_connections = p_node->get_persistent_signal_connection_count();
  310. int num_groups = p_node->get_persistent_group_count();
  311. String msg_temp;
  312. if (num_connections >= 1) {
  313. Array arr;
  314. arr.push_back(num_connections);
  315. msg_temp += TTRN("Node has one connection.", "Node has {num} connections.", num_connections).format(arr, "{num}");
  316. if (num_groups >= 1) {
  317. msg_temp += "\n";
  318. }
  319. }
  320. if (num_groups >= 1) {
  321. msg_temp += TTRN("Node is in this group:", "Node is in the following groups:", num_groups) + "\n";
  322. List<GroupInfo> groups;
  323. p_node->get_groups(&groups);
  324. for (const GroupInfo &E : groups) {
  325. if (E.persistent) {
  326. msg_temp += String::utf8("• ") + String(E.name) + "\n";
  327. }
  328. }
  329. } else {
  330. msg_temp += "\n";
  331. }
  332. Ref<Texture2D> icon_temp;
  333. SceneTreeEditorButton signal_temp = BUTTON_SIGNALS;
  334. String msg_temp_end = TTR("Click to show signals dock.");
  335. if (num_connections >= 1 && num_groups >= 1) {
  336. icon_temp = get_editor_theme_icon(SNAME("SignalsAndGroups"));
  337. } else if (num_connections >= 1) {
  338. icon_temp = get_editor_theme_icon(SNAME("Signals"));
  339. } else if (num_groups >= 1) {
  340. icon_temp = get_editor_theme_icon(SNAME("Groups"));
  341. signal_temp = BUTTON_GROUPS;
  342. msg_temp_end = TTR("Click to show groups dock.");
  343. }
  344. if (num_connections >= 1 || num_groups >= 1) {
  345. msg_temp += msg_temp_end;
  346. item->add_button(0, icon_temp, signal_temp, false, msg_temp);
  347. }
  348. }
  349. {
  350. _update_node_tooltip(p_node, item);
  351. Callable delay_update_tooltip = callable_mp(this, &SceneTreeEditor::_queue_update_node_tooltip);
  352. if (p_node->is_connected("editor_description_changed", delay_update_tooltip)) {
  353. p_node->disconnect("editor_description_changed", delay_update_tooltip);
  354. }
  355. p_node->connect("editor_description_changed", delay_update_tooltip.bind(item));
  356. }
  357. if (can_open_instance && is_scene_tree_dock) { // Show buttons only when necessary (SceneTreeDock) to avoid crashes.
  358. if (!p_node->is_connected(CoreStringName(script_changed), callable_mp(this, &SceneTreeEditor::_node_script_changed))) {
  359. p_node->connect(CoreStringName(script_changed), callable_mp(this, &SceneTreeEditor::_node_script_changed).bind(p_node));
  360. }
  361. Ref<Script> scr = p_node->get_script();
  362. if (!scr.is_null()) {
  363. String additional_notes;
  364. Color button_color = Color(1, 1, 1);
  365. // Can't set tooltip after adding button, need to do it before.
  366. if (scr->is_tool()) {
  367. additional_notes += "\n" + TTR("This script is currently running in the editor.");
  368. button_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  369. }
  370. if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr) {
  371. additional_notes += "\n" + TTR("This script is a custom type.");
  372. button_color.a = 0.5;
  373. }
  374. item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT, false, TTR("Open Script:") + " " + scr->get_path() + additional_notes);
  375. item->set_button_color(0, item->get_button_count(0) - 1, button_color);
  376. }
  377. if (p_node->has_meta("_edit_lock_")) {
  378. item->add_button(0, get_editor_theme_icon(SNAME("Lock")), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
  379. }
  380. if (p_node->has_meta("_edit_group_")) {
  381. item->add_button(0, get_editor_theme_icon(SNAME("Group")), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
  382. }
  383. if (p_node->has_method("is_visible") && p_node->has_method("set_visible") && p_node->has_signal(SceneStringName(visibility_changed))) {
  384. bool is_visible = p_node->call("is_visible");
  385. if (is_visible) {
  386. item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityVisible")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  387. } else {
  388. item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityHidden")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  389. }
  390. const Callable vis_changed = callable_mp(this, &SceneTreeEditor::_node_visibility_changed);
  391. if (!p_node->is_connected(SceneStringName(visibility_changed), vis_changed)) {
  392. p_node->connect(SceneStringName(visibility_changed), vis_changed.bind(p_node));
  393. }
  394. _update_visibility_color(p_node, item);
  395. }
  396. if (p_node->is_class("AnimationMixer")) {
  397. bool is_pinned = AnimationPlayerEditor::get_singleton()->get_editing_node() == p_node && AnimationPlayerEditor::get_singleton()->is_pinned();
  398. if (is_pinned) {
  399. item->add_button(0, get_editor_theme_icon(SNAME("Pin")), BUTTON_PIN, false, TTR("AnimationPlayer is pinned.\nClick to unpin."));
  400. }
  401. }
  402. }
  403. if (editor_selection) {
  404. if (editor_selection->is_selected(p_node)) {
  405. item->select(0);
  406. }
  407. }
  408. if (selected == p_node) {
  409. if (!editor_selection) {
  410. item->select(0);
  411. }
  412. item->set_as_cursor(0);
  413. }
  414. for (int i = 0; i < p_node->get_child_count(); i++) {
  415. _add_nodes(p_node->get_child(i), item);
  416. }
  417. if (valid_types.size()) {
  418. bool valid = false;
  419. for (const StringName &E : valid_types) {
  420. if (p_node->is_class(E) ||
  421. EditorNode::get_singleton()->is_object_of_custom_type(p_node, E)) {
  422. valid = true;
  423. break;
  424. } else {
  425. Ref<Script> node_script = p_node->get_script();
  426. while (node_script.is_valid()) {
  427. if (node_script->get_path() == E) {
  428. valid = true;
  429. break;
  430. }
  431. node_script = node_script->get_base_script();
  432. }
  433. if (valid) {
  434. break;
  435. }
  436. }
  437. }
  438. if (!valid) {
  439. _set_item_custom_color(item, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  440. item->set_selectable(0, false);
  441. }
  442. }
  443. }
  444. void SceneTreeEditor::_queue_update_node_tooltip(Node *p_node, TreeItem *p_item) {
  445. Callable update_tooltip = callable_mp(this, &SceneTreeEditor::_update_node_tooltip);
  446. if (update_node_tooltip_delay->is_connected("timeout", update_tooltip)) {
  447. update_node_tooltip_delay->disconnect("timeout", update_tooltip);
  448. }
  449. update_node_tooltip_delay->connect("timeout", update_tooltip.bind(p_node, p_item));
  450. update_node_tooltip_delay->start();
  451. }
  452. void SceneTreeEditor::_update_node_tooltip(Node *p_node, TreeItem *p_item) {
  453. // Display the node name in all tooltips so that long node names can be previewed
  454. // without having to rename them.
  455. String tooltip = p_node->get_name();
  456. if (p_node == get_scene_node() && p_node->get_scene_inherited_state().is_valid()) {
  457. if (p_item->get_button_by_id(0, BUTTON_SUBSCENE) == -1) {
  458. p_item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  459. }
  460. tooltip += String("\n" + TTR("Inherits:") + " " + p_node->get_scene_inherited_state()->get_path());
  461. } else if (p_node != get_scene_node() && !p_node->get_scene_file_path().is_empty() && can_open_instance) {
  462. if (p_item->get_button_by_id(0, BUTTON_SUBSCENE) == -1) {
  463. p_item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  464. }
  465. tooltip += String("\n" + TTR("Instance:") + " " + p_node->get_scene_file_path());
  466. }
  467. StringName custom_type = EditorNode::get_singleton()->get_object_custom_type_name(p_node);
  468. tooltip += "\n" + TTR("Type:") + " " + (custom_type != StringName() ? String(custom_type) : p_node->get_class());
  469. if (!p_node->get_editor_description().is_empty()) {
  470. const PackedInt32Array boundaries = TS->string_get_word_breaks(p_node->get_editor_description(), "", 80);
  471. tooltip += "\n";
  472. for (int i = 0; i < boundaries.size(); i += 2) {
  473. const int start = boundaries[i];
  474. const int end = boundaries[i + 1];
  475. tooltip += "\n" + p_node->get_editor_description().substr(start, end - start + 1).rstrip("\n");
  476. }
  477. }
  478. p_item->set_tooltip_text(0, tooltip);
  479. }
  480. void SceneTreeEditor::_node_visibility_changed(Node *p_node) {
  481. if (!p_node || (p_node != get_scene_node() && !p_node->get_owner())) {
  482. return;
  483. }
  484. TreeItem *item = _find(tree->get_root(), p_node->get_path());
  485. if (!item) {
  486. return;
  487. }
  488. int idx = item->get_button_by_id(0, BUTTON_VISIBILITY);
  489. ERR_FAIL_COND(idx == -1);
  490. bool node_visible = false;
  491. if (p_node->has_method("is_visible")) {
  492. node_visible = p_node->call("is_visible");
  493. if (p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
  494. CanvasItemEditor::get_singleton()->get_viewport_control()->queue_redraw();
  495. }
  496. }
  497. if (node_visible) {
  498. item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
  499. } else {
  500. item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityHidden")));
  501. }
  502. _update_visibility_color(p_node, item);
  503. }
  504. void SceneTreeEditor::_update_visibility_color(Node *p_node, TreeItem *p_item) {
  505. if (p_node->has_method("is_visible_in_tree")) {
  506. Color color(1, 1, 1, 1);
  507. bool visible_on_screen = p_node->call("is_visible_in_tree");
  508. if (!visible_on_screen) {
  509. color.a = 0.6;
  510. }
  511. int idx = p_item->get_button_by_id(0, BUTTON_VISIBILITY);
  512. p_item->set_button_color(0, idx, color);
  513. }
  514. }
  515. void SceneTreeEditor::_set_item_custom_color(TreeItem *p_item, Color p_color) {
  516. p_item->set_custom_color(0, p_color);
  517. p_item->set_meta(SNAME("custom_color"), p_color);
  518. }
  519. void SceneTreeEditor::_node_script_changed(Node *p_node) {
  520. if (tree_dirty) {
  521. return;
  522. }
  523. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  524. tree_dirty = true;
  525. }
  526. void SceneTreeEditor::_node_removed(Node *p_node) {
  527. if (EditorNode::get_singleton()->is_exiting()) {
  528. return; //speed up exit
  529. }
  530. if (p_node->is_connected(CoreStringName(script_changed), callable_mp(this, &SceneTreeEditor::_node_script_changed))) {
  531. p_node->disconnect(CoreStringName(script_changed), callable_mp(this, &SceneTreeEditor::_node_script_changed));
  532. }
  533. if (p_node->has_signal(SceneStringName(visibility_changed))) {
  534. if (p_node->is_connected(SceneStringName(visibility_changed), callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
  535. p_node->disconnect(SceneStringName(visibility_changed), callable_mp(this, &SceneTreeEditor::_node_visibility_changed));
  536. }
  537. }
  538. if (p_node == selected) {
  539. selected = nullptr;
  540. }
  541. }
  542. void SceneTreeEditor::_node_renamed(Node *p_node) {
  543. if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
  544. return;
  545. }
  546. emit_signal(SNAME("node_renamed"));
  547. if (!tree_dirty) {
  548. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  549. tree_dirty = true;
  550. }
  551. }
  552. void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
  553. if (!is_inside_tree()) {
  554. tree_dirty = false;
  555. return;
  556. }
  557. if (tree->is_editing()) {
  558. return;
  559. }
  560. updating_tree = true;
  561. tree->clear();
  562. last_hash = hash_djb2_one_64(0);
  563. if (get_scene_node()) {
  564. _add_nodes(get_scene_node(), nullptr);
  565. _compute_hash(get_scene_node(), last_hash);
  566. }
  567. updating_tree = false;
  568. tree_dirty = false;
  569. if (!filter.strip_edges().is_empty() || !show_all_nodes) {
  570. _update_filter(nullptr, p_scroll_to_selected);
  571. }
  572. }
  573. bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_selected) {
  574. if (!p_parent) {
  575. p_parent = tree->get_root();
  576. filter_term_warning.clear();
  577. }
  578. if (!p_parent) {
  579. // Tree is empty, nothing to do here.
  580. return false;
  581. }
  582. bool keep_for_children = false;
  583. for (TreeItem *child = p_parent->get_first_child(); child; child = child->get_next()) {
  584. // Always keep if at least one of the children are kept.
  585. keep_for_children = _update_filter(child, p_scroll_to_selected) || keep_for_children;
  586. }
  587. // Now find other reasons to keep this Node, too.
  588. PackedStringArray terms = filter.to_lower().split_spaces();
  589. bool keep = _item_matches_all_terms(p_parent, terms);
  590. bool selectable = keep;
  591. if (keep && !valid_types.is_empty()) {
  592. selectable = false;
  593. Node *n = get_node(p_parent->get_metadata(0));
  594. for (const StringName &E : valid_types) {
  595. if (n->is_class(E) ||
  596. EditorNode::get_singleton()->is_object_of_custom_type(n, E)) {
  597. selectable = true;
  598. break;
  599. } else {
  600. Ref<Script> node_script = n->get_script();
  601. while (node_script.is_valid()) {
  602. if (node_script->get_path() == E) {
  603. selectable = true;
  604. break;
  605. }
  606. node_script = node_script->get_base_script();
  607. }
  608. if (selectable) {
  609. break;
  610. }
  611. }
  612. }
  613. }
  614. if (show_all_nodes) {
  615. p_parent->set_visible(keep_for_children || keep);
  616. } else {
  617. // Show only selectable nodes, or parents of selectable.
  618. p_parent->set_visible(keep_for_children || selectable);
  619. }
  620. if (selectable) {
  621. Color custom_color = p_parent->get_meta(SNAME("custom_color"), Color(0, 0, 0, 0));
  622. if (custom_color == Color(0, 0, 0, 0)) {
  623. p_parent->clear_custom_color(0);
  624. } else {
  625. p_parent->set_custom_color(0, custom_color);
  626. }
  627. p_parent->set_selectable(0, true);
  628. } else if (keep_for_children) {
  629. p_parent->set_custom_color(0, get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
  630. p_parent->set_selectable(0, false);
  631. p_parent->deselect(0);
  632. }
  633. if (editor_selection) {
  634. Node *n = get_node(p_parent->get_metadata(0));
  635. if (selectable) {
  636. if (p_scroll_to_selected && n && editor_selection->is_selected(n)) {
  637. tree->scroll_to_item(p_parent);
  638. }
  639. } else {
  640. if (n && p_parent->is_selected(0)) {
  641. editor_selection->remove_node(n);
  642. p_parent->deselect(0);
  643. }
  644. }
  645. }
  646. return p_parent->is_visible();
  647. }
  648. bool SceneTreeEditor::_item_matches_all_terms(TreeItem *p_item, const PackedStringArray &p_terms) {
  649. if (p_terms.is_empty()) {
  650. return true;
  651. }
  652. for (int i = 0; i < p_terms.size(); i++) {
  653. const String &term = p_terms[i];
  654. // Recognize special filter.
  655. if (term.contains_char(':') && !term.get_slicec(':', 0).is_empty()) {
  656. String parameter = term.get_slicec(':', 0);
  657. String argument = term.get_slicec(':', 1);
  658. if (parameter == "type" || parameter == "t") {
  659. // Filter by Type.
  660. String type = get_node(p_item->get_metadata(0))->get_class();
  661. bool term_in_inherited_class = false;
  662. // Every Node is a Node, duh!
  663. while (type != "Node") {
  664. if (type.to_lower().contains(argument)) {
  665. term_in_inherited_class = true;
  666. break;
  667. }
  668. type = ClassDB::get_parent_class(type);
  669. }
  670. if (!term_in_inherited_class) {
  671. return false;
  672. }
  673. } else if (parameter == "group" || parameter == "g") {
  674. // Filter by Group.
  675. Node *node = get_node(p_item->get_metadata(0));
  676. if (argument.is_empty()) {
  677. // When argument is empty, match all Nodes belonging to any exposed group.
  678. if (node->get_persistent_group_count() == 0) {
  679. return false;
  680. }
  681. } else {
  682. List<Node::GroupInfo> group_info_list;
  683. node->get_groups(&group_info_list);
  684. bool term_in_groups = false;
  685. for (const Node::GroupInfo &group_info : group_info_list) {
  686. if (!group_info.persistent) {
  687. continue; // Ignore internal groups.
  688. }
  689. if (String(group_info.name).to_lower().contains(argument)) {
  690. term_in_groups = true;
  691. break;
  692. }
  693. }
  694. if (!term_in_groups) {
  695. return false;
  696. }
  697. }
  698. } else if (filter_term_warning.is_empty()) {
  699. filter_term_warning = vformat(TTR("\"%s\" is not a known filter."), parameter);
  700. continue;
  701. }
  702. } else {
  703. // Default.
  704. if (!p_item->get_text(0).to_lower().contains(term)) {
  705. return false;
  706. }
  707. }
  708. }
  709. return true;
  710. }
  711. void SceneTreeEditor::_compute_hash(Node *p_node, uint64_t &hash) {
  712. hash = hash_djb2_one_64(p_node->get_instance_id(), hash);
  713. if (p_node->get_parent()) {
  714. hash = hash_djb2_one_64(p_node->get_parent()->get_instance_id(), hash); //so a reparent still produces a different hash
  715. }
  716. for (int i = 0; i < p_node->get_child_count(); i++) {
  717. _compute_hash(p_node->get_child(i), hash);
  718. }
  719. }
  720. void SceneTreeEditor::_test_update_tree() {
  721. pending_test_update = false;
  722. if (!is_inside_tree()) {
  723. return;
  724. }
  725. if (tree_dirty) {
  726. return; // don't even bother
  727. }
  728. uint64_t hash = hash_djb2_one_64(0);
  729. if (get_scene_node()) {
  730. _compute_hash(get_scene_node(), hash);
  731. }
  732. //test hash
  733. if (hash == last_hash) {
  734. return; // did not change
  735. }
  736. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  737. tree_dirty = true;
  738. }
  739. void SceneTreeEditor::_tree_process_mode_changed() {
  740. callable_mp(this, &SceneTreeEditor::_update_tree).call_deferred(false);
  741. tree_dirty = true;
  742. }
  743. void SceneTreeEditor::_tree_changed() {
  744. if (EditorNode::get_singleton()->is_exiting()) {
  745. return; //speed up exit
  746. }
  747. if (pending_test_update) {
  748. return;
  749. }
  750. if (tree_dirty) {
  751. return;
  752. }
  753. callable_mp(this, &SceneTreeEditor::_test_update_tree).call_deferred();
  754. pending_test_update = true;
  755. }
  756. void SceneTreeEditor::_selected_changed() {
  757. TreeItem *s = tree->get_selected();
  758. ERR_FAIL_NULL(s);
  759. NodePath np = s->get_metadata(0);
  760. Node *n = get_node(np);
  761. if (n == selected) {
  762. return;
  763. }
  764. selected = get_node(np);
  765. blocked++;
  766. emit_signal(SNAME("node_selected"));
  767. blocked--;
  768. }
  769. void SceneTreeEditor::_deselect_items() {
  770. // Clear currently selected items in scene tree dock.
  771. if (editor_selection) {
  772. editor_selection->clear();
  773. emit_signal(SNAME("node_changed"));
  774. }
  775. }
  776. void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  777. TreeItem *item = Object::cast_to<TreeItem>(p_object);
  778. ERR_FAIL_NULL(item);
  779. if (!item->is_visible()) {
  780. return;
  781. }
  782. NodePath np = item->get_metadata(0);
  783. Node *n = get_node(np);
  784. if (!n) {
  785. return;
  786. }
  787. if (!editor_selection) {
  788. return;
  789. }
  790. if (p_selected) {
  791. editor_selection->add_node(n);
  792. } else {
  793. editor_selection->remove_node(n);
  794. }
  795. // Emitted "selected" in _selected_changed() when select single node, so select multiple node emit "changed"
  796. if (editor_selection->get_selected_nodes().size() > 1) {
  797. emit_signal(SNAME("node_changed"));
  798. }
  799. }
  800. void SceneTreeEditor::_tree_scroll_to_item(ObjectID p_item_id) {
  801. ERR_FAIL_NULL(tree);
  802. TreeItem *item = Object::cast_to<TreeItem>(ObjectDB::get_instance(p_item_id));
  803. if (item) {
  804. tree->scroll_to_item(item, true);
  805. }
  806. }
  807. void SceneTreeEditor::_notification(int p_what) {
  808. switch (p_what) {
  809. case NOTIFICATION_ENTER_TREE: {
  810. get_tree()->connect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  811. get_tree()->connect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  812. get_tree()->connect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  813. get_tree()->connect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  814. get_tree()->connect(SceneStringName(node_configuration_warning_changed), callable_mp(this, &SceneTreeEditor::_warning_changed));
  815. tree->connect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  816. _update_tree();
  817. } break;
  818. case NOTIFICATION_EXIT_TREE: {
  819. get_tree()->disconnect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
  820. get_tree()->disconnect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
  821. get_tree()->disconnect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
  822. get_tree()->disconnect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
  823. tree->disconnect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
  824. get_tree()->disconnect(SceneStringName(node_configuration_warning_changed), callable_mp(this, &SceneTreeEditor::_warning_changed));
  825. } break;
  826. case NOTIFICATION_THEME_CHANGED: {
  827. tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  828. _update_tree();
  829. } break;
  830. case NOTIFICATION_VISIBILITY_CHANGED: {
  831. if (is_visible()) {
  832. TreeItem *item = nullptr;
  833. if (selected) {
  834. // Scroll to selected node.
  835. item = _find(tree->get_root(), selected->get_path());
  836. } else if (marked.size() == 1) {
  837. // Scroll to a single marked node.
  838. Node *marked_node = *marked.begin();
  839. if (marked_node) {
  840. item = _find(tree->get_root(), marked_node->get_path());
  841. }
  842. }
  843. if (item) {
  844. // Must wait until tree is properly sized before scrolling.
  845. ObjectID item_id = item->get_instance_id();
  846. callable_mp(this, &SceneTreeEditor::_tree_scroll_to_item).call_deferred(item_id);
  847. }
  848. }
  849. } break;
  850. }
  851. }
  852. TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  853. if (!p_node) {
  854. return nullptr;
  855. }
  856. NodePath np = p_node->get_metadata(0);
  857. if (np == p_path) {
  858. return p_node;
  859. }
  860. TreeItem *children = p_node->get_first_child();
  861. while (children) {
  862. TreeItem *n = _find(children, p_path);
  863. if (n) {
  864. return n;
  865. }
  866. children = children->get_next();
  867. }
  868. return nullptr;
  869. }
  870. void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
  871. ERR_FAIL_COND(blocked > 0);
  872. if (pending_test_update) {
  873. _test_update_tree();
  874. }
  875. if (tree_dirty) {
  876. _update_tree();
  877. }
  878. if (selected == p_node) {
  879. return;
  880. }
  881. TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : nullptr;
  882. if (item) {
  883. selected = p_node;
  884. if (auto_expand_selected) {
  885. // Make visible when it's collapsed.
  886. TreeItem *node = item->get_parent();
  887. while (node && node != tree->get_root()) {
  888. node->set_collapsed(false);
  889. node = node->get_parent();
  890. }
  891. item->select(0);
  892. item->set_as_cursor(0);
  893. tree->ensure_cursor_is_visible();
  894. } else {
  895. // Ensure the node is selected and visible for the user if the node
  896. // is not collapsed.
  897. bool collapsed = false;
  898. TreeItem *node = item;
  899. while (node && node != tree->get_root()) {
  900. if (node->is_collapsed()) {
  901. collapsed = true;
  902. break;
  903. }
  904. node = node->get_parent();
  905. }
  906. if (!collapsed) {
  907. item->select(0);
  908. item->set_as_cursor(0);
  909. tree->ensure_cursor_is_visible();
  910. }
  911. }
  912. } else {
  913. if (!p_node) {
  914. selected = nullptr;
  915. }
  916. selected = p_node;
  917. }
  918. if (p_emit_selected) {
  919. emit_signal(SNAME("node_selected"));
  920. }
  921. }
  922. void SceneTreeEditor::rename_node(Node *p_node, const String &p_name, TreeItem *p_item) {
  923. TreeItem *item;
  924. if (p_item) {
  925. item = p_item; // During batch rename the paths may change, so using _find() is unreliable.
  926. } else {
  927. item = _find(tree->get_root(), p_node->get_path());
  928. }
  929. ERR_FAIL_NULL(item);
  930. String new_name = p_name.validate_node_name();
  931. if (new_name != p_name) {
  932. String text = TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::get_invalid_node_name_characters();
  933. if (error->is_visible()) {
  934. if (!error->get_meta("invalid_character", false)) {
  935. error->set_text(error->get_text() + "\n\n" + text);
  936. error->set_meta("invalid_character", true);
  937. }
  938. } else {
  939. error->set_text(text);
  940. error->set_meta("invalid_character", true);
  941. error->set_meta("same_unique_name", false);
  942. error->popup_centered();
  943. }
  944. }
  945. // Trim leading/trailing whitespace to prevent node names from containing accidental whitespace, which would make it more difficult to get the node via `get_node()`.
  946. new_name = new_name.strip_edges();
  947. if (new_name.is_empty() && p_node->get_owner() != nullptr && !p_node->get_scene_file_path().is_empty()) {
  948. // If name is empty and node is root of an instance, revert to the original name.
  949. const Ref<PackedScene> node_scene = ResourceLoader::load(p_node->get_scene_file_path());
  950. if (node_scene.is_valid()) {
  951. const Ref<SceneState> &state = node_scene->get_state();
  952. if (state->get_node_count() > 0) {
  953. new_name = state->get_node_name(0); // Root's name.
  954. }
  955. }
  956. }
  957. if (new_name.is_empty()) {
  958. // If name is still empty, fallback to class name.
  959. if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
  960. new_name = Node::adjust_name_casing(p_node->get_class());
  961. } else {
  962. new_name = p_node->get_class();
  963. }
  964. }
  965. new_name = p_node->get_parent()->prevalidate_child_name(p_node, new_name);
  966. if (new_name == p_node->get_name()) {
  967. item->set_text(0, new_name);
  968. return;
  969. }
  970. // We previously made sure name is not the same as current name so that it won't complain about already used unique name when not changing name.
  971. if (p_node->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name)) {
  972. String text = vformat(TTR("A node with the unique name %s already exists in this scene."), new_name);
  973. if (error->is_visible()) {
  974. if (!error->get_meta("same_unique_name", false)) {
  975. error->set_text(error->get_text() + "\n\n" + text);
  976. error->set_meta("same_unique_name", true);
  977. }
  978. } else {
  979. error->set_text(text);
  980. error->set_meta("same_unique_name", true);
  981. error->set_meta("invalid_character", false);
  982. error->popup_centered();
  983. }
  984. item->set_text(0, p_node->get_name());
  985. return;
  986. }
  987. if (!is_scene_tree_dock) {
  988. p_node->set_name(new_name);
  989. item->set_metadata(0, p_node->get_path());
  990. emit_signal(SNAME("node_renamed"));
  991. } else {
  992. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  993. undo_redo->create_action(TTR("Rename Node"), UndoRedo::MERGE_DISABLE, p_node);
  994. emit_signal(SNAME("node_prerename"), p_node, new_name);
  995. undo_redo->add_undo_method(p_node, "set_name", p_node->get_name());
  996. undo_redo->add_undo_method(item, "set_metadata", 0, p_node->get_path());
  997. undo_redo->add_undo_method(item, "set_text", 0, p_node->get_name());
  998. p_node->set_name(new_name);
  999. undo_redo->add_do_method(p_node, "set_name", new_name);
  1000. undo_redo->add_do_method(item, "set_metadata", 0, p_node->get_path());
  1001. undo_redo->add_do_method(item, "set_text", 0, new_name);
  1002. undo_redo->commit_action();
  1003. }
  1004. }
  1005. void SceneTreeEditor::_edited() {
  1006. TreeItem *which = tree->get_next_selected(nullptr);
  1007. ERR_FAIL_NULL(which);
  1008. TreeItem *edited = tree->get_edited();
  1009. ERR_FAIL_NULL(edited);
  1010. if (is_scene_tree_dock && tree->get_next_selected(which)) {
  1011. List<Node *> nodes_to_rename;
  1012. for (TreeItem *item = which; item; item = tree->get_next_selected(item)) {
  1013. Node *n = get_node(item->get_metadata(0));
  1014. ERR_FAIL_NULL(n);
  1015. nodes_to_rename.push_back(n);
  1016. }
  1017. ERR_FAIL_COND(nodes_to_rename.is_empty());
  1018. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1019. undo_redo->create_action(TTR("Rename Nodes"), UndoRedo::MERGE_DISABLE, nodes_to_rename.front()->get(), true);
  1020. TreeItem *item = which;
  1021. String new_name = edited->get_text(0);
  1022. for (Node *n : nodes_to_rename) {
  1023. rename_node(n, new_name, item);
  1024. item = tree->get_next_selected(item);
  1025. }
  1026. undo_redo->commit_action();
  1027. } else {
  1028. Node *n = get_node(which->get_metadata(0));
  1029. ERR_FAIL_NULL(n);
  1030. rename_node(n, which->get_text(0));
  1031. }
  1032. }
  1033. Node *SceneTreeEditor::get_selected() {
  1034. return selected;
  1035. }
  1036. void SceneTreeEditor::set_marked(const HashSet<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
  1037. if (tree_dirty) {
  1038. _update_tree();
  1039. }
  1040. marked = p_marked;
  1041. marked_selectable = p_selectable;
  1042. marked_children_selectable = p_children_selectable;
  1043. _update_tree();
  1044. }
  1045. void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
  1046. HashSet<Node *> s;
  1047. if (p_marked) {
  1048. s.insert(p_marked);
  1049. }
  1050. set_marked(s, p_selectable, p_children_selectable);
  1051. }
  1052. void SceneTreeEditor::set_filter(const String &p_filter) {
  1053. filter = p_filter;
  1054. _update_filter(nullptr, true);
  1055. }
  1056. String SceneTreeEditor::get_filter() const {
  1057. return filter;
  1058. }
  1059. String SceneTreeEditor::get_filter_term_warning() {
  1060. return filter_term_warning;
  1061. }
  1062. void SceneTreeEditor::set_show_all_nodes(bool p_show_all_nodes) {
  1063. show_all_nodes = p_show_all_nodes;
  1064. _update_filter(nullptr, true);
  1065. }
  1066. void SceneTreeEditor::set_as_scene_tree_dock() {
  1067. is_scene_tree_dock = true;
  1068. }
  1069. void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
  1070. display_foreign = p_display;
  1071. _update_tree();
  1072. }
  1073. void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
  1074. valid_types = p_valid;
  1075. }
  1076. void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
  1077. editor_selection = p_selection;
  1078. tree->set_select_mode(Tree::SELECT_MULTI);
  1079. tree->set_cursor_can_exit_tree(false);
  1080. editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeEditor::_selection_changed));
  1081. }
  1082. void SceneTreeEditor::_update_selection(TreeItem *item) {
  1083. ERR_FAIL_NULL(item);
  1084. NodePath np = item->get_metadata(0);
  1085. if (!has_node(np)) {
  1086. return;
  1087. }
  1088. Node *n = get_node(np);
  1089. if (!n) {
  1090. return;
  1091. }
  1092. if (editor_selection->is_selected(n)) {
  1093. if (!item->is_selected(0)) {
  1094. item->select(0);
  1095. }
  1096. } else {
  1097. if (item->is_selected(0)) {
  1098. TreeItem *previous_cursor_item = tree->get_selected();
  1099. item->deselect(0);
  1100. previous_cursor_item->set_as_cursor(0);
  1101. }
  1102. }
  1103. TreeItem *c = item->get_first_child();
  1104. while (c) {
  1105. _update_selection(c);
  1106. c = c->get_next();
  1107. }
  1108. }
  1109. void SceneTreeEditor::_selection_changed() {
  1110. if (!editor_selection) {
  1111. return;
  1112. }
  1113. TreeItem *root = tree->get_root();
  1114. if (!root) {
  1115. return;
  1116. }
  1117. _update_selection(root);
  1118. }
  1119. void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
  1120. if (updating_tree) {
  1121. return;
  1122. }
  1123. if (!can_rename) {
  1124. return;
  1125. }
  1126. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  1127. if (!ti) {
  1128. return;
  1129. }
  1130. bool collapsed = ti->is_collapsed();
  1131. NodePath np = ti->get_metadata(0);
  1132. Node *n = get_node(np);
  1133. ERR_FAIL_NULL(n);
  1134. n->set_display_folded(collapsed);
  1135. }
  1136. Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1137. if (!can_rename) {
  1138. return Variant(); //not editable tree
  1139. }
  1140. if (tree->get_button_id_at_position(p_point) != -1) {
  1141. return Variant(); //dragging from button
  1142. }
  1143. Vector<Node *> selected_nodes;
  1144. Vector<Ref<Texture2D>> icons;
  1145. TreeItem *next = tree->get_next_selected(nullptr);
  1146. while (next) {
  1147. NodePath np = next->get_metadata(0);
  1148. Node *n = get_node(np);
  1149. if (n) {
  1150. selected_nodes.push_back(n);
  1151. icons.push_back(next->get_icon(0));
  1152. }
  1153. next = tree->get_next_selected(next);
  1154. }
  1155. if (selected_nodes.is_empty()) {
  1156. return Variant();
  1157. }
  1158. VBoxContainer *vb = memnew(VBoxContainer);
  1159. Array objs;
  1160. int list_max = 10;
  1161. float opacity_step = 1.0f / list_max;
  1162. float opacity_item = 1.0f;
  1163. for (int i = 0; i < selected_nodes.size(); i++) {
  1164. if (i < list_max) {
  1165. HBoxContainer *hb = memnew(HBoxContainer);
  1166. TextureRect *tf = memnew(TextureRect);
  1167. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  1168. tf->set_custom_minimum_size(Size2(icon_size, icon_size));
  1169. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1170. tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1171. tf->set_texture(icons[i]);
  1172. hb->add_child(tf);
  1173. Label *label = memnew(Label(selected_nodes[i]->get_name()));
  1174. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1175. hb->add_child(label);
  1176. vb->add_child(hb);
  1177. hb->set_modulate(Color(1, 1, 1, opacity_item));
  1178. opacity_item -= opacity_step;
  1179. }
  1180. NodePath p = selected_nodes[i]->get_path();
  1181. objs.push_back(p);
  1182. }
  1183. set_drag_preview(vb);
  1184. Dictionary drag_data;
  1185. drag_data["type"] = "nodes";
  1186. drag_data["nodes"] = objs;
  1187. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1188. emit_signal(SNAME("nodes_dragged"));
  1189. return drag_data;
  1190. }
  1191. bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
  1192. return (script_types->find(p_type));
  1193. }
  1194. bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1195. if (!can_rename) {
  1196. return false; //not editable tree
  1197. }
  1198. Dictionary d = p_data;
  1199. if (!d.has("type")) {
  1200. return false;
  1201. }
  1202. TreeItem *item = tree->get_item_at_position(p_point);
  1203. if (!item) {
  1204. return false;
  1205. }
  1206. int section = tree->get_drop_section_at_position(p_point);
  1207. if (section < -1 || (section == -1 && !item->get_parent())) {
  1208. return false;
  1209. }
  1210. if (String(d["type"]) == "files") {
  1211. Vector<String> files = d["files"];
  1212. if (files.size() == 0) {
  1213. return false; //weird
  1214. }
  1215. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
  1216. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1217. return true;
  1218. }
  1219. bool scene_drop = true;
  1220. bool audio_drop = true;
  1221. for (int i = 0; i < files.size(); i++) {
  1222. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[i]);
  1223. if (ftype != "PackedScene") {
  1224. scene_drop = false;
  1225. }
  1226. if (audio_drop && !ClassDB::is_parent_class(ftype, "AudioStream")) {
  1227. audio_drop = false;
  1228. }
  1229. }
  1230. if (scene_drop) {
  1231. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1232. return true;
  1233. }
  1234. if (audio_drop) {
  1235. if (files.size() > 1) {
  1236. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  1237. } else {
  1238. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1239. }
  1240. return true;
  1241. }
  1242. if (files.size() > 1) {
  1243. return false;
  1244. }
  1245. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1246. return true;
  1247. }
  1248. if (String(d["type"]) == "script_list_element") {
  1249. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1250. if (se) {
  1251. String sp = se->get_edited_resource()->get_path();
  1252. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1253. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1254. return true;
  1255. }
  1256. }
  1257. }
  1258. if (filter.is_empty() && String(d["type"]) == "nodes") {
  1259. Array nodes = d["nodes"];
  1260. for (int i = 0; i < nodes.size(); i++) {
  1261. Node *n = get_node(nodes[i]);
  1262. // Nodes from an instantiated scene can't be rearranged.
  1263. if (n && n->get_owner() && n->get_owner() != get_scene_node() && !n->get_owner()->get_scene_file_path().is_empty()) {
  1264. return false;
  1265. }
  1266. }
  1267. return true;
  1268. }
  1269. return false;
  1270. }
  1271. void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1272. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1273. return;
  1274. }
  1275. TreeItem *item = tree->get_item_at_position(p_point);
  1276. if (!item) {
  1277. return;
  1278. }
  1279. int section = tree->get_drop_section_at_position(p_point);
  1280. if (section < -1) {
  1281. return;
  1282. }
  1283. NodePath np = item->get_metadata(0);
  1284. Node *n = get_node(np);
  1285. if (!n) {
  1286. return;
  1287. }
  1288. Dictionary d = p_data;
  1289. if (String(d["type"]) == "nodes") {
  1290. Array nodes = d["nodes"];
  1291. emit_signal(SNAME("nodes_rearranged"), nodes, np, section);
  1292. }
  1293. if (String(d["type"]) == "files") {
  1294. Vector<String> files = d["files"];
  1295. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  1296. if (_is_script_type(ftype)) {
  1297. emit_signal(SNAME("script_dropped"), files[0], np);
  1298. } else {
  1299. emit_signal(SNAME("files_dropped"), files, np, section);
  1300. }
  1301. }
  1302. if (String(d["type"]) == "script_list_element") {
  1303. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1304. if (se) {
  1305. String sp = se->get_edited_resource()->get_path();
  1306. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1307. emit_signal(SNAME("script_dropped"), sp, np);
  1308. }
  1309. }
  1310. }
  1311. }
  1312. void SceneTreeEditor::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) {
  1313. if (p_button != MouseButton::RIGHT) {
  1314. return;
  1315. }
  1316. _rmb_select(p_pos);
  1317. }
  1318. void SceneTreeEditor::_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
  1319. if (p_button != MouseButton::RIGHT) {
  1320. return;
  1321. }
  1322. emit_signal(SNAME("rmb_pressed"), tree->get_screen_position() + p_pos);
  1323. }
  1324. void SceneTreeEditor::update_warning() {
  1325. _warning_changed(nullptr);
  1326. }
  1327. void SceneTreeEditor::_warning_changed(Node *p_for_node) {
  1328. //should use a timer
  1329. update_timer->start();
  1330. }
  1331. void SceneTreeEditor::set_auto_expand_selected(bool p_auto, bool p_update_settings) {
  1332. if (p_update_settings) {
  1333. EditorSettings::get_singleton()->set("docks/scene_tree/auto_expand_to_selected", p_auto);
  1334. }
  1335. auto_expand_selected = p_auto;
  1336. }
  1337. void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) {
  1338. connect_to_script_mode = p_enable;
  1339. update_tree();
  1340. }
  1341. void SceneTreeEditor::set_connecting_signal(bool p_enable) {
  1342. connecting_signal = p_enable;
  1343. update_tree();
  1344. }
  1345. void SceneTreeEditor::_bind_methods() {
  1346. ClassDB::bind_method(D_METHOD("_update_tree"), &SceneTreeEditor::_update_tree, DEFVAL(false)); // Still used by UndoRedo.
  1347. ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
  1348. ADD_SIGNAL(MethodInfo("node_selected"));
  1349. ADD_SIGNAL(MethodInfo("node_renamed"));
  1350. ADD_SIGNAL(MethodInfo("node_prerename"));
  1351. ADD_SIGNAL(MethodInfo("node_changed"));
  1352. ADD_SIGNAL(MethodInfo("nodes_dragged"));
  1353. ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1354. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1355. ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
  1356. ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
  1357. ADD_SIGNAL(MethodInfo("open"));
  1358. ADD_SIGNAL(MethodInfo("open_script"));
  1359. }
  1360. SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) {
  1361. selected = nullptr;
  1362. can_rename = p_can_rename;
  1363. can_open_instance = p_can_open_instance;
  1364. editor_selection = nullptr;
  1365. if (p_label) {
  1366. Label *label = memnew(Label);
  1367. label->set_theme_type_variation("HeaderSmall");
  1368. label->set_position(Point2(10, 0));
  1369. label->set_text(TTR("Scene Tree (Nodes):"));
  1370. add_child(label);
  1371. }
  1372. tree = memnew(Tree);
  1373. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1374. tree->set_anchor(SIDE_RIGHT, ANCHOR_END);
  1375. tree->set_anchor(SIDE_BOTTOM, ANCHOR_END);
  1376. tree->set_begin(Point2(0, p_label ? 18 : 0));
  1377. tree->set_end(Point2(0, 0));
  1378. tree->set_allow_reselect(true);
  1379. tree->add_theme_constant_override("button_margin", 0);
  1380. add_child(tree);
  1381. SET_DRAG_FORWARDING_GCD(tree, SceneTreeEditor);
  1382. if (p_can_rename) {
  1383. tree->set_allow_rmb_select(true);
  1384. tree->connect("item_mouse_selected", callable_mp(this, &SceneTreeEditor::_rmb_select));
  1385. tree->connect("empty_clicked", callable_mp(this, &SceneTreeEditor::_empty_clicked));
  1386. }
  1387. tree->connect("cell_selected", callable_mp(this, &SceneTreeEditor::_selected_changed));
  1388. tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_edited));
  1389. tree->connect("multi_selected", callable_mp(this, &SceneTreeEditor::_cell_multi_selected));
  1390. tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
  1391. tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));
  1392. error = memnew(AcceptDialog);
  1393. add_child(error);
  1394. warning = memnew(AcceptDialog);
  1395. add_child(warning);
  1396. warning->set_title(TTR("Node Configuration Warning!"));
  1397. warning->set_flag(Window::FLAG_POPUP, true);
  1398. last_hash = 0;
  1399. blocked = 0;
  1400. update_timer = memnew(Timer);
  1401. update_timer->connect("timeout", callable_mp(this, &SceneTreeEditor::_update_tree).bind(false));
  1402. update_timer->set_one_shot(true);
  1403. update_timer->set_wait_time(0.5);
  1404. add_child(update_timer);
  1405. update_node_tooltip_delay = memnew(Timer);
  1406. update_node_tooltip_delay->set_wait_time(0.5);
  1407. update_node_tooltip_delay->set_one_shot(true);
  1408. add_child(update_node_tooltip_delay);
  1409. revoke_dialog = memnew(ConfirmationDialog);
  1410. revoke_dialog->set_ok_button_text(TTR("Revoke"));
  1411. add_child(revoke_dialog);
  1412. revoke_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SceneTreeEditor::_update_ask_before_revoking_unique_name));
  1413. VBoxContainer *vb = memnew(VBoxContainer);
  1414. revoke_dialog->add_child(vb);
  1415. revoke_dialog_label = memnew(Label);
  1416. vb->add_child(revoke_dialog_label);
  1417. ask_before_revoke_checkbox = memnew(CheckBox(TTR("Don't Ask Again")));
  1418. ask_before_revoke_checkbox->set_tooltip_text(TTR("This dialog can also be enabled/disabled in the Editor Settings: Docks > Scene Tree > Ask Before Revoking Unique Name."));
  1419. vb->add_child(ask_before_revoke_checkbox);
  1420. script_types = memnew(List<StringName>);
  1421. ClassDB::get_inheriters_from_class("Script", script_types);
  1422. }
  1423. SceneTreeEditor::~SceneTreeEditor() {
  1424. memdelete(script_types);
  1425. }
  1426. /******** DIALOG *********/
  1427. void SceneTreeDialog::popup_scenetree_dialog(Node *p_selected_node, Node *p_marked_node, bool p_marked_node_selectable, bool p_marked_node_children_selectable) {
  1428. get_scene_tree()->set_marked(p_marked_node, p_marked_node_selectable, p_marked_node_children_selectable);
  1429. get_scene_tree()->set_selected(p_selected_node);
  1430. popup_centered_clamped(Size2(350, 700) * EDSCALE);
  1431. }
  1432. void SceneTreeDialog::_show_all_nodes_changed(bool p_button_pressed) {
  1433. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", p_button_pressed);
  1434. tree->set_show_all_nodes(p_button_pressed);
  1435. }
  1436. void SceneTreeDialog::set_valid_types(const Vector<StringName> &p_valid) {
  1437. if (allowed_types_hbox) {
  1438. allowed_types_hbox->queue_free();
  1439. allowed_types_hbox = nullptr;
  1440. valid_type_icons.clear();
  1441. }
  1442. tree->set_valid_types(p_valid);
  1443. if (p_valid.is_empty()) {
  1444. return;
  1445. }
  1446. allowed_types_hbox = memnew(HBoxContainer);
  1447. content->add_child(allowed_types_hbox);
  1448. content->move_child(allowed_types_hbox, 0);
  1449. {
  1450. Label *label = memnew(Label);
  1451. allowed_types_hbox->add_child(label);
  1452. label->set_text(TTR("Allowed:"));
  1453. }
  1454. HFlowContainer *hflow = memnew(HFlowContainer);
  1455. allowed_types_hbox->add_child(hflow);
  1456. hflow->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1457. for (const StringName &type : p_valid) {
  1458. HBoxContainer *hb = memnew(HBoxContainer);
  1459. hflow->add_child(hb);
  1460. // Attempt to get the correct name and icon for script path types.
  1461. String name = type;
  1462. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(type);
  1463. // If we can't find a global class icon, try to find one for the script.
  1464. if (icon.is_null() && ResourceLoader::exists(type, "Script")) {
  1465. Ref<Script> node_script = ResourceLoader::load(type);
  1466. if (node_script.is_valid()) {
  1467. name = name.get_file();
  1468. icon = EditorNode::get_singleton()->get_object_icon(node_script.ptr());
  1469. }
  1470. }
  1471. TextureRect *trect = memnew(TextureRect);
  1472. hb->add_child(trect);
  1473. trect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  1474. trect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  1475. trect->set_meta("icon", icon);
  1476. valid_type_icons.push_back(trect);
  1477. Label *label = memnew(Label);
  1478. hb->add_child(label);
  1479. label->set_text(name);
  1480. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1481. }
  1482. show_all_nodes->show();
  1483. if (is_inside_tree()) {
  1484. _update_valid_type_icons();
  1485. }
  1486. }
  1487. void SceneTreeDialog::_notification(int p_what) {
  1488. switch (p_what) {
  1489. case NOTIFICATION_VISIBILITY_CHANGED: {
  1490. if (is_visible()) {
  1491. tree->update_tree();
  1492. // Select the search bar by default.
  1493. callable_mp((Control *)filter, &Control::grab_focus).call_deferred();
  1494. }
  1495. } break;
  1496. case NOTIFICATION_ENTER_TREE: {
  1497. connect(SceneStringName(confirmed), callable_mp(this, &SceneTreeDialog::_select));
  1498. } break;
  1499. case NOTIFICATION_THEME_CHANGED: {
  1500. _update_valid_type_icons();
  1501. } break;
  1502. case NOTIFICATION_EXIT_TREE: {
  1503. disconnect(SceneStringName(confirmed), callable_mp(this, &SceneTreeDialog::_select));
  1504. } break;
  1505. }
  1506. }
  1507. void SceneTreeDialog::_update_valid_type_icons() {
  1508. filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  1509. for (TextureRect *trect : valid_type_icons) {
  1510. trect->set_custom_minimum_size(Vector2(get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)), 0));
  1511. trect->set_texture(trect->get_meta("icon"));
  1512. }
  1513. }
  1514. void SceneTreeDialog::_cancel() {
  1515. hide();
  1516. }
  1517. void SceneTreeDialog::_select() {
  1518. if (tree->get_selected()) {
  1519. // The signal may cause another dialog to be displayed, so be sure to hide this one first.
  1520. hide();
  1521. emit_signal(SNAME("selected"), tree->get_selected()->get_path());
  1522. }
  1523. }
  1524. void SceneTreeDialog::_selected_changed() {
  1525. get_ok_button()->set_disabled(!tree->get_selected());
  1526. }
  1527. void SceneTreeDialog::_filter_changed(const String &p_filter) {
  1528. tree->set_filter(p_filter);
  1529. }
  1530. void SceneTreeDialog::_on_filter_gui_input(const Ref<InputEvent> &p_event) {
  1531. // Redirect navigational key events to the tree.
  1532. Ref<InputEventKey> key = p_event;
  1533. if (key.is_valid()) {
  1534. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  1535. tree->get_scene_tree()->gui_input(key);
  1536. filter->accept_event();
  1537. }
  1538. }
  1539. }
  1540. void SceneTreeDialog::_bind_methods() {
  1541. ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
  1542. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
  1543. }
  1544. SceneTreeDialog::SceneTreeDialog() {
  1545. set_title(TTR("Select a Node"));
  1546. content = memnew(VBoxContainer);
  1547. add_child(content);
  1548. HBoxContainer *filter_hbc = memnew(HBoxContainer);
  1549. content->add_child(filter_hbc);
  1550. filter = memnew(LineEdit);
  1551. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1552. filter->set_placeholder(TTR("Filter Nodes"));
  1553. filter->set_clear_button_enabled(true);
  1554. filter->add_theme_constant_override("minimum_character_width", 0);
  1555. filter->connect(SceneStringName(text_changed), callable_mp(this, &SceneTreeDialog::_filter_changed));
  1556. filter->connect(SceneStringName(gui_input), callable_mp(this, &SceneTreeDialog::_on_filter_gui_input));
  1557. register_text_enter(filter);
  1558. filter_hbc->add_child(filter);
  1559. // Add 'Show All' button to HBoxContainer next to the filter, visible only when valid_types is defined.
  1560. show_all_nodes = memnew(CheckButton);
  1561. show_all_nodes->set_text(TTR("Show All"));
  1562. show_all_nodes->connect(SceneStringName(toggled), callable_mp(this, &SceneTreeDialog::_show_all_nodes_changed));
  1563. show_all_nodes->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
  1564. show_all_nodes->hide();
  1565. filter_hbc->add_child(show_all_nodes);
  1566. tree = memnew(SceneTreeEditor(false, false, true));
  1567. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1568. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &SceneTreeDialog::_select));
  1569. // Initialize button state, must be done after the tree has been created to update its 'show_all_nodes' flag.
  1570. // This is also done before adding the tree to the content to avoid triggering unnecessary tree filtering.
  1571. show_all_nodes->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", false));
  1572. content->add_child(tree);
  1573. // Disable the OK button when no node is selected.
  1574. get_ok_button()->set_disabled(!tree->get_selected());
  1575. tree->connect("node_selected", callable_mp(this, &SceneTreeDialog::_selected_changed));
  1576. }
  1577. SceneTreeDialog::~SceneTreeDialog() {
  1578. }