2
0

groups_editor.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*************************************************************************/
  2. /* groups_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "groups_editor.h"
  31. #include "editor/scene_tree_editor.h"
  32. #include "editor_node.h"
  33. #include "editor_scale.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/resources/packed_scene.h"
  37. void GroupDialog::_group_selected() {
  38. nodes_to_add->clear();
  39. add_node_root = nodes_to_add->create_item();
  40. nodes_to_remove->clear();
  41. remove_node_root = nodes_to_remove->create_item();
  42. if (!groups->is_anything_selected()) {
  43. group_empty->hide();
  44. return;
  45. }
  46. selected_group = groups->get_selected()->get_text(0);
  47. _load_nodes(scene_tree->get_edited_scene_root());
  48. group_empty->set_visible(!remove_node_root->get_children());
  49. }
  50. void GroupDialog::_load_nodes(Node *p_current) {
  51. String item_name = p_current->get_name();
  52. if (p_current != scene_tree->get_edited_scene_root()) {
  53. item_name = String(p_current->get_parent()->get_name()) + "/" + item_name;
  54. }
  55. bool keep = true;
  56. Node *root = scene_tree->get_edited_scene_root();
  57. Node *owner = p_current->get_owner();
  58. if (owner != root && p_current != root && !owner && !root->is_editable_instance(owner)) {
  59. keep = false;
  60. }
  61. TreeItem *node = NULL;
  62. NodePath path = scene_tree->get_edited_scene_root()->get_path_to(p_current);
  63. if (keep && p_current->is_in_group(selected_group)) {
  64. if (remove_filter->get_text().is_subsequence_ofi(String(p_current->get_name()))) {
  65. node = nodes_to_remove->create_item(remove_node_root);
  66. keep = true;
  67. } else {
  68. keep = false;
  69. }
  70. } else if (keep && add_filter->get_text().is_subsequence_ofi(String(p_current->get_name()))) {
  71. node = nodes_to_add->create_item(add_node_root);
  72. keep = true;
  73. } else {
  74. keep = false;
  75. }
  76. if (keep) {
  77. node->set_text(0, item_name);
  78. node->set_metadata(0, path);
  79. node->set_tooltip(0, path);
  80. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(p_current, "Node");
  81. node->set_icon(0, icon);
  82. if (!_can_edit(p_current, selected_group)) {
  83. node->set_selectable(0, false);
  84. node->set_custom_color(0, get_color("disabled_font_color", "Editor"));
  85. }
  86. }
  87. for (int i = 0; i < p_current->get_child_count(); i++) {
  88. _load_nodes(p_current->get_child(i));
  89. }
  90. }
  91. bool GroupDialog::_can_edit(Node *p_node, String p_group) {
  92. Node *n = p_node;
  93. bool can_edit = true;
  94. while (n) {
  95. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  96. if (ss.is_valid()) {
  97. int path = ss->find_node_by_path(n->get_path_to(p_node));
  98. if (path != -1) {
  99. if (ss->is_node_in_group(path, p_group)) {
  100. can_edit = false;
  101. }
  102. }
  103. }
  104. n = n->get_owner();
  105. }
  106. return can_edit;
  107. }
  108. void GroupDialog::_add_pressed() {
  109. TreeItem *selected = nodes_to_add->get_next_selected(NULL);
  110. if (!selected) {
  111. return;
  112. }
  113. undo_redo->create_action(TTR("Add to Group"));
  114. while (selected) {
  115. Node *node = scene_tree->get_edited_scene_root()->get_node(selected->get_metadata(0));
  116. undo_redo->add_do_method(node, "add_to_group", selected_group, true);
  117. undo_redo->add_undo_method(node, "remove_from_group", selected_group);
  118. selected = nodes_to_add->get_next_selected(selected);
  119. }
  120. undo_redo->add_do_method(this, "_group_selected");
  121. undo_redo->add_undo_method(this, "_group_selected");
  122. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  123. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  124. // To force redraw of scene tree.
  125. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  126. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  127. undo_redo->commit_action();
  128. }
  129. void GroupDialog::_removed_pressed() {
  130. TreeItem *selected = nodes_to_remove->get_next_selected(NULL);
  131. if (!selected) {
  132. return;
  133. }
  134. undo_redo->create_action(TTR("Remove from Group"));
  135. while (selected) {
  136. Node *node = scene_tree->get_edited_scene_root()->get_node(selected->get_metadata(0));
  137. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  138. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  139. selected = nodes_to_add->get_next_selected(selected);
  140. }
  141. undo_redo->add_do_method(this, "_group_selected");
  142. undo_redo->add_undo_method(this, "_group_selected");
  143. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  144. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  145. // To force redraw of scene tree.
  146. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  147. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  148. undo_redo->commit_action();
  149. }
  150. void GroupDialog::_remove_filter_changed(const String &p_filter) {
  151. _group_selected();
  152. }
  153. void GroupDialog::_add_filter_changed(const String &p_filter) {
  154. _group_selected();
  155. }
  156. void GroupDialog::_add_group_pressed(const String &p_name) {
  157. _add_group(add_group_text->get_text());
  158. add_group_text->clear();
  159. }
  160. void GroupDialog::_add_group(String p_name) {
  161. if (!is_visible()) {
  162. return; // No need to edit the dialog if it's not being used.
  163. }
  164. String name = p_name.strip_edges();
  165. if (name.empty() || groups->get_item_with_text(name)) {
  166. return;
  167. }
  168. TreeItem *new_group = groups->create_item(groups_root);
  169. new_group->set_text(0, name);
  170. new_group->add_button(0, get_icon("Remove", "EditorIcons"), 0);
  171. new_group->set_editable(0, true);
  172. new_group->select(0);
  173. groups->ensure_cursor_is_visible();
  174. }
  175. void GroupDialog::_group_renamed() {
  176. TreeItem *renamed_group = groups->get_edited();
  177. if (!renamed_group) {
  178. return;
  179. }
  180. const String name = renamed_group->get_text(0).strip_edges();
  181. for (TreeItem *E = groups_root->get_children(); E; E = E->get_next()) {
  182. if (E != renamed_group && E->get_text(0) == name) {
  183. renamed_group->set_text(0, selected_group);
  184. error->set_text(TTR("Group name already exists."));
  185. error->popup_centered();
  186. return;
  187. }
  188. }
  189. if (name == "") {
  190. renamed_group->set_text(0, selected_group);
  191. error->set_text(TTR("Invalid group name."));
  192. error->popup_centered();
  193. return;
  194. }
  195. renamed_group->set_text(0, name); // Spaces trimmed.
  196. undo_redo->create_action(TTR("Rename Group"));
  197. List<Node *> nodes;
  198. scene_tree->get_nodes_in_group(selected_group, &nodes);
  199. bool removed_all = true;
  200. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  201. Node *node = E->get();
  202. if (_can_edit(node, selected_group)) {
  203. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  204. undo_redo->add_undo_method(node, "remove_from_group", name);
  205. undo_redo->add_do_method(node, "add_to_group", name, true);
  206. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  207. } else {
  208. removed_all = false;
  209. }
  210. }
  211. if (!removed_all) {
  212. undo_redo->add_do_method(this, "_add_group", selected_group);
  213. undo_redo->add_undo_method(this, "_delete_group_item", selected_group);
  214. }
  215. undo_redo->add_do_method(this, "_rename_group_item", selected_group, name);
  216. undo_redo->add_undo_method(this, "_rename_group_item", name, selected_group);
  217. undo_redo->add_do_method(this, "_group_selected");
  218. undo_redo->add_undo_method(this, "_group_selected");
  219. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  220. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  221. undo_redo->commit_action();
  222. }
  223. void GroupDialog::_rename_group_item(const String &p_old_name, const String &p_new_name) {
  224. if (!is_visible()) {
  225. return; // No need to edit the dialog if it's not being used.
  226. }
  227. selected_group = p_new_name;
  228. for (TreeItem *E = groups_root->get_children(); E; E = E->get_next()) {
  229. if (E->get_text(0) == p_old_name) {
  230. E->set_text(0, p_new_name);
  231. return;
  232. }
  233. }
  234. }
  235. void GroupDialog::_load_groups(Node *p_current) {
  236. List<Node::GroupInfo> gi;
  237. p_current->get_groups(&gi);
  238. for (List<Node::GroupInfo>::Element *E = gi.front(); E; E = E->next()) {
  239. if (!E->get().persistent) {
  240. continue;
  241. }
  242. _add_group(E->get().name);
  243. }
  244. for (int i = 0; i < p_current->get_child_count(); i++) {
  245. _load_groups(p_current->get_child(i));
  246. }
  247. }
  248. void GroupDialog::_delete_group_pressed(Object *p_item, int p_column, int p_id) {
  249. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  250. if (!ti)
  251. return;
  252. String name = ti->get_text(0);
  253. undo_redo->create_action(TTR("Delete Group"));
  254. List<Node *> nodes;
  255. scene_tree->get_nodes_in_group(name, &nodes);
  256. bool removed_all = true;
  257. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  258. if (_can_edit(E->get(), name)) {
  259. undo_redo->add_do_method(E->get(), "remove_from_group", name);
  260. undo_redo->add_undo_method(E->get(), "add_to_group", name, true);
  261. } else {
  262. removed_all = false;
  263. }
  264. }
  265. if (removed_all) {
  266. undo_redo->add_do_method(this, "_delete_group_item", name);
  267. undo_redo->add_undo_method(this, "_add_group", name);
  268. }
  269. undo_redo->add_do_method(this, "_group_selected");
  270. undo_redo->add_undo_method(this, "_group_selected");
  271. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  272. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  273. // To force redraw of scene tree.
  274. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  275. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  276. undo_redo->commit_action();
  277. }
  278. void GroupDialog::_delete_group_item(const String &p_name) {
  279. if (!is_visible()) {
  280. return; // No need to edit the dialog if it's not being used.
  281. }
  282. if (selected_group == p_name) {
  283. add_filter->clear();
  284. remove_filter->clear();
  285. nodes_to_remove->clear();
  286. nodes_to_add->clear();
  287. groups->deselect_all();
  288. selected_group = "";
  289. }
  290. for (TreeItem *E = groups_root->get_children(); E; E = E->get_next()) {
  291. if (E->get_text(0) == p_name) {
  292. groups_root->remove_child(E);
  293. return;
  294. }
  295. }
  296. }
  297. void GroupDialog::_notification(int p_what) {
  298. switch (p_what) {
  299. case NOTIFICATION_ENTER_TREE: {
  300. add_button->set_icon(get_icon("Forward", "EditorIcons"));
  301. remove_button->set_icon(get_icon("Back", "EditorIcons"));
  302. add_filter->set_right_icon(get_icon("Search", "EditorIcons"));
  303. add_filter->set_clear_button_enabled(true);
  304. remove_filter->set_right_icon(get_icon("Search", "EditorIcons"));
  305. remove_filter->set_clear_button_enabled(true);
  306. } break;
  307. }
  308. }
  309. void GroupDialog::edit() {
  310. popup_centered();
  311. groups->clear();
  312. groups_root = groups->create_item();
  313. nodes_to_add->clear();
  314. nodes_to_remove->clear();
  315. add_group_text->clear();
  316. add_filter->clear();
  317. remove_filter->clear();
  318. _load_groups(scene_tree->get_edited_scene_root());
  319. }
  320. void GroupDialog::_bind_methods() {
  321. ClassDB::bind_method("_add_pressed", &GroupDialog::_add_pressed);
  322. ClassDB::bind_method("_removed_pressed", &GroupDialog::_removed_pressed);
  323. ClassDB::bind_method("_delete_group_pressed", &GroupDialog::_delete_group_pressed);
  324. ClassDB::bind_method("_delete_group_item", &GroupDialog::_delete_group_item);
  325. ClassDB::bind_method("_group_selected", &GroupDialog::_group_selected);
  326. ClassDB::bind_method("_add_group_pressed", &GroupDialog::_add_group_pressed);
  327. ClassDB::bind_method("_add_group", &GroupDialog::_add_group);
  328. ClassDB::bind_method("_add_filter_changed", &GroupDialog::_add_filter_changed);
  329. ClassDB::bind_method("_remove_filter_changed", &GroupDialog::_remove_filter_changed);
  330. ClassDB::bind_method("_group_renamed", &GroupDialog::_group_renamed);
  331. ClassDB::bind_method("_rename_group_item", &GroupDialog::_rename_group_item);
  332. ADD_SIGNAL(MethodInfo("group_edited"));
  333. }
  334. GroupDialog::GroupDialog() {
  335. set_custom_minimum_size(Size2(600, 400) * EDSCALE);
  336. scene_tree = SceneTree::get_singleton();
  337. VBoxContainer *vbc = memnew(VBoxContainer);
  338. add_child(vbc);
  339. vbc->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  340. HBoxContainer *hbc = memnew(HBoxContainer);
  341. vbc->add_child(hbc);
  342. hbc->set_v_size_flags(SIZE_EXPAND_FILL);
  343. VBoxContainer *vbc_left = memnew(VBoxContainer);
  344. hbc->add_child(vbc_left);
  345. vbc_left->set_h_size_flags(SIZE_EXPAND_FILL);
  346. Label *group_title = memnew(Label);
  347. group_title->set_text(TTR("Groups"));
  348. vbc_left->add_child(group_title);
  349. groups = memnew(Tree);
  350. vbc_left->add_child(groups);
  351. groups->set_hide_root(true);
  352. groups->set_select_mode(Tree::SELECT_SINGLE);
  353. groups->set_allow_reselect(true);
  354. groups->set_allow_rmb_select(true);
  355. groups->set_v_size_flags(SIZE_EXPAND_FILL);
  356. groups->add_constant_override("draw_guides", 1);
  357. groups->connect("item_selected", this, "_group_selected");
  358. groups->connect("button_pressed", this, "_delete_group_pressed");
  359. groups->connect("item_edited", this, "_group_renamed");
  360. HBoxContainer *chbc = memnew(HBoxContainer);
  361. vbc_left->add_child(chbc);
  362. chbc->set_h_size_flags(SIZE_EXPAND_FILL);
  363. add_group_text = memnew(LineEdit);
  364. chbc->add_child(add_group_text);
  365. add_group_text->set_h_size_flags(SIZE_EXPAND_FILL);
  366. add_group_text->connect("text_entered", this, "_add_group_pressed");
  367. Button *add_group_button = memnew(Button);
  368. add_group_button->set_text(TTR("Add"));
  369. chbc->add_child(add_group_button);
  370. add_group_button->connect("pressed", this, "_add_group_pressed", varray(String()));
  371. VBoxContainer *vbc_add = memnew(VBoxContainer);
  372. hbc->add_child(vbc_add);
  373. vbc_add->set_h_size_flags(SIZE_EXPAND_FILL);
  374. Label *out_of_group_title = memnew(Label);
  375. out_of_group_title->set_text(TTR("Nodes Not in Group"));
  376. vbc_add->add_child(out_of_group_title);
  377. nodes_to_add = memnew(Tree);
  378. vbc_add->add_child(nodes_to_add);
  379. nodes_to_add->set_hide_root(true);
  380. nodes_to_add->set_hide_folding(true);
  381. nodes_to_add->set_select_mode(Tree::SELECT_MULTI);
  382. nodes_to_add->set_v_size_flags(SIZE_EXPAND_FILL);
  383. nodes_to_add->add_constant_override("draw_guides", 1);
  384. nodes_to_add->connect("item_selected", this, "_nodes_to_add_selected");
  385. HBoxContainer *add_filter_hbc = memnew(HBoxContainer);
  386. add_filter_hbc->add_constant_override("separate", 0);
  387. vbc_add->add_child(add_filter_hbc);
  388. add_filter = memnew(LineEdit);
  389. add_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  390. add_filter->set_placeholder(TTR("Filter nodes"));
  391. add_filter_hbc->add_child(add_filter);
  392. add_filter->connect("text_changed", this, "_add_filter_changed");
  393. VBoxContainer *vbc_buttons = memnew(VBoxContainer);
  394. hbc->add_child(vbc_buttons);
  395. vbc_buttons->set_h_size_flags(SIZE_SHRINK_CENTER);
  396. vbc_buttons->set_v_size_flags(SIZE_SHRINK_CENTER);
  397. add_button = memnew(ToolButton);
  398. add_button->set_text(TTR("Add"));
  399. add_button->connect("pressed", this, "_add_pressed");
  400. vbc_buttons->add_child(add_button);
  401. vbc_buttons->add_spacer();
  402. vbc_buttons->add_spacer();
  403. vbc_buttons->add_spacer();
  404. remove_button = memnew(ToolButton);
  405. remove_button->set_text(TTR("Remove"));
  406. remove_button->connect("pressed", this, "_removed_pressed");
  407. vbc_buttons->add_child(remove_button);
  408. VBoxContainer *vbc_remove = memnew(VBoxContainer);
  409. hbc->add_child(vbc_remove);
  410. vbc_remove->set_h_size_flags(SIZE_EXPAND_FILL);
  411. Label *in_group_title = memnew(Label);
  412. in_group_title->set_text(TTR("Nodes in Group"));
  413. vbc_remove->add_child(in_group_title);
  414. nodes_to_remove = memnew(Tree);
  415. vbc_remove->add_child(nodes_to_remove);
  416. nodes_to_remove->set_v_size_flags(SIZE_EXPAND_FILL);
  417. nodes_to_remove->set_hide_root(true);
  418. nodes_to_remove->set_hide_folding(true);
  419. nodes_to_remove->set_select_mode(Tree::SELECT_MULTI);
  420. nodes_to_remove->add_constant_override("draw_guides", 1);
  421. nodes_to_remove->connect("item_selected", this, "_node_to_remove_selected");
  422. HBoxContainer *remove_filter_hbc = memnew(HBoxContainer);
  423. remove_filter_hbc->add_constant_override("separate", 0);
  424. vbc_remove->add_child(remove_filter_hbc);
  425. remove_filter = memnew(LineEdit);
  426. remove_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  427. remove_filter->set_placeholder(TTR("Filter nodes"));
  428. remove_filter_hbc->add_child(remove_filter);
  429. remove_filter->connect("text_changed", this, "_remove_filter_changed");
  430. group_empty = memnew(Label());
  431. group_empty->set_text(TTR("Empty groups will be automatically removed."));
  432. group_empty->set_valign(Label::VALIGN_CENTER);
  433. group_empty->set_align(Label::ALIGN_CENTER);
  434. group_empty->set_autowrap(true);
  435. group_empty->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  436. nodes_to_remove->add_child(group_empty);
  437. group_empty->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  438. set_title(TTR("Group Editor"));
  439. set_as_toplevel(true);
  440. set_resizable(true);
  441. error = memnew(ConfirmationDialog);
  442. add_child(error);
  443. error->get_ok()->set_text(TTR("Close"));
  444. }
  445. ////////////////////////////////////////////////////////////////////////////////
  446. void GroupsEditor::_add_group(const String &p_group) {
  447. if (!node)
  448. return;
  449. const String name = group_name->get_text().strip_edges();
  450. if (name.empty())
  451. return;
  452. if (node->is_in_group(name))
  453. return;
  454. undo_redo->create_action(TTR("Add to Group"));
  455. undo_redo->add_do_method(node, "add_to_group", name, true);
  456. undo_redo->add_undo_method(node, "remove_from_group", name);
  457. undo_redo->add_do_method(this, "update_tree");
  458. undo_redo->add_undo_method(this, "update_tree");
  459. // To force redraw of scene tree.
  460. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  461. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  462. undo_redo->commit_action();
  463. group_name->clear();
  464. }
  465. void GroupsEditor::_remove_group(Object *p_item, int p_column, int p_id) {
  466. if (!node)
  467. return;
  468. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  469. if (!ti)
  470. return;
  471. String name = ti->get_text(0);
  472. undo_redo->create_action(TTR("Remove from Group"));
  473. undo_redo->add_do_method(node, "remove_from_group", name);
  474. undo_redo->add_undo_method(node, "add_to_group", name, true);
  475. undo_redo->add_do_method(this, "update_tree");
  476. undo_redo->add_undo_method(this, "update_tree");
  477. // To force redraw of scene tree.
  478. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  479. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  480. undo_redo->commit_action();
  481. }
  482. struct _GroupInfoComparator {
  483. bool operator()(const Node::GroupInfo &p_a, const Node::GroupInfo &p_b) const {
  484. return p_a.name.operator String() < p_b.name.operator String();
  485. }
  486. };
  487. void GroupsEditor::update_tree() {
  488. tree->clear();
  489. if (!node)
  490. return;
  491. List<Node::GroupInfo> groups;
  492. node->get_groups(&groups);
  493. groups.sort_custom<_GroupInfoComparator>();
  494. TreeItem *root = tree->create_item();
  495. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  496. Node::GroupInfo gi = E->get();
  497. if (!gi.persistent)
  498. continue;
  499. Node *n = node;
  500. bool can_be_deleted = true;
  501. while (n) {
  502. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  503. if (ss.is_valid()) {
  504. int path = ss->find_node_by_path(n->get_path_to(node));
  505. if (path != -1) {
  506. if (ss->is_node_in_group(path, gi.name)) {
  507. can_be_deleted = false;
  508. }
  509. }
  510. }
  511. n = n->get_owner();
  512. }
  513. TreeItem *item = tree->create_item(root);
  514. item->set_text(0, gi.name);
  515. if (can_be_deleted) {
  516. item->add_button(0, get_icon("Remove", "EditorIcons"), 0);
  517. } else {
  518. item->set_selectable(0, false);
  519. }
  520. }
  521. }
  522. void GroupsEditor::set_current(Node *p_node) {
  523. node = p_node;
  524. update_tree();
  525. }
  526. void GroupsEditor::_show_group_dialog() {
  527. group_dialog->edit();
  528. group_dialog->set_undo_redo(undo_redo);
  529. }
  530. void GroupsEditor::_bind_methods() {
  531. ClassDB::bind_method("_add_group", &GroupsEditor::_add_group);
  532. ClassDB::bind_method("_remove_group", &GroupsEditor::_remove_group);
  533. ClassDB::bind_method("update_tree", &GroupsEditor::update_tree);
  534. ClassDB::bind_method("_show_group_dialog", &GroupsEditor::_show_group_dialog);
  535. }
  536. GroupsEditor::GroupsEditor() {
  537. node = NULL;
  538. VBoxContainer *vbc = this;
  539. group_dialog = memnew(GroupDialog);
  540. group_dialog->set_as_toplevel(true);
  541. add_child(group_dialog);
  542. group_dialog->connect("group_edited", this, "update_tree");
  543. Button *group_dialog_button = memnew(Button);
  544. group_dialog_button->set_text(TTR("Manage Groups"));
  545. vbc->add_child(group_dialog_button);
  546. group_dialog_button->connect("pressed", this, "_show_group_dialog");
  547. HBoxContainer *hbc = memnew(HBoxContainer);
  548. vbc->add_child(hbc);
  549. group_name = memnew(LineEdit);
  550. group_name->set_h_size_flags(SIZE_EXPAND_FILL);
  551. hbc->add_child(group_name);
  552. group_name->connect("text_entered", this, "_add_group");
  553. add = memnew(Button);
  554. add->set_text(TTR("Add"));
  555. hbc->add_child(add);
  556. add->connect("pressed", this, "_add_group", varray(String()));
  557. tree = memnew(Tree);
  558. tree->set_hide_root(true);
  559. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  560. vbc->add_child(tree);
  561. tree->connect("button_pressed", this, "_remove_group");
  562. tree->add_constant_override("draw_guides", 1);
  563. add_constant_override("separation", 3 * EDSCALE);
  564. }
  565. GroupsEditor::~GroupsEditor() {
  566. }