From 74411f2a0d0791f41cb43a02e30571d45cc2a45c Mon Sep 17 00:00:00 2001 From: yds Date: Mon, 30 Sep 2024 00:48:08 -0300 Subject: [PATCH] Make the "Quick Open" dialog available via EditorInterface --- doc/classes/EditorInterface.xml | 8 ++++++++ editor/editor_interface.cpp | 18 ++++++++++++++++++ editor/editor_interface.h | 2 ++ editor/editor_node.cpp | 4 ++++ editor/editor_resource_picker.cpp | 4 ++++ editor/gui/editor_quick_open_dialog.cpp | 2 ++ editor/gui/editor_run_bar.cpp | 4 ++++ editor/scene_tree_dock.cpp | 4 ++++ 8 files changed, 46 insertions(+) diff --git a/doc/classes/EditorInterface.xml b/doc/classes/EditorInterface.xml index 795c5c1c2f0b..8cc096abeb66 100644 --- a/doc/classes/EditorInterface.xml +++ b/doc/classes/EditorInterface.xml @@ -343,6 +343,14 @@ [/codeblock] + + + + + + Pops up an editor dialog for quick selecting a resource file. The [param callback] must take a single argument of type [String] which will contain the path of the selected resource or be empty if the dialog is canceled. If [param base_types] is provided, the dialog will only show resources that match these types. + + diff --git a/editor/editor_interface.cpp b/editor/editor_interface.cpp index fa6198f6950d..1ec4f04b6551 100644 --- a/editor/editor_interface.cpp +++ b/editor/editor_interface.cpp @@ -40,6 +40,7 @@ #include "editor/editor_settings.h" #include "editor/editor_undo_redo_manager.h" #include "editor/filesystem_dock.h" +#include "editor/gui/editor_quick_open_dialog.h" #include "editor/gui/editor_run_bar.h" #include "editor/gui/editor_scene_tabs.h" #include "editor/gui/scene_tree_editor.h" @@ -336,6 +337,18 @@ void EditorInterface::popup_property_selector(Object *p_object, const Callable & property_selector->connect(SNAME("canceled"), canceled_callback, CONNECT_DEFERRED); } +void EditorInterface::popup_quick_open(const Callable &p_callback, const TypedArray &p_base_types) { + EditorQuickOpenDialog *quick_open = EditorNode::get_singleton()->get_quick_open_dialog(); + Vector base_types; + int length = p_base_types.size(); + base_types.resize(length); + for (int i = 0; i < length; i++) { + base_types.write[i] = p_base_types[i]; + } + + quick_open->popup_dialog(base_types, callable_mp(this, &EditorInterface::_quick_open).bind(p_callback)); +} + void EditorInterface::_node_selected(const NodePath &p_node_path, const Callable &p_callback) { const NodePath path = get_edited_scene_root()->get_path().rel_path_to(p_node_path); _call_dialog_callback(p_callback, path, "node selected"); @@ -353,6 +366,10 @@ void EditorInterface::_property_selection_canceled(const Callable &p_callback) { _call_dialog_callback(p_callback, NodePath(), "property selection canceled"); } +void EditorInterface::_quick_open(const String &p_file_path, const Callable &p_callback) { + _call_dialog_callback(p_callback, p_file_path, "quick open"); +} + void EditorInterface::_call_dialog_callback(const Callable &p_callback, const Variant &p_selected, const String &p_context) { Callable::CallError ce; Variant ret; @@ -568,6 +585,7 @@ void EditorInterface::_bind_methods() { ClassDB::bind_method(D_METHOD("popup_node_selector", "callback", "valid_types", "current_value"), &EditorInterface::popup_node_selector, DEFVAL(TypedArray()), DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("popup_property_selector", "object", "callback", "type_filter", "current_value"), &EditorInterface::popup_property_selector, DEFVAL(PackedInt32Array()), DEFVAL(String())); + ClassDB::bind_method(D_METHOD("popup_quick_open", "callback", "base_types"), &EditorInterface::popup_quick_open, DEFVAL(TypedArray())); // Editor docks. diff --git a/editor/editor_interface.h b/editor/editor_interface.h index 20d66d71f53b..4877444dac4c 100644 --- a/editor/editor_interface.h +++ b/editor/editor_interface.h @@ -72,6 +72,7 @@ class EditorInterface : public Object { void _node_selection_canceled(const Callable &p_callback); void _property_selected(const String &p_property_name, const Callable &p_callback); void _property_selection_canceled(const Callable &p_callback); + void _quick_open(const String &p_file_path, const Callable &p_callback); void _call_dialog_callback(const Callable &p_callback, const Variant &p_selected, const String &p_context); // Editor tools. @@ -138,6 +139,7 @@ class EditorInterface : public Object { void popup_node_selector(const Callable &p_callback, const TypedArray &p_valid_types = TypedArray(), Node *p_current_value = nullptr); // Must use Vector because exposing Vector is not supported. void popup_property_selector(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter = PackedInt32Array(), const String &p_current_value = String()); + void popup_quick_open(const Callable &p_callback, const TypedArray &p_base_types = TypedArray()); // Editor docks. diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 2853ebc4994c..4c7900645906 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -4595,6 +4595,10 @@ void EditorNode::_update_recent_scenes() { } void EditorNode::_quick_opened(const String &p_file_path) { + if (p_file_path.is_empty()) { + return; + } + if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(p_file_path), "PackedScene")) { open_request(p_file_path); } else { diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp index e4ae2a620277..5c821d3e876b 100644 --- a/editor/editor_resource_picker.cpp +++ b/editor/editor_resource_picker.cpp @@ -133,6 +133,10 @@ void EditorResourcePicker::_resource_selected() { } void EditorResourcePicker::_file_selected(const String &p_path) { + if (p_path.is_empty()) { + return; + } + Ref loaded_resource = ResourceLoader::load(p_path); ERR_FAIL_COND_MSG(loaded_resource.is_null(), "Cannot load resource from path '" + p_path + "'."); diff --git a/editor/gui/editor_quick_open_dialog.cpp b/editor/gui/editor_quick_open_dialog.cpp index 83b11e702219..d72457999c46 100644 --- a/editor/gui/editor_quick_open_dialog.cpp +++ b/editor/gui/editor_quick_open_dialog.cpp @@ -114,6 +114,8 @@ void EditorQuickOpenDialog::ok_pressed() { } void EditorQuickOpenDialog::cancel_pressed() { + item_selected_callback.call(String()); + container->cleanup(); search_box->clear(); } diff --git a/editor/gui/editor_run_bar.cpp b/editor/gui/editor_run_bar.cpp index 908b1e671953..d4c0655d1dcd 100644 --- a/editor/gui/editor_run_bar.cpp +++ b/editor/gui/editor_run_bar.cpp @@ -122,6 +122,10 @@ void EditorRunBar::_write_movie_toggled(bool p_enabled) { } void EditorRunBar::_quick_run_selected(const String &p_file_path) { + if (p_file_path.is_empty()) { + return; + } + play_custom_scene(p_file_path); } diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index bcab0c2883c7..6a6033a5824a 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -75,6 +75,10 @@ void SceneTreeDock::_nodes_drag_begin() { } void SceneTreeDock::_quick_open(const String &p_file_path) { + if (p_file_path.is_empty()) { + return; + } + instantiate_scenes({ p_file_path }, scene_tree->get_selected()); }