Skip to content

Commit

Permalink
Make the "Quick Open" dialog available via EditorInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
ydeltastar committed Oct 3, 2024
1 parent 5ccbf6e commit 09988b9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/classes/EditorInterface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@
[/codeblock]
</description>
</method>
<method name="popup_quick_open">
<return type="void" />
<param index="0" name="callback" type="Callable" />
<param index="1" name="base_types" type="StringName[]" default="[&amp;&quot;Resource&quot;]" />
<description>
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.
</description>
</method>
<method name="reload_scene_from_path">
<return type="void" />
<param index="0" name="scene_filepath" type="String" />
Expand Down
25 changes: 25 additions & 0 deletions editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -336,6 +337,21 @@ 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<StringName> &p_base_types) {
ERR_FAIL_COND_MSG(p_base_types.is_empty(), "The list of base types for quick open dialog can't be empty.");

Vector<StringName> 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];
}

EditorQuickOpenDialog *quick_open = EditorNode::get_singleton()->get_quick_open_dialog();
quick_open->connect(SNAME("canceled"), callable_mp(this, &EditorInterface::_quick_open).bind(String(), p_callback));
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");
Expand All @@ -353,6 +369,12 @@ 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) {
EditorQuickOpenDialog *quick_open = EditorNode::get_singleton()->get_quick_open_dialog();
quick_open->disconnect(SNAME("canceled"), callable_mp(this, &EditorInterface::_quick_open).bind(p_callback, String()));
_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;
Expand Down Expand Up @@ -568,6 +590,9 @@ void EditorInterface::_bind_methods() {

ClassDB::bind_method(D_METHOD("popup_node_selector", "callback", "valid_types", "current_value"), &EditorInterface::popup_node_selector, DEFVAL(TypedArray<StringName>()), DEFVAL(Variant()));
ClassDB::bind_method(D_METHOD("popup_property_selector", "object", "callback", "type_filter", "current_value"), &EditorInterface::popup_property_selector, DEFVAL(PackedInt32Array()), DEFVAL(String()));
TypedArray<StringName> base_types;
base_types.append("Resource");
ClassDB::bind_method(D_METHOD("popup_quick_open", "callback", "base_types"), &EditorInterface::popup_quick_open, DEFVAL(base_types));

// Editor docks.

Expand Down
2 changes: 2 additions & 0 deletions editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -138,6 +139,7 @@ class EditorInterface : public Object {
void popup_node_selector(const Callable &p_callback, const TypedArray<StringName> &p_valid_types = TypedArray<StringName>(), Node *p_current_value = nullptr);
// Must use Vector<int> because exposing Vector<Variant::Type> 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<StringName> &p_base_types = { "Resource" });

// Editor docks.

Expand Down

0 comments on commit 09988b9

Please sign in to comment.