scene_tree_editor.cpp 32 KB

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