Debian Codegraph @

editor/settings/editor_autoload_settings.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_autoload_settings.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 "editor_autoload_settings.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/core_constants.h"
35 #include "editor/docks/filesystem_dock.h"
36 #include "editor/editor_node.h"
37 #include "editor/editor_string_names.h"
38 #include "editor/editor_undo_redo_manager.h"
39 #include "editor/gui/editor_file_dialog.h"
40 #include "editor/settings/project_settings_editor.h"
41 #include "scene/main/window.h"
42 #include "scene/resources/packed_scene.h"
43
44 #define PREVIEW_LIST_MAX_SIZE 10
45
46 void EditorAutoloadSettings::_notification(int p_what) {
47 switch (p_what) {
48 case NOTIFICATION_ENTER_TREE: {
49 List<String> afn;
50 ResourceLoader::get_recognized_extensions_for_type("Script", &afn);
51 ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn);
52
53 for (const String &E : afn) {
54 file_dialog->add_filter("*." + E);
55 }
56
57 browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));
58 } break;
59
60 case NOTIFICATION_TRANSLATION_CHANGED: {
61 if (!error_message->get_text().is_empty()) {
62 _autoload_text_changed(autoload_add_name->get_text());
63 }
64 } break;
65
66 case NOTIFICATION_THEME_CHANGED: {
67 browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));
68 add_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add")));
69 } break;
70
71 case NOTIFICATION_VISIBILITY_CHANGED: {
72 FileSystemDock *dock = FileSystemDock::get_singleton();
73
74 if (dock != nullptr) {
75 ScriptCreateDialog *dialog = dock->get_script_create_dialog();
76
77 if (dialog != nullptr) {
78 Callable script_created = callable_mp(this, &EditorAutoloadSettings::_script_created);
79
80 if (is_visible_in_tree()) {
81 if (!dialog->is_connected(SNAME("script_created"), script_created)) {
82 dialog->connect("script_created", script_created);
83 }
84 } else {
85 if (dialog->is_connected(SNAME("script_created"), script_created)) {
86 dialog->disconnect("script_created", script_created);
87 }
88 }
89 }
90 }
91 } break;
92 }
93 }
94
95 bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) {
96 if (!p_name.is_valid_unicode_identifier()) {
97 if (r_error) {
98 *r_error = TTR("Must be a valid Unicode identifier.");
99 }
100
101 return false;
102 }
103
104 if (ClassDB::class_exists(p_name)) {
105 if (r_error) {
106 *r_error = TTR("Must not collide with an existing engine class name.");
107 }
108
109 return false;
110 }
111
112 if (ScriptServer::is_global_class(p_name)) {
113 if (r_error) {
114 *r_error = TTR("Must not collide with an existing global script class name.");
115 }
116
117 return false;
118 }
119
120 if (Variant::get_type_by_name(p_name) < Variant::VARIANT_MAX) {
121 if (r_error) {
122 *r_error = TTR("Must not collide with an existing built-in type name.");
123 }
124
125 return false;
126 }
127
128 for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
129 if (CoreConstants::get_global_constant_name(i) == p_name) {
130 if (r_error) {
131 *r_error = TTR("Must not collide with an existing global constant name.");
132 }
133
134 return false;
135 }
136 }
137
138 for (int i = 0; i < ScriptServer::get_language_count(); i++) {
139 for (const String &keyword : ScriptServer::get_language(i)->get_reserved_words()) {
140 if (keyword == p_name) {
141 if (r_error) {
142 *r_error = TTR("Keyword cannot be used as an Autoload name.");
143 }
144
145 return false;
146 }
147 }
148 }
149
150 return true;
151 }
152
153 void EditorAutoloadSettings::_autoload_add() {
154 if (autoload_add_path->get_text().is_empty()) {
155 ScriptCreateDialog *dialog = FileSystemDock::get_singleton()->get_script_create_dialog();
156 String fpath = path;
157 if (!fpath.ends_with("/")) {
158 fpath = fpath.get_base_dir();
159 }
160 dialog->config("Node", fpath.path_join(vformat("%s.gd", autoload_add_name->get_text())), false, false);
161 dialog->popup_centered();
162 } else {
163 if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_text())) {
164 autoload_add_path->set_text("");
165 }
166
167 autoload_add_name->set_text("");
168 add_autoload->set_disabled(true);
169 }
170 }
171
172 void EditorAutoloadSettings::_autoload_selected() {
173 TreeItem *ti = tree->get_selected();
174
175 if (!ti) {
176 return;
177 }
178
179 selected_autoload = "autoload/" + ti->get_text(0);
180 }
181
182 void EditorAutoloadSettings::_autoload_edited() {
183 if (updating_autoload) {
184 return;
185 }
186
187 TreeItem *ti = tree->get_edited();
188 int column = tree->get_edited_column();
189
190 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
191
192 if (column == 0) {
193 String name = ti->get_text(0);
194 String old_name = selected_autoload.get_slicec('/', 1);
195
196 if (name == old_name) {
197 return;
198 }
199
200 String error;
201 if (!_autoload_name_is_valid(name, &error)) {
202 ti->set_text(0, old_name);
203 EditorNode::get_singleton()->show_warning(error);
204 return;
205 }
206
207 if (ProjectSettings::get_singleton()->has_setting("autoload/" + name)) {
208 ti->set_text(0, old_name);
209 EditorNode::get_singleton()->show_warning(vformat(TTR("Autoload '%s' already exists!"), name));
210 return;
211 }
212
213 updating_autoload = true;
214
215 name = "autoload/" + name;
216
217 int order = ProjectSettings::get_singleton()->get_order(selected_autoload);
218 String scr_path = GLOBAL_GET(selected_autoload);
219
220 undo_redo->create_action(TTR("Rename Autoload"));
221
222 undo_redo->add_do_property(ProjectSettings::get_singleton(), name, scr_path);
223 undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, order);
224 undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", selected_autoload);
225
226 undo_redo->add_undo_property(ProjectSettings::get_singleton(), selected_autoload, scr_path);
227 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", selected_autoload, order);
228 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name);
229
230 undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload");
231 undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_autoload");
232
233 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
234 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
235
236 undo_redo->commit_action();
237
238 selected_autoload = name;
239 } else if (column == 2) {
240 updating_autoload = true;
241
242 bool checked = ti->is_checked(2);
243 String base = "autoload/" + ti->get_text(0);
244
245 int order = ProjectSettings::get_singleton()->get_order(base);
246 String scr_path = GLOBAL_GET(base);
247
248 if (scr_path.begins_with("*")) {
249 scr_path = scr_path.substr(1);
250 }
251
252 // Singleton autoloads are represented with a leading "*" in their path.
253 if (checked) {
254 scr_path = "*" + scr_path;
255 }
256
257 undo_redo->create_action(TTR("Toggle Autoload Globals"));
258
259 undo_redo->add_do_property(ProjectSettings::get_singleton(), base, scr_path);
260 undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, GLOBAL_GET(base));
261
262 undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", base, order);
263 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", base, order);
264
265 undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload");
266 undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_autoload");
267
268 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
269 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
270
271 undo_redo->commit_action();
272 }
273
274 updating_autoload = false;
275 }
276
277 void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
278 if (p_mouse_button != MouseButton::LEFT) {
279 return;
280 }
281 TreeItem *ti = Object::cast_to<TreeItem>(p_item);
282
283 String name = "autoload/" + ti->get_text(0);
284
285 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
286
287 switch (p_button) {
288 case BUTTON_OPEN: {
289 _autoload_open(ti->get_text(1));
290 } break;
291 case BUTTON_MOVE_UP:
292 case BUTTON_MOVE_DOWN: {
293 TreeItem *swap = nullptr;
294
295 if (p_button == BUTTON_MOVE_UP) {
296 swap = ti->get_prev();
297 } else {
298 swap = ti->get_next();
299 }
300
301 if (!swap) {
302 return;
303 }
304
305 String swap_name = "autoload/" + swap->get_text(0);
306
307 int order = ProjectSettings::get_singleton()->get_order(name);
308 int swap_order = ProjectSettings::get_singleton()->get_order(swap_name);
309
310 undo_redo->create_action(TTR("Move Autoload"));
311
312 undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, swap_order);
313 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
314
315 undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", swap_name, order);
316 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", swap_name, swap_order);
317
318 undo_redo->add_do_method(this, "update_autoload");
319 undo_redo->add_undo_method(this, "update_autoload");
320
321 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
322 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
323
324 undo_redo->commit_action();
325 } break;
326 case BUTTON_DELETE: {
327 int order = ProjectSettings::get_singleton()->get_order(name);
328
329 undo_redo->create_action(TTR("Remove Autoload"));
330
331 undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
332
333 undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));
334 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
335
336 undo_redo->add_do_method(this, "update_autoload");
337 undo_redo->add_undo_method(this, "update_autoload");
338
339 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
340 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
341
342 undo_redo->commit_action();
343 } break;
344 }
345 }
346
347 void EditorAutoloadSettings::_autoload_activated() {
348 TreeItem *ti = tree->get_selected();
349 if (!ti) {
350 return;
351 }
352 _autoload_open(ti->get_text(1));
353 }
354
355 void EditorAutoloadSettings::_autoload_open(const String &fpath) {
356 EditorNode::get_singleton()->load_scene_or_resource(fpath);
357 ProjectSettingsEditor::get_singleton()->hide();
358 }
359
360 void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) {
361 // Convert the file name to PascalCase, which is the convention for classes in GDScript.
362 const String class_name = p_path.get_file().get_basename().to_pascal_case();
363
364 // If the name collides with a built-in class, prefix the name to make it possible to add without having to edit the name.
365 // The prefix is subjective, but it provides better UX than leaving the Add button disabled :)
366 const String prefix = ClassDB::class_exists(class_name) ? "Global" : "";
367
368 autoload_add_name->set_text(prefix + class_name);
369 add_autoload->set_disabled(false);
370 }
371
372 void EditorAutoloadSettings::_autoload_text_submitted(const String &p_name) {
373 if (!autoload_add_path->get_text().is_empty() && _autoload_name_is_valid(p_name, nullptr)) {
374 _autoload_add();
375 }
376 }
377
378 void EditorAutoloadSettings::_autoload_path_text_changed(const String &p_path) {
379 add_autoload->set_disabled(!_autoload_name_is_valid(autoload_add_name->get_text(), nullptr));
380 }
381
382 void EditorAutoloadSettings::_autoload_text_changed(const String &p_name) {
383 String error_string;
384 bool is_name_valid = _autoload_name_is_valid(p_name, &error_string);
385 add_autoload->set_disabled(!is_name_valid);
386 error_message->set_text(error_string);
387 error_message->set_visible(!autoload_add_name->get_text().is_empty() && !is_name_valid);
388 }
389
390 Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
391 Node *n = nullptr;
392 if (ResourceLoader::get_resource_type(p_path) == "PackedScene") {
393 // Cache the scene reference before loading it (for cyclic references)
394 Ref<PackedScene> scn;
395 scn.instantiate();
396 scn->set_path(ResourceUID::ensure_path(p_path));
397 scn->reload_from_file();
398 ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from UID or path: %s.", p_path));
399
400 if (scn.is_valid()) {
401 n = scn->instantiate();
402 }
403 } else {
404 Ref<Resource> res = ResourceLoader::load(p_path);
405 ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Failed to create an autoload, can't load from UID or path: %s.", p_path));
406
407 Ref<Script> scr = res;
408 if (scr.is_valid()) {
409 ERR_FAIL_COND_V_MSG(!scr->is_valid(), nullptr, vformat("Failed to create an autoload, script '%s' is not compiling.", p_path));
410
411 StringName ibt = scr->get_instance_base_type();
412 bool valid_type = ClassDB::is_parent_class(ibt, "Node");
413 ERR_FAIL_COND_V_MSG(!valid_type, nullptr, vformat("Failed to create an autoload, script '%s' does not inherit from 'Node'.", p_path));
414
415 Object *obj = ClassDB::instantiate(ibt);
416 ERR_FAIL_NULL_V_MSG(obj, nullptr, vformat("Failed to create an autoload, cannot instantiate '%s'.", ibt));
417
418 n = Object::cast_to<Node>(obj);
419 n->set_script(scr);
420 }
421 }
422
423 ERR_FAIL_NULL_V_MSG(n, nullptr, vformat("Failed to create an autoload, path is not pointing to a scene or a script: %s.", p_path));
424
425 return n;
426 }
427
428 void EditorAutoloadSettings::init_autoloads() {
429 for (AutoloadInfo &info : autoload_cache) {
430 info.node = _create_autoload(ResourceUID::ensure_path(info.path));
431
432 if (info.node) {
433 Ref<Script> scr = info.node->get_script();
434 info.in_editor = scr.is_valid() && scr->is_tool();
435 info.node->set_name(info.name);
436 }
437
438 if (info.is_singleton) {
439 for (int i = 0; i < ScriptServer::get_language_count(); i++) {
440 ScriptServer::get_language(i)->add_named_global_constant(info.name, info.node);
441 }
442 }
443
444 if (!info.is_singleton && !info.in_editor && info.node != nullptr) {
445 memdelete(info.node);
446 info.node = nullptr;
447 }
448 }
449
450 for (const AutoloadInfo &info : autoload_cache) {
451 if (info.node && info.in_editor) {
452 // It's important to add the node without deferring because code in plugins or tool scripts
453 // could use the autoload node when they are enabled.
454 get_tree()->get_root()->add_child(info.node);
455 }
456 }
457 }
458
459 void EditorAutoloadSettings::update_autoload() {
460 if (updating_autoload) {
461 return;
462 }
463
464 updating_autoload = true;
465
466 HashMap<String, AutoloadInfo> to_remove;
467 List<AutoloadInfo *> to_add;
468
469 for (const AutoloadInfo &info : autoload_cache) {
470 to_remove.insert(info.name, info);
471 }
472
473 autoload_cache.clear();
474
475 tree->clear();
476 TreeItem *root = tree->create_item();
477
478 List<PropertyInfo> props;
479 ProjectSettings::get_singleton()->get_property_list(&props);
480
481 for (const PropertyInfo &pi : props) {
482 if (!pi.name.begins_with("autoload/")) {
483 continue;
484 }
485
486 String name = pi.name.get_slicec('/', 1);
487 String scr_path = GLOBAL_GET(pi.name);
488
489 if (name.is_empty()) {
490 continue;
491 }
492
493 AutoloadInfo info;
494 info.is_singleton = scr_path.begins_with("*");
495
496 if (info.is_singleton) {
497 scr_path = scr_path.substr(1);
498 }
499
500 info.name = name;
501 info.path = ResourceUID::get_singleton()->path_to_uid(scr_path);
502 info.order = ProjectSettings::get_singleton()->get_order(pi.name);
503
504 bool need_to_add = true;
505 if (to_remove.has(name)) {
506 AutoloadInfo &old_info = to_remove[name];
507 if (old_info.path == info.path) {
508 // Still the same resource, check status
509 info.node = old_info.node;
510 if (info.node) {
511 Ref<Script> scr = info.node->get_script();
512 info.in_editor = scr.is_valid() && scr->is_tool();
513 if (info.is_singleton == old_info.is_singleton && info.in_editor == old_info.in_editor) {
514 to_remove.erase(name);
515 need_to_add = false;
516 } else {
517 info.node = nullptr;
518 }
519 }
520 }
521 }
522
523 autoload_cache.push_back(info);
524
525 if (need_to_add) {
526 to_add.push_back(&(autoload_cache.back()->get()));
527 }
528
529 TreeItem *item = tree->create_item(root);
530 item->set_text(0, name);
531 item->set_editable(0, true);
532
533 item->set_text(1, ResourceUID::ensure_path(scr_path));
534 item->set_selectable(1, true);
535
536 item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
537 item->set_editable(2, true);
538 item->set_text(2, TTRC("Enable"));
539 item->set_checked(2, info.is_singleton);
540 item->add_button(3, get_editor_theme_icon(SNAME("Load")), BUTTON_OPEN);
541 item->add_button(3, get_editor_theme_icon(SNAME("MoveUp")), BUTTON_MOVE_UP);
542 item->add_button(3, get_editor_theme_icon(SNAME("MoveDown")), BUTTON_MOVE_DOWN);
543 item->add_button(3, get_editor_theme_icon(SNAME("Remove")), BUTTON_DELETE);
544 item->set_selectable(3, false);
545 }
546
547 // Remove deleted/changed autoloads
548 for (KeyValue<String, AutoloadInfo> &E : to_remove) {
549 AutoloadInfo &info = E.value;
550 if (info.is_singleton) {
551 for (int i = 0; i < ScriptServer::get_language_count(); i++) {
552 ScriptServer::get_language(i)->remove_named_global_constant(info.name);
553 }
554 }
555 if (info.in_editor) {
556 ERR_CONTINUE(!info.node);
557 callable_mp((Node *)get_tree()->get_root(), &Node::remove_child).call_deferred(info.node);
558 }
559
560 if (info.node) {
561 info.node->queue_free();
562 info.node = nullptr;
563 }
564 }
565
566 // Load new/changed autoloads
567 List<Node *> nodes_to_add;
568 for (AutoloadInfo *info : to_add) {
569 info->node = _create_autoload(info->path);
570
571 ERR_CONTINUE(!info->node);
572 info->node->set_name(info->name);
573
574 Ref<Script> scr = info->node->get_script();
575 info->in_editor = scr.is_valid() && scr->is_tool();
576
577 if (info->in_editor) {
578 //defer so references are all valid on _ready()
579 nodes_to_add.push_back(info->node);
580 }
581
582 if (info->is_singleton) {
583 for (int i = 0; i < ScriptServer::get_language_count(); i++) {
584 ScriptServer::get_language(i)->add_named_global_constant(info->name, info->node);
585 }
586 }
587
588 if (!info->in_editor && !info->is_singleton) {
589 // No reason to keep this node
590 memdelete(info->node);
591 info->node = nullptr;
592 }
593 }
594
595 for (Node *E : nodes_to_add) {
596 get_tree()->get_root()->add_child(E);
597 }
598
599 updating_autoload = false;
600 }
601
602 void EditorAutoloadSettings::_script_created(Ref<Script> p_script) {
603 FileSystemDock::get_singleton()->get_script_create_dialog()->hide();
604 path = p_script->get_path().get_base_dir();
605 autoload_add_path->set_text(p_script->get_path());
606 _autoload_add();
607 }
608
609 LineEdit *EditorAutoloadSettings::get_path_box() const {
610 return autoload_add_path;
611 }
612
613 Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) {
614 if (autoload_cache.size() <= 1) {
615 return false;
616 }
617
618 PackedStringArray autoloads;
619
620 TreeItem *next = tree->get_next_selected(nullptr);
621
622 while (next) {
623 autoloads.push_back(next->get_text(0));
624 next = tree->get_next_selected(next);
625 }
626
627 if (autoloads.is_empty() || autoloads.size() == autoload_cache.size()) {
628 return Variant();
629 }
630
631 VBoxContainer *preview = memnew(VBoxContainer);
632
633 int max_size = MIN(PREVIEW_LIST_MAX_SIZE, autoloads.size());
634
635 for (int i = 0; i < max_size; i++) {
636 Label *label = memnew(Label(autoloads[i]));
637 label->set_self_modulate(Color(1, 1, 1, Math::lerp(1, 0, float(i) / PREVIEW_LIST_MAX_SIZE)));
638 label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
639
640 preview->add_child(label);
641 }
642
643 tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
644 tree->set_drag_preview(preview);
645
646 Dictionary drop_data;
647 drop_data["type"] = "autoload";
648 drop_data["autoloads"] = autoloads;
649
650 return drop_data;
651 }
652
653 bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const {
654 if (updating_autoload) {
655 return false;
656 }
657
658 Dictionary drop_data = p_data;
659
660 if (!drop_data.has("type")) {
661 return false;
662 }
663
664 if (drop_data.has("type")) {
665 TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_selected() : tree->get_item_at_position(p_point);
666
667 if (!ti) {
668 return false;
669 }
670
671 int section = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_drop_section_at_position(tree->get_item_rect(ti).position) : tree->get_drop_section_at_position(p_point);
672
673 return section >= -1;
674 }
675
676 return false;
677 }
678
679 void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) {
680 TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_selected() : tree->get_item_at_position(p_point);
681
682 if (!ti) {
683 return;
684 }
685
686 int section = (p_point == Vector2(Math::INF, Math::INF)) ? tree->get_drop_section_at_position(tree->get_item_rect(ti).position) : tree->get_drop_section_at_position(p_point);
687
688 if (section < -1) {
689 return;
690 }
691
692 String name;
693 bool move_to_back = false;
694
695 if (section < 0) {
696 name = ti->get_text(0);
697 } else if (ti->get_next()) {
698 name = ti->get_next()->get_text(0);
699 } else {
700 name = ti->get_text(0);
701 move_to_back = true;
702 }
703
704 int order = ProjectSettings::get_singleton()->get_order("autoload/" + name);
705
706 AutoloadInfo aux;
707 List<AutoloadInfo>::Element *E = nullptr;
708
709 if (!move_to_back) {
710 aux.order = order;
711 E = autoload_cache.find(aux);
712 }
713
714 Dictionary drop_data = p_data;
715 PackedStringArray autoloads = drop_data["autoloads"];
716
717 // Store the initial order of the autoloads for comparison.
718 Vector<int> initial_orders;
719 initial_orders.resize(autoload_cache.size());
720 int idx = 0;
721 for (const AutoloadInfo &F : autoload_cache) {
722 initial_orders.write[idx++] = F.order;
723 }
724
725 // Perform the drag-and-drop operation.
726 Vector<int> orders;
727 orders.resize(autoload_cache.size());
728
729 for (int i = 0; i < autoloads.size(); i++) {
730 aux.order = ProjectSettings::get_singleton()->get_order("autoload/" + autoloads[i]);
731
732 List<AutoloadInfo>::Element *I = autoload_cache.find(aux);
733
734 if (move_to_back) {
735 autoload_cache.move_to_back(I);
736 } else if (E != I) {
737 autoload_cache.move_before(I, E);
738 } else if (E->next()) {
739 E = E->next();
740 } else {
741 break;
742 }
743 }
744
745 idx = 0;
746 for (const AutoloadInfo &F : autoload_cache) {
747 orders.write[idx++] = F.order;
748 }
749
750 // If the order didn't change, we shouldn't create undo/redo actions.
751 if (orders == initial_orders) {
752 return;
753 }
754
755 orders.sort();
756
757 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
758
759 undo_redo->create_action(TTR("Rearrange Autoloads"));
760
761 idx = 0;
762 for (const AutoloadInfo &F : autoload_cache) {
763 undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F.name, orders[idx++]);
764 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F.name, F.order);
765 }
766
767 orders.clear();
768
769 undo_redo->add_do_method(this, "update_autoload");
770 undo_redo->add_undo_method(this, "update_autoload");
771
772 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
773 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
774
775 undo_redo->commit_action();
776 }
777
778 bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_path) {
779 String name = p_name;
780
781 String error;
782 if (!_autoload_name_is_valid(name, &error)) {
783 EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + vformat(TTR("%s is an invalid name."), p_name) + " " + error);
784 return false;
785 }
786
787 if (!FileAccess::exists(p_path)) {
788 EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + vformat(TTR("%s is an invalid path. File does not exist."), p_path));
789 return false;
790 }
791
792 if (!p_path.begins_with("res://")) {
793 EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + vformat(TTR("%s is an invalid path. Not in resource path (res://)."), p_path));
794 return false;
795 }
796
797 name = "autoload/" + name;
798
799 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
800
801 undo_redo->create_action(TTR("Add Autoload"));
802 // Singleton autoloads are represented with a leading "*" in their path.
803 undo_redo->add_do_property(ProjectSettings::get_singleton(), name, "*" + ResourceUID::get_singleton()->path_to_uid(p_path));
804
805 if (ProjectSettings::get_singleton()->has_setting(name)) {
806 undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));
807 } else {
808 undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, Variant());
809 }
810
811 undo_redo->add_do_method(this, "update_autoload");
812 undo_redo->add_undo_method(this, "update_autoload");
813
814 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
815 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
816
817 undo_redo->commit_action();
818
819 return true;
820 }
821
822 void EditorAutoloadSettings::autoload_remove(const String &p_name) {
823 String name = "autoload/" + p_name;
824
825 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
826
827 int order = ProjectSettings::get_singleton()->get_order(name);
828
829 undo_redo->create_action(TTR("Remove Autoload"));
830
831 undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
832
833 undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));
834 undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
835
836 undo_redo->add_do_method(this, "update_autoload");
837 undo_redo->add_undo_method(this, "update_autoload");
838
839 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
840 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
841
842 undo_redo->commit_action();
843 }
844
845 void EditorAutoloadSettings::_bind_methods() {
846 ClassDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);
847 ClassDB::bind_method("autoload_add", &EditorAutoloadSettings::autoload_add);
848 ClassDB::bind_method("autoload_remove", &EditorAutoloadSettings::autoload_remove);
849
850 ADD_SIGNAL(MethodInfo("autoload_changed"));
851 }
852
853 EditorAutoloadSettings::EditorAutoloadSettings() {
854 ProjectSettings::get_singleton()->add_hidden_prefix("autoload/");
855
856 // Make first cache
857 List<PropertyInfo> props;
858 ProjectSettings::get_singleton()->get_property_list(&props);
859 for (const PropertyInfo &pi : props) {
860 if (!pi.name.begins_with("autoload/")) {
861 continue;
862 }
863
864 String name = pi.name.get_slicec('/', 1);
865 String scr_path = GLOBAL_GET(pi.name);
866
867 if (name.is_empty()) {
868 continue;
869 }
870
871 AutoloadInfo info;
872 info.is_singleton = scr_path.begins_with("*");
873
874 if (info.is_singleton) {
875 scr_path = scr_path.substr(1);
876 }
877
878 info.name = name;
879 info.path = ResourceUID::get_singleton()->path_to_uid(scr_path);
880 info.order = ProjectSettings::get_singleton()->get_order(pi.name);
881
882 if (info.is_singleton) {
883 // Make sure name references work before parsing scripts
884 for (int i = 0; i < ScriptServer::get_language_count(); i++) {
885 ScriptServer::get_language(i)->add_named_global_constant(info.name, Variant());
886 }
887 }
888
889 autoload_cache.push_back(info);
890 }
891
892 HBoxContainer *hbc = memnew(HBoxContainer);
893 add_child(hbc);
894
895 error_message = memnew(Label);
896 error_message->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
897 error_message->set_focus_mode(FOCUS_ACCESSIBILITY);
898 error_message->hide();
899 error_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
900 error_message->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));
901 add_child(error_message);
902
903 Label *l = memnew(Label);
904 l->set_text(TTRC("Path:"));
905 hbc->add_child(l);
906
907 autoload_add_path = memnew(LineEdit);
908 hbc->add_child(autoload_add_path);
909 autoload_add_path->set_accessibility_name(TTRC("Autoload Path"));
910 autoload_add_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
911 autoload_add_path->set_clear_button_enabled(true);
912 autoload_add_path->set_placeholder(TTRC("Set path or press \"Add\" to create a script."));
913 autoload_add_path->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_path_text_changed));
914
915 browse_button = memnew(Button);
916 hbc->add_child(browse_button);
917 browse_button->set_accessibility_name(TTRC("Select Autoload Path"));
918 browse_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_browse_autoload_add_path));
919
920 file_dialog = memnew(EditorFileDialog);
921 hbc->add_child(file_dialog);
922 file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
923 file_dialog->connect("dir_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
924 file_dialog->connect("files_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
925
926 hbc->set_h_size_flags(SIZE_EXPAND_FILL);
927 file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
928 file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_file_callback));
929
930 l = memnew(Label);
931 l->set_text(TTRC("Node Name:"));
932 hbc->add_child(l);
933
934 autoload_add_name = memnew(LineEdit);
935 autoload_add_name->set_accessibility_name(TTRC("Node Name:"));
936 autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);
937 autoload_add_name->connect(SceneStringName(text_submitted), callable_mp(this, &EditorAutoloadSettings::_autoload_text_submitted));
938 autoload_add_name->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_text_changed));
939 hbc->add_child(autoload_add_name);
940
941 add_autoload = memnew(Button);
942 add_autoload->set_text(TTRC("Add"));
943 add_autoload->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_autoload_add));
944 // The button will be enabled once a valid name is entered (either automatically or manually).
945 add_autoload->set_disabled(true);
946 hbc->add_child(add_autoload);
947
948 MarginContainer *mc = memnew(MarginContainer);
949 mc->set_v_size_flags(SIZE_EXPAND_FILL);
950 mc->set_theme_type_variation("NoBorderHorizontalBottomWide");
951 add_child(mc);
952
953 tree = memnew(Tree);
954 tree->set_accessibility_name(TTRC("Autoloads"));
955 tree->set_hide_root(true);
956 tree->set_select_mode(Tree::SELECT_MULTI);
957 tree->set_allow_reselect(true);
958
959 SET_DRAG_FORWARDING_GCD(tree, EditorAutoloadSettings);
960
961 tree->set_theme_type_variation("TreeTable");
962 tree->set_hide_folding(true);
963 tree->set_columns(4);
964 tree->set_column_titles_visible(true);
965
966 tree->set_column_title(0, TTRC("Name"));
967 tree->set_column_title_alignment(0, HORIZONTAL_ALIGNMENT_LEFT);
968 tree->set_column_expand(0, true);
969 tree->set_column_expand_ratio(0, 1);
970
971 tree->set_column_title(1, TTRC("Path"));
972 tree->set_column_title_alignment(1, HORIZONTAL_ALIGNMENT_LEFT);
973 tree->set_column_expand(1, true);
974 tree->set_column_clip_content(1, true);
975 tree->set_column_expand_ratio(1, 2);
976
977 tree->set_column_title(2, TTRC("Global Variable"));
978 tree->set_column_expand(2, false);
979
980 tree->set_column_expand(3, false);
981
982 tree->connect("cell_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_selected));
983 tree->connect("item_edited", callable_mp(this, &EditorAutoloadSettings::_autoload_edited));
984 tree->connect("button_clicked", callable_mp(this, &EditorAutoloadSettings::_autoload_button_pressed));
985 tree->connect("item_activated", callable_mp(this, &EditorAutoloadSettings::_autoload_activated));
986
987 mc->add_child(tree, true);
988 }
989
990 EditorAutoloadSettings::~EditorAutoloadSettings() {
991 for (const AutoloadInfo &info : autoload_cache) {
992 if (info.node && !info.in_editor) {
993 memdelete(info.node);
994 }
995 }
996 }
997
998 void EditorAutoloadSettings::_set_autoload_add_path(const String &p_text) {
999 autoload_add_path->set_text(p_text);
1000 autoload_add_path->emit_signal(SceneStringName(text_submitted), p_text);
1001 }
1002
1003 void EditorAutoloadSettings::_browse_autoload_add_path() {
1004 file_dialog->popup_file_dialog();
1005 }