Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/create_dialog.cpp
20871 views
1
/**************************************************************************/
2
/* create_dialog.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 "create_dialog.h"
32
33
#include "core/io/resource_loader.h"
34
#include "core/object/class_db.h"
35
#include "editor/doc/editor_help.h"
36
#include "editor/editor_node.h"
37
#include "editor/editor_string_names.h"
38
#include "editor/file_system/editor_paths.h"
39
#include "editor/gui/filter_line_edit.h"
40
#include "editor/settings/editor_feature_profile.h"
41
#include "editor/settings/editor_settings.h"
42
#include "editor/themes/editor_scale.h"
43
#include "scene/gui/button.h"
44
#include "scene/gui/item_list.h"
45
#include "scene/gui/tree.h"
46
47
void CreateDialog::popup_create(bool p_dont_clear, bool p_replace_mode, const String &p_current_type, const String &p_current_name) {
48
_fill_type_list();
49
50
icon_fallback = search_options->has_theme_icon(base_type, EditorStringName(EditorIcons)) ? base_type : "Object";
51
52
if (p_dont_clear) {
53
search_box->select_all();
54
} else {
55
search_box->clear();
56
}
57
58
if (p_replace_mode) {
59
search_box->set_text(p_current_type);
60
}
61
62
search_box->grab_focus();
63
_update_search();
64
65
if (p_replace_mode) {
66
set_title(vformat(TTR("Change Type of \"%s\""), p_current_name));
67
set_ok_button_text(TTR("Change"));
68
} else {
69
set_title(vformat(TTR("Create New %s"), base_type));
70
set_ok_button_text(TTR("Create"));
71
}
72
73
_load_favorites_and_history();
74
_save_and_update_favorite_list();
75
76
// Restore valid window bounds or pop up at default size.
77
Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "create_new_node", Rect2());
78
if (saved_size != Rect2()) {
79
popup(saved_size);
80
} else {
81
popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8);
82
}
83
}
84
85
void CreateDialog::for_inherit() {
86
allow_abstract_scripts = true;
87
}
88
89
void CreateDialog::_fill_type_list() {
90
LocalVector<StringName> complete_type_list;
91
ClassDB::get_class_list(complete_type_list);
92
ScriptServer::get_global_class_list(complete_type_list);
93
94
EditorData &ed = EditorNode::get_editor_data();
95
HashMap<String, DocData::ClassDoc> &class_docs_list = EditorHelp::get_doc_data()->class_list;
96
97
for (const StringName &type : complete_type_list) {
98
if (!_should_hide_type(type)) {
99
TypeInfo type_info;
100
type_info.type_name = type;
101
102
const DocData::ClassDoc *class_docs = class_docs_list.getptr(type);
103
if (class_docs) {
104
type_info.search_keywords = class_docs->keywords.split(",");
105
106
for (int i = 0; i < type_info.search_keywords.size(); i++) {
107
type_info.search_keywords.set(i, type_info.search_keywords[i].strip_edges());
108
}
109
}
110
111
type_info_list.push_back(type_info);
112
113
if (!ed.get_custom_types().has(type)) {
114
continue;
115
}
116
117
const Vector<EditorData::CustomType> &ct = ed.get_custom_types()[type];
118
for (int i = 0; i < ct.size(); i++) {
119
custom_type_parents[ct[i].name] = type;
120
custom_type_indices[ct[i].name] = i;
121
122
TypeInfo custom_type_info;
123
custom_type_info.type_name = ct[i].name;
124
type_info_list.push_back(custom_type_info);
125
}
126
}
127
}
128
129
struct TypeInfoCompare {
130
StringName::AlphCompare compare;
131
132
_FORCE_INLINE_ bool operator()(const TypeInfo &l, const TypeInfo &r) const {
133
return compare(l.type_name, r.type_name);
134
}
135
};
136
137
type_info_list.sort_custom<TypeInfoCompare>();
138
}
139
140
bool CreateDialog::_is_type_preferred(const String &p_type) const {
141
if (ClassDB::class_exists(p_type)) {
142
return ClassDB::is_parent_class(p_type, preferred_search_result_type);
143
}
144
145
return EditorNode::get_editor_data().script_class_is_parent(p_type, preferred_search_result_type);
146
}
147
148
void CreateDialog::_script_button_clicked(TreeItem *p_item, int p_column, int p_button_id, MouseButton p_mouse_button_index) {
149
if (p_mouse_button_index != MouseButton::LEFT) {
150
return;
151
}
152
// The id of opening-script button is 1.
153
if (p_button_id != 1) {
154
return;
155
}
156
157
String scr_path = ScriptServer::get_global_class_path(p_item->get_text(0));
158
Ref<Script> scr = ResourceLoader::load(scr_path, "Script");
159
ERR_FAIL_COND_MSG(scr.is_null(), vformat("Could not load the script from resource path: %s", scr_path));
160
EditorNode::get_singleton()->push_item_no_inspector(scr.ptr());
161
162
hide();
163
_cleanup();
164
}
165
166
bool CreateDialog::_is_class_disabled_by_feature_profile(const StringName &p_class) const {
167
Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
168
169
return profile.is_valid() && profile->is_class_disabled(p_class);
170
}
171
172
bool CreateDialog::_should_hide_type(const StringName &p_type) const {
173
if (_is_class_disabled_by_feature_profile(p_type)) {
174
return true;
175
}
176
177
if (is_base_type_node && p_type.operator String().begins_with("Editor")) {
178
return true; // Do not show editor nodes.
179
}
180
181
if (ClassDB::class_exists(p_type)) {
182
if (!ClassDB::can_instantiate(p_type) || ClassDB::is_virtual(p_type)) {
183
return true; // Can't create abstract or virtual class.
184
}
185
186
if (!ClassDB::is_parent_class(p_type, base_type)) {
187
return true; // Wrong inheritance.
188
}
189
190
if (!ClassDB::is_class_exposed(p_type)) {
191
return true; // Unexposed types.
192
}
193
194
for (const StringName &E : type_blacklist) {
195
if (ClassDB::is_parent_class(p_type, E)) {
196
return true; // Parent type is blacklisted.
197
}
198
}
199
for (const StringName &F : custom_type_blocklist) {
200
if (ClassDB::is_parent_class(p_type, F)) {
201
return true; // Parent type is excluded in custom type blocklist.
202
}
203
}
204
} else {
205
if (!ScriptServer::is_global_class(p_type)) {
206
return true;
207
}
208
if (!EditorNode::get_editor_data().script_class_is_parent(p_type, base_type)) {
209
return true; // Wrong inheritance.
210
}
211
212
StringName native_type = ScriptServer::get_global_class_native_base(p_type);
213
if (ClassDB::class_exists(native_type)) {
214
if (!ClassDB::can_instantiate(native_type)) {
215
return true;
216
} else if (custom_type_blocklist.has(p_type) || custom_type_blocklist.has(native_type)) {
217
return true;
218
}
219
}
220
221
String script_path = ScriptServer::get_global_class_path(p_type);
222
if (script_path.begins_with("res://addons/")) {
223
int i = script_path.find_char('/', 13); // 13 is length of "res://addons/".
224
while (i > -1) {
225
const String plugin_path = script_path.substr(0, i).path_join("plugin.cfg");
226
if (FileAccess::exists(plugin_path)) {
227
return !EditorNode::get_singleton()->is_addon_plugin_enabled(plugin_path);
228
}
229
i = script_path.find_char('/', i + 1);
230
}
231
}
232
// Abstract scripts cannot be instantiated.
233
String path = ScriptServer::get_global_class_path(p_type);
234
Ref<Script> scr = ResourceLoader::load(path, "Script");
235
return scr.is_null() || (!allow_abstract_scripts && scr->is_abstract());
236
}
237
238
return false;
239
}
240
241
void CreateDialog::_update_search() {
242
search_options->clear();
243
search_options_types.clear();
244
245
TreeItem *root = search_options->create_item();
246
root->set_text(0, base_type);
247
root->set_icon(0, search_options->get_editor_theme_icon(icon_fallback));
248
search_options_types[base_type] = root;
249
_configure_search_option_item(root, base_type, ClassDB::class_exists(base_type) ? TypeCategory::CPP_TYPE : TypeCategory::OTHER_TYPE, "");
250
251
const String search_text = search_box->get_text();
252
253
float highest_score = 0.0f;
254
StringName best_match;
255
256
for (const TypeInfo &candidate : type_info_list) {
257
String match_keyword;
258
259
// First check if the name matches. If it does not, try the search keywords.
260
float score = _score_type(candidate.type_name, search_text);
261
if (score < 0.0f) {
262
for (const String &keyword : candidate.search_keywords) {
263
score = _score_type(keyword, search_text);
264
265
// Reduce the score of keywords, since they are an indirect match.
266
score *= 0.1f;
267
268
if (score >= 0.0f) {
269
match_keyword = keyword;
270
break;
271
}
272
}
273
}
274
275
// Search did not match.
276
if (score < 0.0f) {
277
continue;
278
}
279
280
_add_type(candidate.type_name, ClassDB::class_exists(candidate.type_name) ? TypeCategory::CPP_TYPE : TypeCategory::OTHER_TYPE, match_keyword);
281
282
if (score > highest_score) {
283
highest_score = score;
284
best_match = candidate.type_name;
285
}
286
}
287
288
// Select the best result.
289
if (search_text.is_empty()) {
290
select_type(base_type);
291
} else if (best_match != StringName()) {
292
select_type(best_match);
293
} else {
294
favorite->set_disabled(true);
295
help_bit->set_custom_text(String(), String(), vformat(TTR("No results for \"%s\"."), search_text.replace("[", "[lb]")));
296
get_ok_button()->set_disabled(true);
297
search_options->deselect_all();
298
}
299
}
300
301
void CreateDialog::_add_type(const StringName &p_type, TypeCategory p_type_category, const String &p_match_keyword) {
302
if (search_options_types.has(p_type)) {
303
return;
304
}
305
306
TypeCategory inherited_type = TypeCategory::OTHER_TYPE;
307
308
StringName inherits;
309
if (p_type_category == TypeCategory::CPP_TYPE) {
310
inherits = ClassDB::get_parent_class(p_type);
311
inherited_type = TypeCategory::CPP_TYPE;
312
} else {
313
if (p_type_category == TypeCategory::PATH_TYPE) {
314
ERR_FAIL_COND(!ResourceLoader::exists(p_type, "Script"));
315
Ref<Script> scr = ResourceLoader::load(p_type, "Script");
316
ERR_FAIL_COND(scr.is_null());
317
318
Ref<Script> base = scr->get_base_script();
319
if (base.is_null()) {
320
// Must be a native base type.
321
StringName extends = scr->get_instance_base_type();
322
if (extends == StringName()) {
323
// Not a valid script (has compile errors), we therefore ignore it as it can not be instantiated anyway (when selected).
324
return;
325
}
326
327
inherits = extends;
328
inherited_type = TypeCategory::CPP_TYPE;
329
} else {
330
inherits = base->get_global_name();
331
332
if (inherits == StringName()) {
333
inherits = base->get_path();
334
inherited_type = TypeCategory::PATH_TYPE;
335
}
336
}
337
} else if (ScriptServer::is_global_class(p_type)) {
338
inherits = ScriptServer::get_global_class_base(p_type);
339
bool is_native_class = ClassDB::class_exists(inherits);
340
inherited_type = is_native_class ? TypeCategory::CPP_TYPE : TypeCategory::OTHER_TYPE;
341
} else {
342
inherits = custom_type_parents[p_type];
343
if (ClassDB::class_exists(inherits)) {
344
inherited_type = TypeCategory::CPP_TYPE;
345
}
346
}
347
}
348
349
// Should never happen, but just in case...
350
ERR_FAIL_COND(inherits == StringName());
351
352
_add_type(inherits, inherited_type, "");
353
354
TreeItem *item = search_options->create_item(search_options_types[inherits]);
355
search_options_types[p_type] = item;
356
_configure_search_option_item(item, p_type, p_type_category, p_match_keyword);
357
}
358
359
void CreateDialog::_configure_search_option_item(TreeItem *r_item, const StringName &p_type, TypeCategory p_type_category, const String &p_match_keyword) {
360
bool script_type = ScriptServer::is_global_class(p_type);
361
bool is_abstract = false;
362
bool is_custom_type = false;
363
String type_name;
364
String text;
365
if (p_type_category == TypeCategory::CPP_TYPE) {
366
type_name = p_type;
367
text = p_type;
368
} else if (p_type_category == TypeCategory::PATH_TYPE) {
369
type_name = "\"" + p_type + "\"";
370
text = "\"" + p_type + "\"";
371
} else if (script_type) {
372
is_custom_type = true;
373
type_name = p_type;
374
text = p_type;
375
376
if (!allow_abstract_scripts) {
377
is_abstract = ScriptServer::is_global_class_abstract(p_type);
378
}
379
380
String tooltip = TTR("Script path: %s");
381
bool is_tool = ScriptServer::is_global_class_tool(p_type);
382
if (is_tool) {
383
tooltip = TTR("The script will run in the editor.") + "\n" + tooltip;
384
}
385
const String script_path = ScriptServer::get_global_class_path(p_type);
386
r_item->add_button(0, get_editor_theme_icon(SNAME("Script")), 1, false, vformat(tooltip, script_path));
387
r_item->set_meta(SNAME("_script_path"), script_path);
388
if (is_tool) {
389
int button_index = r_item->get_button_count(0) - 1;
390
r_item->set_button_color(0, button_index, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
391
}
392
} else {
393
is_custom_type = true;
394
type_name = custom_type_parents[p_type];
395
text = p_type;
396
}
397
398
if (!p_match_keyword.is_empty()) {
399
text += " - " + TTR(vformat("Matches the \"%s\" keyword.", p_match_keyword));
400
}
401
r_item->set_text(0, text);
402
403
Array meta;
404
meta.append(is_custom_type);
405
meta.append(type_name);
406
r_item->set_metadata(0, meta);
407
408
bool can_instantiate = (p_type_category == TypeCategory::CPP_TYPE && ClassDB::can_instantiate(p_type)) ||
409
(p_type_category == TypeCategory::OTHER_TYPE && !(!allow_abstract_scripts && is_abstract));
410
bool instantiable = can_instantiate && !(ClassDB::class_exists(p_type) && ClassDB::is_virtual(p_type));
411
412
r_item->set_meta(SNAME("__instantiable"), instantiable);
413
414
r_item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_type));
415
if (!instantiable) {
416
r_item->set_custom_color(0, search_options->get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor)));
417
}
418
419
HashMap<String, DocData::ClassDoc>::Iterator class_doc = EditorHelp::get_doc_data()->class_list.find(p_type);
420
421
bool is_deprecated = (class_doc && class_doc->value.is_deprecated);
422
bool is_experimental = (class_doc && class_doc->value.is_experimental);
423
424
if (is_deprecated) {
425
r_item->add_button(0, get_editor_theme_icon("StatusError"), 0, false, TTR("This class is marked as deprecated."));
426
} else if (is_experimental) {
427
r_item->add_button(0, get_editor_theme_icon("NodeWarning"), 0, false, TTR("This class is marked as experimental."));
428
}
429
430
if (!search_box->get_text().is_empty()) {
431
r_item->set_collapsed(false);
432
} else {
433
// Don't collapse the root node or an abstract node on the first tree level.
434
bool should_collapse = p_type != base_type && (r_item->get_parent()->get_text(0) != base_type || can_instantiate);
435
436
if (should_collapse && bool(EDITOR_GET("docks/scene_tree/start_create_dialog_fully_expanded"))) {
437
should_collapse = false; // Collapse all nodes anyway.
438
}
439
r_item->set_collapsed(should_collapse);
440
}
441
442
const String &description = DTR(class_doc ? class_doc->value.brief_description : "");
443
r_item->set_tooltip_text(0, description);
444
445
if (p_type_category == TypeCategory::OTHER_TYPE && !script_type) {
446
Ref<Texture2D> icon = EditorNode::get_editor_data().get_custom_types()[custom_type_parents[p_type]][custom_type_indices[p_type]].icon;
447
if (icon.is_valid()) {
448
r_item->set_icon(0, icon);
449
}
450
}
451
}
452
453
float CreateDialog::_score_type(const String &p_type, const String &p_search) const {
454
if (p_search.is_empty()) {
455
return 0.0f;
456
}
457
458
// Determine the best match for a non-empty search.
459
if (!p_search.is_subsequence_ofn(p_type)) {
460
return -1.0f;
461
}
462
463
if (p_type == p_search) {
464
// Always favor an exact match (case-sensitive), since clicking a favorite will set the search text to the type.
465
return 1.0f;
466
}
467
468
float inverse_length = 1.f / float(p_type.length());
469
470
// Favor types where search term is a substring close to the start of the type.
471
float w = 0.5f;
472
int pos = p_type.findn(p_search);
473
float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
474
475
// Favor shorter items: they resemble the search term more.
476
w = 0.9f;
477
score *= (1 - w) + w * MIN(1.0f, p_search.length() * inverse_length);
478
479
score *= _is_type_preferred(p_type) ? 1.0f : 0.9f;
480
481
// Add score for being a favorite type.
482
score *= favorite_list.has(p_type) ? 1.0f : 0.8f;
483
484
// Look through at most 5 recent items
485
bool in_recent = false;
486
constexpr int RECENT_COMPLETION_SIZE = 5;
487
for (int i = 0; i < MIN(RECENT_COMPLETION_SIZE - 1, recent->get_item_count()); i++) {
488
if (recent->get_item_text(i) == p_type) {
489
in_recent = true;
490
break;
491
}
492
}
493
score *= in_recent ? 1.0f : 0.9f;
494
495
return score;
496
}
497
498
void CreateDialog::_cleanup() {
499
type_info_list.clear();
500
favorite_list.clear();
501
favorites->clear();
502
recent->clear();
503
custom_type_parents.clear();
504
custom_type_indices.clear();
505
}
506
507
void CreateDialog::_confirmed() {
508
String selected_item = get_selected_type_name();
509
if (selected_item.is_empty()) {
510
return;
511
}
512
513
TreeItem *selected = search_options->get_selected();
514
if (!selected->get_meta("__instantiable", true)) {
515
return;
516
}
517
518
{
519
Ref<FileAccess> f = FileAccess::open(EditorPaths::get_singleton()->get_project_settings_dir().path_join("create_recent." + base_type), FileAccess::WRITE);
520
if (f.is_valid()) {
521
f->store_line(selected_item);
522
523
constexpr int RECENT_HISTORY_SIZE = 15;
524
for (int i = 0; i < MIN(RECENT_HISTORY_SIZE - 1, recent->get_item_count()); i++) {
525
if (recent->get_item_text(i) != selected_item) {
526
f->store_line(recent->get_item_text(i));
527
}
528
}
529
}
530
}
531
532
// To prevent, emitting an error from the transient window (shader dialog for example) hide this dialog before emitting the "create" signal.
533
hide();
534
535
emit_signal(SNAME("create"));
536
_cleanup();
537
}
538
539
void CreateDialog::_text_changed(const String &p_newtext) {
540
_update_search();
541
}
542
543
void CreateDialog::_sbox_input(const Ref<InputEvent> &p_event) {
544
// Redirect navigational key events to the tree.
545
Ref<InputEventKey> key = p_event;
546
if (key.is_valid()) {
547
if (key->is_action_pressed("ui_select", true)) {
548
TreeItem *ti = search_options->get_selected();
549
if (ti) {
550
ti->set_collapsed(!ti->is_collapsed());
551
}
552
search_box->accept_event();
553
}
554
}
555
}
556
557
void CreateDialog::_notification(int p_what) {
558
switch (p_what) {
559
case NOTIFICATION_ENTER_TREE: {
560
connect(SceneStringName(confirmed), callable_mp(this, &CreateDialog::_confirmed));
561
} break;
562
563
case NOTIFICATION_EXIT_TREE: {
564
disconnect(SceneStringName(confirmed), callable_mp(this, &CreateDialog::_confirmed));
565
} break;
566
567
case NOTIFICATION_VISIBILITY_CHANGED: {
568
if (is_visible()) {
569
callable_mp((Control *)search_box, &Control::grab_focus).call_deferred(false); // Still not visible.
570
search_box->select_all();
571
} else {
572
EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "create_new_node", Rect2(get_position(), get_size()));
573
}
574
} break;
575
576
case NOTIFICATION_THEME_CHANGED: {
577
const int icon_width = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
578
search_options->add_theme_constant_override("icon_max_width", icon_width);
579
favorites->add_theme_constant_override("icon_max_width", icon_width);
580
recent->set_fixed_icon_size(Size2(icon_width, icon_width));
581
582
favorite->set_button_icon(get_editor_theme_icon(SNAME("Favorites")));
583
} break;
584
}
585
}
586
587
void CreateDialog::select_type(const String &p_type, bool p_center_on_item) {
588
if (!search_options_types.has(p_type)) {
589
return;
590
}
591
592
TreeItem *to_select = search_options_types[p_type];
593
to_select->select(0);
594
search_options->scroll_to_item(to_select, p_center_on_item);
595
596
help_bit->parse_symbol("class|" + p_type + "|");
597
598
favorite->set_disabled(false);
599
favorite->set_pressed(favorite_list.has(p_type));
600
601
if (to_select->get_meta("__instantiable", true)) {
602
get_ok_button()->set_disabled(false);
603
get_ok_button()->set_tooltip_text(String());
604
} else {
605
get_ok_button()->set_disabled(true);
606
get_ok_button()->set_tooltip_text(TTR("The selected class can't be instantiated."));
607
}
608
}
609
610
void CreateDialog::select_base() {
611
if (search_options_types.is_empty()) {
612
_update_search();
613
}
614
select_type(base_type, false);
615
}
616
617
String CreateDialog::get_selected_type() {
618
TreeItem *selected = search_options->get_selected();
619
620
if (!selected) {
621
return String();
622
}
623
624
String type = selected->get_text(0).get_slicec(' ', 0);
625
if (ClassDB::class_exists(type)) {
626
return type; // CPP type - from the core or GDExtensions
627
}
628
629
const EditorData::CustomType *custom_type = EditorNode::get_editor_data().get_custom_type_by_name(type);
630
if (custom_type != nullptr) {
631
return custom_type->script->get_path(); // Types via EditorPlugin::add_custom_type()
632
}
633
634
return String(selected->get_meta("_script_path", "")); // Script types
635
}
636
637
String CreateDialog::get_selected_type_name() {
638
TreeItem *selected = search_options->get_selected();
639
if (!selected) {
640
return String();
641
}
642
return selected->get_text(0).get_slicec(' ', 0);
643
}
644
645
void CreateDialog::set_base_type(const String &p_base) {
646
base_type = p_base;
647
is_base_type_node = ClassDB::is_parent_class(p_base, "Node");
648
}
649
650
Variant CreateDialog::instantiate_selected() {
651
TreeItem *selected = search_options->get_selected();
652
653
if (!selected) {
654
return Variant();
655
}
656
657
Array meta = selected->get_metadata(0).operator Array();
658
ERR_FAIL_COND_V(meta.size() != 2, Variant());
659
660
bool is_custom_type = meta[0].operator bool();
661
String type_name = meta[1].operator String();
662
Variant obj;
663
if (is_custom_type) {
664
if (ScriptServer::is_global_class(type_name)) {
665
obj = EditorNode::get_editor_data().script_class_instance(type_name);
666
Node *n = Object::cast_to<Node>(obj);
667
if (n) {
668
n->set_name(type_name);
669
}
670
} else {
671
obj = EditorNode::get_editor_data().instantiate_custom_type(selected->get_text(0), type_name);
672
}
673
} else {
674
obj = ClassDB::instantiate(type_name);
675
}
676
EditorNode::get_editor_data().instantiate_object_properties(obj);
677
678
return obj;
679
}
680
681
void CreateDialog::_item_selected() {
682
select_type(get_selected_type_name(), false);
683
}
684
685
void CreateDialog::_hide_requested() {
686
_cancel_pressed(); // From AcceptDialog.
687
}
688
689
void CreateDialog::cancel_pressed() {
690
_cleanup();
691
}
692
693
void CreateDialog::_favorite_toggled() {
694
TreeItem *item = search_options->get_selected();
695
if (!item) {
696
return;
697
}
698
699
String name = get_selected_type_name();
700
701
if (favorite_list.has(name)) {
702
favorite_list.erase(name);
703
favorite->set_pressed(false);
704
} else {
705
favorite_list.push_back(name);
706
favorite->set_pressed(true);
707
}
708
709
_save_and_update_favorite_list();
710
}
711
712
void CreateDialog::_history_selected(int p_idx) {
713
search_box->set_text(recent->get_item_text(p_idx));
714
favorites->deselect_all();
715
_update_search();
716
}
717
718
void CreateDialog::_favorite_selected() {
719
TreeItem *item = favorites->get_selected();
720
if (!item) {
721
return;
722
}
723
724
search_box->set_text(item->get_text(0));
725
recent->deselect_all();
726
_update_search();
727
}
728
729
void CreateDialog::_history_activated(int p_idx) {
730
_history_selected(p_idx);
731
_confirmed();
732
}
733
734
void CreateDialog::_favorite_activated() {
735
_favorite_selected();
736
_confirmed();
737
}
738
739
Variant CreateDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
740
TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? favorites->get_selected() : favorites->get_item_at_position(p_point);
741
if (ti) {
742
Dictionary d;
743
d["type"] = "create_favorite_drag";
744
d["class"] = ti->get_text(0);
745
746
Button *tb = memnew(Button);
747
tb->set_flat(true);
748
tb->set_button_icon(ti->get_icon(0));
749
tb->set_text(ti->get_text(0));
750
tb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
751
favorites->set_drag_preview(tb);
752
753
return d;
754
}
755
756
return Variant();
757
}
758
759
bool CreateDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
760
Dictionary d = p_data;
761
if (d.has("type") && String(d["type"]) == "create_favorite_drag") {
762
favorites->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
763
return true;
764
}
765
766
return false;
767
}
768
769
void CreateDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
770
Dictionary d = p_data;
771
772
TreeItem *ti = (p_point == Vector2(Math::INF, Math::INF)) ? favorites->get_selected() : favorites->get_item_at_position(p_point);
773
if (!ti) {
774
return;
775
}
776
777
String drop_at = ti->get_text(0);
778
int ds = (p_point == Vector2(Math::INF, Math::INF)) ? favorites->get_drop_section_at_position(favorites->get_item_rect(ti).position) : favorites->get_drop_section_at_position(p_point);
779
780
int drop_idx = favorite_list.find(drop_at);
781
if (drop_idx < 0) {
782
return;
783
}
784
785
String type = d["class"];
786
787
int from_idx = favorite_list.find(type);
788
if (from_idx < 0) {
789
return;
790
}
791
792
if (drop_idx == from_idx) {
793
ds = -1; //cause it will be gone
794
} else if (drop_idx > from_idx) {
795
drop_idx--;
796
}
797
798
favorite_list.remove_at(from_idx);
799
800
if (ds < 0) {
801
favorite_list.insert(drop_idx, type);
802
} else {
803
if (drop_idx >= favorite_list.size() - 1) {
804
favorite_list.push_back(type);
805
} else {
806
favorite_list.insert(drop_idx + 1, type);
807
}
808
}
809
810
_save_and_update_favorite_list();
811
}
812
813
void CreateDialog::_save_and_update_favorite_list() {
814
favorites->clear();
815
TreeItem *root = favorites->create_item();
816
817
{
818
Ref<FileAccess> f = FileAccess::open(EditorPaths::get_singleton()->get_project_settings_dir().path_join("favorites." + base_type), FileAccess::WRITE);
819
if (f.is_valid()) {
820
for (const String &name : favorite_list) {
821
if (!EditorNode::get_editor_data().is_type_recognized(name)) {
822
continue;
823
}
824
f->store_line(name);
825
826
if (_is_class_disabled_by_feature_profile(name)) {
827
continue;
828
}
829
830
TreeItem *ti = favorites->create_item(root);
831
ti->set_text(0, name);
832
ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(name));
833
}
834
}
835
}
836
837
emit_signal(SNAME("favorites_updated"));
838
}
839
840
void CreateDialog::_load_favorites_and_history() {
841
String dir = EditorPaths::get_singleton()->get_project_settings_dir();
842
Ref<FileAccess> f = FileAccess::open(dir.path_join("create_recent." + base_type), FileAccess::READ);
843
if (f.is_valid()) {
844
while (!f->eof_reached()) {
845
String name = f->get_line().strip_edges();
846
847
if (EditorNode::get_editor_data().is_type_recognized(name) && !_is_class_disabled_by_feature_profile(name)) {
848
recent->add_item(name, EditorNode::get_singleton()->get_class_icon(name));
849
}
850
}
851
}
852
853
f = FileAccess::open(dir.path_join("favorites." + base_type), FileAccess::READ);
854
if (f.is_valid()) {
855
while (!f->eof_reached()) {
856
String name = f->get_line().strip_edges();
857
858
if (!name.is_empty()) {
859
favorite_list.push_back(name);
860
}
861
}
862
}
863
}
864
865
void CreateDialog::_bind_methods() {
866
ADD_SIGNAL(MethodInfo("create"));
867
ADD_SIGNAL(MethodInfo("favorites_updated"));
868
}
869
870
CreateDialog::CreateDialog() {
871
base_type = "Object";
872
preferred_search_result_type = "";
873
874
type_blacklist.insert("PluginScript"); // PluginScript must be initialized before use, which is not possible here.
875
type_blacklist.insert("ScriptCreateDialog"); // This is an exposed editor Node that doesn't have an Editor prefix.
876
type_blacklist.insert("MissingNode");
877
type_blacklist.insert("MissingResource");
878
879
HSplitContainer *hsc = memnew(HSplitContainer);
880
add_child(hsc);
881
882
VSplitContainer *vsc = memnew(VSplitContainer);
883
hsc->add_child(vsc);
884
885
VSplitContainer *vsc_right = memnew(VSplitContainer);
886
hsc->add_child(vsc_right);
887
888
VBoxContainer *fav_vb = memnew(VBoxContainer);
889
fav_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
890
fav_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
891
vsc->add_child(fav_vb);
892
893
favorites = memnew(Tree);
894
favorites->set_accessibility_name(TTRC("Favorites:"));
895
favorites->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
896
favorites->set_hide_root(true);
897
favorites->set_hide_folding(true);
898
favorites->set_allow_reselect(true);
899
favorites->connect("cell_selected", callable_mp(this, &CreateDialog::_favorite_selected));
900
favorites->connect("item_activated", callable_mp(this, &CreateDialog::_favorite_activated));
901
favorites->add_theme_constant_override("draw_guides", 1);
902
favorites->set_theme_type_variation("TreeSecondary");
903
SET_DRAG_FORWARDING_GCD(favorites, CreateDialog);
904
fav_vb->add_margin_child(TTR("Favorites:"), favorites, true);
905
906
VBoxContainer *rec_vb = memnew(VBoxContainer);
907
vsc->add_child(rec_vb);
908
rec_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
909
rec_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
910
911
recent = memnew(ItemList);
912
recent->set_accessibility_name(TTRC("Recent:"));
913
recent->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
914
rec_vb->add_margin_child(TTR("Recent:"), recent, true);
915
recent->set_allow_reselect(true);
916
recent->connect(SceneStringName(item_selected), callable_mp(this, &CreateDialog::_history_selected));
917
recent->connect("item_activated", callable_mp(this, &CreateDialog::_history_activated));
918
recent->add_theme_constant_override("draw_guides", 1);
919
recent->set_theme_type_variation("ItemListSecondary");
920
921
VBoxContainer *vbc = memnew(VBoxContainer);
922
vbc->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
923
vbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
924
vbc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
925
vsc_right->add_child(vbc);
926
927
search_box = memnew(FilterLineEdit);
928
search_box->set_accessibility_name(TTRC("Search"));
929
search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
930
search_box->connect(SceneStringName(text_changed), callable_mp(this, &CreateDialog::_text_changed));
931
932
HBoxContainer *search_hb = memnew(HBoxContainer);
933
search_hb->add_child(search_box);
934
935
favorite = memnew(Button);
936
favorite->set_toggle_mode(true);
937
favorite->set_tooltip_text(TTR("(Un)favorite selected item."));
938
favorite->connect(SceneStringName(pressed), callable_mp(this, &CreateDialog::_favorite_toggled));
939
search_hb->add_child(favorite);
940
vbc->add_margin_child(TTR("Search:"), search_hb);
941
942
search_options = memnew(Tree);
943
search_box->set_forward_control(search_options);
944
search_options->set_accessibility_name(TTRC("Matches:"));
945
search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
946
search_options->connect("item_activated", callable_mp(this, &CreateDialog::_confirmed));
947
search_options->connect("cell_selected", callable_mp(this, &CreateDialog::_item_selected));
948
search_options->connect("button_clicked", callable_mp(this, &CreateDialog::_script_button_clicked));
949
search_options->set_theme_type_variation("TreeSecondary");
950
vbc->add_margin_child(TTR("Matches:"), search_options, true);
951
952
help_bit = memnew(EditorHelpBit);
953
help_bit->set_accessibility_name(TTRC("Description:"));
954
help_bit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
955
help_bit->set_content_height_limits(80 * EDSCALE, 80 * EDSCALE);
956
help_bit->connect("request_hide", callable_mp(this, &CreateDialog::_hide_requested));
957
958
VBoxContainer *vbc_desc = memnew(VBoxContainer);
959
vbc_desc->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
960
vbc_desc->add_margin_child(TTR("Description:"), help_bit, true);
961
962
vsc_right->add_child(vbc_desc);
963
964
register_text_enter(search_box);
965
set_hide_on_ok(false);
966
set_clamp_to_embedder(true);
967
}
968
969