scene_tree_editor.cpp 41 KB

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