group_settings_editor.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /**************************************************************************/
  2. /* group_settings_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 "group_settings_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/filesystem_dock.h"
  35. #include "editor/gui/editor_validation_panel.h"
  36. #include "editor/scene_tree_dock.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/gui/line_edit.h"
  39. #include "scene/resources/packed_scene.h"
  40. void GroupSettingsEditor::_notification(int p_what) {
  41. switch (p_what) {
  42. case NOTIFICATION_ENTER_TREE: {
  43. update_groups();
  44. } break;
  45. case NOTIFICATION_THEME_CHANGED: {
  46. add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  47. } break;
  48. }
  49. }
  50. void GroupSettingsEditor::_item_edited() {
  51. if (updating_groups) {
  52. return;
  53. }
  54. TreeItem *ti = tree->get_edited();
  55. int column = tree->get_edited_column();
  56. if (!ti) {
  57. return;
  58. }
  59. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  60. if (column == 1) {
  61. // Description Edited.
  62. String name = ti->get_text(0);
  63. String new_description = ti->get_text(1);
  64. String old_description = ti->get_meta("__description");
  65. if (new_description == old_description) {
  66. return;
  67. }
  68. name = GLOBAL_GROUP_PREFIX + name;
  69. undo_redo->create_action(TTR("Set Group Description"));
  70. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, new_description);
  71. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, old_description);
  72. undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_groups");
  73. undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_groups");
  74. undo_redo->add_do_method(this, "emit_signal", group_changed);
  75. undo_redo->add_undo_method(this, "emit_signal", group_changed);
  76. undo_redo->commit_action();
  77. }
  78. }
  79. void GroupSettingsEditor::_item_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  80. if (p_button != MouseButton::LEFT) {
  81. return;
  82. }
  83. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  84. if (!ti) {
  85. return;
  86. }
  87. ti->select(0);
  88. _show_remove_dialog();
  89. }
  90. String GroupSettingsEditor::_check_new_group_name(const String &p_name) {
  91. if (p_name.is_empty()) {
  92. return TTR("Invalid group name. It cannot be empty.");
  93. }
  94. if (ProjectSettings::get_singleton()->has_global_group(p_name)) {
  95. return vformat(TTR("A group with the name '%s' already exists."), p_name);
  96. }
  97. return "";
  98. }
  99. void GroupSettingsEditor::_check_rename() {
  100. String new_name = rename_group->get_text().strip_edges();
  101. String old_name = rename_group_dialog->get_meta("__name");
  102. if (new_name == old_name) {
  103. return;
  104. }
  105. if (new_name.is_empty()) {
  106. rename_validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Group can't be empty."), EditorValidationPanel::MSG_ERROR);
  107. } else if (ProjectSettings::get_singleton()->has_global_group(new_name)) {
  108. rename_validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Group already exists."), EditorValidationPanel::MSG_ERROR);
  109. }
  110. }
  111. void GroupSettingsEditor::_bind_methods() {
  112. ClassDB::bind_method(D_METHOD("remove_references"), &GroupSettingsEditor::remove_references);
  113. ClassDB::bind_method(D_METHOD("rename_references"), &GroupSettingsEditor::rename_references);
  114. ClassDB::bind_method(D_METHOD("update_groups"), &GroupSettingsEditor::update_groups);
  115. ADD_SIGNAL(MethodInfo("group_changed"));
  116. }
  117. void GroupSettingsEditor::_add_group(const String &p_name, const String &p_description) {
  118. String name = p_name.strip_edges();
  119. String error = _check_new_group_name(name);
  120. if (!error.is_empty()) {
  121. show_message(error);
  122. return;
  123. }
  124. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  125. undo_redo->create_action(TTR("Add Group"));
  126. name = GLOBAL_GROUP_PREFIX + name;
  127. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, p_description);
  128. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, Variant());
  129. undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_groups");
  130. undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_groups");
  131. undo_redo->add_do_method(this, "emit_signal", group_changed);
  132. undo_redo->add_undo_method(this, "emit_signal", group_changed);
  133. undo_redo->commit_action();
  134. group_name->clear();
  135. group_description->clear();
  136. }
  137. void GroupSettingsEditor::_add_group() {
  138. _add_group(group_name->get_text(), group_description->get_text());
  139. }
  140. void GroupSettingsEditor::_text_submitted(const String &p_text) {
  141. if (!add_button->is_disabled()) {
  142. _add_group();
  143. }
  144. }
  145. void GroupSettingsEditor::_group_name_text_changed(const String &p_name) {
  146. String error = _check_new_group_name(p_name.strip_edges());
  147. add_button->set_tooltip_text(error);
  148. add_button->set_disabled(!error.is_empty());
  149. }
  150. void GroupSettingsEditor::_modify_references(const StringName &p_name, const StringName &p_new_name, bool p_is_rename) {
  151. HashSet<String> scenes;
  152. HashMap<StringName, HashSet<StringName>> scene_groups_cache = ProjectSettings::get_singleton()->get_scene_groups_cache();
  153. for (const KeyValue<StringName, HashSet<StringName>> &E : scene_groups_cache) {
  154. if (E.value.has(p_name)) {
  155. scenes.insert(E.key);
  156. }
  157. }
  158. int steps = scenes.size();
  159. Vector<EditorData::EditedScene> edited_scenes = EditorNode::get_editor_data().get_edited_scenes();
  160. for (const EditorData::EditedScene &es : edited_scenes) {
  161. if (!es.root) {
  162. continue;
  163. }
  164. if (es.path.is_empty()) {
  165. ++steps;
  166. } else if (!scenes.has(es.path)) {
  167. ++steps;
  168. }
  169. }
  170. String progress_task = p_is_rename ? "rename_reference" : "remove_references";
  171. String progress_label = p_is_rename ? TTR("Renaming Group References") : TTR("Removing Group References");
  172. EditorProgress progress(progress_task, progress_label, steps);
  173. int step = 0;
  174. // Update opened scenes.
  175. HashSet<String> edited_scenes_path;
  176. for (const EditorData::EditedScene &es : edited_scenes) {
  177. if (!es.root) {
  178. continue;
  179. }
  180. progress.step(es.path, step++);
  181. bool edited = p_is_rename ? rename_node_references(es.root, p_name, p_new_name) : remove_node_references(es.root, p_name);
  182. if (!es.path.is_empty()) {
  183. scenes.erase(es.path);
  184. if (edited) {
  185. edited_scenes_path.insert(es.path);
  186. }
  187. }
  188. }
  189. if (!edited_scenes_path.is_empty()) {
  190. EditorNode::get_singleton()->save_scene_list(edited_scenes_path);
  191. SceneTreeDock::get_singleton()->get_tree_editor()->update_tree();
  192. }
  193. for (const String &E : scenes) {
  194. Ref<PackedScene> packed_scene = ResourceLoader::load(E);
  195. progress.step(E, step++);
  196. ERR_CONTINUE(packed_scene.is_null());
  197. if (p_is_rename) {
  198. if (packed_scene->get_state()->rename_group_references(p_name, p_new_name)) {
  199. ResourceSaver::save(packed_scene, E);
  200. }
  201. } else {
  202. if (packed_scene->get_state()->remove_group_references(p_name)) {
  203. ResourceSaver::save(packed_scene, E);
  204. }
  205. }
  206. }
  207. }
  208. void GroupSettingsEditor::remove_references(const StringName &p_name) {
  209. _modify_references(p_name, StringName(), false);
  210. }
  211. void GroupSettingsEditor::rename_references(const StringName &p_old_name, const StringName &p_new_name) {
  212. _modify_references(p_old_name, p_new_name, true);
  213. }
  214. bool GroupSettingsEditor::remove_node_references(Node *p_node, const StringName &p_name) {
  215. bool edited = false;
  216. if (p_node->is_in_group(p_name)) {
  217. p_node->remove_from_group(p_name);
  218. edited = true;
  219. }
  220. for (int i = 0; i < p_node->get_child_count(); i++) {
  221. edited |= remove_node_references(p_node->get_child(i), p_name);
  222. }
  223. return edited;
  224. }
  225. bool GroupSettingsEditor::rename_node_references(Node *p_node, const StringName &p_old_name, const StringName &p_new_name) {
  226. bool edited = false;
  227. if (p_node->is_in_group(p_old_name)) {
  228. p_node->remove_from_group(p_old_name);
  229. p_node->add_to_group(p_new_name, true);
  230. edited = true;
  231. }
  232. for (int i = 0; i < p_node->get_child_count(); i++) {
  233. edited |= rename_node_references(p_node->get_child(i), p_old_name, p_new_name);
  234. }
  235. return edited;
  236. }
  237. void GroupSettingsEditor::update_groups() {
  238. if (updating_groups) {
  239. return;
  240. }
  241. updating_groups = true;
  242. groups_cache = ProjectSettings::get_singleton()->get_global_groups_list();
  243. tree->clear();
  244. TreeItem *root = tree->create_item();
  245. List<StringName> keys;
  246. for (const KeyValue<StringName, String> &E : groups_cache) {
  247. keys.push_back(E.key);
  248. }
  249. keys.sort_custom<NoCaseComparator>();
  250. for (const StringName &E : keys) {
  251. TreeItem *item = tree->create_item(root);
  252. item->set_meta("__name", E);
  253. item->set_meta("__description", groups_cache[E]);
  254. item->set_text(0, E);
  255. item->set_editable(0, false);
  256. item->set_text(1, groups_cache[E]);
  257. item->set_editable(1, true);
  258. item->add_button(2, get_editor_theme_icon(SNAME("Remove")));
  259. item->set_selectable(2, false);
  260. }
  261. updating_groups = false;
  262. }
  263. void GroupSettingsEditor::connect_filesystem_dock_signals(FileSystemDock *p_fs_dock) {
  264. p_fs_dock->connect("files_moved", callable_mp(ProjectSettings::get_singleton(), &ProjectSettings::remove_scene_groups_cache).unbind(1));
  265. p_fs_dock->connect("file_removed", callable_mp(ProjectSettings::get_singleton(), &ProjectSettings::remove_scene_groups_cache));
  266. }
  267. void GroupSettingsEditor::_confirm_rename() {
  268. TreeItem *ti = tree->get_selected();
  269. if (!ti) {
  270. return;
  271. }
  272. String old_name = ti->get_meta("__name");
  273. String new_name = rename_group->get_text().strip_edges();
  274. if (old_name == new_name) {
  275. return;
  276. }
  277. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  278. undo_redo->create_action(TTR("Rename Group"));
  279. String property_new_name = GLOBAL_GROUP_PREFIX + new_name;
  280. String property_old_name = GLOBAL_GROUP_PREFIX + old_name;
  281. String description = ti->get_meta("__description");
  282. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_new_name, description);
  283. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_new_name, Variant());
  284. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_old_name, Variant());
  285. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_old_name, description);
  286. if (rename_check_box->is_pressed()) {
  287. undo_redo->add_do_method(this, "rename_references", old_name, new_name);
  288. undo_redo->add_undo_method(this, "rename_references", new_name, old_name);
  289. }
  290. undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_groups");
  291. undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_groups");
  292. undo_redo->add_do_method(this, "emit_signal", group_changed);
  293. undo_redo->add_undo_method(this, "emit_signal", group_changed);
  294. undo_redo->commit_action();
  295. }
  296. void GroupSettingsEditor::_confirm_delete() {
  297. TreeItem *ti = tree->get_selected();
  298. if (!ti) {
  299. return;
  300. }
  301. String name = ti->get_text(0);
  302. String description = groups_cache[name];
  303. String property_name = GLOBAL_GROUP_PREFIX + name;
  304. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  305. undo_redo->create_action(TTR("Remove Group"));
  306. undo_redo->add_do_property(ProjectSettings::get_singleton(), property_name, Variant());
  307. undo_redo->add_undo_property(ProjectSettings::get_singleton(), property_name, description);
  308. if (remove_check_box->is_pressed()) {
  309. undo_redo->add_do_method(this, "remove_references", name);
  310. }
  311. undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_groups");
  312. undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_groups");
  313. undo_redo->add_do_method(this, "emit_signal", group_changed);
  314. undo_redo->add_undo_method(this, "emit_signal", group_changed);
  315. undo_redo->commit_action();
  316. }
  317. void GroupSettingsEditor::show_message(const String &p_message) {
  318. message->set_text(p_message);
  319. message->popup_centered();
  320. }
  321. void GroupSettingsEditor::_show_remove_dialog() {
  322. if (!remove_dialog) {
  323. remove_dialog = memnew(ConfirmationDialog);
  324. remove_dialog->connect(SceneStringName(confirmed), callable_mp(this, &GroupSettingsEditor::_confirm_delete));
  325. VBoxContainer *vbox = memnew(VBoxContainer);
  326. remove_label = memnew(Label);
  327. remove_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  328. vbox->add_child(remove_label);
  329. remove_check_box = memnew(CheckBox);
  330. remove_check_box->set_text(TTRC("Delete references from all scenes"));
  331. vbox->add_child(remove_check_box);
  332. remove_dialog->add_child(vbox);
  333. add_child(remove_dialog);
  334. }
  335. TreeItem *ti = tree->get_selected();
  336. if (!ti) {
  337. return;
  338. }
  339. remove_check_box->set_pressed(false);
  340. remove_label->set_text(vformat(TTR("Delete group \"%s\"?"), ti->get_text(0)));
  341. remove_dialog->reset_size();
  342. remove_dialog->popup_centered();
  343. }
  344. void GroupSettingsEditor::_show_rename_dialog() {
  345. if (!rename_group_dialog) {
  346. rename_group_dialog = memnew(ConfirmationDialog);
  347. rename_group_dialog->set_title(TTRC("Rename Group"));
  348. rename_group_dialog->connect(SceneStringName(confirmed), callable_mp(this, &GroupSettingsEditor::_confirm_rename));
  349. VBoxContainer *vbc = memnew(VBoxContainer);
  350. rename_group_dialog->add_child(vbc);
  351. HBoxContainer *hbc = memnew(HBoxContainer);
  352. hbc->add_child(memnew(Label(TTRC("Name:"))));
  353. rename_group = memnew(LineEdit);
  354. rename_group->set_custom_minimum_size(Size2(300 * EDSCALE, 1));
  355. hbc->add_child(rename_group);
  356. vbc->add_child(hbc);
  357. rename_group_dialog->register_text_enter(rename_group);
  358. rename_validation_panel = memnew(EditorValidationPanel);
  359. rename_validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Group name is valid."));
  360. rename_validation_panel->set_update_callback(callable_mp(this, &GroupSettingsEditor::_check_rename));
  361. rename_validation_panel->set_accept_button(rename_group_dialog->get_ok_button());
  362. rename_group->connect(SceneStringName(text_changed), callable_mp(rename_validation_panel, &EditorValidationPanel::update).unbind(1));
  363. vbc->add_child(rename_validation_panel);
  364. rename_check_box = memnew(CheckBox);
  365. rename_check_box->set_text(TTRC("Rename references in all scenes"));
  366. vbc->add_child(rename_check_box);
  367. add_child(rename_group_dialog);
  368. }
  369. TreeItem *ti = tree->get_selected();
  370. if (!ti) {
  371. return;
  372. }
  373. rename_check_box->set_pressed(false);
  374. String name = ti->get_meta("__name");
  375. rename_group->set_text(name);
  376. rename_group_dialog->set_meta("__name", name);
  377. rename_validation_panel->update();
  378. rename_group_dialog->reset_size();
  379. rename_group_dialog->popup_centered();
  380. rename_group->select_all();
  381. rename_group->grab_focus();
  382. }
  383. LineEdit *GroupSettingsEditor::get_name_box() const {
  384. return group_name;
  385. }
  386. GroupSettingsEditor::GroupSettingsEditor() {
  387. ProjectSettings::get_singleton()->add_hidden_prefix("global_group/");
  388. HBoxContainer *hbc = memnew(HBoxContainer);
  389. add_child(hbc);
  390. Label *l = memnew(Label);
  391. l->set_text(TTRC("Name:"));
  392. hbc->add_child(l);
  393. group_name = memnew(LineEdit);
  394. group_name->set_h_size_flags(SIZE_EXPAND_FILL);
  395. group_name->set_clear_button_enabled(true);
  396. group_name->set_accessibility_name(TTRC("Group Name"));
  397. group_name->connect(SceneStringName(text_changed), callable_mp(this, &GroupSettingsEditor::_group_name_text_changed));
  398. group_name->connect(SceneStringName(text_submitted), callable_mp(this, &GroupSettingsEditor::_text_submitted));
  399. hbc->add_child(group_name);
  400. l = memnew(Label);
  401. l->set_text(TTRC("Description:"));
  402. hbc->add_child(l);
  403. group_description = memnew(LineEdit);
  404. group_description->set_clear_button_enabled(true);
  405. group_description->set_accessibility_name(TTRC("Group Description"));
  406. group_description->set_h_size_flags(SIZE_EXPAND_FILL);
  407. group_description->connect(SceneStringName(text_submitted), callable_mp(this, &GroupSettingsEditor::_text_submitted));
  408. hbc->add_child(group_description);
  409. add_button = memnew(Button);
  410. add_button->set_text(TTRC("Add"));
  411. add_button->set_disabled(true);
  412. add_button->connect(SceneStringName(pressed), callable_mp(this, &GroupSettingsEditor::_add_group));
  413. hbc->add_child(add_button);
  414. tree = memnew(Tree);
  415. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  416. tree->set_hide_root(true);
  417. tree->set_select_mode(Tree::SELECT_SINGLE);
  418. tree->set_allow_reselect(true);
  419. tree->set_columns(3);
  420. tree->set_column_titles_visible(true);
  421. tree->set_column_title(0, TTRC("Name"));
  422. tree->set_column_title(1, TTRC("Description"));
  423. tree->set_column_expand(2, false);
  424. tree->connect("item_edited", callable_mp(this, &GroupSettingsEditor::_item_edited));
  425. tree->connect("item_activated", callable_mp(this, &GroupSettingsEditor::_show_rename_dialog));
  426. tree->connect("button_clicked", callable_mp(this, &GroupSettingsEditor::_item_button_pressed));
  427. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  428. add_child(tree, true);
  429. message = memnew(AcceptDialog);
  430. add_child(message);
  431. }