Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass current value to EditorInterface node/property popups #94323

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/classes/EditorInterface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,9 @@
<return type="void" />
<param index="0" name="callback" type="Callable" />
<param index="1" name="valid_types" type="StringName[]" default="[]" />
<param index="2" name="current_value" type="Node" default="null" />
<description>
Pops up an editor dialog for selecting a [Node] from the edited scene. The [param callback] must take a single argument of type [NodePath]. It is called on the selected [NodePath] or the empty path [code]^""[/code] if the dialog is canceled. If [param valid_types] is provided, the dialog will only show Nodes that match one of the listed Node types.
Pops up an editor dialog for selecting a [Node] from the edited scene. The [param callback] must take a single argument of type [NodePath]. It is called on the selected [NodePath] or the empty path [code]^""[/code] if the dialog is canceled. If [param valid_types] is provided, the dialog will only show Nodes that match one of the listed Node types. If [param current_value] is provided, the Node will be automatically selected in the tree, if it exists.
[b]Example:[/b] Display the node selection dialog as soon as this node is added to the tree for the first time:
[codeblock]
func _ready():
Expand All @@ -326,8 +327,9 @@
<param index="0" name="object" type="Object" />
<param index="1" name="callback" type="Callable" />
<param index="2" name="type_filter" type="PackedInt32Array" default="PackedInt32Array()" />
<param index="3" name="current_value" type="String" default="&quot;&quot;" />
<description>
Pops up an editor dialog for selecting properties from [param object]. The [param callback] must take a single argument of type [NodePath]. It is called on the selected property path (see [method NodePath.get_as_property_path]) or the empty path [code]^""[/code] if the dialog is canceled. If [param type_filter] is provided, the dialog will only show properties that match one of the listed [enum Variant.Type] values.
Pops up an editor dialog for selecting properties from [param object]. The [param callback] must take a single argument of type [NodePath]. It is called on the selected property path (see [method NodePath.get_as_property_path]) or the empty path [code]^""[/code] if the dialog is canceled. If [param type_filter] is provided, the dialog will only show properties that match one of the listed [enum Variant.Type] values. If [param current_value] is provided, the property will be selected automatically in the property list, if it exists.
[codeblock]
func _ready():
if Engine.is_editor_hint():
Expand Down
48 changes: 48 additions & 0 deletions editor/editor_interface.compat.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**************************************************************************/
/* editor_interface.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "editor_interface.h"

#ifndef DISABLE_DEPRECATED

void EditorInterface::_popup_node_selector_bind_compat_94323(const Callable &p_callback, const TypedArray<StringName> &p_valid_types) {
popup_node_selector(p_callback, p_valid_types, nullptr);
}

void EditorInterface::_popup_property_selector_bind_compat_94323(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter) {
popup_property_selector(p_object, p_callback, p_type_filter, String());
}

void EditorInterface::_bind_compatibility_methods() {
ClassDB::bind_compatibility_method(D_METHOD("popup_node_selector", "callback", "valid_types"), &EditorInterface::_popup_node_selector_bind_compat_94323, DEFVAL(TypedArray<StringName>()));
ClassDB::bind_compatibility_method(D_METHOD("popup_property_selector", "object", "callback", "type_filter"), &EditorInterface::_popup_property_selector_bind_compat_94323, DEFVAL(PackedInt32Array()));
}

#endif
13 changes: 7 additions & 6 deletions editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/**************************************************************************/

#include "editor_interface.h"
#include "editor_interface.compat.inc"

#include "editor/editor_command_palette.h"
#include "editor/editor_feature_profile.h"
Expand Down Expand Up @@ -276,7 +277,7 @@ void EditorInterface::set_current_feature_profile(const String &p_profile_name)

// Editor dialogs.

