group_settings_editor.cpp 18 KB

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