2
0

scene_tree_editor.cpp 34 KB

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