groups_editor.cpp 23 KB

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