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_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/gui/scene_tree_editor.h"
  36. #include "editor/scene_tree_dock.h"
  37. #include "scene/gui/button.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/gui/line_edit.h"
  40. #include "scene/gui/tree.h"
  41. #include "scene/resources/packed_scene.h"
  42. static bool can_edit(Node *p_node, String p_group) {
  43. Node *n = p_node;
  44. bool can_edit = true;
  45. while (n) {
  46. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  47. if (ss.is_valid()) {
  48. int path = ss->find_node_by_path(n->get_path_to(p_node));
  49. if (path != -1) {
  50. if (ss->is_node_in_group(path, p_group)) {
  51. can_edit = false;
  52. }
  53. }
  54. }
  55. n = n->get_owner();
  56. }
  57. return can_edit;
  58. }
  59. void GroupDialog::_group_selected() {
  60. nodes_to_add->clear();
  61. add_node_root = nodes_to_add->create_item();
  62. nodes_to_remove->clear();
  63. remove_node_root = nodes_to_remove->create_item();
  64. if (!groups->is_anything_selected()) {
  65. group_empty->hide();
  66. return;
  67. }
  68. selected_group = groups->get_selected()->get_text(0);
  69. _load_nodes(scene_tree->get_edited_scene_root());
  70. group_empty->set_visible(!remove_node_root->get_first_child());
  71. }
  72. void GroupDialog::_load_nodes(Node *p_current) {
  73. String item_name = p_current->get_name();
  74. if (p_current != scene_tree->get_edited_scene_root()) {
  75. item_name = String(p_current->get_parent()->get_name()) + "/" + item_name;
  76. }
  77. bool keep = true;
  78. Node *root = scene_tree->get_edited_scene_root();
  79. Node *owner = p_current->get_owner();
  80. if (owner != root && p_current != root && !owner && !root->is_editable_instance(owner)) {
  81. keep = false;
  82. }
  83. TreeItem *node = nullptr;
  84. NodePath path = scene_tree->get_edited_scene_root()->get_path_to(p_current);
  85. if (keep && p_current->is_in_group(selected_group)) {
  86. if (remove_filter->get_text().is_subsequence_ofn(String(p_current->get_name()))) {
  87. node = nodes_to_remove->create_item(remove_node_root);
  88. keep = true;
  89. } else {
  90. keep = false;
  91. }
  92. } else if (keep && add_filter->get_text().is_subsequence_ofn(String(p_current->get_name()))) {
  93. node = nodes_to_add->create_item(add_node_root);
  94. keep = true;
  95. } else {
  96. keep = false;
  97. }
  98. if (keep) {
  99. node->set_text(0, item_name);
  100. node->set_metadata(0, path);
  101. node->set_tooltip_text(0, path);
  102. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(p_current, "Node");
  103. node->set_icon(0, icon);
  104. if (!can_edit(p_current, selected_group)) {
  105. node->set_selectable(0, false);
  106. node->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), EditorStringName(Editor)));
  107. }
  108. }
  109. for (int i = 0; i < p_current->get_child_count(); i++) {
  110. _load_nodes(p_current->get_child(i));
  111. }
  112. }
  113. void GroupDialog::_add_pressed() {
  114. TreeItem *selected = nodes_to_add->get_next_selected(nullptr);
  115. if (!selected) {
  116. return;
  117. }
  118. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  119. undo_redo->create_action(TTR("Add to Group"));
  120. while (selected) {
  121. Node *node = scene_tree->get_edited_scene_root()->get_node(selected->get_metadata(0));
  122. undo_redo->add_do_method(node, "add_to_group", selected_group, true);
  123. undo_redo->add_undo_method(node, "remove_from_group", selected_group);
  124. selected = nodes_to_add->get_next_selected(selected);
  125. }
  126. undo_redo->add_do_method(this, "_group_selected");
  127. undo_redo->add_undo_method(this, "_group_selected");
  128. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  129. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  130. // To force redraw of scene tree.
  131. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  132. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  133. undo_redo->commit_action();
  134. }
  135. void GroupDialog::_removed_pressed() {
  136. TreeItem *selected = nodes_to_remove->get_next_selected(nullptr);
  137. if (!selected) {
  138. return;
  139. }
  140. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  141. undo_redo->create_action(TTR("Remove from Group"));
  142. while (selected) {
  143. Node *node = scene_tree->get_edited_scene_root()->get_node(selected->get_metadata(0));
  144. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  145. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  146. selected = nodes_to_add->get_next_selected(selected);
  147. }
  148. undo_redo->add_do_method(this, "_group_selected");
  149. undo_redo->add_undo_method(this, "_group_selected");
  150. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  151. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  152. // To force redraw of scene tree.
  153. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  154. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  155. undo_redo->commit_action();
  156. }
  157. void GroupDialog::_remove_filter_changed(const String &p_filter) {
  158. _group_selected();
  159. }
  160. void GroupDialog::_add_filter_changed(const String &p_filter) {
  161. _group_selected();
  162. }
  163. void GroupDialog::_add_group_pressed(const String &p_name) {
  164. _add_group(add_group_text->get_text());
  165. add_group_text->clear();
  166. }
  167. void GroupDialog::_add_group(String p_name) {
  168. if (!is_visible()) {
  169. return; // No need to edit the dialog if it's not being used.
  170. }
  171. String name = p_name.strip_edges();
  172. if (name.is_empty() || groups->get_item_with_text(name)) {
  173. return;
  174. }
  175. TreeItem *new_group = groups->create_item(groups_root);
  176. new_group->set_text(0, name);
  177. new_group->add_button(0, get_editor_theme_icon(SNAME("Remove")), DELETE_GROUP);
  178. new_group->add_button(0, get_editor_theme_icon(SNAME("ActionCopy")), COPY_GROUP);
  179. new_group->set_editable(0, true);
  180. new_group->select(0);
  181. groups->ensure_cursor_is_visible();
  182. }
  183. void GroupDialog::_add_group_text_changed(const String &p_new_text) {
  184. add_group_button->set_disabled(p_new_text.strip_edges().is_empty());
  185. }
  186. void GroupDialog::_group_renamed() {
  187. TreeItem *renamed_group = groups->get_selected();
  188. if (!renamed_group) {
  189. return;
  190. }
  191. const String name = renamed_group->get_text(0).strip_edges();
  192. if (name == selected_group) {
  193. return;
  194. }
  195. if (name.is_empty()) {
  196. renamed_group->set_text(0, selected_group);
  197. error->set_text(TTR("Invalid group name."));
  198. error->popup_centered();
  199. return;
  200. }
  201. for (TreeItem *E = groups_root->get_first_child(); E; E = E->get_next()) {
  202. if (E != renamed_group && E->get_text(0) == name) {
  203. renamed_group->set_text(0, selected_group);
  204. error->set_text(TTR("Group name already exists."));
  205. error->popup_centered();
  206. return;
  207. }
  208. }
  209. renamed_group->set_text(0, name); // Spaces trimmed.
  210. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  211. undo_redo->create_action(TTR("Rename Group"));
  212. List<Node *> nodes;
  213. scene_tree->get_nodes_in_group(selected_group, &nodes);
  214. bool removed_all = true;
  215. for (Node *node : nodes) {
  216. if (can_edit(node, selected_group)) {
  217. undo_redo->add_do_method(node, "remove_from_group", selected_group);
  218. undo_redo->add_undo_method(node, "remove_from_group", name);
  219. undo_redo->add_do_method(node, "add_to_group", name, true);
  220. undo_redo->add_undo_method(node, "add_to_group", selected_group, true);
  221. } else {
  222. removed_all = false;
  223. }
  224. }
  225. if (!removed_all) {
  226. undo_redo->add_do_method(this, "_add_group", selected_group);
  227. undo_redo->add_undo_method(this, "_delete_group_item", selected_group);
  228. }
  229. undo_redo->add_do_method(this, "_rename_group_item", selected_group, name);
  230. undo_redo->add_undo_method(this, "_rename_group_item", name, selected_group);
  231. undo_redo->add_do_method(this, "_group_selected");
  232. undo_redo->add_undo_method(this, "_group_selected");
  233. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  234. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  235. undo_redo->commit_action();
  236. }
  237. void GroupDialog::_rename_group_item(const String &p_old_name, const String &p_new_name) {
  238. if (!is_visible()) {
  239. return; // No need to edit the dialog if it's not being used.
  240. }
  241. selected_group = p_new_name;
  242. for (TreeItem *E = groups_root->get_first_child(); E; E = E->get_next()) {
  243. if (E->get_text(0) == p_old_name) {
  244. E->set_text(0, p_new_name);
  245. return;
  246. }
  247. }
  248. }
  249. void GroupDialog::_load_groups(Node *p_current) {
  250. List<Node::GroupInfo> gi;
  251. p_current->get_groups(&gi);
  252. for (const Node::GroupInfo &E : gi) {
  253. if (!E.persistent) {
  254. continue;
  255. }
  256. _add_group(E.name);
  257. }
  258. for (int i = 0; i < p_current->get_child_count(); i++) {
  259. _load_groups(p_current->get_child(i));
  260. }
  261. }
  262. void GroupDialog::_modify_group_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  263. if (p_button != MouseButton::LEFT) {
  264. return;
  265. }
  266. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  267. if (!ti) {
  268. return;
  269. }
  270. switch (p_id) {
  271. case DELETE_GROUP: {
  272. String name = ti->get_text(0);
  273. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  274. undo_redo->create_action(TTR("Delete Group"));
  275. List<Node *> nodes;
  276. scene_tree->get_nodes_in_group(name, &nodes);
  277. bool removed_all = true;
  278. for (Node *E : nodes) {
  279. if (can_edit(E, name)) {
  280. undo_redo->add_do_method(E, "remove_from_group", name);
  281. undo_redo->add_undo_method(E, "add_to_group", name, true);
  282. } else {
  283. removed_all = false;
  284. }
  285. }
  286. if (removed_all) {
  287. undo_redo->add_do_method(this, "_delete_group_item", name);
  288. undo_redo->add_undo_method(this, "_add_group", name);
  289. }
  290. undo_redo->add_do_method(this, "_group_selected");
  291. undo_redo->add_undo_method(this, "_group_selected");
  292. undo_redo->add_do_method(this, "emit_signal", "group_edited");
  293. undo_redo->add_undo_method(this, "emit_signal", "group_edited");
  294. // To force redraw of scene tree.
  295. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  296. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  297. undo_redo->commit_action();
  298. } break;
  299. case COPY_GROUP: {
  300. DisplayServer::get_singleton()->clipboard_set(ti->get_text(p_column));
  301. } break;
  302. }
  303. }
  304. void GroupDialog::_delete_group_item(const String &p_name) {
  305. if (!is_visible()) {
  306. return; // No need to edit the dialog if it's not being used.
  307. }
  308. if (selected_group == p_name) {
  309. add_filter->clear();
  310. remove_filter->clear();
  311. nodes_to_remove->clear();
  312. nodes_to_add->clear();
  313. groups->deselect_all();
  314. selected_group = "";
  315. }
  316. for (TreeItem *E = groups_root->get_first_child(); E; E = E->get_next()) {
  317. if (E->get_text(0) == p_name) {
  318. groups_root->remove_child(E);
  319. return;
  320. }
  321. }
  322. }
  323. void GroupDialog::_notification(int p_what) {
  324. switch (p_what) {
  325. case NOTIFICATION_TRANSLATION_CHANGED:
  326. case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  327. case NOTIFICATION_THEME_CHANGED: {
  328. if (is_layout_rtl()) {
  329. add_button->set_icon(get_editor_theme_icon(SNAME("Back")));
  330. remove_button->set_icon(get_editor_theme_icon(SNAME("Forward")));
  331. } else {
  332. add_button->set_icon(get_editor_theme_icon(SNAME("Forward")));
  333. remove_button->set_icon(get_editor_theme_icon(SNAME("Back")));
  334. }
  335. add_filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  336. remove_filter->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  337. } break;
  338. }
  339. }
  340. void GroupDialog::edit() {
  341. popup_centered();
  342. groups->clear();
  343. groups_root = groups->create_item();
  344. nodes_to_add->clear();
  345. nodes_to_remove->clear();
  346. add_group_text->clear();
  347. add_filter->clear();
  348. remove_filter->clear();
  349. _load_groups(scene_tree->get_edited_scene_root());
  350. }
  351. void GroupDialog::_bind_methods() {
  352. ClassDB::bind_method("_delete_group_item", &GroupDialog::_delete_group_item);
  353. ClassDB::bind_method("_add_group", &GroupDialog::_add_group);
  354. ClassDB::bind_method("_rename_group_item", &GroupDialog::_rename_group_item);
  355. ClassDB::bind_method("_group_selected", &GroupDialog::_group_selected);
  356. ADD_SIGNAL(MethodInfo("group_edited"));
  357. }
  358. GroupDialog::GroupDialog() {
  359. set_min_size(Size2(600, 400) * EDSCALE);
  360. scene_tree = SceneTree::get_singleton();
  361. VBoxContainer *vbc = memnew(VBoxContainer);
  362. add_child(vbc);
  363. vbc->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  364. HBoxContainer *hbc = memnew(HBoxContainer);
  365. vbc->add_child(hbc);
  366. hbc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  367. VBoxContainer *vbc_left = memnew(VBoxContainer);
  368. hbc->add_child(vbc_left);
  369. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  370. Label *group_title = memnew(Label);
  371. group_title->set_theme_type_variation("HeaderSmall");
  372. group_title->set_text(TTR("Groups"));
  373. vbc_left->add_child(group_title);
  374. groups = memnew(Tree);
  375. vbc_left->add_child(groups);
  376. groups->set_hide_root(true);
  377. groups->set_select_mode(Tree::SELECT_SINGLE);
  378. groups->set_allow_reselect(true);
  379. groups->set_allow_rmb_select(true);
  380. groups->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  381. groups->add_theme_constant_override("draw_guides", 1);
  382. groups->connect("item_selected", callable_mp(this, &GroupDialog::_group_selected));
  383. groups->connect("button_clicked", callable_mp(this, &GroupDialog::_modify_group_pressed));
  384. groups->connect("item_edited", callable_mp(this, &GroupDialog::_group_renamed));
  385. HBoxContainer *chbc = memnew(HBoxContainer);
  386. vbc_left->add_child(chbc);
  387. chbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  388. add_group_text = memnew(LineEdit);
  389. chbc->add_child(add_group_text);
  390. add_group_text->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  391. add_group_text->connect("text_submitted", callable_mp(this, &GroupDialog::_add_group_pressed));
  392. add_group_text->connect("text_changed", callable_mp(this, &GroupDialog::_add_group_text_changed));
  393. add_group_button = memnew(Button);
  394. add_group_button->set_text(TTR("Add"));
  395. chbc->add_child(add_group_button);
  396. add_group_button->connect("pressed", callable_mp(this, &GroupDialog::_add_group_pressed).bind(String()));
  397. VBoxContainer *vbc_add = memnew(VBoxContainer);
  398. hbc->add_child(vbc_add);
  399. vbc_add->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  400. Label *out_of_group_title = memnew(Label);
  401. out_of_group_title->set_theme_type_variation("HeaderSmall");
  402. out_of_group_title->set_text(TTR("Nodes Not in Group"));
  403. vbc_add->add_child(out_of_group_title);
  404. nodes_to_add = memnew(Tree);
  405. vbc_add->add_child(nodes_to_add);
  406. nodes_to_add->set_hide_root(true);
  407. nodes_to_add->set_hide_folding(true);
  408. nodes_to_add->set_select_mode(Tree::SELECT_MULTI);
  409. nodes_to_add->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  410. nodes_to_add->add_theme_constant_override("draw_guides", 1);
  411. HBoxContainer *add_filter_hbc = memnew(HBoxContainer);
  412. add_filter_hbc->add_theme_constant_override("separate", 0);
  413. vbc_add->add_child(add_filter_hbc);
  414. add_filter = memnew(LineEdit);
  415. add_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  416. add_filter->set_placeholder(TTR("Filter Nodes"));
  417. add_filter->set_clear_button_enabled(true);
  418. add_filter_hbc->add_child(add_filter);
  419. add_filter->connect("text_changed", callable_mp(this, &GroupDialog::_add_filter_changed));
  420. VBoxContainer *vbc_buttons = memnew(VBoxContainer);
  421. hbc->add_child(vbc_buttons);
  422. vbc_buttons->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  423. vbc_buttons->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  424. add_button = memnew(Button);
  425. add_button->set_flat(true);
  426. add_button->set_text(TTR("Add"));
  427. add_button->connect("pressed", callable_mp(this, &GroupDialog::_add_pressed));
  428. vbc_buttons->add_child(add_button);
  429. vbc_buttons->add_spacer();
  430. vbc_buttons->add_spacer();
  431. vbc_buttons->add_spacer();
  432. remove_button = memnew(Button);
  433. remove_button->set_flat(true);
  434. remove_button->set_text(TTR("Remove"));
  435. remove_button->connect("pressed", callable_mp(this, &GroupDialog::_removed_pressed));
  436. vbc_buttons->add_child(remove_button);
  437. VBoxContainer *vbc_remove = memnew(VBoxContainer);
  438. hbc->add_child(vbc_remove);
  439. vbc_remove->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  440. Label *in_group_title = memnew(Label);
  441. in_group_title->set_theme_type_variation("HeaderSmall");
  442. in_group_title->set_text(TTR("Nodes in Group"));
  443. vbc_remove->add_child(in_group_title);
  444. nodes_to_remove = memnew(Tree);
  445. vbc_remove->add_child(nodes_to_remove);
  446. nodes_to_remove->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  447. nodes_to_remove->set_hide_root(true);
  448. nodes_to_remove->set_hide_folding(true);
  449. nodes_to_remove->set_select_mode(Tree::SELECT_MULTI);
  450. nodes_to_remove->add_theme_constant_override("draw_guides", 1);
  451. HBoxContainer *remove_filter_hbc = memnew(HBoxContainer);
  452. remove_filter_hbc->add_theme_constant_override("separate", 0);
  453. vbc_remove->add_child(remove_filter_hbc);
  454. remove_filter = memnew(LineEdit);
  455. remove_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  456. remove_filter->set_placeholder(TTR("Filter Nodes"));
  457. remove_filter->set_clear_button_enabled(true);
  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_editor_theme_icon(SNAME("Remove")), DELETE_GROUP);
  617. item->add_button(0, get_editor_theme_icon(SNAME("ActionCopy")), 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. }