Skip to content

Commit 4cf1b0d

Browse files
committed
Merge pull request #100135 from Lazy-Rabbit-2001/expose-create-dialog-new
Add `popup_create_dialog()` for `EditorInterface` to create custom create dialog
2 parents 26fec37 + 9c34ad1 commit 4cf1b0d

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

doc/classes/EditorInterface.xml

+29
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,35 @@
270270
Plays the main scene.
271271
</description>
272272
</method>
273+
<method name="popup_create_dialog">
274+
<return type="void" />
275+
<param index="0" name="callback" type="Callable" />
276+
<param index="1" name="base_type" type="StringName" default="&quot;&quot;" />
277+
<param index="2" name="current_type" type="String" default="&quot;&quot;" />
278+
<param index="3" name="dialog_title" type="String" default="&quot;&quot;" />
279+
<param index="4" name="type_blocklist" type="StringName[]" default="[]" />
280+
<param index="5" name="type_suffixes" type="Dictionary" default="{}" />
281+
<description>
282+
Pops up an editor dialog for creating an object.
283+
The [param callback] must take a single argument of type [StringName] which will contain the type name of the selected object or be empty if no item is selected.
284+
The [param base_type] specifies the base type of objects to display. For example, if you set this to "Resource", all types derived from [Resource] will display in the create dialog.
285+
The [param current_type] will be passed in the search box of the create dialog, and the specified type can be immediately selected when the dialog pops up. If the [param current_type] is not derived from [param base_type], there will be no result of the type in the dialog.
286+
The [param dialog_title] allows you to define a custom title for the dialog. This is useful if you want to accurately hint the usage of the dialog. If the [param dialog_title] is an empty string, the dialog will use "Create New 'Base Type'" as the default title.
287+
The [param type_blocklist] contains a list of type names, and the types in the blocklist will be hidden from the create dialog.
288+
The [param type_suffixes] is a dictionary, with keys being [StringName]s and values being [String]s. Custom suffixes override the default suffixes which are file names of their scripts. For example, if you set a custom suffix as "Custom Suffix" for a global script type,
289+
[codeblock lang=text]
290+
Node
291+
|- MyCustomNode (my_custom_node.gd)
292+
[/codeblock]
293+
will be
294+
[codeblock lang=text]
295+
Node
296+
|- MyCustomNode (Custom Suffix)
297+
[/codeblock]
298+
Bear in mind that when a built-in type does not have any custom suffix, its suffix will be removed. The suffix of a type created from a script will fall back to its script file name. For global types by scripts, if you customize their suffixes to an empty string, their suffixes will be removed.
299+
[b]Note:[/b] Trying to list the base type in the [param type_blocklist] will hide all types derived from the base type from the create dialog.
300+
</description>
301+
</method>
273302
<method name="popup_dialog">
274303
<return type="void" />
275304
<param index="0" name="dialog" type="Window" />

editor/create_dialog.cpp

