2
0

scene_tree_editor.cpp 38 KB

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