void EditorInterface::popup_node_selector(const Callable &p_callback, const TypedArray<StringName> &p_valid_types) {
void EditorInterface::popup_node_selector(const Callable &p_callback, const TypedArray<StringName> &p_valid_types, Node *p_current_value) {
// TODO: Should reuse dialog instance instead of creating a fresh one, but need to rework set_valid_types first.
if (node_selector) {
node_selector->disconnect(SNAME("selected"), callable_mp(this, &EditorInterface::_node_selected).bind(p_callback));
Expand All @@ -296,7 +297,7 @@ void EditorInterface::popup_node_selector(const Callable &p_callback, const Type

get_base_control()->add_child(node_selector);

node_selector->popup_scenetree_dialog();
node_selector->popup_scenetree_dialog(p_current_value);

const Callable selected_callback = callable_mp(this, &EditorInterface::_node_selected).bind(p_callback);
node_selector->connect(SNAME("selected"), selected_callback, CONNECT_DEFERRED);
Expand All @@ -305,7 +306,7 @@ void EditorInterface::popup_node_selector(const Callable &p_callback, const Type
node_selector->connect(SNAME("canceled"), canceled_callback, CONNECT_DEFERRED);
}

void EditorInterface::popup_property_selector(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter) {
void EditorInterface::popup_property_selector(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter, const String &p_current_value) {
// TODO: Should reuse dialog instance instead of creating a fresh one, but need to rework set_type_filter first.
if (property_selector) {
property_selector->disconnect(SNAME("selected"), callable_mp(this, &EditorInterface::_property_selected).bind(p_callback));
Expand All @@ -325,7 +326,7 @@ void EditorInterface::popup_property_selector(Object *p_object, const Callable &

get_base_control()->add_child(property_selector);

property_selector->select_property_from_instance(p_object);
property_selector->select_property_from_instance(p_object, p_current_value);

const Callable selected_callback = callable_mp(this, &EditorInterface::_property_selected).bind(p_callback);
property_selector->connect(SNAME("selected"), selected_callback, CONNECT_DEFERRED);
Expand Down Expand Up @@ -564,8 +565,8 @@ void EditorInterface::_bind_methods() {

// Editor dialogs.

ClassDB::bind_method(D_METHOD("popup_node_selector", "callback", "valid_types"), &EditorInterface::popup_node_selector, DEFVAL(TypedArray<StringName>()));
ClassDB::bind_method(D_METHOD("popup_property_selector", "object", "callback", "type_filter"), &EditorInterface::popup_property_selector, DEFVAL(PackedInt32Array()));
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()));

// Editor docks.

Expand Down
11 changes: 9 additions & 2 deletions editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class EditorInterface : public Object {
protected:
static void _bind_methods();

#ifndef DISABLE_DEPRECATED
void _popup_node_selector_bind_compat_94323(const Callable &p_callback, const TypedArray<StringName> &p_valid_types = TypedArray<StringName>());
void _popup_property_selector_bind_compat_94323(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter = PackedInt32Array());

static void _bind_compatibility_methods();
#endif

public:
static EditorInterface *get_singleton() { return singleton; }

Expand Down Expand Up @@ -128,9 +135,9 @@ class EditorInterface : public Object {

// Editor dialogs.

void popup_node_selector(const Callable &p_callback, const TypedArray<StringName> &p_valid_types = TypedArray<StringName>());
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());
void popup_property_selector(Object *p_object, const Callable &p_callback, const PackedInt32Array &p_type_filter = PackedInt32Array(), const String &p_current_value = String());

// Editor docks.

Expand Down
17 changes: 17 additions & 0 deletions editor/property_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ void PropertySelector::_update_search() {
if (!found && !search_box->get_text().is_empty() && E.name.containsn(search_text)) {
item->select(0);
found = true;
} else if (!found && search_box->get_text().is_empty() && E.name == selected) {
item->select(0);
found = true;
}

item->set_selectable(0, true);
Expand All @@ -173,6 +176,12 @@ void PropertySelector::_update_search() {
if (category && category->get_first_child() == nullptr) {
memdelete(category); //old category was unused
}

if (found) {
// As we call this while adding items, defer until list is completely populated.
callable_mp(search_options, &Tree::scroll_to_item).call_deferred(search_options->get_selected(), true);
}

} else {
List<MethodInfo> methods;

Expand Down Expand Up @@ -305,12 +314,20 @@ void PropertySelector::_update_search() {
if (!found && !search_box->get_text().is_empty() && name.containsn(search_text)) {
item->select(0);
found = true;
} else if (!found && search_box->get_text().is_empty() && name == selected) {
item->select(0);
found = true;
}
}

if (category && category->get_first_child() == nullptr) {
memdelete(category); //old category was unused
}

if (found) {
// As we call this while adding items, defer until list is completely populated.
callable_mp(search_options, &Tree::scroll_to_item).call_deferred(search_options->get_selected(), true);
}
}

get_ok_button()->set_disabled(root->get_first_child() == nullptr);
Expand Down
8 changes: 8 additions & 0 deletions misc/extension_api_validation/4.3-stable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ Validate extension JSON: Error: Field 'classes/AudioStreamPlayer2D/properties/pl
Validate extension JSON: Error: Field 'classes/AudioStreamPlayer3D/properties/playing': setter changed value in new API, from "_set_playing" to &"set_playing".

These setters have been renamed to expose them. GDExtension language bindings couldn't have exposed these properties before.


GH-94322
--------
Validate extension JSON: Error: Field 'classes/EditorInterface/methods/popup_node_selector/arguments': size changed value in new API, from 2 to 3.
Validate extension JSON: Error: Field 'classes/EditorInterface/methods/popup_property_selector/arguments': size changed value in new API, from 3 to 4.

Added optional argument to popup_property_selector and popup_node_selector to specify the current value.
Loading