groups_editor.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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/editor_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/gui/scene_tree_editor.h"
  35. #include "editor/scene_tree_dock.h"
  36. #include "scene/gui/button.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/line_edit.h"
  39. #include "scene/gui/tree.h"
  40. #include "scene/resources/packed_scene.h"
  41. static bool can_edit(Node *p_node, String p_group) {
  42. Node *n = p_node;
  43. bool can_edit = true;
  44. while (n) {
  45. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  46. if (ss.is_valid()) {
  47. int path = ss->find_node_by_path(n->get_path_to(p_node));
  48. if (path != -1) {
  49. if (ss->is_node_in_group(path, p_group)) {
  50. can_edit = false;
  51. }
  52. }
  53. }
  54. n = n->get_owner();
  55. }
  56. return can_edit;
  57. }
  58. void GroupDialog::_group_selected() {
  59. nodes_to_add->clear();
  60. add_node_root = nodes_to_add->create_item();
  61. nodes_to_remove->clear();
  62. remove_node_root = nodes_to_remove->create_item();
  63. if (!groups->is_anything_selected()) {
  64. group_empty->hide();
  65. return;
  66. }
  67. selected_group = groups->get_selected()->get_text(0);
  68. _load_nodes(scene_tree->get_edited_scene_root());
  69. group_empty->set_visible(!remove_node_root->get_first_child());
  70. }
  71. void GroupDialog::_load_nodes(Node *p_current) {
  72. String item_name = p_current->get_name();
  73. if (p_current != scene_tree->get_edited_scene_root()) {
  74. item_name = String(p_current->get_parent()->get_name()) + "/" + item_name;
  75. }
  76. bool keep = true;
  77. Node *root = scene_tree->get_edited_scene_root();
  78. Node *owner = p_current->get_owner();
  79. if (owner != root && p_current != root && !owner && !root->is_editable_instance(owner)) {
  80. keep = false;
  81. }
  82. TreeItem *node = nullptr;
  83. NodePath path = scene_tree->get_edited_scene_root()->get_path_to(p_current);
  84. if (keep && p_current->is_in_group(selected_group)) {
  85. if (remove_filter->get_text().is_subsequence_ofn(String(p_current->get_name()))) {
  86. node = nodes_to_remove->create_item(remove_node_root);
  87. keep = true;
  88. } else {
  89. keep = false;
  90. }
  91. } else if (keep && add_filter->get_text().is_subsequence_ofn(String(p_current->get_name()))) {
  92. node = nodes_to_add->create_item(add_node_root);
  93. keep = true;
  94. } else {
  95. keep = false;
  96. }
  97. if (keep) {
  98. node->set_text(0, item_name);
  99. node->set_metadata(0, path);
  100. node->set_tooltip_text(0, path);
  101. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(p_current, "Node");
  102. node->set_icon(0, icon);
  103. if (!can_edit(p_current, selected_group)) {
  104. node->set_selectable(0, false);
  105. node->set_custom_color(0, groups->get_theme_color(SNAME("disabled_font_color"), SNAME("Editor")));
  106. }
  107. }
  108. for (int i = 0; i < p_current->get_child_count(); i++) {
  109. _load_nodes(p_current->get_child(i));
  110. }
  111. }
  112. void GroupDialog::_add_pressed() {
  113. TreeItem *selected = nodes_to_add->get_next_selected(nullptr);
  114. if (!selected) {
  115. return;
  116. }
  117. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  118. undo_redo->create_action(TTR("Add to Group"));
  119. while (selected) {
  120. Node *node = scene_tree->get_edited_scene_root()->get_node(selected->get_metadata(0));
  121. undo_redo->add_do_method(node, "add_to_group", selected_group, true);
  122. undo_redo->add_undo_method(node, "remove_from_group", selected_group);
  123. selected = nodes_to_add->get_next_selected(selected);
  124. }
  125. undo_redo->add_do_method(this, "_group_selected");
  126. undo_redo->add_undo_method(this, "_group_selected");
  127. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  128. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  129. // To force redraw of scene tree.
  130. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  131. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  132. undo_redo->commit_action();
  133. }
  134. void GroupDialog::_removed_pressed() {
  135. TreeItem *selected = nodes_to_remove->get_next_selected(nullptr);
  136. if (!selected) {
  137. return;
  138. }
  139. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  140. undo_redo->create_action(TTR("Remove from Group"));
  141. while (selected) {
  142. Node *node = scene_tree->get_edited_scene_root()->get_node(selected->get_metadata(0));
  143. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  144. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  145. selected = nodes_to_add->get_next_selected(selected);
  146. }
  147. undo_redo->add_do_method(this, "_group_selected");
  148. undo_redo->add_undo_method(this, "_group_selected");
  149. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  150. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  151. // To force redraw of scene tree.
  152. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  153. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  154. undo_redo->commit_action();
  155. }
  156. void GroupDialog::_remove_filter_changed(const String &p_filter) {
  157. _group_selected();
  158. }
  159. void GroupDialog::_add_filter_changed(const String &p_filter) {
  160. _group_selected();
  161. }
  162. void GroupDialog::_add_group_pressed(const String &p_name) {
  163. _add_group(add_group_text->get_text());
  164. add_group_text->clear();
  165. }
  166. void GroupDialog::_add_group(String p_name) {
  167. if (!is_visible()) {
  168. return; // No need to edit the dialog if it's not being used.
  169. }
  170. String name = p_name.strip_edges();
  171. if (name.is_empty() || groups->get_item_with_text(name)) {
  172. return;
  173. }
  174. TreeItem *new_group = groups->create_item(groups_root);
  175. new_group->set_text(0, name);
  176. new_group->add_button(0, groups->get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), DELETE_GROUP);
  177. new_group->add_button(0, groups->get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")), COPY_GROUP);
  178. new_group->set_editable(0, true);
  179. new_group->select(0);
  180. groups->ensure_cursor_is_visible();
  181. }
  182. void GroupDialog::_add_group_text_changed(const String &p_new_text) {
  183. add_group_button->set_disabled(p_new_text.strip_edges().is_empty());
  184. }
  185. void GroupDialog::_group_renamed() {
  186. TreeItem *renamed_group = groups->get_selected();
  187. if (!renamed_group) {
  188. return;
  189. }
  190. const String name = renamed_group->get_text(0).strip_edges();
  191. if (name == selected_group) {
  192. return;
  193. }
  194. if (name.is_empty()) {
  195. renamed_group->set_text(0, selected_group);
  196. error->set_text(TTR("Invalid group name."));
  197. error->popup_centered();
  198. return;
  199. }
  200. for (TreeItem *E = groups_root->get_first_child(); E; E = E->get_next()) {
  201. if (E != renamed_group && E->get_text(0) == name) {
  202. renamed_group->set_text(0, selected_group);
  203. error->set_text(TTR("Group name already exists."));
  204. error->popup_centered();
  205. return;
  206. }
  207. }
  208. renamed_group->set_text(0, name); // Spaces trimmed.
  209. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  210. undo_redo->create_action(TTR("Rename Group"));
  211. List<Node *> nodes;
  212. scene_tree->get_nodes_in_group(selected_group, &nodes);
  213. bool removed_all = true;
  214. for (Node *node : nodes) {
  215. if (can_edit(node, selected_group)) {
  216. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  217. undo_redo->add_undo_method(node, "remove_from_group", name);
  218. undo_redo->add_do_method(node, "add_to_group", name, true);
  219. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  220. } else {
  221. removed_all = false;
  222. }
  223. }
  224. if (!removed_all) {
  225. undo_redo->add_do_method(this, "_add_group", selected_group);
  226. undo_redo->add_undo_method(this, "_delete_group_item", selected_group);
  227. }
  228. undo_redo->add_do_method(this, "_rename_group_item", selected_group, name);
  229. undo_redo->add_undo_method(this, "_rename_group_item", name, selected_group);
  230. undo_redo->add_do_method(this, "_group_selected");
  231. undo_redo->add_undo_method(this, "_group_selected");
  232. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  233. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  234. undo_redo->commit_action();
  235. }
  236. void GroupDialog::_rename_group_item(const String &p_old_name, const String &p_new_name) {
  237. if (!is_visible()) {
  238. return; // No need to edit the dialog if it's not being used.
  239. }
  240. selected_group = p_new_name;
  241. for (TreeItem *E = groups_root->get_first_child(); E; E = E->get_next()) {
  242. if (E->get_text(0) == p_old_name) {
  243. E->set_text(0, p_new_name);
  244. return;
  245. }
  246. }
  247. }
  248. void GroupDialog::_load_groups(Node *p_current) {
  249. List<Node::GroupInfo> gi;
  250. p_current->get_groups(&gi);
  251. for (const Node::GroupInfo &E : gi) {
  252. if (!E.persistent) {
  253. continue;
  254. }
  255. _add_group(E.name);
  256. }
  257. for (int i = 0; i < p_current->get_child_count(); i++) {
  258. _load_groups(p_current->get_child(i));
  259. }
  260. }
  261. void GroupDialog::_modify_group_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  262. if (p_button != MouseButton::LEFT) {
  263. return;
  264. }
  265. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  266. if (!ti) {
  267. return;
  268. }
  269. switch (p_id) {
  270. case DELETE_GROUP: {
  271. String name = ti->get_text(0);
  272. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  273. undo_redo->create_action(TTR("Delete Group"));
  274. List<Node *> nodes;
  275. scene_tree->get_nodes_in_group(name, &nodes);
  276. bool removed_all = true;
  277. for (Node *E : nodes) {
  278. if (can_edit(E, name)) {
  279. undo_redo->add_do_method(E, "remove_from_group", name);
  280. undo_redo->add_undo_method(E, "add_to_group", name, true);
  281. } else {
  282. removed_all = false;
  283. }
  284. }
  285. if (removed_all) {
  286. undo_redo->add_do_method(this, "_delete_group_item", name);
  287. undo_redo->add_undo_method(this, "_add_group", name);
  288. }
  289. undo_redo->add_do_method(this, "_group_selected");
  290. undo_redo->add_undo_method(this, "_group_selected");
  291. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  292. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  293. // To force redraw of scene tree.
  294. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  295. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  296. undo_redo->commit_action();
  297. } break;
  298. case COPY_GROUP: {
  299. DisplayServer::get_singleton()->clipboard_set(ti->get_text(p_column));
  300. } break;
  301. }
  302. }
  303. void GroupDialog::_delete_group_item(const String &p_name) {
  304. if (!is_visible()) {
  305. return; // No need to edit the dialog if it's not being used.
  306. }
  307. if (selected_group == p_name) {
  308. add_filter->clear();
  309. remove_filter->clear();
  310. nodes_to_remove->clear();
  311. nodes_to_add->clear();
  312. groups->deselect_all();
  313. selected_group = "";
  314. }
  315. for (TreeItem *E = groups_root->get_first_child(); E; E = E->get_next()) {
  316. if (E->get_text(0) == p_name) {
  317. groups_root->remove_child(E);
  318. return;
  319. }
  320. }
  321. }
  322. void GroupDialog::_notification(int p_what) {
  323. switch (p_what) {
  324. case NOTIFICATION_TRANSLATION_CHANGED:
  325. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  326. case NOTIFICATION_ENTER_TREE:
  327. case NOTIFICATION_THEME_CHANGED: {
  328. if (is_layout_rtl()) {
  329. add_button->set_icon(groups->get_theme_icon(SNAME("Back"), SNAME("EditorIcons")));
  330. remove_button->set_icon(groups->get_theme_icon(SNAME("Forward"), SNAME("EditorIcons")));
  331. } else {
  332. add_button->set_icon(groups->get_theme_icon(SNAME("Forward"), SNAME("EditorIcons")));
  333. remove_button->set_icon(groups->get_theme_icon(SNAME("Back"), SNAME("EditorIcons")));
  334. }
  335. add_filter->set_right_icon(groups->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  336. add_filter->set_clear_button_enabled(true);
  337. remove_filter->set_right_icon(groups->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  338. remove_filter->set_clear_button_enabled(true);
  339. } break;
  340. }
  341. }
  342. void GroupDialog::edit() {
  343. popup_centered();
  344. groups->clear();
  345. groups_root = groups->create_item();
  346. nodes_to_add->clear();
  347. nodes_to_remove->clear();
  348. add_group_text->clear();
  349. add_filter->clear();
  350. remove_filter->clear();
  351. _load_groups(scene_tree->get_edited_scene_root());
  352. }
  353. void GroupDialog::_bind_methods() {
  354. ClassDB::bind_method("_delete_group_item", &GroupDialog::_delete_group_item);
  355. ClassDB::bind_method("_add_group", &GroupDialog::_add_group);
  356. ClassDB::bind_method("_rename_group_item", &GroupDialog::_rename_group_item);
  357. ClassDB::bind_method("_group_selected", &GroupDialog::_group_selected);
  358. ADD_SIGNAL(MethodInfo("group_edited"));
  359. }
  360. GroupDialog::GroupDialog() {
  361. set_min_size(Size2(600, 400) * EDSCALE);
  362. scene_tree = SceneTree::get_singleton();
  363. VBoxContainer *vbc = memnew(VBoxContainer);
  364. add_child(vbc);
  365. vbc->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  366. HBoxContainer *hbc = memnew(HBoxContainer);
  367. vbc->add_child(hbc);
  368. hbc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  369. VBoxContainer *vbc_left = memnew(VBoxContainer);
  370. hbc->add_child(vbc_left);
  371. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  372. Label *group_title = memnew(Label);
  373. group_title->set_theme_type_variation("HeaderSmall");
  374. group_title->set_text(TTR("Groups"));
  375. vbc_left->add_child(group_title);
  376. groups = memnew(Tree);
  377. vbc_left->add_child(groups);
  378. groups->set_hide_root(true);
  379. groups->set_select_mode(Tree::SELECT_SINGLE);
  380. groups->set_allow_reselect(true);
  381. groups->set_allow_rmb_select(true);
  382. groups->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  383. groups->add_theme_constant_override("draw_guides", 1);
  384. groups->connect("item_selected", callable_mp(this, &GroupDialog::_group_selected));
  385. groups->connect("button_clicked", callable_mp(this, &GroupDialog::_modify_group_pressed));
  386. groups->connect("item_edited", callable_mp(this, &GroupDialog::_group_renamed));
  387. HBoxContainer *chbc = memnew(HBoxContainer);
  388. vbc_left->add_child(chbc);
  389. chbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  390. add_group_text = memnew(LineEdit);
  391. chbc->add_child(add_group_text);
  392. add_group_text->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  393. add_group_text->connect("text_submitted", callable_mp(this, &GroupDialog::_add_group_pressed));
  394. add_group_text->connect("text_changed", callable_mp(this, &GroupDialog::_add_group_text_changed));
  395. add_group_button = memnew(Button);
  396. add_group_button->set_text(TTR("Add"));
  397. chbc->add_child(add_group_button);
  398. add_group_button->connect("pressed", callable_mp(this, &GroupDialog::_add_group_pressed).bind(String()));
  399. VBoxContainer *vbc_add = memnew(VBoxContainer);
  400. hbc->add_child(vbc_add);
  401. vbc_add->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  402. Label *out_of_group_title = memnew(Label);
  403. out_of_group_title->set_theme_type_variation("HeaderSmall");
  404. out_of_group_title->set_text(TTR("Nodes Not in Group"));
  405. vbc_add->add_child(out_of_group_title);
  406. nodes_to_add = memnew(Tree);
  407. vbc_add->add_child(nodes_to_add);
  408. nodes_to_add->set_hide_root(true);
  409. nodes_to_add->set_hide_folding(true);
  410. nodes_to_add->set_select_mode(Tree::SELECT_MULTI);
  411. nodes_to_add->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  412. nodes_to_add->add_theme_constant_override("draw_guides", 1);
  413. HBoxContainer *add_filter_hbc = memnew(HBoxContainer);
  414. add_filter_hbc->add_theme_constant_override("separate", 0);
  415. vbc_add->add_child(add_filter_hbc);
  416. add_filter = memnew(LineEdit);
  417. add_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  418. add_filter->set_placeholder(TTR("Filter Nodes"));
  419. add_filter_hbc->add_child(add_filter);
  420. add_filter->connect("text_changed", callable_mp(this, &GroupDialog::_add_filter_changed));
  421. VBoxContainer *vbc_buttons = memnew(VBoxContainer);
  422. hbc->add_child(vbc_buttons);
  423. vbc_buttons->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  424. vbc_buttons->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  425. add_button = memnew(Button);
  426. add_button->set_flat(true);
  427. add_button->set_text(TTR("Add"));
  428. add_button->connect("pressed", callable_mp(this, &GroupDialog::_add_pressed));
  429. vbc_buttons->add_child(add_button);
  430. vbc_buttons->add_spacer();
  431. vbc_buttons->add_spacer();
  432. vbc_buttons->add_spacer();
  433. remove_button = memnew(Button);
  434. remove_button->set_flat(true);
  435. remove_button->set_text(TTR("Remove"));
  436. remove_button->connect("pressed", callable_mp(this, &GroupDialog::_removed_pressed));
  437. vbc_buttons->add_child(remove_button);
  438. VBoxContainer *vbc_remove = memnew(VBoxContainer);
  439. hbc->add_child(vbc_remove);
  440. vbc_remove->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  441. Label *in_group_title = memnew(Label);
  442. in_group_title->set_theme_type_variation("HeaderSmall");
  443. in_group_title->set_text(TTR("Nodes in Group"));
  444. vbc_remove->add_child(in_group_title);
  445. nodes_to_remove = memnew(Tree);
  446. vbc_remove->add_child(nodes_to_remove);
  447. nodes_to_remove->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  448. nodes_to_remove->set_hide_root(true);
  449. nodes_to_remove->set_hide_folding(true);
  450. nodes_to_remove->set_select_mode(Tree::SELECT_MULTI);
  451. nodes_to_remove->add_theme_constant_override("draw_guides", 1);
  452. HBoxContainer *remove_filter_hbc = memnew(HBoxContainer);
  453. remove_filter_hbc->add_theme_constant_override("separate", 0);
  454. vbc_remove->add_child(remove_filter_hbc);
  455. remove_filter = memnew(LineEdit);
  456. remove_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  457. remove_filter->set_placeholder(TTR("Filter Nodes"));
  458. remove_filter_hbc->add_child(remove_filter);
  459. remove_filter->connect("text_changed", callable_mp(this, &GroupDialog::_remove_filter_changed));
  460. group_empty = memnew(Label());
  461. group_empty->set_theme_type_variation("HeaderSmall");
  462. group_empty->set_text(TTR("Empty groups will be automatically removed."));
  463. group_empty->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  464. group_empty->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  465. group_empty->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  466. group_empty->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  467. nodes_to_remove->add_child(group_empty);
  468. group_empty->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  469. set_title(TTR("Group Editor"));
  470. error = memnew(AcceptDialog);
  471. add_child(error);
  472. error->set_ok_button_text(TTR("Close"));
  473. _add_group_text_changed("");
  474. }
  475. ////////////////////////////////////////////////////////////////////////////////
  476. void GroupsEditor::_add_group(const String &p_group) {
  477. if (!node) {
  478. return;
  479. }
  480. const String name = group_name->get_text().strip_edges();
  481. group_name->clear();
  482. if (node->is_in_group(name)) {
  483. error->set_text(TTR("Group name already exists."));
  484. error->popup_centered();
  485. return;
  486. }
  487. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  488. undo_redo->create_action(TTR("Add to Group"));
  489. undo_redo->add_do_method(node, "add_to_group", name, true);
  490. undo_redo->add_undo_method(node, "remove_from_group", name);
  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(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  495. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  496. undo_redo->commit_action();
  497. }
  498. void GroupsEditor::_group_selected() {
  499. if (!tree->is_anything_selected()) {
  500. return;
  501. }
  502. selected_group = tree->get_selected()->get_text(0);
  503. }
  504. void GroupsEditor::_group_renamed() {
  505. if (!node || !can_edit(node, selected_group)) {
  506. return;
  507. }
  508. TreeItem *ti = tree->get_selected();
  509. if (!ti) {
  510. return;
  511. }
  512. const String name = ti->get_text(0).strip_edges();
  513. if (name == selected_group) {
  514. return;
  515. }
  516. if (name.is_empty()) {
  517. ti->set_text(0, selected_group);
  518. error->set_text(TTR("Invalid group name."));
  519. error->popup_centered();
  520. return;
  521. }
  522. for (TreeItem *E = groups_root->get_first_child(); E; E = E->get_next()) {
  523. if (E != ti && E->get_text(0) == name) {
  524. ti->set_text(0, selected_group);
  525. error->set_text(TTR("Group name already exists."));
  526. error->popup_centered();
  527. return;
  528. }
  529. }
  530. ti->set_text(0, name); // Spaces trimmed.
  531. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  532. undo_redo->create_action(TTR("Rename Group"));
  533. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  534. undo_redo->add_undo_method(node, "remove_from_group", name);
  535. undo_redo->add_do_method(node, "add_to_group", name, true);
  536. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  537. undo_redo->add_do_method(this, "_group_selected");
  538. undo_redo->add_undo_method(this, "_group_selected");
  539. undo_redo->add_do_method(this, "update_tree");
  540. undo_redo->add_undo_method(this, "update_tree");
  541. // To force redraw of scene tree.
  542. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  543. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  544. undo_redo->commit_action();
  545. }
  546. void GroupsEditor::_modify_group(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  547. if (p_button != MouseButton::LEFT) {
  548. return;
  549. }
  550. if (!node) {
  551. return;
  552. }
  553. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  554. if (!ti) {
  555. return;
  556. }
  557. switch (p_id) {
  558. case DELETE_GROUP: {
  559. const String name = ti->get_text(0);
  560. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  561. undo_redo->create_action(TTR("Remove from Group"));
  562. undo_redo->add_do_method(node, "remove_from_group", name);
  563. undo_redo->add_undo_method(node, "add_to_group", name, true);
  564. undo_redo->add_do_method(this, "update_tree");
  565. undo_redo->add_undo_method(this, "update_tree");
  566. // To force redraw of scene tree.
  567. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  568. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  569. undo_redo->commit_action();
  570. } break;
  571. case COPY_GROUP: {
  572. DisplayServer::get_singleton()->clipboard_set(ti->get_text(p_column));
  573. } break;
  574. }
  575. }
  576. void GroupsEditor::_group_name_changed(const String &p_new_text) {
  577. add->set_disabled(p_new_text.strip_edges().is_empty());
  578. }
  579. struct _GroupInfoComparator {
  580. bool operator()(const Node::GroupInfo &p_a, const Node::GroupInfo &p_b) const {
  581. return p_a.name.operator String() < p_b.name.operator String();
  582. }
  583. };
  584. void GroupsEditor::update_tree() {
  585. tree->clear();
  586. if (!node) {
  587. return;
  588. }
  589. List<Node::GroupInfo> groups;
  590. node->get_groups(&groups);
  591. groups.sort_custom<_GroupInfoComparator>();
  592. TreeItem *root = tree->create_item();
  593. groups_root = root;
  594. for (const GroupInfo &gi : groups) {
  595. if (!gi.persistent) {
  596. continue;
  597. }
  598. Node *n = node;
  599. bool can_be_deleted = true;
  600. while (n) {
  601. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  602. if (ss.is_valid()) {
  603. int path = ss->find_node_by_path(n->get_path_to(node));
  604. if (path != -1) {
  605. if (ss->is_node_in_group(path, gi.name)) {
  606. can_be_deleted = false;
  607. }
  608. }
  609. }
  610. n = n->get_owner();
  611. }
  612. TreeItem *item = tree->create_item(root);
  613. item->set_text(0, gi.name);
  614. item->set_editable(0, true);
  615. if (can_be_deleted) {
  616. item->add_button(0, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), DELETE_GROUP);
  617. item->add_button(0, get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")), COPY_GROUP);
  618. } else {
  619. item->set_selectable(0, false);
  620. }
  621. }
  622. }
  623. void GroupsEditor::set_current(Node *p_node) {
  624. node = p_node;
  625. update_tree();
  626. }
  627. void GroupsEditor::_show_group_dialog() {
  628. group_dialog->edit();
  629. }
  630. void GroupsEditor::_bind_methods() {
  631. ClassDB::bind_method("update_tree", &GroupsEditor::update_tree);
  632. ClassDB::bind_method("_group_selected", &GroupsEditor::_group_selected);
  633. }
  634. GroupsEditor::GroupsEditor() {
  635. node = nullptr;
  636. VBoxContainer *vbc = this;
  637. group_dialog = memnew(GroupDialog);
  638. add_child(group_dialog);
  639. group_dialog->connect("group_edited", callable_mp(this, &GroupsEditor::update_tree));
  640. Button *group_dialog_button = memnew(Button);
  641. group_dialog_button->set_text(TTR("Manage Groups"));
  642. vbc->add_child(group_dialog_button);
  643. group_dialog_button->connect("pressed", callable_mp(this, &GroupsEditor::_show_group_dialog));
  644. HBoxContainer *hbc = memnew(HBoxContainer);
  645. vbc->add_child(hbc);
  646. group_name = memnew(LineEdit);
  647. group_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  648. hbc->add_child(group_name);
  649. group_name->connect("text_submitted", callable_mp(this, &GroupsEditor::_add_group));
  650. group_name->connect("text_changed", callable_mp(this, &GroupsEditor::_group_name_changed));
  651. add = memnew(Button);
  652. add->set_text(TTR("Add"));
  653. hbc->add_child(add);
  654. add->connect("pressed", callable_mp(this, &GroupsEditor::_add_group).bind(String()));
  655. tree = memnew(Tree);
  656. vbc->add_child(tree);
  657. tree->set_hide_root(true);
  658. tree->set_allow_reselect(true);
  659. tree->set_allow_rmb_select(true);
  660. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  661. tree->connect("item_selected", callable_mp(this, &GroupsEditor::_group_selected));
  662. tree->connect("button_clicked", callable_mp(this, &GroupsEditor::_modify_group));
  663. tree->connect("item_edited", callable_mp(this, &GroupsEditor::_group_renamed));
  664. tree->add_theme_constant_override("draw_guides", 1);
  665. add_theme_constant_override("separation", 3 * EDSCALE);
  666. error = memnew(AcceptDialog);
  667. add_child(error);
  668. error->get_ok_button()->set_text(TTR("Close"));
  669. _group_name_changed("");
  670. }
  671. GroupsEditor::~GroupsEditor() {
  672. }