+25-4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ bool CreateDialog::_should_hide_type(const StringName &p_type) const {
145145
return true; // Parent type is blacklisted.
146146
}
147147
}
148+
for (const StringName &F : custom_type_blocklist) {
149+
if (ClassDB::is_parent_class(p_type, F)) {
150+
return true; // Parent type is excluded in custom type blocklist.
151+
}
152+
}
148153
} else {
149154
if (!ScriptServer::is_global_class(p_type)) {
150155
return true;
@@ -154,8 +159,12 @@ bool CreateDialog::_should_hide_type(const StringName &p_type) const {
154159
}
155160

156161
StringName native_type = ScriptServer::get_global_class_native_base(p_type);
157-
if (ClassDB::class_exists(native_type) && !ClassDB::can_instantiate(native_type)) {
158-
return true;
162+
if (ClassDB::class_exists(native_type)) {
163+
if (!ClassDB::can_instantiate(native_type)) {
164+
return true;
165+
} else if (custom_type_blocklist.has(p_type) || custom_type_blocklist.has(native_type)) {
166+
return true;
167+
}
159168
}
160169

161170
String script_path = ScriptServer::get_global_class_path(p_type);
@@ -283,15 +292,27 @@ void CreateDialog::_configure_search_option_item(TreeItem *r_item, const StringN
283292
bool is_abstract = false;
284293
if (p_type_category == TypeCategory::CPP_TYPE) {
285294
r_item->set_text(0, p_type);
295+
if (custom_type_suffixes.has(p_type)) {
296+
String suffix = custom_type_suffixes.get(p_type);
297+
if (!suffix.is_empty()) {
298+
r_item->set_suffix(0, "(" + suffix + ")");
299+
}
300+
}
286301
} else if (p_type_category == TypeCategory::PATH_TYPE) {
287302
r_item->set_text(0, "\"" + p_type + "\"");
288303
} else if (script_type) {
289304
r_item->set_metadata(0, p_type);
290305
r_item->set_text(0, p_type);
291306
String script_path = ScriptServer::get_global_class_path(p_type);
292-
r_item->set_suffix(0, "(" + script_path.get_file() + ")");
293-
294307
Ref<Script> scr = ResourceLoader::load(script_path, "Script");
308+
String suffix = script_path.get_file();
309+
if (scr.is_valid() && custom_type_suffixes.has(p_type)) {
310+
suffix = custom_type_suffixes.get(p_type);
311+
}
312+
if (!suffix.is_empty()) {
313+
r_item->set_suffix(0, "(" + suffix + ")");
314+
}
315+
295316
ERR_FAIL_COND(!scr.is_valid());
296317
is_abstract = scr->is_abstract();
297318
} else {

editor/create_dialog.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class CreateDialog : public ConfirmationDialog {
6666
HashMap<String, int> custom_type_indices;
6767
List<StringName> type_list;
6868
HashSet<StringName> type_blacklist;
69+
HashSet<StringName> custom_type_blocklist;
70+
HashMap<StringName, String> custom_type_suffixes;
6971

7072
void _update_search();
7173
bool _should_hide_type(const StringName &p_type) const;
@@ -115,8 +117,10 @@ class CreateDialog : public ConfirmationDialog {
115117
String get_base_type() const { return base_type; }
116118
void select_base();
117119

120+
void set_type_blocklist(const HashSet<StringName> &p_blocklist) { custom_type_blocklist = p_blocklist; }
121+
void set_type_suffixes(const HashMap<StringName, String> &p_suffixes) { custom_type_suffixes = p_suffixes; }
122+
118123
void set_preferred_search_result_type(const String &p_preferred_type) { preferred_search_result_type = p_preferred_type; }
119-
String get_preferred_search_result_type() { return preferred_search_result_type; }
120124

121125
void popup_create(bool p_dont_clear, bool p_replace_mode = false, const String &p_current_type = "", const String &p_current_name = "");
122126

editor/editor_interface.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "editor_interface.compat.inc"
3333

3434
#include "core/config/project_settings.h"
35+
#include "editor/create_dialog.h"
3536
#include "editor/editor_command_palette.h"
3637
#include "editor/editor_feature_profile.h"
3738
#include "editor/editor_main_screen.h"
@@ -512,6 +513,45 @@ void EditorInterface::popup_quick_open(const Callable &p_callback, const TypedAr
512513
quick_open->popup_dialog(base_types, callable_mp(this, &EditorInterface::_quick_open).bind(p_callback));
513514
}
514515

516+
void EditorInterface::popup_create_dialog(const Callable &p_callback, const StringName &p_base_type, const String &p_current_type, const String &p_dialog_title, const TypedArray<StringName> &p_custom_type_blocklist, const Dictionary &p_custom_suffix) {
517+
if (!create_dialog) {
518+
create_dialog = memnew(CreateDialog);
519+
get_base_control()->add_child(create_dialog);
520+
}
521+
522+
HashSet<StringName> blocklist;
523+
for (const Variant &E : p_custom_type_blocklist) {
524+
blocklist.insert(E);
525+
}
526+
create_dialog->set_type_blocklist(blocklist);
527+
528+
HashMap<StringName, String> suffix_map;
529+
List<Variant> keys;
530+
p_custom_suffix.get_key_list(&keys);
531+
for (Variant &k : keys) {
532+
const StringName key = k;
533+
if (key.is_empty()) {
534+
continue;
535+
}
536+
suffix_map.insert(key, p_custom_suffix[key]);
537+
}
538+
create_dialog->set_type_suffixes(suffix_map);
539+
540+
String safe_base_type = p_base_type;
541+
if (p_base_type.is_empty() || (!ClassDB::class_exists(p_base_type) && !ScriptServer::is_global_class(p_base_type))) {
542+
ERR_PRINT(vformat("Invalid base type '%s'. The base type has fallen back to 'Object'.", p_base_type));
543+
safe_base_type = "Object";
544+
}
545+
546+
create_dialog->set_base_type(safe_base_type);
547+
create_dialog->popup_create(false, true, p_current_type, "");
548+
create_dialog->set_title(p_dialog_title.is_empty() ? vformat(TTR("Create New %s"), p_base_type) : p_dialog_title);
549+
550+
const Callable callback = callable_mp(this, &EditorInterface::_create_dialog_item_selected);
551+
create_dialog->connect(SNAME("create"), callback.bind(false, p_callback), CONNECT_DEFERRED);
552+
create_dialog->connect(SNAME("canceled"), callback.bind(true, p_callback), CONNECT_DEFERRED);
553+
}
554+
515555
void EditorInterface::_node_selected(const NodePath &p_node_path, const Callable &p_callback) {
516556
const Callable callback = callable_mp(this, &EditorInterface::_node_selected);
517557
node_selector->disconnect(SNAME("selected"), callback);
@@ -555,6 +595,13 @@ void EditorInterface::_quick_open(const String &p_file_path, const Callable &p_c
555595
_call_dialog_callback(p_callback, p_file_path, "quick open");
556596
}
557597

598+
void EditorInterface::_create_dialog_item_selected(bool p_is_canceled, const Callable &p_callback) {
599+
const Callable callback = callable_mp(this, &EditorInterface::_create_dialog_item_selected);
600+
create_dialog->disconnect(SNAME("create"), callback);
601+
create_dialog->disconnect(SNAME("canceled"), callback);
602+
_call_dialog_callback(p_callback, p_is_canceled ? "" : create_dialog->get_selected_type(), "create dialog");
603+
}
604+
558605
void EditorInterface::_call_dialog_callback(const Callable &p_callback, const Variant &p_selected, const String &p_context) {
559606
Callable::CallError ce;
560607
Variant ret;
@@ -773,6 +820,7 @@ void EditorInterface::_bind_methods() {
773820
ClassDB::bind_method(D_METHOD("popup_property_selector", "object", "callback", "type_filter", "current_value"), &EditorInterface::popup_property_selector, DEFVAL(PackedInt32Array()), DEFVAL(String()));
774821
ClassDB::bind_method(D_METHOD("popup_method_selector", "object", "callback", "current_value"), &EditorInterface::popup_method_selector, DEFVAL(String()));
775822
ClassDB::bind_method(D_METHOD("popup_quick_open", "callback", "base_types"), &EditorInterface::popup_quick_open, DEFVAL(TypedArray<StringName>()));
823+
ClassDB::bind_method(D_METHOD("popup_create_dialog", "callback", "base_type", "current_type", "dialog_title", "type_blocklist", "type_suffixes"), &EditorInterface::popup_create_dialog, DEFVAL(""), DEFVAL(""), DEFVAL(""), DEFVAL(TypedArray<StringName>()), DEFVAL(Dictionary()));
776824

777825
// Editor docks.
778826

editor/editor_interface.h

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "core/object/script_language.h"
3838

3939
class Control;
40+
class CreateDialog;
4041
class EditorCommandPalette;
4142
class EditorFileSystem;
4243
class EditorInspector;
@@ -69,11 +70,13 @@ class EditorInterface : public Object {
6970
PropertySelector *property_selector = nullptr;
7071
PropertySelector *method_selector = nullptr;
7172
SceneTreeDialog *node_selector = nullptr;
73+
CreateDialog *create_dialog = nullptr;
7274

7375
void _node_selected(const NodePath &p_node_paths, const Callable &p_callback);
7476
void _property_selected(const String &p_property_name, const Callable &p_callback);
7577
void _method_selected(const String &p_property_name, const Callable &p_callback);
7678
void _quick_open(const String &p_file_path, const Callable &p_callback);
79+
void _create_dialog_item_selected(bool p_is_canceled, const Callable &p_callback);
7780
void _call_dialog_callback(const Callable &p_callback, const Variant &p_selected, const String &p_context);
7881

7982
// Editor tools.
@@ -145,6 +148,7 @@ class EditorInterface : public Object {
145148
void popup_property_selector(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter = PackedInt32Array(), const String &p_current_value = String());
146149
void popup_method_selector(Object *p_object, const Callable &p_callback, const String &p_current_value = String());
147150
void popup_quick_open(const Callable &p_callback, const TypedArray<StringName> &p_base_types = TypedArray<StringName>());
151+
void popup_create_dialog(const Callable &p_callback, const StringName &p_base_type = "", const String &p_current_type = "", const String &p_dialog_title = "", const TypedArray<StringName> &p_custom_type_blocklist = TypedArray<String>(), const Dictionary &p_custom_suffix = Dictionary());
148152

149153
// Editor docks.
150154

0 commit comments

Comments
 (0)