scene_tree_editor.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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/message_queue.h"
  32. #include "core/print_string.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/node_dock.h"
  35. #include "editor/plugins/animation_player_editor_plugin.h"
  36. #include "editor/plugins/canvas_item_editor_plugin.h"
  37. #include "scene/3d/lod_manager.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/main/viewport.h"
  40. #include "scene/resources/packed_scene.h"
  41. Node *SceneTreeEditor::get_scene_node() const {
  42. ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
  43. return get_tree()->get_edited_scene_root();
  44. }
  45. void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id) {
  46. if (connect_to_script_mode) {
  47. return; //don't do anything in this mode
  48. }
  49. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  50. ERR_FAIL_COND(!item);
  51. NodePath np = item->get_metadata(0);
  52. Node *n = get_node(np);
  53. ERR_FAIL_COND(!n);
  54. if (p_id == BUTTON_SUBSCENE) {
  55. if (n == get_scene_node()) {
  56. if (n && n->get_scene_inherited_state().is_valid()) {
  57. emit_signal("open", n->get_scene_inherited_state()->get_path());
  58. }
  59. } else {
  60. emit_signal("open", n->get_filename());
  61. }
  62. } else if (p_id == BUTTON_SCRIPT) {
  63. RefPtr script = n->get_script();
  64. Ref<Script> script_typed = script;
  65. if (!script_typed.is_null()) {
  66. emit_signal("open_script", script);
  67. }
  68. } else if (p_id == BUTTON_VISIBILITY) {
  69. undo_redo->create_action(TTR("Toggle Visible"));
  70. _toggle_visible(n);
  71. List<Node *> selection = editor_selection->get_selected_node_list();
  72. if (selection.size() > 1 && selection.find(n) != nullptr) {
  73. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  74. Node *nv = E->get();
  75. ERR_FAIL_COND(!nv);
  76. if (nv == n) {
  77. continue;
  78. }
  79. _toggle_visible(nv);
  80. }
  81. }
  82. undo_redo->commit_action();
  83. } else if (p_id == BUTTON_LOCK) {
  84. undo_redo->create_action(TTR("Unlock Node"));
  85. if (n->is_class("CanvasItem") || n->is_class("Spatial")) {
  86. undo_redo->add_do_method(n, "remove_meta", "_edit_lock_");
  87. undo_redo->add_undo_method(n, "set_meta", "_edit_lock_", true);
  88. undo_redo->add_do_method(this, "_update_tree", Variant());
  89. undo_redo->add_undo_method(this, "_update_tree", Variant());
  90. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  91. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  92. }
  93. undo_redo->commit_action();
  94. } else if (p_id == BUTTON_PIN) {
  95. if (n->is_class("AnimationPlayer")) {
  96. AnimationPlayerEditor::singleton->unpin();
  97. _update_tree();
  98. }
  99. } else if (p_id == BUTTON_GROUP) {
  100. undo_redo->create_action(TTR("Button Group"));
  101. if (n->is_class("CanvasItem") || n->is_class("Spatial")) {
  102. undo_redo->add_do_method(n, "remove_meta", "_edit_group_");
  103. undo_redo->add_undo_method(n, "set_meta", "_edit_group_", true);
  104. undo_redo->add_do_method(this, "_update_tree", Variant());
  105. undo_redo->add_undo_method(this, "_update_tree", Variant());
  106. undo_redo->add_do_method(this, "emit_signal", "node_changed");
  107. undo_redo->add_undo_method(this, "emit_signal", "node_changed");
  108. }
  109. undo_redo->commit_action();
  110. } else if (p_id == BUTTON_WARNING) {
  111. String config_err = n->get_configuration_warning();
  112. if (config_err == String()) {
  113. return;
  114. }
  115. config_err = config_err.word_wrap(80);
  116. warning->set_text(config_err);
  117. warning->popup_centered_minsize();
  118. } else if (p_id == BUTTON_SIGNALS) {
  119. editor_selection->clear();
  120. editor_selection->add_node(n);
  121. set_selected(n);
  122. NodeDock::singleton->get_parent()->call("set_current_tab", NodeDock::singleton->get_index());
  123. NodeDock::singleton->show_connections();
  124. } else if (p_id == BUTTON_GROUPS) {
  125. editor_selection->clear();
  126. editor_selection->add_node(n);
  127. set_selected(n);
  128. NodeDock::singleton->get_parent()->call("set_current_tab", NodeDock::singleton->get_index());
  129. NodeDock::singleton->show_groups();
  130. } else if (p_id == BUTTON_UNIQUE) {
  131. undo_redo->create_action(TTR("Disable Scene Unique Name"));
  132. undo_redo->add_do_method(n, "set_unique_name_in_owner", false);
  133. undo_redo->add_undo_method(n, "set_unique_name_in_owner", true);
  134. undo_redo->add_do_method(this, "_update_tree");
  135. undo_redo->add_undo_method(this, "_update_tree");
  136. undo_redo->commit_action();
  137. }
  138. }
  139. void SceneTreeEditor::_toggle_visible(Node *p_node) {
  140. if (p_node->has_method("is_visible") && p_node->has_method("set_visible")) {
  141. bool v = bool(p_node->call("is_visible"));
  142. undo_redo->add_do_method(p_node, "set_visible", !v);
  143. undo_redo->add_undo_method(p_node, "set_visible", v);
  144. }
  145. }
  146. void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent, bool p_disable_visibility) {
  147. if (!p_node) {
  148. return;
  149. }
  150. // only owned nodes are editable, since nodes can create their own (manually owned) child nodes,
  151. // which the editor needs not to know about.
  152. bool part_of_subscene = false;
  153. if (!display_foreign && p_node->get_owner() != get_scene_node() && p_node != get_scene_node()) {
  154. if ((show_enabled_subscene || can_open_instance) && p_node->get_owner() && (get_scene_node()->is_editable_instance(p_node->get_owner()))) {
  155. part_of_subscene = true;
  156. //allow
  157. } else {
  158. return;
  159. }
  160. } else {
  161. 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;
  162. }
  163. TreeItem *item = tree->create_item(p_parent);
  164. item->set_text(0, p_node->get_name());
  165. if (can_rename && !part_of_subscene /*(p_node->get_owner() == get_scene_node() || p_node==get_scene_node())*/) {
  166. item->set_editable(0, true);
  167. }
  168. item->set_selectable(0, true);
  169. if (can_rename) {
  170. #ifndef DISABLE_DEPRECATED
  171. if (p_node->has_meta("_editor_collapsed")) {
  172. //remove previous way of storing folding, which did not get along with scene inheritance and instancing
  173. if ((bool)p_node->get_meta("_editor_collapsed")) {
  174. p_node->set_display_folded(true);
  175. }
  176. p_node->set_meta("_editor_collapsed", Variant());
  177. }
  178. #endif
  179. bool collapsed = p_node->is_displayed_folded();
  180. if (collapsed) {
  181. item->set_collapsed(true);
  182. }
  183. }
  184. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(p_node, "Node");
  185. item->set_icon(0, icon);
  186. item->set_metadata(0, p_node->get_path());
  187. if (connect_to_script_mode) {
  188. Color accent = get_color("accent_color", "Editor");
  189. Ref<Script> script = p_node->get_script();
  190. if (!script.is_null() && EditorNode::get_singleton()->get_object_custom_type_base(p_node) != script) {
  191. //has script
  192. item->add_button(0, get_icon("Script", "EditorIcons"), BUTTON_SCRIPT);
  193. } else {
  194. //has no script (or script is a custom type)
  195. item->set_custom_color(0, get_color("disabled_font_color", "Editor"));
  196. item->set_selectable(0, false);
  197. if (!script.is_null()) { // make sure to mark the script if a custom type
  198. item->add_button(0, get_icon("Script", "EditorIcons"), BUTTON_SCRIPT);
  199. item->set_button_disabled(0, item->get_button_count(0) - 1, true);
  200. }
  201. accent.a *= 0.7;
  202. }
  203. if (marked.has(p_node)) {
  204. String node_name = p_node->get_name();
  205. if (connecting_signal) {
  206. node_name += " " + TTR("(Connecting From)");
  207. }
  208. item->set_text(0, node_name);
  209. item->set_custom_color(0, accent);
  210. }
  211. } else if (part_of_subscene) {
  212. if (valid_types.size() == 0) {
  213. item->set_custom_color(0, get_color("warning_color", "Editor"));
  214. }
  215. } else if (marked.has(p_node)) {
  216. String node_name = p_node->get_name();
  217. if (connecting_signal) {
  218. node_name += " " + TTR("(Connecting From)");
  219. }
  220. item->set_text(0, node_name);
  221. item->set_selectable(0, marked_selectable);
  222. item->set_custom_color(0, get_color("accent_color", "Editor"));
  223. } else if (!marked_selectable && !marked_children_selectable) {
  224. Node *node = p_node;
  225. while (node) {
  226. if (marked.has(node)) {
  227. item->set_selectable(0, false);
  228. item->set_custom_color(0, get_color("error_color", "Editor"));
  229. break;
  230. }
  231. node = node->get_parent();
  232. }
  233. }
  234. if (can_rename) { //should be can edit..
  235. String warning = p_node->get_configuration_warning();
  236. if (warning != String()) {
  237. item->add_button(0, get_icon("NodeWarning", "EditorIcons"), BUTTON_WARNING, false, TTR("Node configuration warning:") + "\n" + p_node->get_configuration_warning());
  238. }
  239. if (p_node->is_unique_name_in_owner()) {
  240. item->add_button(0, get_icon("SceneUniqueName", "EditorIcons"), BUTTON_UNIQUE, false, vformat(TTR("This node can be accessed from within anywhere in the scene by preceding it with the '%s' prefix in a node path.\nClick to disable this."), UNIQUE_NODE_PREFIX));
  241. }
  242. int num_connections = p_node->get_persistent_signal_connection_count();
  243. int num_groups = p_node->get_persistent_group_count();
  244. if (num_connections >= 1 && num_groups >= 1) {
  245. item->add_button(
  246. 0,
  247. get_icon("SignalsAndGroups", "EditorIcons"),
  248. BUTTON_SIGNALS,
  249. false,
  250. vformat(TTR("Node has %s connection(s) and %s group(s).\nClick to show signals dock."), num_connections, num_groups));
  251. } else if (num_connections >= 1) {
  252. item->add_button(
  253. 0,
  254. get_icon("Signals", "EditorIcons"),
  255. BUTTON_SIGNALS,
  256. false,
  257. vformat(TTR("Node has %s connection(s).\nClick to show signals dock."), num_connections));
  258. } else if (num_groups >= 1) {
  259. item->add_button(
  260. 0,
  261. get_icon("Groups", "EditorIcons"),
  262. BUTTON_GROUPS,
  263. false,
  264. vformat(TTR("Node is in %s group(s).\nClick to show groups dock."), num_groups));
  265. }
  266. }
  267. // Display the node name in all tooltips so that long node names can be previewed
  268. // without having to rename them.
  269. if (p_node == get_scene_node() && p_node->get_scene_inherited_state().is_valid()) {
  270. item->add_button(0, get_icon("InstanceOptions", "EditorIcons"), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  271. String tooltip = String(p_node->get_name()) + "\n" + TTR("Inherits:") + " " + p_node->get_scene_inherited_state()->get_path() + "\n" + TTR("Type:") + " " + p_node->get_class();
  272. if (p_node->get_editor_description() != String()) {
  273. tooltip += "\n\n" + p_node->get_editor_description();
  274. }
  275. item->set_tooltip(0, tooltip);
  276. } else if (p_node != get_scene_node() && p_node->get_filename() != "" && can_open_instance) {
  277. item->add_button(0, get_icon("InstanceOptions", "EditorIcons"), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
  278. String tooltip = String(p_node->get_name()) + "\n" + TTR("Instance:") + " " + p_node->get_filename() + "\n" + TTR("Type:") + " " + p_node->get_class();
  279. if (p_node->get_editor_description() != String()) {
  280. tooltip += "\n\n" + p_node->get_editor_description();
  281. }
  282. item->set_tooltip(0, tooltip);
  283. } else {
  284. StringName type = EditorNode::get_singleton()->get_object_custom_type_name(p_node);
  285. if (type == StringName()) {
  286. type = p_node->get_class();
  287. }
  288. String tooltip = String(p_node->get_name()) + "\n" + TTR("Type:") + " " + type;
  289. if (p_node->get_editor_description() != String()) {
  290. tooltip += "\n\n" + p_node->get_editor_description();
  291. }
  292. item->set_tooltip(0, tooltip);
  293. }
  294. if (can_open_instance && undo_redo) { //Show buttons only when necessary(SceneTreeDock) to avoid crashes
  295. if (!p_node->is_connected("script_changed", this, "_node_script_changed")) {
  296. p_node->connect("script_changed", this, "_node_script_changed", varray(p_node));
  297. }
  298. Ref<Script> script = p_node->get_script();
  299. if (!script.is_null()) {
  300. String additional_notes;
  301. Color button_color = Color(1, 1, 1);
  302. // Can't set tooltip after adding button, need to do it before.
  303. if (script->is_tool()) {
  304. additional_notes += "\n" + TTR("This script is currently running in the editor.");
  305. button_color = get_color("accent_color", "Editor");
  306. }
  307. if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == script) {
  308. additional_notes += "\n" + TTR("This script is a custom type.");
  309. button_color.a = 0.5;
  310. }
  311. item->add_button(0, get_icon("Script", "EditorIcons"), BUTTON_SCRIPT, false, TTR("Open Script:") + " " + script->get_path() + additional_notes);
  312. item->set_button_color(0, item->get_button_count(0) - 1, button_color);
  313. }
  314. if (p_node->is_class("CanvasItem")) {
  315. bool is_locked = p_node->has_meta("_edit_lock_"); //_edit_group_
  316. if (is_locked) {
  317. item->add_button(0, get_icon("Lock", "EditorIcons"), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
  318. }
  319. bool is_grouped = p_node->has_meta("_edit_group_");
  320. if (is_grouped) {
  321. item->add_button(0, get_icon("Group", "EditorIcons"), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
  322. }
  323. bool v = p_node->call("is_visible");
  324. if (v) {
  325. item->add_button(0, get_icon("GuiVisibilityVisible", "EditorIcons"), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  326. } else {
  327. item->add_button(0, get_icon("GuiVisibilityHidden", "EditorIcons"), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  328. }
  329. if (!p_node->is_connected("visibility_changed", this, "_node_visibility_changed")) {
  330. p_node->connect("visibility_changed", this, "_node_visibility_changed", varray(p_node));
  331. }
  332. _update_visibility_color(p_node, item);
  333. } else if (p_node->is_class("CanvasLayer")) {
  334. bool v = p_node->call("is_visible");
  335. if (v) {
  336. item->add_button(0, get_icon("GuiVisibilityVisible", "EditorIcons"), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  337. } else {
  338. item->add_button(0, get_icon("GuiVisibilityHidden", "EditorIcons"), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
  339. }
  340. if (!p_node->is_connected("visibility_changed", this, "_node_visibility_changed")) {
  341. p_node->connect("visibility_changed", this, "_node_visibility_changed", varray(p_node));
  342. }
  343. } else if (p_node->is_class("Spatial")) {
  344. bool is_locked = p_node->has_meta("_edit_lock_");
  345. if (is_locked) {
  346. item->add_button(0, get_icon("Lock", "EditorIcons"), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
  347. }
  348. bool is_grouped = p_node->has_meta("_edit_group_");
  349. if (is_grouped) {
  350. item->add_button(0, get_icon("Group", "EditorIcons"), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
  351. }
  352. bool v = p_node->call("is_visible");
  353. if (v) {
  354. item->add_button(0, get_icon("GuiVisibilityVisible", "EditorIcons"), BUTTON_VISIBILITY, p_disable_visibility, TTR("Toggle Visibility"));
  355. } else {
  356. item->add_button(0, get_icon("GuiVisibilityHidden", "EditorIcons"), BUTTON_VISIBILITY, p_disable_visibility, TTR("Toggle Visibility"));
  357. }
  358. if (!p_node->is_connected("visibility_changed", this, "_node_visibility_changed")) {
  359. p_node->connect("visibility_changed", this, "_node_visibility_changed", varray(p_node));
  360. }
  361. _update_visibility_color(p_node, item);
  362. } else if (p_node->is_class("AnimationPlayer")) {
  363. bool is_pinned = AnimationPlayerEditor::singleton->get_player() == p_node && AnimationPlayerEditor::singleton->is_pinned();
  364. if (is_pinned) {
  365. item->add_button(0, get_icon("Pin", "EditorIcons"), BUTTON_PIN, false, TTR("AnimationPlayer is pinned.\nClick to unpin."));
  366. }
  367. }
  368. }
  369. if (editor_selection) {
  370. if (editor_selection->is_selected(p_node)) {
  371. item->select(0);
  372. }
  373. }
  374. if (selected == p_node) {
  375. if (!editor_selection) {
  376. item->select(0);
  377. }
  378. item->set_as_cursor(0);
  379. }
  380. // In some cases we want to disable visibility control by the user
  381. // for automatically visibility-controlled children.
  382. bool disable_visibility = p_node->is_class("LOD") && LODManager::is_enabled();
  383. for (int i = 0; i < p_node->get_child_count(); i++) {
  384. _add_nodes(p_node->get_child(i), item, disable_visibility);
  385. }
  386. if (valid_types.size()) {
  387. bool valid = false;
  388. for (int i = 0; i < valid_types.size(); i++) {
  389. if (p_node->is_class(valid_types[i])) {
  390. valid = true;
  391. break;
  392. }
  393. }
  394. if (!valid) {
  395. item->set_custom_color(0, get_color("disabled_font_color", "Editor"));
  396. item->set_selectable(0, false);
  397. }
  398. }
  399. }
  400. void SceneTreeEditor::_node_visibility_changed(Node *p_node) {
  401. if (!p_node || (p_node != get_scene_node() && !p_node->get_owner())) {
  402. return;
  403. }
  404. TreeItem *item = _find(tree->get_root(), p_node->get_path());
  405. if (!item) {
  406. return;
  407. }
  408. int idx = item->get_button_by_id(0, BUTTON_VISIBILITY);
  409. ERR_FAIL_COND(idx == -1);
  410. bool visible = false;
  411. if (p_node->is_class("CanvasItem")) {
  412. visible = p_node->call("is_visible");
  413. CanvasItemEditor::get_singleton()->get_viewport_control()->update();
  414. } else if (p_node->is_class("CanvasLayer")) {
  415. visible = p_node->call("is_visible");
  416. CanvasItemEditor::get_singleton()->get_viewport_control()->update();
  417. } else if (p_node->is_class("Spatial")) {
  418. visible = p_node->call("is_visible");
  419. }
  420. if (visible) {
  421. item->set_button(0, idx, get_icon("GuiVisibilityVisible", "EditorIcons"));
  422. } else {
  423. item->set_button(0, idx, get_icon("GuiVisibilityHidden", "EditorIcons"));
  424. }
  425. _update_visibility_color(p_node, item);
  426. }
  427. void SceneTreeEditor::_update_visibility_color(Node *p_node, TreeItem *p_item) {
  428. if (p_node->is_class("CanvasItem") || p_node->is_class("Spatial")) {
  429. Color color(1, 1, 1, 1);
  430. bool visible_on_screen = p_node->call("is_visible_in_tree");
  431. if (!visible_on_screen) {
  432. color.a = 0.6;
  433. }
  434. int idx = p_item->get_button_by_id(0, BUTTON_VISIBILITY);
  435. p_item->set_button_color(0, idx, color);
  436. }
  437. }
  438. void SceneTreeEditor::_node_script_changed(Node *p_node) {
  439. if (tree_dirty) {
  440. return;
  441. }
  442. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  443. tree_dirty = true;
  444. /*
  445. changes the order :|
  446. TreeItem* item=p_node?_find(tree->get_root(),p_node->get_path()):NULL;
  447. if (p_node->get_script().is_null()) {
  448. int idx=item->get_button_by_id(0,2);
  449. if (idx>=0)
  450. item->erase_button(0,idx);
  451. } else {
  452. int idx=item->get_button_by_id(0,2);
  453. if (idx<0)
  454. item->add_button(0,get_icon("Script","EditorIcons"),2);
  455. }*/
  456. }
  457. void SceneTreeEditor::_node_removed(Node *p_node) {
  458. if (EditorNode::get_singleton()->is_exiting()) {
  459. return; //speed up exit
  460. }
  461. if (p_node->is_connected("script_changed", this, "_node_script_changed")) {
  462. p_node->disconnect("script_changed", this, "_node_script_changed");
  463. }
  464. if (p_node->is_class("Spatial") || p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer")) {
  465. if (p_node->is_connected("visibility_changed", this, "_node_visibility_changed")) {
  466. p_node->disconnect("visibility_changed", this, "_node_visibility_changed");
  467. }
  468. }
  469. if (p_node == selected) {
  470. selected = nullptr;
  471. emit_signal("node_selected");
  472. }
  473. }
  474. void SceneTreeEditor::_node_renamed(Node *p_node) {
  475. if (p_node != get_scene_node() && !get_scene_node()->is_a_parent_of(p_node)) {
  476. return;
  477. }
  478. emit_signal("node_renamed");
  479. if (!tree_dirty) {
  480. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  481. tree_dirty = true;
  482. }
  483. }
  484. void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
  485. if (!is_inside_tree()) {
  486. tree_dirty = false;
  487. return;
  488. }
  489. updating_tree = true;
  490. tree->clear();
  491. if (get_scene_node()) {
  492. _add_nodes(get_scene_node(), nullptr);
  493. last_hash = hash_djb2_one_64(0);
  494. _compute_hash(get_scene_node(), last_hash);
  495. }
  496. updating_tree = false;
  497. tree_dirty = false;
  498. if (!filter.strip_edges().empty()) {
  499. _update_filter(nullptr, p_scroll_to_selected);
  500. }
  501. }
  502. bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_selected) {
  503. if (!p_parent) {
  504. p_parent = tree->get_root();
  505. filter_term_warning.clear();
  506. }
  507. if (!p_parent) {
  508. // Tree is empty, nothing to do here.
  509. return false;
  510. }
  511. bool keep_for_children = false;
  512. // Get the list of children ahead of time, as the list may be invalidated by deleting one of them.
  513. LocalVector<TreeItem *> children;
  514. for (TreeItem *child = p_parent->get_children(); child; child = child->get_next()) {
  515. children.push_back(child);
  516. }
  517. for (uint32_t n = 0; n < children.size(); n++) {
  518. // Always keep if at least one of the children are kept.
  519. keep_for_children = _update_filter(children[n], p_scroll_to_selected) || keep_for_children;
  520. }
  521. // Now find other reasons to keep this Node, too.
  522. Vector<String> terms = filter.to_lower().split_spaces();
  523. bool keep = _item_matches_all_terms(p_parent, terms);
  524. if (keep_for_children || keep) {
  525. if (keep_for_children) {
  526. if (!keep) {
  527. const Color original_color = p_parent->get_custom_color(0);
  528. const Color disabled_color = get_color("disabled_font_color", "Editor");
  529. if (original_color == Color()) {
  530. p_parent->set_custom_color(0, disabled_color);
  531. } else {
  532. p_parent->set_custom_color(0, original_color.linear_interpolate(disabled_color, 0.5));
  533. p_parent->set_selectable(0, false);
  534. p_parent->deselect(0);
  535. }
  536. }
  537. }
  538. }
  539. if (editor_selection) {
  540. Node *n = get_node_or_null(p_parent->get_metadata(0));
  541. if (keep) {
  542. if (p_scroll_to_selected && n && editor_selection->is_selected(n)) {
  543. tree->scroll_to_item(p_parent);
  544. }
  545. } else {
  546. if (n && p_parent->is_selected(0)) {
  547. editor_selection->remove_node(n);
  548. p_parent->deselect(0);
  549. }
  550. }
  551. }
  552. if (!(keep || keep_for_children)) {
  553. memdelete(p_parent);
  554. }
  555. return keep || keep_for_children;
  556. }
  557. bool SceneTreeEditor::_item_matches_all_terms(TreeItem *p_item, Vector<String> p_terms) {
  558. if (p_terms.empty()) {
  559. return true;
  560. }
  561. for (int i = 0; i < p_terms.size(); i++) {
  562. String term = p_terms[i];
  563. // Recognise special filter.
  564. if (term.find(":") > -1 && !term.get_slicec(':', 0).empty()) {
  565. String parameter = term.get_slicec(':', 0);
  566. String argument = term.get_slicec(':', 1);
  567. if (parameter == "type" || parameter == "t") {
  568. // Filter by Type.
  569. String type = get_node(p_item->get_metadata(0))->get_class();
  570. bool term_in_inherited_class = false;
  571. // Every Node is is a Node, duh!
  572. while (type != "Node") {
  573. if (type.to_lower().find(argument) > -1) {
  574. term_in_inherited_class = true;
  575. break;
  576. }
  577. type = ClassDB::get_parent_class(type);
  578. }
  579. if (!term_in_inherited_class) {
  580. return false;
  581. }
  582. } else if (parameter == "group" || parameter == "g") {
  583. // Filter by Group.
  584. Node *node = get_node(p_item->get_metadata(0));
  585. if (argument.empty()) {
  586. // When argument is empty, match all Nodes belonging to any exposed group.
  587. if (node->get_persistent_group_count() == 0) {
  588. return false;
  589. }
  590. } else {
  591. List<Node::GroupInfo> group_info_list;
  592. node->get_groups(&group_info_list);
  593. bool term_in_groups = false;
  594. for (int j = 0; j < group_info_list.size(); j++) {
  595. if (!group_info_list[j].persistent) {
  596. continue; // Ignore internal groups.
  597. }
  598. if (String(group_info_list[j].name).to_lower().find(argument) > -1) {
  599. term_in_groups = true;
  600. break;
  601. }
  602. }
  603. if (!term_in_groups) {
  604. return false;
  605. }
  606. }
  607. } else if (filter_term_warning.empty()) {
  608. filter_term_warning = vformat(TTR("\"%s\" is not a known filter."), parameter);
  609. continue;
  610. }
  611. } else {
  612. // Default.
  613. if (p_item->get_text(0).to_lower().find(term) == -1) {
  614. return false;
  615. }
  616. }
  617. }
  618. return true;
  619. }
  620. void SceneTreeEditor::_compute_hash(Node *p_node, uint64_t &hash) {
  621. hash = hash_djb2_one_64(p_node->get_instance_id(), hash);
  622. if (p_node->get_parent()) {
  623. hash = hash_djb2_one_64(p_node->get_parent()->get_instance_id(), hash); //so a reparent still produces a different hash
  624. }
  625. for (int i = 0; i < p_node->get_child_count(); i++) {
  626. _compute_hash(p_node->get_child(i), hash);
  627. }
  628. }
  629. void SceneTreeEditor::_test_update_tree() {
  630. pending_test_update = false;
  631. if (!is_inside_tree()) {
  632. return;
  633. }
  634. if (tree_dirty) {
  635. return; // don't even bother
  636. }
  637. uint64_t hash = hash_djb2_one_64(0);
  638. if (get_scene_node()) {
  639. _compute_hash(get_scene_node(), hash);
  640. }
  641. //test hash
  642. if (hash == last_hash) {
  643. return; // did not change
  644. }
  645. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  646. tree_dirty = true;
  647. }
  648. void SceneTreeEditor::_tree_changed() {
  649. if (EditorNode::get_singleton()->is_exiting()) {
  650. return; //speed up exit
  651. }
  652. if (pending_test_update) {
  653. return;
  654. }
  655. if (tree_dirty) {
  656. return;
  657. }
  658. MessageQueue::get_singleton()->push_call(this, "_test_update_tree");
  659. pending_test_update = true;
  660. }
  661. void SceneTreeEditor::_selected_changed() {
  662. TreeItem *s = tree->get_selected();
  663. ERR_FAIL_COND(!s);
  664. NodePath np = s->get_metadata(0);
  665. Node *n = get_node(np);
  666. if (n == selected) {
  667. return;
  668. }
  669. selected = get_node(np);
  670. blocked++;
  671. emit_signal("node_selected");
  672. blocked--;
  673. }
  674. void SceneTreeEditor::_deselect_items() {
  675. // Clear currently selected items in scene tree dock.
  676. if (editor_selection) {
  677. editor_selection->clear();
  678. emit_signal("node_changed");
  679. }
  680. }
  681. void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  682. TreeItem *item = Object::cast_to<TreeItem>(p_object);
  683. ERR_FAIL_COND(!item);
  684. NodePath np = item->get_metadata(0);
  685. Node *n = get_node(np);
  686. if (!n) {
  687. return;
  688. }
  689. if (!editor_selection) {
  690. return;
  691. }
  692. if (p_selected) {
  693. editor_selection->add_node(n);
  694. } else {
  695. editor_selection->remove_node(n);
  696. }
  697. emit_signal("node_changed");
  698. }
  699. void SceneTreeEditor::_notification(int p_what) {
  700. switch (p_what) {
  701. case NOTIFICATION_ENTER_TREE: {
  702. get_tree()->connect("tree_changed", this, "_tree_changed");
  703. get_tree()->connect("node_removed", this, "_node_removed");
  704. get_tree()->connect("node_renamed", this, "_node_renamed");
  705. get_tree()->connect("node_configuration_warning_changed", this, "_warning_changed");
  706. tree->connect("item_collapsed", this, "_cell_collapsed");
  707. _update_tree();
  708. } break;
  709. case NOTIFICATION_EXIT_TREE: {
  710. get_tree()->disconnect("tree_changed", this, "_tree_changed");
  711. get_tree()->disconnect("node_removed", this, "_node_removed");
  712. get_tree()->disconnect("node_renamed", this, "_node_renamed");
  713. tree->disconnect("item_collapsed", this, "_cell_collapsed");
  714. get_tree()->disconnect("node_configuration_warning_changed", this, "_warning_changed");
  715. } break;
  716. case NOTIFICATION_THEME_CHANGED: {
  717. _update_tree();
  718. } break;
  719. }
  720. }
  721. TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  722. if (!p_node) {
  723. return nullptr;
  724. }
  725. NodePath np = p_node->get_metadata(0);
  726. if (np == p_path) {
  727. return p_node;
  728. }
  729. TreeItem *children = p_node->get_children();
  730. while (children) {
  731. TreeItem *n = _find(children, p_path);
  732. if (n) {
  733. return n;
  734. }
  735. children = children->get_next();
  736. }
  737. return nullptr;
  738. }
  739. void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
  740. ERR_FAIL_COND(blocked > 0);
  741. if (pending_test_update) {
  742. _test_update_tree();
  743. }
  744. if (tree_dirty) {
  745. _update_tree();
  746. }
  747. if (selected == p_node) {
  748. return;
  749. }
  750. TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : nullptr;
  751. if (item) {
  752. // make visible when it's collapsed
  753. TreeItem *node = item->get_parent();
  754. while (node && node != tree->get_root()) {
  755. node->set_collapsed(false);
  756. node = node->get_parent();
  757. }
  758. item->select(0);
  759. item->set_as_cursor(0);
  760. selected = p_node;
  761. tree->ensure_cursor_is_visible();
  762. } else {
  763. if (!p_node) {
  764. selected = nullptr;
  765. }
  766. _update_tree();
  767. selected = p_node;
  768. }
  769. if (p_emit_selected) {
  770. emit_signal("node_selected");
  771. }
  772. }
  773. void SceneTreeEditor::_rename_node(ObjectID p_node, const String &p_name) {
  774. Object *o = ObjectDB::get_instance(p_node);
  775. ERR_FAIL_COND(!o);
  776. Node *n = Object::cast_to<Node>(o);
  777. ERR_FAIL_COND(!n);
  778. TreeItem *item = _find(tree->get_root(), n->get_path());
  779. ERR_FAIL_COND(!item);
  780. n->set_name(p_name);
  781. item->set_metadata(0, n->get_path());
  782. item->set_text(0, p_name);
  783. }
  784. void SceneTreeEditor::_renamed() {
  785. TreeItem *which = tree->get_edited();
  786. ERR_FAIL_COND(!which);
  787. NodePath np = which->get_metadata(0);
  788. Node *n = get_node(np);
  789. ERR_FAIL_COND(!n);
  790. // Empty node names are not allowed, so resets it to previous text and show warning
  791. if (which->get_text(0).strip_edges().empty()) {
  792. which->set_text(0, n->get_name());
  793. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  794. return;
  795. }
  796. String raw_new_name = which->get_text(0);
  797. String new_name = raw_new_name.validate_node_name();
  798. if (new_name != raw_new_name) {
  799. error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::invalid_node_name_characters);
  800. error->popup_centered_minsize();
  801. if (new_name.empty()) {
  802. which->set_text(0, n->get_name());
  803. return;
  804. }
  805. which->set_text(0, new_name);
  806. }
  807. if (new_name == n->get_name()) {
  808. return;
  809. }
  810. // 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()`.
  811. new_name = new_name.strip_edges();
  812. if (n->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name) != nullptr) {
  813. error->set_text(TTR("Another node already uses this unique name in the scene."));
  814. error->popup_centered();
  815. which->set_text(0, n->get_name());
  816. return;
  817. }
  818. if (!undo_redo) {
  819. n->set_name(new_name);
  820. which->set_metadata(0, n->get_path());
  821. emit_signal("node_renamed");
  822. } else {
  823. undo_redo->create_action(TTR("Rename Node"));
  824. emit_signal("node_prerename", n, new_name);
  825. undo_redo->add_do_method(this, "_rename_node", n->get_instance_id(), new_name);
  826. undo_redo->add_undo_method(this, "_rename_node", n->get_instance_id(), n->get_name());
  827. undo_redo->commit_action();
  828. }
  829. }
  830. Node *SceneTreeEditor::get_selected() {
  831. return selected;
  832. }
  833. void SceneTreeEditor::set_marked(const Set<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
  834. if (tree_dirty) {
  835. _update_tree();
  836. }
  837. marked = p_marked;
  838. marked_selectable = p_selectable;
  839. marked_children_selectable = p_children_selectable;
  840. _update_tree();
  841. }
  842. void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
  843. Set<Node *> s;
  844. if (p_marked) {
  845. s.insert(p_marked);
  846. }
  847. set_marked(s, p_selectable, p_children_selectable);
  848. }
  849. void SceneTreeEditor::set_filter(const String &p_filter) {
  850. filter = p_filter;
  851. _update_tree(true);
  852. }
  853. String SceneTreeEditor::get_filter() const {
  854. return filter;
  855. }
  856. String SceneTreeEditor::get_filter_term_warning() const {
  857. return filter_term_warning;
  858. }
  859. void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
  860. display_foreign = p_display;
  861. _update_tree();
  862. }
  863. bool SceneTreeEditor::get_display_foreign_nodes() const {
  864. return display_foreign;
  865. }
  866. void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
  867. valid_types = p_valid;
  868. }
  869. void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
  870. editor_selection = p_selection;
  871. tree->set_select_mode(Tree::SELECT_MULTI);
  872. tree->set_cursor_can_exit_tree(false);
  873. editor_selection->connect("selection_changed", this, "_selection_changed");
  874. }
  875. void SceneTreeEditor::_update_selection(TreeItem *item) {
  876. ERR_FAIL_COND(!item);
  877. NodePath np = item->get_metadata(0);
  878. if (!has_node(np)) {
  879. return;
  880. }
  881. Node *n = get_node(np);
  882. if (!n) {
  883. return;
  884. }
  885. if (editor_selection->is_selected(n)) {
  886. item->select(0);
  887. } else {
  888. item->deselect(0);
  889. }
  890. TreeItem *c = item->get_children();
  891. while (c) {
  892. _update_selection(c);
  893. c = c->get_next();
  894. }
  895. }
  896. void SceneTreeEditor::_selection_changed() {
  897. if (!editor_selection) {
  898. return;
  899. }
  900. TreeItem *root = tree->get_root();
  901. if (!root) {
  902. return;
  903. }
  904. _update_selection(root);
  905. }
  906. void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
  907. if (updating_tree) {
  908. return;
  909. }
  910. if (!can_rename) {
  911. return;
  912. }
  913. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  914. if (!ti) {
  915. return;
  916. }
  917. bool collapsed = ti->is_collapsed();
  918. NodePath np = ti->get_metadata(0);
  919. Node *n = get_node(np);
  920. ERR_FAIL_COND(!n);
  921. n->set_display_folded(collapsed);
  922. }
  923. Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  924. if (!can_rename) {
  925. return Variant(); //not editable tree
  926. }
  927. if (tree->get_button_id_at_position(p_point) != -1) {
  928. return Variant(); //dragging from button
  929. }
  930. Vector<Node *> selected;
  931. Vector<Ref<Texture>> icons;
  932. TreeItem *next = tree->get_next_selected(nullptr);
  933. while (next) {
  934. NodePath np = next->get_metadata(0);
  935. Node *n = get_node(np);
  936. if (n) {
  937. selected.push_back(n);
  938. icons.push_back(next->get_icon(0));
  939. }
  940. next = tree->get_next_selected(next);
  941. }
  942. if (selected.empty()) {
  943. return Variant();
  944. }
  945. VBoxContainer *vb = memnew(VBoxContainer);
  946. Array objs;
  947. int list_max = 10;
  948. float opacity_step = 1.0f / list_max;
  949. float opacity_item = 1.0f;
  950. for (int i = 0; i < selected.size(); i++) {
  951. if (i < list_max) {
  952. HBoxContainer *hb = memnew(HBoxContainer);
  953. TextureRect *tf = memnew(TextureRect);
  954. tf->set_texture(icons[i]);
  955. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  956. hb->add_child(tf);
  957. Label *label = memnew(Label(selected[i]->get_name()));
  958. hb->add_child(label);
  959. vb->add_child(hb);
  960. hb->set_modulate(Color(1, 1, 1, opacity_item));
  961. opacity_item -= opacity_step;
  962. }
  963. NodePath p = selected[i]->get_path();
  964. objs.push_back(p);
  965. }
  966. set_drag_preview(vb);
  967. Dictionary drag_data;
  968. drag_data["type"] = "nodes";
  969. drag_data["nodes"] = objs;
  970. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  971. emit_signal("nodes_dragged");
  972. return drag_data;
  973. }
  974. bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
  975. return (script_types->find(p_type));
  976. }
  977. bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  978. if (!can_rename) {
  979. return false; //not editable tree
  980. }
  981. Dictionary d = p_data;
  982. if (!d.has("type")) {
  983. return false;
  984. }
  985. TreeItem *item = tree->get_item_at_position(p_point);
  986. if (!item) {
  987. return false;
  988. }
  989. int section = tree->get_drop_section_at_position(p_point);
  990. if (section < -1 || (section == -1 && !item->get_parent())) {
  991. return false;
  992. }
  993. if (String(d["type"]) == "files") {
  994. Vector<String> files = d["files"];
  995. if (files.size() == 0) {
  996. return false; //weird
  997. }
  998. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
  999. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1000. return true;
  1001. }
  1002. bool scene_drop = true;
  1003. for (int i = 0; i < files.size(); i++) {
  1004. String file = files[i];
  1005. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  1006. if (ftype != "PackedScene") {
  1007. scene_drop = false;
  1008. break;
  1009. }
  1010. }
  1011. if (scene_drop) {
  1012. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  1013. } else {
  1014. if (files.size() > 1) {
  1015. return false;
  1016. }
  1017. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1018. }
  1019. return true;
  1020. }
  1021. if (String(d["type"]) == "script_list_element") {
  1022. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1023. if (se) {
  1024. String sp = se->get_edited_resource()->get_path();
  1025. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1026. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1027. return true;
  1028. }
  1029. }
  1030. }
  1031. if (filter.empty() && String(d["type"]) == "nodes") {
  1032. Array nodes = d["nodes"];
  1033. for (int i = 0; i < nodes.size(); i++) {
  1034. Node *n = get_node(nodes[i]);
  1035. // Only allow selection if not part of an instanced scene.
  1036. if (n && n->get_owner() && n->get_owner() != get_scene_node() && !n->get_owner()->get_filename().empty()) {
  1037. return false;
  1038. }
  1039. }
  1040. return true;
  1041. }
  1042. return false;
  1043. }
  1044. void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1045. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  1046. return;
  1047. }
  1048. TreeItem *item = tree->get_item_at_position(p_point);
  1049. if (!item) {
  1050. return;
  1051. }
  1052. int section = tree->get_drop_section_at_position(p_point);
  1053. if (section < -1) {
  1054. return;
  1055. }
  1056. NodePath np = item->get_metadata(0);
  1057. Node *n = get_node(np);
  1058. if (!n) {
  1059. return;
  1060. }
  1061. Dictionary d = p_data;
  1062. if (String(d["type"]) == "nodes") {
  1063. Array nodes = d["nodes"];
  1064. emit_signal("nodes_rearranged", nodes, np, section);
  1065. }
  1066. if (String(d["type"]) == "files") {
  1067. Vector<String> files = d["files"];
  1068. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  1069. if (_is_script_type(ftype)) {
  1070. emit_signal("script_dropped", files[0], np);
  1071. } else {
  1072. emit_signal("files_dropped", files, np, section);
  1073. }
  1074. }
  1075. if (String(d["type"]) == "script_list_element") {
  1076. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  1077. if (se) {
  1078. String sp = se->get_edited_resource()->get_path();
  1079. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  1080. emit_signal("script_dropped", sp, np);
  1081. }
  1082. }
  1083. }
  1084. }
  1085. void SceneTreeEditor::_rmb_select(const Vector2 &p_pos) {
  1086. emit_signal("rmb_pressed", tree->get_global_transform().xform(p_pos));
  1087. }
  1088. void SceneTreeEditor::_warning_changed(Node *p_for_node) {
  1089. //should use a timer
  1090. update_timer->start();
  1091. }
  1092. void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) {
  1093. connect_to_script_mode = p_enable;
  1094. update_tree();
  1095. }
  1096. void SceneTreeEditor::set_connecting_signal(bool p_enable) {
  1097. connecting_signal = p_enable;
  1098. update_tree();
  1099. }
  1100. void SceneTreeEditor::_bind_methods() {
  1101. ClassDB::bind_method("_tree_changed", &SceneTreeEditor::_tree_changed);
  1102. ClassDB::bind_method(D_METHOD("_update_tree", "scroll_to_selected"), &SceneTreeEditor::_update_tree, DEFVAL(false));
  1103. ClassDB::bind_method("_node_removed", &SceneTreeEditor::_node_removed);
  1104. ClassDB::bind_method("_node_renamed", &SceneTreeEditor::_node_renamed);
  1105. ClassDB::bind_method("_selected_changed", &SceneTreeEditor::_selected_changed);
  1106. ClassDB::bind_method("_deselect_items", &SceneTreeEditor::_deselect_items);
  1107. ClassDB::bind_method("_renamed", &SceneTreeEditor::_renamed);
  1108. ClassDB::bind_method("_rename_node", &SceneTreeEditor::_rename_node);
  1109. ClassDB::bind_method("_test_update_tree", &SceneTreeEditor::_test_update_tree);
  1110. ClassDB::bind_method("_cell_multi_selected", &SceneTreeEditor::_cell_multi_selected);
  1111. ClassDB::bind_method("_selection_changed", &SceneTreeEditor::_selection_changed);
  1112. ClassDB::bind_method("_cell_button_pressed", &SceneTreeEditor::_cell_button_pressed);
  1113. ClassDB::bind_method("_cell_collapsed", &SceneTreeEditor::_cell_collapsed);
  1114. ClassDB::bind_method("_rmb_select", &SceneTreeEditor::_rmb_select);
  1115. ClassDB::bind_method("_warning_changed", &SceneTreeEditor::_warning_changed);
  1116. ClassDB::bind_method("_node_script_changed", &SceneTreeEditor::_node_script_changed);
  1117. ClassDB::bind_method("_node_visibility_changed", &SceneTreeEditor::_node_visibility_changed);
  1118. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SceneTreeEditor::get_drag_data_fw);
  1119. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SceneTreeEditor::can_drop_data_fw);
  1120. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SceneTreeEditor::drop_data_fw);
  1121. ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
  1122. ADD_SIGNAL(MethodInfo("node_selected"));
  1123. ADD_SIGNAL(MethodInfo("node_renamed"));
  1124. ADD_SIGNAL(MethodInfo("node_prerename"));
  1125. ADD_SIGNAL(MethodInfo("node_changed"));
  1126. ADD_SIGNAL(MethodInfo("nodes_dragged"));
  1127. ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1128. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  1129. ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
  1130. ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
  1131. ADD_SIGNAL(MethodInfo("open"));
  1132. ADD_SIGNAL(MethodInfo("open_script"));
  1133. }
  1134. SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) {
  1135. connect_to_script_mode = false;
  1136. connecting_signal = false;
  1137. undo_redo = nullptr;
  1138. tree_dirty = true;
  1139. selected = nullptr;
  1140. marked_selectable = false;
  1141. marked_children_selectable = false;
  1142. can_rename = p_can_rename;
  1143. can_open_instance = p_can_open_instance;
  1144. display_foreign = false;
  1145. editor_selection = nullptr;
  1146. if (p_label) {
  1147. Label *label = memnew(Label);
  1148. label->set_position(Point2(10, 0));
  1149. label->set_text(TTR("Scene Tree (Nodes):"));
  1150. add_child(label);
  1151. }
  1152. tree = memnew(Tree);
  1153. tree->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  1154. tree->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  1155. tree->set_begin(Point2(0, p_label ? 18 : 0));
  1156. tree->set_end(Point2(0, 0));
  1157. tree->add_constant_override("button_margin", 0);
  1158. tree->set_allow_reselect(true);
  1159. add_child(tree);
  1160. tree->set_drag_forwarding(this);
  1161. if (p_can_rename) {
  1162. tree->set_allow_rmb_select(true);
  1163. tree->connect("item_rmb_selected", this, "_rmb_select");
  1164. tree->connect("empty_tree_rmb_selected", this, "_rmb_select");
  1165. }
  1166. tree->connect("cell_selected", this, "_selected_changed");
  1167. tree->connect("item_edited", this, "_renamed", varray(), CONNECT_DEFERRED);
  1168. tree->connect("multi_selected", this, "_cell_multi_selected");
  1169. tree->connect("button_pressed", this, "_cell_button_pressed");
  1170. tree->connect("nothing_selected", this, "_deselect_items");
  1171. //tree->connect("item_edited", this,"_renamed",Vector<Variant>(),true);
  1172. error = memnew(AcceptDialog);
  1173. add_child(error);
  1174. warning = memnew(AcceptDialog);
  1175. add_child(warning);
  1176. warning->set_title(TTR("Node Configuration Warning!"));
  1177. show_enabled_subscene = false;
  1178. last_hash = 0;
  1179. pending_test_update = false;
  1180. updating_tree = false;
  1181. blocked = 0;
  1182. update_timer = memnew(Timer);
  1183. update_timer->connect("timeout", this, "_update_tree", varray(false));
  1184. update_timer->set_one_shot(true);
  1185. update_timer->set_wait_time(0.5);
  1186. add_child(update_timer);
  1187. script_types = memnew(List<StringName>);
  1188. ClassDB::get_inheriters_from_class("Script", script_types);
  1189. }
  1190. SceneTreeEditor::~SceneTreeEditor() {
  1191. memdelete(script_types);
  1192. }
  1193. /******** DIALOG *********/
  1194. void SceneTreeDialog::_notification(int p_what) {
  1195. switch (p_what) {
  1196. case NOTIFICATION_ENTER_TREE: {
  1197. connect("confirmed", this, "_select");
  1198. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  1199. filter->set_clear_button_enabled(true);
  1200. } break;
  1201. case NOTIFICATION_EXIT_TREE: {
  1202. disconnect("confirmed", this, "_select");
  1203. } break;
  1204. case NOTIFICATION_VISIBILITY_CHANGED: {
  1205. if (is_visible_in_tree()) {
  1206. tree->update_tree();
  1207. }
  1208. } break;
  1209. }
  1210. }
  1211. void SceneTreeDialog::_cancel() {
  1212. hide();
  1213. }
  1214. void SceneTreeDialog::_select() {
  1215. if (tree->get_selected()) {
  1216. emit_signal("selected", tree->get_selected()->get_path());
  1217. hide();
  1218. }
  1219. }
  1220. void SceneTreeDialog::_selected_changed() {
  1221. get_ok()->set_disabled(!tree->get_selected());
  1222. }
  1223. void SceneTreeDialog::_filter_changed(const String &p_filter) {
  1224. tree->set_filter(p_filter);
  1225. }
  1226. void SceneTreeDialog::_bind_methods() {
  1227. ClassDB::bind_method("_select", &SceneTreeDialog::_select);
  1228. ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
  1229. ClassDB::bind_method(D_METHOD("_selected_changed"), &SceneTreeDialog::_selected_changed);
  1230. ClassDB::bind_method(D_METHOD("_filter_changed"), &SceneTreeDialog::_filter_changed);
  1231. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
  1232. }
  1233. SceneTreeDialog::SceneTreeDialog() {
  1234. set_title(TTR("Select a Node"));
  1235. VBoxContainer *vbc = memnew(VBoxContainer);
  1236. add_child(vbc);
  1237. filter = memnew(LineEdit);
  1238. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1239. filter->set_placeholder(TTR("Filter nodes"));
  1240. filter->add_constant_override("minimum_spaces", 0);
  1241. filter->connect("text_changed", this, "_filter_changed");
  1242. vbc->add_child(filter);
  1243. tree = memnew(SceneTreeEditor(false, false, true));
  1244. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1245. tree->get_scene_tree()->connect("item_activated", this, "_select");
  1246. vbc->add_child(tree);
  1247. // Disable the OK button when no node is selected.
  1248. get_ok()->set_disabled(!tree->get_selected());
  1249. tree->connect("node_selected", this, "_selected_changed");
  1250. }
  1251. SceneTreeDialog::~SceneTreeDialog() {
  1252. }