groups_editor.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /**************************************************************************/
  2. /* groups_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = nullptr;
  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(nullptr);
  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(nullptr);
  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_text_changed(const String &p_new_text) {
  161. add_group_button->set_disabled(p_new_text.strip_edges().empty());
  162. }
  163. void GroupDialog::_add_group(String p_name) {
  164. if (!is_visible()) {
  165. return; // No need to edit the dialog if it's not being used.
  166. }
  167. String name = p_name.strip_edges();
  168. if (name.empty() || groups->get_item_with_text(name)) {
  169. return;
  170. }
  171. TreeItem *new_group = groups->create_item(groups_root);
  172. new_group->set_text(0, name);
  173. new_group->add_button(0, get_icon("Remove", "EditorIcons"), DELETE_GROUP);
  174. new_group->add_button(0, get_icon("ActionCopy", "EditorIcons"), COPY_GROUP);
  175. new_group->set_editable(0, true);
  176. new_group->select(0);
  177. groups->ensure_cursor_is_visible();
  178. }
  179. void GroupDialog::_group_renamed() {
  180. TreeItem *renamed_group = groups->get_edited();
  181. if (!renamed_group) {
  182. return;
  183. }
  184. const String name = renamed_group->get_text(0).strip_edges();
  185. for (TreeItem *E = groups_root->get_children(); E; E = E->get_next()) {
  186. if (E != renamed_group && E->get_text(0) == name) {
  187. renamed_group->set_text(0, selected_group);
  188. error->set_text(TTR("Group name already exists."));
  189. error->popup_centered();
  190. return;
  191. }
  192. }
  193. if (name == "") {
  194. renamed_group->set_text(0, selected_group);
  195. error->set_text(TTR("Invalid group name."));
  196. error->popup_centered();
  197. return;
  198. }
  199. renamed_group->set_text(0, name); // Spaces trimmed.
  200. undo_redo->create_action(TTR("Rename Group"));
  201. List<Node *> nodes;
  202. scene_tree->get_nodes_in_group(selected_group, &nodes);
  203. bool removed_all = true;
  204. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  205. Node *node = E->get();
  206. if (_can_edit(node, selected_group)) {
  207. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  208. undo_redo->add_undo_method(node, "remove_from_group", name);
  209. undo_redo->add_do_method(node, "add_to_group", name, true);
  210. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  211. } else {
  212. removed_all = false;
  213. }
  214. }
  215. if (!removed_all) {
  216. undo_redo->add_do_method(this, "_add_group", selected_group);
  217. undo_redo->add_undo_method(this, "_delete_group_item", selected_group);
  218. }
  219. undo_redo->add_do_method(this, "_rename_group_item", selected_group, name);
  220. undo_redo->add_undo_method(this, "_rename_group_item", name, selected_group);
  221. undo_redo->add_do_method(this, "_group_selected");
  222. undo_redo->add_undo_method(this, "_group_selected");
  223. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  224. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  225. undo_redo->commit_action();
  226. }
  227. void GroupDialog::_rename_group_item(const String &p_old_name, const String &p_new_name) {
  228. if (!is_visible()) {
  229. return; // No need to edit the dialog if it's not being used.
  230. }
  231. selected_group = p_new_name;
  232. for (TreeItem *E = groups_root->get_children(); E; E = E->get_next()) {
  233. if (E->get_text(0) == p_old_name) {
  234. E->set_text(0, p_new_name);
  235. return;
  236. }
  237. }
  238. }
  239. void GroupDialog::_load_groups(Node *p_current) {
  240. List<Node::GroupInfo> gi;
  241. p_current->get_groups(&gi);
  242. for (List<Node::GroupInfo>::Element *E = gi.front(); E; E = E->next()) {
  243. if (!E->get().persistent) {
  244. continue;
  245. }
  246. _add_group(E->get().name);
  247. }
  248. for (int i = 0; i < p_current->get_child_count(); i++) {
  249. _load_groups(p_current->get_child(i));
  250. }
  251. }
  252. void GroupDialog::_modify_group_pressed(Object *p_item, int p_column, int p_id) {
  253. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  254. if (!ti) {
  255. return;
  256. }
  257. switch (p_id) {
  258. case DELETE_GROUP: {
  259. String name = ti->get_text(0);
  260. undo_redo->create_action(TTR("Delete Group"));
  261. List<Node *> nodes;
  262. scene_tree->get_nodes_in_group(name, &nodes);
  263. bool removed_all = true;
  264. for (List<Node *>::Element *E = nodes.front(); E; E = E->next()) {
  265. if (_can_edit(E->get(), name)) {
  266. undo_redo->add_do_method(E->get(), "remove_from_group", name);
  267. undo_redo->add_undo_method(E->get(), "add_to_group", name, true);
  268. } else {
  269. removed_all = false;
  270. }
  271. }
  272. if (removed_all) {
  273. undo_redo->add_do_method(this, "_delete_group_item", name);
  274. undo_redo->add_undo_method(this, "_add_group", name);
  275. }
  276. undo_redo->add_do_method(this, "_group_selected");
  277. undo_redo->add_undo_method(this, "_group_selected");
  278. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  279. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  280. // To force redraw of scene tree.
  281. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  282. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  283. undo_redo->commit_action();
  284. } break;
  285. case COPY_GROUP: {
  286. OS::get_singleton()->set_clipboard(ti->get_text(p_column));
  287. } break;
  288. }
  289. }
  290. void GroupDialog::_delete_group_item(const String &p_name) {
  291. if (!is_visible()) {
  292. return; // No need to edit the dialog if it's not being used.
  293. }
  294. if (selected_group == p_name) {
  295. add_filter->clear();
  296. remove_filter->clear();
  297. nodes_to_remove->clear();
  298. nodes_to_add->clear();
  299. groups->deselect_all();
  300. selected_group = "";
  301. }
  302. for (TreeItem *E = groups_root->get_children(); E; E = E->get_next()) {
  303. if (E->get_text(0) == p_name) {
  304. groups_root->remove_child(E);
  305. return;
  306. }
  307. }
  308. }
  309. void GroupDialog::_notification(int p_what) {
  310. switch (p_what) {
  311. case NOTIFICATION_THEME_CHANGED:
  312. case NOTIFICATION_ENTER_TREE: {
  313. add_button->set_icon(get_icon("Forward", "EditorIcons"));
  314. remove_button->set_icon(get_icon("Back", "EditorIcons"));
  315. add_filter->set_right_icon(get_icon("Search", "EditorIcons"));
  316. add_filter->set_clear_button_enabled(true);
  317. remove_filter->set_right_icon(get_icon("Search", "EditorIcons"));
  318. remove_filter->set_clear_button_enabled(true);
  319. } break;
  320. }
  321. }
  322. void GroupDialog::edit() {
  323. popup_centered();
  324. groups->clear();
  325. groups_root = groups->create_item();
  326. nodes_to_add->clear();
  327. nodes_to_remove->clear();
  328. add_group_text->clear();
  329. add_filter->clear();
  330. remove_filter->clear();
  331. _load_groups(scene_tree->get_edited_scene_root());
  332. }
  333. void GroupDialog::_bind_methods() {
  334. ClassDB::bind_method("_add_pressed", &GroupDialog::_add_pressed);
  335. ClassDB::bind_method("_removed_pressed", &GroupDialog::_removed_pressed);
  336. ClassDB::bind_method("_modify_group_pressed", &GroupDialog::_modify_group_pressed);
  337. ClassDB::bind_method("_delete_group_item", &GroupDialog::_delete_group_item);
  338. ClassDB::bind_method("_group_selected", &GroupDialog::_group_selected);
  339. ClassDB::bind_method("_add_group_pressed", &GroupDialog::_add_group_pressed);
  340. ClassDB::bind_method("_add_group", &GroupDialog::_add_group);
  341. ClassDB::bind_method("_add_group_text_changed", &GroupDialog::_add_group_text_changed);
  342. ClassDB::bind_method("_add_filter_changed", &GroupDialog::_add_filter_changed);
  343. ClassDB::bind_method("_remove_filter_changed", &GroupDialog::_remove_filter_changed);
  344. ClassDB::bind_method("_group_renamed", &GroupDialog::_group_renamed);
  345. ClassDB::bind_method("_rename_group_item", &GroupDialog::_rename_group_item);
  346. ADD_SIGNAL(MethodInfo("group_edited"));
  347. }
  348. GroupDialog::GroupDialog() {
  349. set_custom_minimum_size(Size2(600, 400) * EDSCALE);
  350. scene_tree = SceneTree::get_singleton();
  351. VBoxContainer *vbc = memnew(VBoxContainer);
  352. add_child(vbc);
  353. vbc->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  354. HBoxContainer *hbc = memnew(HBoxContainer);
  355. vbc->add_child(hbc);
  356. hbc->set_v_size_flags(SIZE_EXPAND_FILL);
  357. VBoxContainer *vbc_left = memnew(VBoxContainer);
  358. hbc->add_child(vbc_left);
  359. vbc_left->set_h_size_flags(SIZE_EXPAND_FILL);
  360. Label *group_title = memnew(Label);
  361. group_title->set_text(TTR("Groups"));
  362. vbc_left->add_child(group_title);
  363. groups = memnew(Tree);
  364. vbc_left->add_child(groups);
  365. groups->set_hide_root(true);
  366. groups->set_select_mode(Tree::SELECT_SINGLE);
  367. groups->set_allow_reselect(true);
  368. groups->set_allow_rmb_select(true);
  369. groups->set_v_size_flags(SIZE_EXPAND_FILL);
  370. groups->add_constant_override("draw_guides", 1);
  371. groups->connect("item_selected", this, "_group_selected");
  372. groups->connect("button_pressed", this, "_modify_group_pressed");
  373. groups->connect("item_edited", this, "_group_renamed");
  374. HBoxContainer *chbc = memnew(HBoxContainer);
  375. vbc_left->add_child(chbc);
  376. chbc->set_h_size_flags(SIZE_EXPAND_FILL);
  377. add_group_text = memnew(LineEdit);
  378. chbc->add_child(add_group_text);
  379. add_group_text->set_h_size_flags(SIZE_EXPAND_FILL);
  380. add_group_text->connect("text_entered", this, "_add_group_pressed");
  381. add_group_text->connect("text_changed", this, "_add_group_text_changed");
  382. add_group_button = memnew(Button);
  383. add_group_button->set_text(TTR("Add"));
  384. chbc->add_child(add_group_button);
  385. add_group_button->connect("pressed", this, "_add_group_pressed", varray(String()));
  386. VBoxContainer *vbc_add = memnew(VBoxContainer);
  387. hbc->add_child(vbc_add);
  388. vbc_add->set_h_size_flags(SIZE_EXPAND_FILL);
  389. Label *out_of_group_title = memnew(Label);
  390. out_of_group_title->set_text(TTR("Nodes Not in Group"));
  391. vbc_add->add_child(out_of_group_title);
  392. nodes_to_add = memnew(Tree);
  393. vbc_add->add_child(nodes_to_add);
  394. nodes_to_add->set_hide_root(true);
  395. nodes_to_add->set_hide_folding(true);
  396. nodes_to_add->set_select_mode(Tree::SELECT_MULTI);
  397. nodes_to_add->set_v_size_flags(SIZE_EXPAND_FILL);
  398. nodes_to_add->add_constant_override("draw_guides", 1);
  399. nodes_to_add->connect("item_selected", this, "_nodes_to_add_selected");
  400. HBoxContainer *add_filter_hbc = memnew(HBoxContainer);
  401. add_filter_hbc->add_constant_override("separate", 0);
  402. vbc_add->add_child(add_filter_hbc);
  403. add_filter = memnew(LineEdit);
  404. add_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  405. add_filter->set_placeholder(TTR("Filter nodes"));
  406. add_filter_hbc->add_child(add_filter);
  407. add_filter->connect("text_changed", this, "_add_filter_changed");
  408. VBoxContainer *vbc_buttons = memnew(VBoxContainer);
  409. hbc->add_child(vbc_buttons);
  410. vbc_buttons->set_h_size_flags(SIZE_SHRINK_CENTER);
  411. vbc_buttons->set_v_size_flags(SIZE_SHRINK_CENTER);
  412. add_button = memnew(ToolButton);
  413. add_button->set_text(TTR("Add"));
  414. add_button->connect("pressed", this, "_add_pressed");
  415. vbc_buttons->add_child(add_button);
  416. vbc_buttons->add_spacer();
  417. vbc_buttons->add_spacer();
  418. vbc_buttons->add_spacer();
  419. remove_button = memnew(ToolButton);
  420. remove_button->set_text(TTR("Remove"));
  421. remove_button->connect("pressed", this, "_removed_pressed");
  422. vbc_buttons->add_child(remove_button);
  423. VBoxContainer *vbc_remove = memnew(VBoxContainer);
  424. hbc->add_child(vbc_remove);
  425. vbc_remove->set_h_size_flags(SIZE_EXPAND_FILL);
  426. Label *in_group_title = memnew(Label);
  427. in_group_title->set_text(TTR("Nodes in Group"));
  428. vbc_remove->add_child(in_group_title);
  429. nodes_to_remove = memnew(Tree);
  430. vbc_remove->add_child(nodes_to_remove);
  431. nodes_to_remove->set_v_size_flags(SIZE_EXPAND_FILL);
  432. nodes_to_remove->set_hide_root(true);
  433. nodes_to_remove->set_hide_folding(true);
  434. nodes_to_remove->set_select_mode(Tree::SELECT_MULTI);
  435. nodes_to_remove->add_constant_override("draw_guides", 1);
  436. nodes_to_remove->connect("item_selected", this, "_node_to_remove_selected");
  437. HBoxContainer *remove_filter_hbc = memnew(HBoxContainer);
  438. remove_filter_hbc->add_constant_override("separate", 0);
  439. vbc_remove->add_child(remove_filter_hbc);
  440. remove_filter = memnew(LineEdit);
  441. remove_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  442. remove_filter->set_placeholder(TTR("Filter nodes"));
  443. remove_filter_hbc->add_child(remove_filter);
  444. remove_filter->connect("text_changed", this, "_remove_filter_changed");
  445. group_empty = memnew(Label());
  446. group_empty->set_text(TTR("Empty groups will be automatically removed."));
  447. group_empty->set_valign(Label::VALIGN_CENTER);
  448. group_empty->set_align(Label::ALIGN_CENTER);
  449. group_empty->set_autowrap(true);
  450. group_empty->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  451. nodes_to_remove->add_child(group_empty);
  452. group_empty->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  453. set_title(TTR("Group Editor"));
  454. set_as_toplevel(true);
  455. set_resizable(true);
  456. error = memnew(ConfirmationDialog);
  457. add_child(error);
  458. error->get_ok()->set_text(TTR("Close"));
  459. _add_group_text_changed("");
  460. }
  461. ////////////////////////////////////////////////////////////////////////////////
  462. void GroupsEditor::_add_group(const String &p_group) {
  463. if (!node) {
  464. return;
  465. }
  466. const String name = group_name->get_text().strip_edges();
  467. if (name.empty()) {
  468. return;
  469. }
  470. group_name->clear();
  471. if (node->is_in_group(name)) {
  472. return;
  473. }
  474. undo_redo->create_action(TTR("Add to Group"));
  475. undo_redo->add_do_method(node, "add_to_group", name, true);
  476. undo_redo->add_undo_method(node, "remove_from_group", name);
  477. undo_redo->add_do_method(this, "update_tree");
  478. undo_redo->add_undo_method(this, "update_tree");
  479. // To force redraw of scene tree.
  480. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  481. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  482. undo_redo->commit_action();
  483. }
  484. void GroupsEditor::_modify_group(Object *p_item, int p_column, int p_id) {
  485. if (!node) {
  486. return;
  487. }
  488. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  489. if (!ti) {
  490. return;
  491. }
  492. switch (p_id) {
  493. case DELETE_GROUP: {
  494. String name = ti->get_text(0);
  495. undo_redo->create_action(TTR("Remove from Group"));
  496. undo_redo->add_do_method(node, "remove_from_group", name);
  497. undo_redo->add_undo_method(node, "add_to_group", name, true);
  498. undo_redo->add_do_method(this, "update_tree");
  499. undo_redo->add_undo_method(this, "update_tree");
  500. // To force redraw of scene tree.
  501. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  502. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  503. undo_redo->commit_action();
  504. } break;
  505. case COPY_GROUP: {
  506. OS::get_singleton()->set_clipboard(ti->get_text(p_column));
  507. } break;
  508. }
  509. }
  510. void GroupsEditor::_group_name_changed(const String &p_new_text) {
  511. add->set_disabled(p_new_text.strip_edges().empty());
  512. }
  513. struct _GroupInfoComparator {
  514. bool operator()(const Node::GroupInfo &p_a, const Node::GroupInfo &p_b) const {
  515. return p_a.name.operator String() < p_b.name.operator String();
  516. }
  517. };
  518. void GroupsEditor::update_tree() {
  519. tree->clear();
  520. if (!node) {
  521. return;
  522. }
  523. List<Node::GroupInfo> groups;
  524. node->get_groups(&groups);
  525. groups.sort_custom<_GroupInfoComparator>();
  526. TreeItem *root = tree->create_item();
  527. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  528. Node::GroupInfo gi = E->get();
  529. if (!gi.persistent) {
  530. continue;
  531. }
  532. Node *n = node;
  533. bool can_be_deleted = true;
  534. while (n) {
  535. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  536. if (ss.is_valid()) {
  537. int path = ss->find_node_by_path(n->get_path_to(node));
  538. if (path != -1) {
  539. if (ss->is_node_in_group(path, gi.name)) {
  540. can_be_deleted = false;
  541. }
  542. }
  543. }
  544. n = n->get_owner();
  545. }
  546. TreeItem *item = tree->create_item(root);
  547. item->set_text(0, gi.name);
  548. if (can_be_deleted) {
  549. item->add_button(0, get_icon("Remove", "EditorIcons"), DELETE_GROUP);
  550. item->add_button(0, get_icon("ActionCopy", "EditorIcons"), COPY_GROUP);
  551. } else {
  552. item->set_selectable(0, false);
  553. }
  554. }
  555. }
  556. void GroupsEditor::set_current(Node *p_node) {
  557. node = p_node;
  558. update_tree();
  559. }
  560. void GroupsEditor::_show_group_dialog() {
  561. group_dialog->edit();
  562. group_dialog->set_undo_redo(undo_redo);
  563. }
  564. void GroupsEditor::_bind_methods() {
  565. ClassDB::bind_method("_add_group", &GroupsEditor::_add_group);
  566. ClassDB::bind_method("_modify_group", &GroupsEditor::_modify_group);
  567. ClassDB::bind_method("_group_name_changed", &GroupsEditor::_group_name_changed);
  568. ClassDB::bind_method("update_tree", &GroupsEditor::update_tree);
  569. ClassDB::bind_method("_show_group_dialog", &GroupsEditor::_show_group_dialog);
  570. }
  571. GroupsEditor::GroupsEditor() {
  572. node = nullptr;
  573. VBoxContainer *vbc = this;
  574. group_dialog = memnew(GroupDialog);
  575. group_dialog->set_as_toplevel(true);
  576. add_child(group_dialog);
  577. group_dialog->connect("group_edited", this, "update_tree");
  578. Button *group_dialog_button = memnew(Button);
  579. group_dialog_button->set_text(TTR("Manage Groups"));
  580. vbc->add_child(group_dialog_button);
  581. group_dialog_button->connect("pressed", this, "_show_group_dialog");
  582. HBoxContainer *hbc = memnew(HBoxContainer);
  583. vbc->add_child(hbc);
  584. group_name = memnew(LineEdit);
  585. group_name->set_h_size_flags(SIZE_EXPAND_FILL);
  586. hbc->add_child(group_name);
  587. group_name->connect("text_entered", this, "_add_group");
  588. group_name->connect("text_changed", this, "_group_name_changed");
  589. add = memnew(Button);
  590. add->set_text(TTR("Add"));
  591. hbc->add_child(add);
  592. add->connect("pressed", this, "_add_group", varray(String()));
  593. tree = memnew(Tree);
  594. tree->set_hide_root(true);
  595. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  596. vbc->add_child(tree);
  597. tree->connect("button_pressed", this, "_modify_group");
  598. tree->add_constant_override("draw_guides", 1);
  599. add_constant_override("separation", 3 * EDSCALE);
  600. _group_name_changed("");
  601. }
  602. GroupsEditor::~GroupsEditor() {
  603. }