Debian Codegraph @

editor/scene/group_settings_editor.cpp

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