scene_tree_editor.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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) {
  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. if (p_what == NOTIFICATION_ENTER_TREE) {
  428. get_tree()->connect("tree_changed", this, "_tree_changed");
  429. get_tree()->connect("node_removed", this, "_node_removed");
  430. get_tree()->connect("node_configuration_warning_changed", this, "_warning_changed");
  431. tree->connect("item_collapsed", this, "_cell_collapsed");
  432. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  433. _editor_settings_changed();
  434. //get_scene()->connect("tree_changed",this,"_tree_changed",Vector<Variant>(),CONNECT_DEFERRED);
  435. //get_scene()->connect("node_removed",this,"_node_removed",Vector<Variant>(),CONNECT_DEFERRED);
  436. _update_tree();
  437. }
  438. if (p_what == NOTIFICATION_EXIT_TREE) {
  439. get_tree()->disconnect("tree_changed", this, "_tree_changed");
  440. get_tree()->disconnect("node_removed", this, "_node_removed");
  441. tree->disconnect("item_collapsed", this, "_cell_collapsed");
  442. get_tree()->disconnect("node_configuration_warning_changed", this, "_warning_changed");
  443. EditorSettings::get_singleton()->disconnect("settings_changed", this, "_editor_settings_changed");
  444. }
  445. if (p_what == NOTIFICATION_THEME_CHANGED) {
  446. _update_tree();
  447. }
  448. }
  449. TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
  450. if (!p_node)
  451. return NULL;
  452. NodePath np = p_node->get_metadata(0);
  453. if (np == p_path)
  454. return p_node;
  455. TreeItem *children = p_node->get_children();
  456. while (children) {
  457. TreeItem *n = _find(children, p_path);
  458. if (n)
  459. return n;
  460. children = children->get_next();
  461. }
  462. return NULL;
  463. }
  464. void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
  465. ERR_FAIL_COND(blocked > 0);
  466. if (pending_test_update)
  467. _test_update_tree();
  468. if (tree_dirty)
  469. _update_tree();
  470. if (selected == p_node)
  471. return;
  472. TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : NULL;
  473. if (item) {
  474. // make visible when it's collapsed
  475. TreeItem *node = item->get_parent();
  476. while (node && node != tree->get_root()) {
  477. node->set_collapsed(false);
  478. node = node->get_parent();
  479. }
  480. item->select(0);
  481. item->set_as_cursor(0);
  482. selected = p_node;
  483. tree->ensure_cursor_is_visible();
  484. } else {
  485. if (!p_node)
  486. selected = NULL;
  487. _update_tree();
  488. selected = p_node;
  489. }
  490. if (p_emit_selected) {
  491. emit_signal("node_selected");
  492. }
  493. }
  494. void SceneTreeEditor::_rename_node(ObjectID p_node, const String &p_name) {
  495. Object *o = ObjectDB::get_instance(p_node);
  496. ERR_FAIL_COND(!o);
  497. Node *n = Object::cast_to<Node>(o);
  498. ERR_FAIL_COND(!n);
  499. TreeItem *item = _find(tree->get_root(), n->get_path());
  500. ERR_FAIL_COND(!item);
  501. n->set_name(p_name);
  502. item->set_metadata(0, n->get_path());
  503. item->set_text(0, p_name);
  504. emit_signal("node_renamed");
  505. if (!tree_dirty) {
  506. MessageQueue::get_singleton()->push_call(this, "_update_tree");
  507. tree_dirty = true;
  508. }
  509. }
  510. void SceneTreeEditor::_renamed() {
  511. TreeItem *which = tree->get_edited();
  512. ERR_FAIL_COND(!which);
  513. NodePath np = which->get_metadata(0);
  514. Node *n = get_node(np);
  515. ERR_FAIL_COND(!n);
  516. // Empty node names are not allowed, so resets it to previous text and show warning
  517. if (which->get_text(0).strip_edges().empty()) {
  518. which->set_text(0, n->get_name());
  519. EditorNode::get_singleton()->show_warning(TTR("No name provided."));
  520. return;
  521. }
  522. String new_name = which->get_text(0);
  523. if (!Node::_validate_node_name(new_name)) {
  524. error->set_text(TTR("Invalid node name, the following characters are not allowed:") + "\n" + Node::invalid_character);
  525. error->popup_centered_minsize();
  526. which->set_text(0, new_name);
  527. }
  528. if (new_name == n->get_name())
  529. return;
  530. if (!undo_redo) {
  531. n->set_name(new_name);
  532. which->set_metadata(0, n->get_path());
  533. emit_signal("node_renamed");
  534. } else {
  535. undo_redo->create_action(TTR("Rename Node"));
  536. emit_signal("node_prerename", n, new_name);
  537. undo_redo->add_do_method(this, "_rename_node", n->get_instance_id(), new_name);
  538. undo_redo->add_undo_method(this, "_rename_node", n->get_instance_id(), n->get_name());
  539. undo_redo->commit_action();
  540. }
  541. }
  542. Node *SceneTreeEditor::get_selected() {
  543. return selected;
  544. }
  545. void SceneTreeEditor::set_marked(const Set<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
  546. if (tree_dirty)
  547. _update_tree();
  548. marked = p_marked;
  549. marked_selectable = p_selectable;
  550. marked_children_selectable = p_children_selectable;
  551. _update_tree();
  552. }
  553. void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
  554. Set<Node *> s;
  555. if (p_marked)
  556. s.insert(p_marked);
  557. set_marked(s, p_selectable, p_children_selectable);
  558. }
  559. void SceneTreeEditor::set_filter(const String &p_filter) {
  560. filter = p_filter;
  561. _update_tree();
  562. }
  563. String SceneTreeEditor::get_filter() const {
  564. return filter;
  565. }
  566. void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
  567. display_foreign = p_display;
  568. _update_tree();
  569. }
  570. bool SceneTreeEditor::get_display_foreign_nodes() const {
  571. return display_foreign;
  572. }
  573. void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
  574. valid_types = p_valid;
  575. }
  576. void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
  577. editor_selection = p_selection;
  578. tree->set_select_mode(Tree::SELECT_MULTI);
  579. tree->set_cursor_can_exit_tree(false);
  580. editor_selection->connect("selection_changed", this, "_selection_changed");
  581. }
  582. void SceneTreeEditor::_update_selection(TreeItem *item) {
  583. ERR_FAIL_COND(!item);
  584. NodePath np = item->get_metadata(0);
  585. if (!has_node(np))
  586. return;
  587. Node *n = get_node(np);
  588. if (!n)
  589. return;
  590. if (editor_selection->is_selected(n))
  591. item->select(0);
  592. else
  593. item->deselect(0);
  594. TreeItem *c = item->get_children();
  595. while (c) {
  596. _update_selection(c);
  597. c = c->get_next();
  598. }
  599. }
  600. void SceneTreeEditor::_selection_changed() {
  601. if (!editor_selection)
  602. return;
  603. TreeItem *root = tree->get_root();
  604. if (!root)
  605. return;
  606. _update_selection(root);
  607. }
  608. void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
  609. if (updating_tree)
  610. return;
  611. if (!can_rename)
  612. return;
  613. TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
  614. if (!ti)
  615. return;
  616. bool collapsed = ti->is_collapsed();
  617. NodePath np = ti->get_metadata(0);
  618. Node *n = get_node(np);
  619. ERR_FAIL_COND(!n);
  620. n->set_display_folded(collapsed);
  621. }
  622. Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  623. if (!can_rename)
  624. return Variant(); //not editable tree
  625. Vector<Node *> selected;
  626. Vector<Ref<Texture> > icons;
  627. TreeItem *next = tree->get_next_selected(NULL);
  628. while (next) {
  629. NodePath np = next->get_metadata(0);
  630. Node *n = get_node(np);
  631. if (n) {
  632. // Only allow selection if not part of an instanced scene.
  633. if (!n->get_owner() || n->get_owner() == get_scene_node() || n->get_owner()->get_filename() == String()) {
  634. selected.push_back(n);
  635. icons.push_back(next->get_icon(0));
  636. }
  637. }
  638. next = tree->get_next_selected(next);
  639. }
  640. if (selected.empty())
  641. return Variant();
  642. VBoxContainer *vb = memnew(VBoxContainer);
  643. Array objs;
  644. int list_max = 10;
  645. float opacity_step = 1.0f / list_max;
  646. float opacity_item = 1.0f;
  647. for (int i = 0; i < selected.size(); i++) {
  648. if (i < list_max) {
  649. HBoxContainer *hb = memnew(HBoxContainer);
  650. TextureRect *tf = memnew(TextureRect);
  651. tf->set_texture(icons[i]);
  652. hb->add_child(tf);
  653. Label *label = memnew(Label(selected[i]->get_name()));
  654. hb->add_child(label);
  655. vb->add_child(hb);
  656. hb->set_modulate(Color(1, 1, 1, opacity_item));
  657. opacity_item -= opacity_step;
  658. }
  659. NodePath p = selected[i]->get_path();
  660. objs.push_back(p);
  661. }
  662. set_drag_preview(vb);
  663. Dictionary drag_data;
  664. drag_data["type"] = "nodes";
  665. drag_data["nodes"] = objs;
  666. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
  667. emit_signal("nodes_dragged");
  668. return drag_data;
  669. }
  670. bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
  671. return (script_types->find(p_type));
  672. }
  673. bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  674. if (!can_rename)
  675. return false; //not editable tree
  676. if (filter != String())
  677. return false; //can't rearrange tree with filter turned on
  678. Dictionary d = p_data;
  679. if (!d.has("type"))
  680. return false;
  681. TreeItem *item = tree->get_item_at_position(p_point);
  682. if (!item)
  683. return false;
  684. int section = tree->get_drop_section_at_position(p_point);
  685. if (section < -1 || (section == -1 && !item->get_parent()))
  686. return false;
  687. if (String(d["type"]) == "files") {
  688. Vector<String> files = d["files"];
  689. if (files.size() == 0)
  690. return false; //weird
  691. if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
  692. tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  693. return true;
  694. }
  695. for (int i = 0; i < files.size(); i++) {
  696. String file = files[i];
  697. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  698. if (ftype != "PackedScene")
  699. return false;
  700. }
  701. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM); //so it works..
  702. return true;
  703. }
  704. if (String(d["type"]) == "nodes") {
  705. return true;
  706. }
  707. return false;
  708. }
  709. void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  710. if (!can_drop_data_fw(p_point, p_data, p_from))
  711. return;
  712. TreeItem *item = tree->get_item_at_position(p_point);
  713. if (!item)
  714. return;
  715. int section = tree->get_drop_section_at_position(p_point);
  716. if (section < -1)
  717. return;
  718. NodePath np = item->get_metadata(0);
  719. Node *n = get_node(np);
  720. if (!n)
  721. return;
  722. Dictionary d = p_data;
  723. if (String(d["type"]) == "nodes") {
  724. Array nodes = d["nodes"];
  725. emit_signal("nodes_rearranged", nodes, np, section);
  726. }
  727. if (String(d["type"]) == "files") {
  728. Vector<String> files = d["files"];
  729. String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
  730. if (_is_script_type(ftype)) {
  731. emit_signal("script_dropped", files[0], np);
  732. } else {
  733. emit_signal("files_dropped", files, np, section);
  734. }
  735. }
  736. }
  737. void SceneTreeEditor::_rmb_select(const Vector2 &p_pos) {
  738. emit_signal("rmb_pressed", tree->get_global_transform().xform(p_pos));
  739. }
  740. void SceneTreeEditor::_warning_changed(Node *p_for_node) {
  741. //should use a timer
  742. update_timer->start();
  743. }
  744. void SceneTreeEditor::_editor_settings_changed() {
  745. bool enable_rl = EditorSettings::get_singleton()->get("docks/scene_tree/draw_relationship_lines");
  746. Color rl_color = EditorSettings::get_singleton()->get("docks/scene_tree/relationship_line_color");
  747. if (enable_rl) {
  748. tree->add_constant_override("draw_relationship_lines", 1);
  749. tree->add_color_override("relationship_line_color", rl_color);
  750. tree->add_constant_override("draw_guides", 0);
  751. } else {
  752. tree->add_constant_override("draw_relationship_lines", 0);
  753. tree->add_constant_override("draw_guides", 1);
  754. }
  755. }
  756. void SceneTreeEditor::_bind_methods() {
  757. ClassDB::bind_method("_tree_changed", &SceneTreeEditor::_tree_changed);
  758. ClassDB::bind_method("_update_tree", &SceneTreeEditor::_update_tree);
  759. ClassDB::bind_method("_node_removed", &SceneTreeEditor::_node_removed);
  760. ClassDB::bind_method("_selected_changed", &SceneTreeEditor::_selected_changed);
  761. ClassDB::bind_method("_deselect_items", &SceneTreeEditor::_deselect_items);
  762. ClassDB::bind_method("_renamed", &SceneTreeEditor::_renamed);
  763. ClassDB::bind_method("_rename_node", &SceneTreeEditor::_rename_node);
  764. ClassDB::bind_method("_test_update_tree", &SceneTreeEditor::_test_update_tree);
  765. ClassDB::bind_method("_cell_multi_selected", &SceneTreeEditor::_cell_multi_selected);
  766. ClassDB::bind_method("_selection_changed", &SceneTreeEditor::_selection_changed);
  767. ClassDB::bind_method("_cell_button_pressed", &SceneTreeEditor::_cell_button_pressed);
  768. ClassDB::bind_method("_cell_collapsed", &SceneTreeEditor::_cell_collapsed);
  769. ClassDB::bind_method("_rmb_select", &SceneTreeEditor::_rmb_select);
  770. ClassDB::bind_method("_warning_changed", &SceneTreeEditor::_warning_changed);
  771. ClassDB::bind_method("_node_script_changed", &SceneTreeEditor::_node_script_changed);
  772. ClassDB::bind_method("_node_visibility_changed", &SceneTreeEditor::_node_visibility_changed);
  773. ClassDB::bind_method("_editor_settings_changed", &SceneTreeEditor::_editor_settings_changed);
  774. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &SceneTreeEditor::get_drag_data_fw);
  775. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SceneTreeEditor::can_drop_data_fw);
  776. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SceneTreeEditor::drop_data_fw);
  777. ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
  778. ADD_SIGNAL(MethodInfo("node_selected"));
  779. ADD_SIGNAL(MethodInfo("node_renamed"));
  780. ADD_SIGNAL(MethodInfo("node_prerename"));
  781. ADD_SIGNAL(MethodInfo("node_changed"));
  782. ADD_SIGNAL(MethodInfo("nodes_dragged"));
  783. ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  784. ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::POOL_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
  785. ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
  786. ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
  787. ADD_SIGNAL(MethodInfo("open"));
  788. ADD_SIGNAL(MethodInfo("open_script"));
  789. }
  790. SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) {
  791. undo_redo = NULL;
  792. tree_dirty = true;
  793. selected = NULL;
  794. marked_selectable = false;
  795. marked_children_selectable = false;
  796. can_rename = p_can_rename;
  797. can_open_instance = p_can_open_instance;
  798. display_foreign = false;
  799. editor_selection = NULL;
  800. if (p_label) {
  801. Label *label = memnew(Label);
  802. label->set_position(Point2(10, 0));
  803. label->set_text(TTR("Scene Tree (Nodes):"));
  804. add_child(label);
  805. }
  806. tree = memnew(Tree);
  807. tree->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  808. tree->set_anchor(MARGIN_BOTTOM, ANCHOR_END);
  809. tree->set_begin(Point2(0, p_label ? 18 : 0));
  810. tree->set_end(Point2(0, 0));
  811. tree->add_constant_override("button_margin", 0);
  812. add_child(tree);
  813. tree->set_drag_forwarding(this);
  814. if (p_can_rename) {
  815. tree->set_allow_rmb_select(true);
  816. tree->connect("item_rmb_selected", this, "_rmb_select");
  817. tree->connect("empty_tree_rmb_selected", this, "_rmb_select");
  818. }
  819. tree->connect("cell_selected", this, "_selected_changed");
  820. tree->connect("item_edited", this, "_renamed", varray(), CONNECT_DEFERRED);
  821. tree->connect("multi_selected", this, "_cell_multi_selected");
  822. tree->connect("button_pressed", this, "_cell_button_pressed");
  823. tree->connect("nothing_selected", this, "_deselect_items");
  824. //tree->connect("item_edited", this,"_renamed",Vector<Variant>(),true);
  825. error = memnew(AcceptDialog);
  826. add_child(error);
  827. warning = memnew(AcceptDialog);
  828. add_child(warning);
  829. warning->set_title(TTR("Node Configuration Warning!"));
  830. show_enabled_subscene = false;
  831. last_hash = 0;
  832. pending_test_update = false;
  833. updating_tree = false;
  834. blocked = 0;
  835. update_timer = memnew(Timer);
  836. update_timer->connect("timeout", this, "_update_tree");
  837. update_timer->set_one_shot(true);
  838. update_timer->set_wait_time(0.5);
  839. add_child(update_timer);
  840. script_types = memnew(List<StringName>);
  841. ClassDB::get_inheriters_from_class("Script", script_types);
  842. }
  843. SceneTreeEditor::~SceneTreeEditor() {
  844. memdelete(script_types);
  845. }
  846. /******** DIALOG *********/
  847. void SceneTreeDialog::_notification(int p_what) {
  848. if (p_what == NOTIFICATION_ENTER_TREE) {
  849. connect("confirmed", this, "_select");
  850. }
  851. if (p_what == NOTIFICATION_EXIT_TREE) {
  852. disconnect("confirmed", this, "_select");
  853. }
  854. if (p_what == NOTIFICATION_DRAW) {
  855. RID ci = get_canvas_item();
  856. get_stylebox("panel", "PopupMenu")->draw(ci, Rect2(Point2(), get_size()));
  857. }
  858. if (p_what == NOTIFICATION_VISIBILITY_CHANGED && is_visible_in_tree()) {
  859. tree->update_tree();
  860. }
  861. }
  862. void SceneTreeDialog::_cancel() {
  863. hide();
  864. }
  865. void SceneTreeDialog::_select() {
  866. if (tree->get_selected()) {
  867. emit_signal("selected", tree->get_selected()->get_path());
  868. hide();
  869. }
  870. }
  871. void SceneTreeDialog::_bind_methods() {
  872. ClassDB::bind_method("_select", &SceneTreeDialog::_select);
  873. ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
  874. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
  875. }
  876. SceneTreeDialog::SceneTreeDialog() {
  877. set_title(TTR("Select a Node"));
  878. tree = memnew(SceneTreeEditor(false, false, true));
  879. add_child(tree);
  880. //set_child_rect(tree);
  881. tree->get_scene_tree()->connect("item_activated", this, "_select");
  882. }
  883. SceneTreeDialog::~SceneTreeDialog() {
  884. }