groups_editor.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/gui/editor_validation_panel.h"
  36. #include "editor/project_settings_editor.h"
  37. #include "editor/scene_tree_dock.h"
  38. #include "editor/themes/editor_scale.h"
  39. #include "scene/gui/box_container.h"
  40. #include "scene/gui/check_button.h"
  41. #include "scene/gui/grid_container.h"
  42. #include "scene/gui/label.h"
  43. #include "scene/resources/packed_scene.h"
  44. static bool can_edit(Node *p_node, const String &p_group) {
  45. Node *n = p_node;
  46. bool can_edit = true;
  47. while (n) {
  48. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  49. if (ss.is_valid()) {
  50. int path = ss->find_node_by_path(n->get_path_to(p_node));
  51. if (path != -1) {
  52. if (ss->is_node_in_group(path, p_group)) {
  53. can_edit = false;
  54. break;
  55. }
  56. }
  57. }
  58. n = n->get_owner();
  59. }
  60. return can_edit;
  61. }
  62. struct _GroupInfoComparator {
  63. bool operator()(const Node::GroupInfo &p_a, const Node::GroupInfo &p_b) const {
  64. return p_a.name.operator String() < p_b.name.operator String();
  65. }
  66. };
  67. void GroupsEditor::_add_scene_group(const String &p_name) {
  68. scene_groups[p_name] = true;
  69. }
  70. void GroupsEditor::_remove_scene_group(const String &p_name) {
  71. scene_groups.erase(p_name);
  72. ProjectSettingsEditor::get_singleton()->get_group_settings()->remove_node_references(scene_root_node, p_name);
  73. }
  74. void GroupsEditor::_rename_scene_group(const String &p_old_name, const String &p_new_name) {
  75. scene_groups[p_new_name] = scene_groups[p_old_name];
  76. scene_groups.erase(p_old_name);
  77. ProjectSettingsEditor::get_singleton()->get_group_settings()->rename_node_references(scene_root_node, p_old_name, p_new_name);
  78. }
  79. void GroupsEditor::_set_group_checked(const String &p_name, bool p_checked) {
  80. TreeItem *ti = tree->get_item_with_text(p_name);
  81. if (!ti) {
  82. return;
  83. }
  84. ti->set_checked(0, p_checked);
  85. }
  86. bool GroupsEditor::_has_group(const String &p_name) {
  87. return global_groups.has(p_name) || scene_groups.has(p_name);
  88. }
  89. void GroupsEditor::_modify_group(Object *p_item, int p_column, int p_id, MouseButton p_mouse_button) {
  90. if (p_mouse_button != MouseButton::LEFT) {
  91. return;
  92. }
  93. if (!node) {
  94. return;
  95. }
  96. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  97. if (!ti) {
  98. return;
  99. }
  100. if (p_id == COPY_GROUP) {
  101. DisplayServer::get_singleton()->clipboard_set(ti->get_text(p_column));
  102. }
  103. }
  104. void GroupsEditor::_load_scene_groups(Node *p_node) {
  105. List<Node::GroupInfo> groups;
  106. p_node->get_groups(&groups);
  107. for (const GroupInfo &gi : groups) {
  108. if (!gi.persistent) {
  109. continue;
  110. }
  111. if (global_groups.has(gi.name)) {
  112. continue;
  113. }
  114. bool is_editable = can_edit(p_node, gi.name);
  115. if (scene_groups.has(gi.name)) {
  116. scene_groups[gi.name] = scene_groups[gi.name] && is_editable;
  117. } else {
  118. scene_groups[gi.name] = is_editable;
  119. }
  120. }
  121. for (int i = 0; i < p_node->get_child_count(); i++) {
  122. _load_scene_groups(p_node->get_child(i));
  123. }
  124. }
  125. void GroupsEditor::_update_groups() {
  126. if (!is_visible_in_tree()) {
  127. groups_dirty = true;
  128. return;
  129. }
  130. if (updating_groups) {
  131. return;
  132. }
  133. updating_groups = true;
  134. global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
  135. _load_scene_groups(scene_root_node);
  136. for (HashMap<StringName, bool>::Iterator E = scene_groups.begin(); E;) {
  137. HashMap<StringName, bool>::Iterator next = E;
  138. ++next;
  139. if (global_groups.has(E->key)) {
  140. scene_groups.erase(E->key);
  141. }
  142. E = next;
  143. }
  144. updating_groups = false;
  145. }
  146. void GroupsEditor::_update_tree() {
  147. if (!is_visible_in_tree()) {
  148. groups_dirty = true;
  149. return;
  150. }
  151. if (!node) {
  152. return;
  153. }
  154. if (updating_tree) {
  155. return;
  156. }
  157. updating_tree = true;
  158. tree->clear();
  159. List<Node::GroupInfo> groups;
  160. node->get_groups(&groups);
  161. groups.sort_custom<_GroupInfoComparator>();
  162. List<StringName> current_groups;
  163. for (const Node::GroupInfo &gi : groups) {
  164. current_groups.push_back(gi.name);
  165. }
  166. TreeItem *root = tree->create_item();
  167. TreeItem *local_root = tree->create_item(root);
  168. local_root->set_text(0, TTR("Scene Groups"));
  169. local_root->set_icon(0, get_editor_theme_icon(SNAME("PackedScene")));
  170. local_root->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  171. local_root->set_selectable(0, false);
  172. List<StringName> scene_keys;
  173. for (const KeyValue<StringName, bool> &E : scene_groups) {
  174. scene_keys.push_back(E.key);
  175. }
  176. scene_keys.sort_custom<NoCaseComparator>();
  177. for (const StringName &E : scene_keys) {
  178. if (!filter->get_text().is_subsequence_ofn(E)) {
  179. continue;
  180. }
  181. TreeItem *item = tree->create_item(local_root);
  182. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  183. item->set_editable(0, can_edit(node, E));
  184. item->set_checked(0, current_groups.find(E) != nullptr);
  185. item->set_text(0, E);
  186. item->set_meta("__local", true);
  187. item->set_meta("__name", E);
  188. item->set_meta("__description", "");
  189. if (!scene_groups[E]) {
  190. item->add_button(0, get_editor_theme_icon(SNAME("Lock")), -1, true, TTR("This group belongs to another scene and can't be edited."));
  191. }
  192. item->add_button(0, get_editor_theme_icon(SNAME("ActionCopy")), COPY_GROUP, false, TTR("Copy group name to clipboard."));
  193. }
  194. List<StringName> keys;
  195. for (const KeyValue<StringName, String> &E : global_groups) {
  196. keys.push_back(E.key);
  197. }
  198. keys.sort_custom<NoCaseComparator>();
  199. TreeItem *global_root = tree->create_item(root);
  200. global_root->set_text(0, TTR("Global Groups"));
  201. global_root->set_icon(0, get_editor_theme_icon(SNAME("Environment")));
  202. global_root->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  203. global_root->set_selectable(0, false);
  204. for (const StringName &E : keys) {
  205. if (!filter->get_text().is_subsequence_ofn(E)) {
  206. continue;
  207. }
  208. TreeItem *item = tree->create_item(global_root);
  209. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  210. item->set_editable(0, can_edit(node, E));
  211. item->set_checked(0, current_groups.find(E) != nullptr);
  212. item->set_text(0, E);
  213. item->set_meta("__local", false);
  214. item->set_meta("__name", E);
  215. item->set_meta("__description", global_groups[E]);
  216. if (!global_groups[E].is_empty()) {
  217. item->set_tooltip_text(0, vformat("%s\n\n%s", E, global_groups[E]));
  218. }
  219. item->add_button(0, get_editor_theme_icon(SNAME("ActionCopy")), COPY_GROUP, false, TTR("Copy group name to clipboard."));
  220. }
  221. updating_tree = false;
  222. }
  223. void GroupsEditor::_queue_update_groups_and_tree() {
  224. if (update_groups_and_tree_queued) {
  225. return;
  226. }
  227. update_groups_and_tree_queued = true;
  228. callable_mp(this, &GroupsEditor::_update_groups_and_tree).call_deferred();
  229. }
  230. void GroupsEditor::_update_groups_and_tree() {
  231. update_groups_and_tree_queued = false;
  232. _update_groups();
  233. _update_tree();
  234. }
  235. void GroupsEditor::_update_scene_groups(const ObjectID &p_id) {
  236. HashMap<ObjectID, HashMap<StringName, bool>>::Iterator I = scene_groups_cache.find(p_id);
  237. if (I) {
  238. scene_groups = I->value;
  239. scene_groups_cache.remove(I);
  240. } else {
  241. scene_groups = HashMap<StringName, bool>();
  242. }
  243. }
  244. void GroupsEditor::_cache_scene_groups(const ObjectID &p_id) {
  245. const int edited_scene_count = EditorNode::get_editor_data().get_edited_scene_count();
  246. for (int i = 0; i < edited_scene_count; i++) {
  247. Node *edited_scene_root = EditorNode::get_editor_data().get_edited_scene_root(i);
  248. if (edited_scene_root && p_id == edited_scene_root->get_instance_id()) {
  249. scene_groups_cache[p_id] = scene_groups_for_caching;
  250. break;
  251. }
  252. }
  253. }
  254. void GroupsEditor::set_current(Node *p_node) {
  255. if (node == p_node) {
  256. return;
  257. }
  258. node = p_node;
  259. if (!node) {
  260. return;
  261. }
  262. if (scene_tree->get_edited_scene_root() != scene_root_node) {
  263. scene_root_node = scene_tree->get_edited_scene_root();
  264. _update_scene_groups(scene_root_node->get_instance_id());
  265. _update_groups();
  266. }
  267. _update_tree();
  268. }
  269. void GroupsEditor::_item_edited() {
  270. TreeItem *ti = tree->get_edited();
  271. if (!ti) {
  272. return;
  273. }
  274. String name = ti->get_text(0);
  275. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  276. if (ti->is_checked(0)) {
  277. undo_redo->create_action(TTR("Add to Group"));
  278. undo_redo->add_do_method(node, "add_to_group", name, true);
  279. undo_redo->add_undo_method(node, "remove_from_group", name);
  280. undo_redo->add_do_method(this, "_set_group_checked", name, true);
  281. undo_redo->add_undo_method(this, "_set_group_checked", name, false);
  282. // To force redraw of scene tree.
  283. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  284. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  285. undo_redo->commit_action();
  286. } else {
  287. undo_redo->create_action(TTR("Remove from Group"));
  288. undo_redo->add_do_method(node, "remove_from_group", name);
  289. undo_redo->add_undo_method(node, "add_to_group", name, true);
  290. undo_redo->add_do_method(this, "_set_group_checked", name, false);
  291. undo_redo->add_undo_method(this, "_set_group_checked", name, true);
  292. // To force redraw of scene tree.
  293. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  294. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  295. undo_redo->commit_action();
  296. }
  297. }
  298. void GroupsEditor::_notification(int p_what) {
  299. switch (p_what) {
  300. case NOTIFICATION_READY: {
  301. get_tree()->connect("node_added", callable_mp(this, &GroupsEditor::_load_scene_groups));
  302. get_tree()->connect("node_removed", callable_mp(this, &GroupsEditor::_node_removed));
  303. } break;
  304. case NOTIFICATION_THEME_CHANGED: {
  305. filter->set_right_icon(get_editor_theme_icon("Search"));
  306. add->set_icon(get_editor_theme_icon("Add"));
  307. } break;
  308. case NOTIFICATION_VISIBILITY_CHANGED: {
  309. if (groups_dirty && is_visible_in_tree()) {
  310. groups_dirty = false;
  311. _update_groups_and_tree();
  312. }
  313. } break;
  314. }
  315. }
  316. void GroupsEditor::_menu_id_pressed(int p_id) {
  317. TreeItem *ti = tree->get_selected();
  318. if (!ti) {
  319. return;
  320. }
  321. bool is_local = ti->get_meta("__local");
  322. String group_name = ti->get_meta("__name");
  323. switch (p_id) {
  324. case DELETE_GROUP: {
  325. if (!is_local || scene_groups[group_name]) {
  326. _show_remove_group_dialog();
  327. }
  328. } break;
  329. case RENAME_GROUP: {
  330. if (!is_local || scene_groups[group_name]) {
  331. _show_rename_group_dialog();
  332. }
  333. } break;
  334. case CONVERT_GROUP: {
  335. String description = ti->get_meta("__description");
  336. String property_name = GLOBAL_GROUP_PREFIX + group_name;
  337. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  338. if (is_local) {
  339. undo_redo->create_action(TTR("Convert to Global Group"));
  340. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_name, "");
  341. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_name, Variant());
  342. undo_redo->add_do_method(ProjectSettings::get_singleton(), "save");
  343. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "save");
  344. undo_redo->add_undo_method(this, "_add_scene_group", group_name);
  345. undo_redo->add_do_method(this, "_update_groups_and_tree");
  346. undo_redo->add_undo_method(this, "_update_groups_and_tree");
  347. undo_redo->commit_action();
  348. } else {
  349. undo_redo->create_action(TTR("Convert to Scene Group"));
  350. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_name, Variant());
  351. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_name, description);
  352. undo_redo->add_do_method(ProjectSettings::get_singleton(), "save");
  353. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "save");
  354. undo_redo->add_do_method(this, "_add_scene_group", group_name);
  355. undo_redo->add_do_method(this, "_update_groups_and_tree");
  356. undo_redo->add_undo_method(this, "_update_groups_and_tree");
  357. undo_redo->commit_action();
  358. }
  359. } break;
  360. }
  361. }
  362. void GroupsEditor::_item_mouse_selected(const Vector2 &p_pos, MouseButton p_mouse_button) {
  363. TreeItem *ti = tree->get_selected();
  364. if (!ti) {
  365. return;
  366. }
  367. if (p_mouse_button == MouseButton::LEFT) {
  368. callable_mp(this, &GroupsEditor::_item_edited).call_deferred();
  369. } else if (p_mouse_button == MouseButton::RIGHT) {
  370. // Restore the previous state after clicking RMB.
  371. if (ti->is_editable(0)) {
  372. ti->set_checked(0, !ti->is_checked(0));
  373. }
  374. menu->clear();
  375. if (ti->get_meta("__local")) {
  376. menu->add_icon_item(get_editor_theme_icon(SNAME("Environment")), TTR("Convert to Global Group"), CONVERT_GROUP);
  377. } else {
  378. menu->add_icon_item(get_editor_theme_icon(SNAME("PackedScene")), TTR("Convert to Scene Group"), CONVERT_GROUP);
  379. }
  380. String group_name = ti->get_meta("__name");
  381. if (global_groups.has(group_name) || scene_groups[group_name]) {
  382. menu->add_separator();
  383. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Rename")), ED_GET_SHORTCUT("groups_editor/rename"), RENAME_GROUP);
  384. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("Remove")), ED_GET_SHORTCUT("groups_editor/delete"), DELETE_GROUP);
  385. }
  386. menu->set_position(tree->get_screen_position() + p_pos);
  387. menu->reset_size();
  388. menu->popup();
  389. }
  390. }
  391. void GroupsEditor::_confirm_add() {
  392. String name = add_group_name->get_text().strip_edges();
  393. String description = add_group_description->get_text();
  394. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  395. undo_redo->create_action(TTR("Add to Group"));
  396. undo_redo->add_do_method(node, "add_to_group", name, true);
  397. undo_redo->add_undo_method(node, "remove_from_group", name);
  398. bool is_local = !global_group_button->is_pressed();
  399. if (is_local) {
  400. undo_redo->add_do_method(this, "_add_scene_group", name);
  401. undo_redo->add_undo_method(this, "_remove_scene_group", name);
  402. } else {
  403. String property_name = GLOBAL_GROUP_PREFIX + name;
  404. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_name, description);
  405. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_name, Variant());
  406. undo_redo->add_do_method(ProjectSettings::get_singleton(), "save");
  407. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "save");
  408. undo_redo->add_do_method(this, "_update_groups");
  409. undo_redo->add_undo_method(this, "_update_groups");
  410. }
  411. undo_redo->add_do_method(this, "_update_tree");
  412. undo_redo->add_undo_method(this, "_update_tree");
  413. // To force redraw of scene tree.
  414. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  415. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  416. undo_redo->commit_action();
  417. tree->grab_focus();
  418. }
  419. void GroupsEditor::_confirm_rename() {
  420. TreeItem *ti = tree->get_selected();
  421. if (!ti) {
  422. return;
  423. }
  424. String old_name = ti->get_meta("__name");
  425. String new_name = rename_group->get_text().strip_edges();
  426. if (old_name == new_name) {
  427. return;
  428. }
  429. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  430. undo_redo->create_action(TTR("Rename Group"));
  431. if (!global_groups.has(old_name)) {
  432. undo_redo->add_do_method(this, "_rename_scene_group", old_name, new_name);
  433. undo_redo->add_undo_method(this, "_rename_scene_group", new_name, old_name);
  434. } else {
  435. String property_new_name = GLOBAL_GROUP_PREFIX + new_name;
  436. String property_old_name = GLOBAL_GROUP_PREFIX + old_name;
  437. String description = ti->get_meta("__description");
  438. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_new_name, description);
  439. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_new_name, Variant());
  440. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_old_name, Variant());
  441. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_old_name, description);
  442. if (rename_check_box->is_pressed()) {
  443. undo_redo->add_do_method(ProjectSettingsEditor::get_singleton()->get_group_settings(), "rename_references", old_name, new_name);
  444. undo_redo->add_undo_method(ProjectSettingsEditor::get_singleton()->get_group_settings(), "rename_references", new_name, old_name);
  445. }
  446. undo_redo->add_do_method(ProjectSettings::get_singleton(), "save");
  447. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "save");
  448. undo_redo->add_do_method(this, "_update_groups");
  449. undo_redo->add_undo_method(this, "_update_groups");
  450. }
  451. undo_redo->add_do_method(this, "_update_tree");
  452. undo_redo->add_undo_method(this, "_update_tree");
  453. undo_redo->commit_action();
  454. tree->grab_focus();
  455. }
  456. void GroupsEditor::_confirm_delete() {
  457. TreeItem *ti = tree->get_selected();
  458. if (!ti) {
  459. return;
  460. }
  461. String name = ti->get_meta("__name");
  462. bool is_local = ti->get_meta("__local");
  463. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  464. undo_redo->create_action(TTR("Remove Group"));
  465. if (is_local) {
  466. undo_redo->add_do_method(this, "_remove_scene_group", name);
  467. undo_redo->add_undo_method(this, "_add_scene_group", name);
  468. } else {
  469. String property_name = GLOBAL_GROUP_PREFIX + name;
  470. String description = ti->get_meta("__description");
  471. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_name, Variant());
  472. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_name, description);
  473. if (remove_check_box->is_pressed()) {
  474. undo_redo->add_do_method(ProjectSettingsEditor::get_singleton()->get_group_settings(), "remove_references", name);
  475. }
  476. undo_redo->add_do_method(ProjectSettings::get_singleton(), "save");
  477. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "save");
  478. undo_redo->add_do_method(this, "_update_groups");
  479. undo_redo->add_undo_method(this, "_update_groups");
  480. }
  481. undo_redo->add_do_method(this, "_update_tree");
  482. undo_redo->add_undo_method(this, "_update_tree");
  483. undo_redo->commit_action();
  484. tree->grab_focus();
  485. }
  486. void GroupsEditor::_show_add_group_dialog() {
  487. if (!add_group_dialog) {
  488. add_group_dialog = memnew(ConfirmationDialog);
  489. add_group_dialog->set_title(TTR("Create New Group"));
  490. add_group_dialog->connect("confirmed", callable_mp(this, &GroupsEditor::_confirm_add));
  491. VBoxContainer *vbc = memnew(VBoxContainer);
  492. add_group_dialog->add_child(vbc);
  493. GridContainer *gc = memnew(GridContainer);
  494. gc->set_columns(2);
  495. vbc->add_child(gc);
  496. Label *label_name = memnew(Label(TTR("Name:")));
  497. label_name->set_h_size_flags(SIZE_SHRINK_BEGIN);
  498. gc->add_child(label_name);
  499. HBoxContainer *hbc = memnew(HBoxContainer);
  500. hbc->set_h_size_flags(SIZE_EXPAND_FILL);
  501. gc->add_child(hbc);
  502. add_group_name = memnew(LineEdit);
  503. add_group_name->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
  504. add_group_name->set_h_size_flags(SIZE_EXPAND_FILL);
  505. hbc->add_child(add_group_name);
  506. global_group_button = memnew(CheckButton);
  507. global_group_button->set_text(TTR("Global"));
  508. hbc->add_child(global_group_button);
  509. Label *label_description = memnew(Label(TTR("Description:")));
  510. label_name->set_h_size_flags(SIZE_SHRINK_BEGIN);
  511. gc->add_child(label_description);
  512. add_group_description = memnew(LineEdit);
  513. add_group_description->set_h_size_flags(SIZE_EXPAND_FILL);
  514. add_group_description->set_editable(false);
  515. gc->add_child(add_group_description);
  516. global_group_button->connect("toggled", callable_mp(add_group_description, &LineEdit::set_editable));
  517. add_group_dialog->register_text_enter(add_group_name);
  518. add_group_dialog->register_text_enter(add_group_description);
  519. add_validation_panel = memnew(EditorValidationPanel);
  520. add_validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Group name is valid."));
  521. add_validation_panel->set_update_callback(callable_mp(this, &GroupsEditor::_check_add));
  522. add_validation_panel->set_accept_button(add_group_dialog->get_ok_button());
  523. add_group_name->connect("text_changed", callable_mp(add_validation_panel, &EditorValidationPanel::update).unbind(1));
  524. vbc->add_child(add_validation_panel);
  525. add_child(add_group_dialog);
  526. }
  527. add_group_name->clear();
  528. add_group_description->clear();
  529. global_group_button->set_pressed(false);
  530. add_validation_panel->update();
  531. add_group_dialog->popup_centered();
  532. add_group_name->grab_focus();
  533. }
  534. void GroupsEditor::_show_rename_group_dialog() {
  535. if (!rename_group_dialog) {
  536. rename_group_dialog = memnew(ConfirmationDialog);
  537. rename_group_dialog->set_title(TTR("Rename Group"));
  538. rename_group_dialog->connect("confirmed", callable_mp(this, &GroupsEditor::_confirm_rename));
  539. VBoxContainer *vbc = memnew(VBoxContainer);
  540. rename_group_dialog->add_child(vbc);
  541. HBoxContainer *hbc = memnew(HBoxContainer);
  542. hbc->add_child(memnew(Label(TTR("Name:"))));
  543. rename_group = memnew(LineEdit);
  544. rename_group->set_custom_minimum_size(Size2(300 * EDSCALE, 1));
  545. hbc->add_child(rename_group);
  546. vbc->add_child(hbc);
  547. rename_group_dialog->register_text_enter(rename_group);
  548. rename_validation_panel = memnew(EditorValidationPanel);
  549. rename_validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Group name is valid."));
  550. rename_validation_panel->set_update_callback(callable_mp(this, &GroupsEditor::_check_rename));
  551. rename_validation_panel->set_accept_button(rename_group_dialog->get_ok_button());
  552. rename_group->connect("text_changed", callable_mp(rename_validation_panel, &EditorValidationPanel::update).unbind(1));
  553. vbc->add_child(rename_validation_panel);
  554. rename_check_box = memnew(CheckBox);
  555. rename_check_box->set_text(TTR("Rename references in all scenes"));
  556. vbc->add_child(rename_check_box);
  557. add_child(rename_group_dialog);
  558. }
  559. TreeItem *ti = tree->get_selected();
  560. if (!ti) {
  561. return;
  562. }
  563. bool is_global = !ti->get_meta("__local");
  564. rename_check_box->set_visible(is_global);
  565. rename_check_box->set_pressed(false);
  566. String name = ti->get_meta("__name");
  567. rename_group->set_text(name);
  568. rename_group_dialog->set_meta("__name", name);
  569. rename_validation_panel->update();
  570. rename_group_dialog->reset_size();
  571. rename_group_dialog->popup_centered();
  572. rename_group->select_all();
  573. rename_group->grab_focus();
  574. }
  575. void GroupsEditor::_show_remove_group_dialog() {
  576. if (!remove_group_dialog) {
  577. remove_group_dialog = memnew(ConfirmationDialog);
  578. remove_group_dialog->connect("confirmed", callable_mp(this, &GroupsEditor::_confirm_delete));
  579. VBoxContainer *vbox = memnew(VBoxContainer);
  580. remove_label = memnew(Label);
  581. vbox->add_child(remove_label);
  582. remove_check_box = memnew(CheckBox);
  583. remove_check_box->set_text(TTR("Delete references from all scenes"));
  584. vbox->add_child(remove_check_box);
  585. remove_group_dialog->add_child(vbox);
  586. add_child(remove_group_dialog);
  587. }
  588. TreeItem *ti = tree->get_selected();
  589. if (!ti) {
  590. return;
  591. }
  592. bool is_global = !ti->get_meta("__local");
  593. remove_check_box->set_visible(is_global);
  594. remove_check_box->set_pressed(false);
  595. remove_label->set_text(vformat(TTR("Delete group \"%s\" and all its references?"), ti->get_text(0)));
  596. remove_group_dialog->reset_size();
  597. remove_group_dialog->popup_centered();
  598. }
  599. void GroupsEditor::_check_add() {
  600. String group_name = add_group_name->get_text().strip_edges();
  601. _validate_name(group_name, add_validation_panel);
  602. }
  603. void GroupsEditor::_check_rename() {
  604. String group_name = rename_group->get_text().strip_edges();
  605. String old_name = rename_group_dialog->get_meta("__name");
  606. if (group_name == old_name) {
  607. return;
  608. }
  609. _validate_name(group_name, rename_validation_panel);
  610. }
  611. void GroupsEditor::_validate_name(const String &p_name, EditorValidationPanel *p_validation_panel) {
  612. if (p_name.is_empty()) {
  613. p_validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Group can't be empty."), EditorValidationPanel::MSG_ERROR);
  614. } else if (_has_group(p_name)) {
  615. p_validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Group already exists."), EditorValidationPanel::MSG_ERROR);
  616. }
  617. }
  618. void GroupsEditor::_groups_gui_input(Ref<InputEvent> p_event) {
  619. Ref<InputEventKey> key = p_event;
  620. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  621. if (ED_IS_SHORTCUT("groups_editor/delete", p_event)) {
  622. _menu_id_pressed(DELETE_GROUP);
  623. } else if (ED_IS_SHORTCUT("groups_editor/rename", p_event)) {
  624. _menu_id_pressed(RENAME_GROUP);
  625. } else if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
  626. filter->grab_focus();
  627. filter->select_all();
  628. } else {
  629. return;
  630. }
  631. accept_event();
  632. }
  633. }
  634. void GroupsEditor::_bind_methods() {
  635. ClassDB::bind_method("_update_tree", &GroupsEditor::_update_tree);
  636. ClassDB::bind_method("_update_groups", &GroupsEditor::_update_groups);
  637. ClassDB::bind_method("_update_groups_and_tree", &GroupsEditor::_update_groups_and_tree);
  638. ClassDB::bind_method("_add_scene_group", &GroupsEditor::_add_scene_group);
  639. ClassDB::bind_method("_rename_scene_group", &GroupsEditor::_rename_scene_group);
  640. ClassDB::bind_method("_remove_scene_group", &GroupsEditor::_remove_scene_group);
  641. ClassDB::bind_method("_set_group_checked", &GroupsEditor::_set_group_checked);
  642. }
  643. void GroupsEditor::_node_removed(Node *p_node) {
  644. if (scene_root_node == p_node) {
  645. scene_groups_for_caching = scene_groups;
  646. callable_mp(this, &GroupsEditor::_cache_scene_groups).call_deferred(p_node->get_instance_id());
  647. scene_root_node = nullptr;
  648. }
  649. if (scene_root_node && scene_root_node == p_node->get_owner()) {
  650. _queue_update_groups_and_tree();
  651. }
  652. }
  653. GroupsEditor::GroupsEditor() {
  654. node = nullptr;
  655. scene_tree = SceneTree::get_singleton();
  656. ED_SHORTCUT("groups_editor/delete", TTR("Delete"), Key::KEY_DELETE);
  657. ED_SHORTCUT("groups_editor/rename", TTR("Rename"), Key::F2);
  658. ED_SHORTCUT_OVERRIDE("groups_editor/rename", "macos", Key::ENTER);
  659. HBoxContainer *hbc = memnew(HBoxContainer);
  660. add_child(hbc);
  661. add = memnew(Button);
  662. add->set_flat(true);
  663. add->set_tooltip_text(TTR("Add a new group."));
  664. add->connect(SceneStringName(pressed), callable_mp(this, &GroupsEditor::_show_add_group_dialog));
  665. hbc->add_child(add);
  666. filter = memnew(LineEdit);
  667. filter->set_clear_button_enabled(true);
  668. filter->set_placeholder(TTR("Filter Groups"));
  669. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  670. filter->connect("text_changed", callable_mp(this, &GroupsEditor::_update_tree).unbind(1));
  671. hbc->add_child(filter);
  672. tree = memnew(Tree);
  673. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  674. tree->set_hide_root(true);
  675. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  676. tree->set_allow_rmb_select(true);
  677. tree->set_select_mode(Tree::SelectMode::SELECT_SINGLE);
  678. tree->connect("button_clicked", callable_mp(this, &GroupsEditor::_modify_group));
  679. tree->connect("item_mouse_selected", callable_mp(this, &GroupsEditor::_item_mouse_selected));
  680. tree->connect(SceneStringName(gui_input), callable_mp(this, &GroupsEditor::_groups_gui_input));
  681. add_child(tree);
  682. menu = memnew(PopupMenu);
  683. menu->connect("id_pressed", callable_mp(this, &GroupsEditor::_menu_id_pressed));
  684. tree->add_child(menu);
  685. ProjectSettingsEditor::get_singleton()->get_group_settings()->connect("group_changed", callable_mp(this, &GroupsEditor::_update_groups_and_tree));
  686. }
  687. GroupsEditor::~GroupsEditor() {
  688. }