scene_tree_editor.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. emit_signal("node_renamed");
  439. if (!tree_dirty) {
  440. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  441. tree_dirty = true;
  442. }
  443. }
  444. void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
  445. if (!is_inside_tree()) {
  446. tree_dirty = false;
  447. return;
  448. }
  449. updating_tree = true;
  450. tree->clear();
  451. if (get_scene_node()) {
  452. _add_nodes(get_scene_node(), NULL, p_scroll_to_selected);
  453. last_hash = hash_djb2_one_64(0);
  454. _compute_hash(get_scene_node(), last_hash);
  455. }
  456. updating_tree = false;
  457. tree_dirty = false;
  458. }
  459. void SceneTreeEditor::_compute_hash(Node *p_node, uint64_t &hash) {
  460. hash = hash_djb2_one_64(p_node->get_instance_id(), hash);
  461. if (p_node->get_parent())
  462. hash = hash_djb2_one_64(p_node->get_parent()->get_instance_id(), hash); //so a reparent still produces a different hash
  463. for (int i = 0; i < p_node->get_child_count(); i++) {
  464. _compute_hash(p_node->get_child(i), hash);
  465. }
  466. }
  467. void SceneTreeEditor::_test_update_tree() {
  468. pending_test_update = false;
  469. if (!is_inside_tree())
  470. return;
  471. if (tree_dirty)
  472. return; // don't even bother
  473. uint64_t hash = hash_djb2_one_64(0);
  474. if (get_scene_node())
  475. _compute_hash(get_scene_node(), hash);
  476. //test hash
  477. if (hash == last_hash)
  478. return; // did not change
  479. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  480. tree_dirty = true;
  481. }
  482. void SceneTreeEditor::_tree_changed() {
  483. if (EditorNode::get_singleton()->is_exiting())
  484. return; //speed up exit
  485. if (pending_test_update)
  486. return;
  487. if (tree_dirty)
  488. return;
  489. MessageQueue::get_singleton()->push_call(this, "_test_update_tree");
  490. pending_test_update = true;
  491. }
  492. void SceneTreeEditor::_selected_changed() {
  493. TreeItem *s = tree->get_selected();
  494. ERR_FAIL_COND(!s);
  495. NodePath np = s->get_metadata(0);
  496. Node *n = get_node(np);
  497. if (n == selected)
  498. return;
  499. selected = get_node(np);
  500. blocked++;
  501. emit_signal("node_selected");
  502. blocked--;
  503. }
  504. void SceneTreeEditor::_deselect_items() {
  505. // Clear currently selected items in scene tree dock.
  506. if (editor_selection) {
  507. editor_selection->clear();
  508. emit_signal("node_changed");
  509. }
  510. }
  511. void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  512. TreeItem *item = Object::cast_to<TreeItem>(p_object);
  513. ERR_FAIL_COND(!item);
  514. NodePath np = item->get_metadata(0);
  515. Node *n = get_node(np);
  516. if (!n)
  517. return;
  518. if (!editor_selection)
  519. return;
  520. if (p_selected) {
  521. editor_selection->add_node(n);
  522. } else {
  523. editor_selection->remove_node(n);
  524. }
  525. emit_signal("node_changed");
  526. }
  527. void SceneTreeEditor::_notification(int p_what) {
  528. switch (p_what) {
  529. case NOTIFICATION_ENTER_TREE: {
  530. get_tree()->connect("tree_changed", this, "_tree_changed");
  531. get_tree()->connect("node_removed", this, "_node_removed");
  532. get_tree()->connect("node_renamed", this, "_node_renamed");
  533. get_tree()->connect("node_configuration_warning_changed", this, "_warning_changed");
  534. tree->connect("item_collapsed", this, "_cell_collapsed");
  535. _update_tree();
  536. } break;
  537. case NOTIFICATION_EXIT_TREE: {
  538. get_tree()->disconnect("tree_changed", this, "_tree_changed");
  539. get_tree()->disconnect("node_removed", this, "_node_removed");
  540. get_tree()->disconnect("node_renamed", this, "_node_renamed");
  541. tree->disconnect("item_collapsed", this, "_cell_collapsed");
  542. get_tree()->disconnect("node_configuration_warning_changed", this, "_warning_changed");
  543. } break;
  544. case NOTIFICATION_THEME_CHANGED: {
  545. _update_tree();
  546. } break;
  547. }
  548. }
  549. TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  550. if (!p_node)
  551. return NULL;
  552. NodePath np = p_node->get_metadata(0);
  553. if (np == p_path)
  554. return p_node;
  555. TreeItem *children = p_node->get_children();
  556. while (children) {
  557. TreeItem *n = _find(children, p_path);
  558. if (n)
  559. return n;
  560. children = children->get_next();
  561. }
  562. return NULL;
  563. }
  564. void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
  565. ERR_FAIL_COND(blocked > 0);
  566. if (pending_test_update)
  567. _test_update_tree();
  568. if (tree_dirty)
  569. _update_tree();
  570. if (selected == p_node)
  571. return;
  572. TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : NULL;
  573. if (item) {
  574. // make visible when it's collapsed
  575. TreeItem *node = item->get_parent();
  576. while (node && node != tree->get_root()) {
  577. node->set_collapsed(false);
  578. node = node->get_parent();
  579. }
  580. item->select(0);
  581. item->set_as_cursor(0);
  582. selected = p_node;
  583. tree->ensure_cursor_is_visible();
  584. } else {
  585. if (!p_node)
  586. selected = NULL;
  587. _update_tree();
  588. selected = p_node;
  589. }
  590. if (p_emit_selected) {
  591. emit_signal("node_selected");
  592. }
  593. }
  594. void SceneTreeEditor::_rename_node(ObjectID p_node, const String &p_name) {
  595. Object *o = ObjectDB::get_instance(p_node);
  596. ERR_FAIL_COND(!o);
  597. Node *n = Object::cast_to<Node>(o);
  598. ERR_FAIL_COND(!n);
  599. TreeItem *item = _find(tree->get_root(), n->get_path());
  600. ERR_FAIL_COND(!item);
  601. n->set_name(p_name);
  602. item->set_metadata(0, n->get_path());
  603. item->set_text(0, p_name);
  604. }
  605. void SceneTreeEditor::_renamed() {
  606. TreeItem *which = tree->get_edited();
  607. ERR_FAIL_COND(!which);
  608. NodePath np = which->get_metadata(0);
  609. Node *n = get_node(np);
  610. ERR_FAIL_COND(!n);
  611. // Empty node names are not allowed, so resets it to previous text and show warning
  612. if (which->get_text(0).strip_edges().empty()) {
  613. which->set_text(0, n->get_name());
  614. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  615. return;
  616. }
  617. String raw_new_name = which->get_text(0);
  618. String new_name = raw_new_name.validate_node_name();
  619. if (new_name != raw_new_name) {
  620. error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::invalid_node_name_characters);
  621. error->popup_centered_minsize();
  622. if (new_name.empty()) {
  623. which->set_text(0, n->get_name());
  624. return;
  625. }
  626. which->set_text(0, new_name);
  627. }
  628. if (new_name == n->get_name())
  629. return;
  630. // 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()`.
  631. new_name = new_name.strip_edges();
  632. if (!undo_redo) {
  633. n->set_name(new_name);
  634. which->set_metadata(0, n->get_path());
  635. emit_signal("node_renamed");
  636. } else {
  637. undo_redo->create_action(TTR("Rename Node"));
  638. emit_signal("node_prerename", n, new_name);
  639. undo_redo->add_do_method(this, "_rename_node", n->get_instance_id(), new_name);
  640. undo_redo->add_undo_method(this, "_rename_node", n->get_instance_id(), n->get_name());
  641. undo_redo->commit_action();
  642. }
  643. }
  644. Node *SceneTreeEditor::get_selected() {
  645. return selected;
  646. }
  647. void SceneTreeEditor::set_marked(const Set<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
  648. if (tree_dirty)
  649. _update_tree();
  650. marked = p_marked;
  651. marked_selectable = p_selectable;
  652. marked_children_selectable = p_children_selectable;
  653. _update_tree();
  654. }
  655. void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
  656. Set<Node *> s;
  657. if (p_marked)
  658. s.insert(p_marked);
  659. set_marked(s, p_selectable, p_children_selectable);
  660. }
  661. void SceneTreeEditor::set_filter(const String &p_filter) {
  662. filter = p_filter;
  663. _update_tree(true);
  664. }
  665. String SceneTreeEditor::get_filter() const {
  666. return filter;
  667. }
  668. void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
  669. display_foreign = p_display;
  670. _update_tree();
  671. }
  672. bool SceneTreeEditor::get_display_foreign_nodes() const {
  673. return display_foreign;
  674. }
  675. void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
  676. valid_types = p_valid;
  677. }
  678. void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
  679. editor_selection = p_selection;
  680. tree->set_select_mode(Tree::SELECT_MULTI);
  681. tree->set_cursor_can_exit_tree(false);
  682. editor_selection->connect("selection_changed", this, "_selection_changed");
  683. }
  684. void SceneTreeEditor::_update_selection(TreeItem *item) {
  685. ERR_FAIL_COND(!item);
  686. NodePath np = item->get_metadata(0);
  687. if (!has_node(np))
  688. return;
  689. Node *n = get_node(np);
  690. if (!n)
  691. return;
  692. if (editor_selection->is_selected(n))
  693. item->select(0);
  694. else
  695. item->deselect(0);
  696. TreeItem *c = item->get_children();
  697. while (c) {
  698. _update_selection(c);
  699. c = c->get_next();
  700. }
  701. }
  702. void SceneTreeEditor::_selection_changed() {
  703. if (!editor_selection)
  704. return;
  705. TreeItem *root = tree->get_root();
  706. if (!root)
  707. return;
  708. _update_selection(root);
  709. }
  710. void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
  711. if (updating_tree)
  712. return;
  713. if (!can_rename)
  714. return;
  715. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  716. if (!ti)
  717. return;
  718. bool collapsed = ti->is_collapsed();
  719. NodePath np = ti->get_metadata(0);
  720. Node *n = get_node(np);
  721. ERR_FAIL_COND(!n);
  722. n->set_display_folded(collapsed);
  723. }
  724. Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  725. if (!can_rename)
  726. return Variant(); //not editable tree
  727. if (tree->get_button_id_at_position(p_point) != -1) {
  728. return Variant(); //dragging from button
  729. }
  730. Vector<Node *> selected;
  731. Vector<Ref<Texture> > icons;
  732. TreeItem *next = tree->get_next_selected(NULL);
  733. while (next) {
  734. NodePath np = next->get_metadata(0);
  735. Node *n = get_node(np);
  736. if (n) {
  737. // Only allow selection if not part of an instanced scene.
  738. if (!n->get_owner() || n->get_owner() == get_scene_node() || n->get_owner()->get_filename() == String()) {
  739. selected.push_back(n);
  740. icons.push_back(next->get_icon(0));
  741. }
  742. }
  743. next = tree->get_next_selected(next);
  744. }
  745. if (selected.empty())
  746. return Variant();
  747. VBoxContainer *vb = memnew(VBoxContainer);
  748. Array objs;
  749. int list_max = 10;
  750. float opacity_step = 1.0f / list_max;
  751. float opacity_item = 1.0f;
  752. for (int i = 0; i < selected.size(); i++) {
  753. if (i < list_max) {
  754. HBoxContainer *hb = memnew(HBoxContainer);
  755. TextureRect *tf = memnew(TextureRect);
  756. tf->set_texture(icons[i]);
  757. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  758. hb->add_child(tf);
  759. Label *label = memnew(Label(selected[i]->get_name()));
  760. hb->add_child(label);
  761. vb->add_child(hb);
  762. hb->set_modulate(Color(1, 1, 1, opacity_item));
  763. opacity_item -= opacity_step;
  764. }
  765. NodePath p = selected[i]->get_path();
  766. objs.push_back(p);
  767. }
  768. set_drag_preview(vb);
  769. Dictionary drag_data;
  770. drag_data["type"] = "nodes";
  771. drag_data["nodes"] = objs;
  772. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  773. emit_signal("nodes_dragged");
  774. return drag_data;
  775. }
  776. bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
  777. return (script_types->find(p_type));
  778. }
  779. bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  780. if (!can_rename)
  781. return false; //not editable tree
  782. if (filter != String())
  783. return false; //can't rearrange tree with filter turned on
  784. Dictionary d = p_data;
  785. if (!d.has("type"))
  786. return false;
  787. TreeItem *item = tree->get_item_at_position(p_point);
  788. if (!item)
  789. return false;
  790. int section = tree->get_drop_section_at_position(p_point);
  791. if (section < -1 || (section == -1 && !item->get_parent()))
  792. return false;
  793. if (String(d["type"]) == "files") {
  794. Vector<String> files = d["files"];
  795. if (files.size() == 0)
  796. return false; //weird
  797. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
  798. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  799. return true;
  800. }
  801. for (int i = 0; i < files.size(); i++) {
  802. String file = files[i];
  803. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  804. if (ftype != "PackedScene")
  805. return false;
  806. }
  807. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM); //so it works..
  808. return true;
  809. }
  810. if (String(d["type"]) == "script_list_element") {
  811. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  812. if (se) {
  813. String sp = se->get_edited_resource()->get_path();
  814. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  815. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  816. return true;
  817. }
  818. }
  819. }
  820. return String(d["type"]) == "nodes";
  821. }
  822. void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  823. if (!can_drop_data_fw(p_point, p_data, p_from))
  824. return;
  825. TreeItem *item = tree->get_item_at_position(p_point);
  826. if (!item)
  827. return;
  828. int section = tree->get_drop_section_at_position(p_point);
  829. if (section < -1)
  830. return;
  831. NodePath np = item->get_metadata(0);
  832. Node *n = get_node(np);
  833. if (!n)
  834. return;
  835. Dictionary d = p_data;
  836. if (String(d["type"]) == "nodes") {
  837. Array nodes = d["nodes"];
  838. emit_signal("nodes_rearranged", nodes, np, section);
  839. }
  840. if (String(d["type"]) == "files") {
  841. Vector<String> files = d["files"];
  842. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  843. if (_is_script_type(ftype)) {
  844. emit_signal("script_dropped", files[0], np);
  845. } else {
  846. emit_signal("files_dropped", files, np, section);
  847. }
  848. }
  849. if (String(d["type"]) == "script_list_element") {
  850. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
  851. if (se) {
  852. String sp = se->get_edited_resource()->get_path();
  853. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
  854. emit_signal("script_dropped", sp, np);
  855. }
  856. }
  857. }
  858. }
  859. void SceneTreeEditor::_rmb_select(const Vector2 &p_pos) {
  860. emit_signal("rmb_pressed", tree->get_global_transform().xform(p_pos));
  861. }
  862. void SceneTreeEditor::_warning_changed(Node *p_for_node) {
  863. //should use a timer
  864. update_timer->start();
  865. }
  866. void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) {
  867. connect_to_script_mode = p_enable;
  868. update_tree();
  869. }
  870. void SceneTreeEditor::set_connecting_signal(bool p_enable) {
  871. connecting_signal = p_enable;
  872. update_tree();
  873. }
  874. void SceneTreeEditor::_bind_methods() {
  875. ClassDB::bind_method("_tree_changed", &SceneTreeEditor::_tree_changed);
  876. ClassDB::bind_method(D_METHOD("_update_tree", "scroll_to_selected"), &SceneTreeEditor::_update_tree, DEFVAL(false));
  877. ClassDB::bind_method("_node_removed", &SceneTreeEditor::_node_removed);
  878. ClassDB::bind_method("_node_renamed", &SceneTreeEditor::_node_renamed);
  879. ClassDB::bind_method("_selected_changed", &SceneTreeEditor::_selected_changed);
  880. ClassDB::bind_method("_deselect_items", &SceneTreeEditor::_deselect_items);
  881. ClassDB::bind_method("_renamed", &SceneTreeEditor::_renamed);
  882. ClassDB::bind_method("_rename_node", &SceneTreeEditor::_rename_node);
  883. ClassDB::bind_method("_test_update_tree", &SceneTreeEditor::_test_update_tree);
  884. ClassDB::bind_method("_cell_multi_selected", &SceneTreeEditor::_cell_multi_selected);
  885. ClassDB::bind_method("_selection_changed", &SceneTreeEditor::_selection_changed);
  886. ClassDB::bind_method("_cell_button_pressed", &SceneTreeEditor::_cell_button_pressed);
  887. ClassDB::bind_method("_cell_collapsed", &SceneTreeEditor::_cell_collapsed);
  888. ClassDB::bind_method("_rmb_select", &SceneTreeEditor::_rmb_select);
  889. ClassDB::bind_method("_warning_changed", &SceneTreeEditor::_warning_changed);
  890. ClassDB::bind_method("_node_script_changed", &SceneTreeEditor::_node_script_changed);
  891. ClassDB::bind_method("_node_visibility_changed", &SceneTreeEditor::_node_visibility_changed);
  892. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SceneTreeEditor::get_drag_data_fw);
  893. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SceneTreeEditor::can_drop_data_fw);
  894. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SceneTreeEditor::drop_data_fw);
  895. ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
  896. ADD_SIGNAL(MethodInfo("node_selected"));
  897. ADD_SIGNAL(MethodInfo("node_renamed"));
  898. ADD_SIGNAL(MethodInfo("node_prerename"));
  899. ADD_SIGNAL(MethodInfo("node_changed"));
  900. ADD_SIGNAL(MethodInfo("nodes_dragged"));
  901. ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  902. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  903. ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
  904. ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
  905. ADD_SIGNAL(MethodInfo("open"));
  906. ADD_SIGNAL(MethodInfo("open_script"));
  907. }
  908. SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) {
  909. connect_to_script_mode = false;
  910. connecting_signal = false;
  911. undo_redo = NULL;
  912. tree_dirty = true;
  913. selected = NULL;
  914. marked_selectable = false;
  915. marked_children_selectable = false;
  916. can_rename = p_can_rename;
  917. can_open_instance = p_can_open_instance;
  918. display_foreign = false;
  919. editor_selection = NULL;
  920. if (p_label) {
  921. Label *label = memnew(Label);
  922. label->set_position(Point2(10, 0));
  923. label->set_text(TTR("Scene Tree (Nodes):"));
  924. add_child(label);
  925. }
  926. tree = memnew(Tree);
  927. tree->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  928. tree->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  929. tree->set_begin(Point2(0, p_label ? 18 : 0));
  930. tree->set_end(Point2(0, 0));
  931. tree->add_constant_override("button_margin", 0);
  932. tree->set_allow_reselect(true);
  933. add_child(tree);
  934. tree->set_drag_forwarding(this);
  935. if (p_can_rename) {
  936. tree->set_allow_rmb_select(true);
  937. tree->connect("item_rmb_selected", this, "_rmb_select");
  938. tree->connect("empty_tree_rmb_selected", this, "_rmb_select");
  939. }
  940. tree->connect("cell_selected", this, "_selected_changed");
  941. tree->connect("item_edited", this, "_renamed", varray(), CONNECT_DEFERRED);
  942. tree->connect("multi_selected", this, "_cell_multi_selected");
  943. tree->connect("button_pressed", this, "_cell_button_pressed");
  944. tree->connect("nothing_selected", this, "_deselect_items");
  945. //tree->connect("item_edited", this,"_renamed",Vector<Variant>(),true);
  946. error = memnew(AcceptDialog);
  947. add_child(error);
  948. warning = memnew(AcceptDialog);
  949. add_child(warning);
  950. warning->set_title(TTR("Node Configuration Warning!"));
  951. show_enabled_subscene = false;
  952. last_hash = 0;
  953. pending_test_update = false;
  954. updating_tree = false;
  955. blocked = 0;
  956. update_timer = memnew(Timer);
  957. update_timer->connect("timeout", this, "_update_tree", varray(false));
  958. update_timer->set_one_shot(true);
  959. update_timer->set_wait_time(0.5);
  960. add_child(update_timer);
  961. script_types = memnew(List<StringName>);
  962. ClassDB::get_inheriters_from_class("Script", script_types);
  963. }
  964. SceneTreeEditor::~SceneTreeEditor() {
  965. memdelete(script_types);
  966. }
  967. /******** DIALOG *********/
  968. void SceneTreeDialog::_notification(int p_what) {
  969. switch (p_what) {
  970. case NOTIFICATION_ENTER_TREE: {
  971. connect("confirmed", this, "_select");
  972. filter->set_right_icon(get_icon("Search", "EditorIcons"));
  973. filter->set_clear_button_enabled(true);
  974. } break;
  975. case NOTIFICATION_EXIT_TREE: {
  976. disconnect("confirmed", this, "_select");
  977. } break;
  978. case NOTIFICATION_VISIBILITY_CHANGED: {
  979. if (is_visible_in_tree())
  980. tree->update_tree();
  981. } break;
  982. }
  983. }
  984. void SceneTreeDialog::_cancel() {
  985. hide();
  986. }
  987. void SceneTreeDialog::_select() {
  988. if (tree->get_selected()) {
  989. emit_signal("selected", tree->get_selected()->get_path());
  990. hide();
  991. }
  992. }
  993. void SceneTreeDialog::_filter_changed(const String &p_filter) {
  994. tree->set_filter(p_filter);
  995. }
  996. void SceneTreeDialog::_bind_methods() {
  997. ClassDB::bind_method("_select", &SceneTreeDialog::_select);
  998. ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
  999. ClassDB::bind_method(D_METHOD("_filter_changed"), &SceneTreeDialog::_filter_changed);
  1000. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
  1001. }
  1002. SceneTreeDialog::SceneTreeDialog() {
  1003. set_title(TTR("Select a Node"));
  1004. VBoxContainer *vbc = memnew(VBoxContainer);
  1005. add_child(vbc);
  1006. filter = memnew(LineEdit);
  1007. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1008. filter->set_placeholder(TTR("Filter nodes"));
  1009. filter->add_constant_override("minimum_spaces", 0);
  1010. filter->connect("text_changed", this, "_filter_changed");
  1011. vbc->add_child(filter);
  1012. tree = memnew(SceneTreeEditor(false, false, true));
  1013. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1014. tree->get_scene_tree()->connect("item_activated", this, "_select");
  1015. vbc->add_child(tree);
  1016. }
  1017. SceneTreeDialog::~SceneTreeDialog() {
  1018. }