From c4e7bc1c455083db6c4ef5944751bf532375fb1a Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 31 Jul 2022 00:01:58 +0200 Subject: [PATCH] Reused the old unused PROPERTY_HINT_SPRITE_FRAMES as PROPERTY_HINT_BUTTON. It allows you to add buttons to the inspectors. --- core/global_constants.cpp | 1 + core/object.h | 2 +- doc/classes/@GlobalScope.xml | 3 ++ editor/editor_properties.cpp | 69 ++++++++++++++++++++++++++++++++- editor/editor_properties.h | 23 +++++++++++ modules/web/http/csrf_token.cpp | 1 + 6 files changed, 96 insertions(+), 3 deletions(-) diff --git a/core/global_constants.cpp b/core/global_constants.cpp index 8827ac789..4c46bbcf0 100644 --- a/core/global_constants.cpp +++ b/core/global_constants.cpp @@ -587,6 +587,7 @@ void register_global_constants() { BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_ENUM_SUGGESTION); BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_EXP_EASING); BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_LENGTH); + BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_BUTTON); BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_KEY_ACCEL); BIND_GLOBAL_ENUM_CONSTANT(PROPERTY_HINT_FLAGS); diff --git a/core/object.h b/core/object.h index 03355ba25..8bdbc32ac 100644 --- a/core/object.h +++ b/core/object.h @@ -61,7 +61,7 @@ enum PropertyHint { PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc" PROPERTY_HINT_EXP_EASING, /// exponential easing function (Math::ease) use "attenuation" hint string to revert (flip h), "full" to also include in/out. (ie: "attenuation,inout") PROPERTY_HINT_LENGTH, ///< hint_text= "length" (as integer) - PROPERTY_HINT_SPRITE_FRAME, // FIXME: Obsolete: drop whenever we can break compat. Keeping now for GDNative compat. + PROPERTY_HINT_BUTTON, // Use a button in the inspector for this property. The property's type has to be Variant::NIL. hint_text="call_func:name/theme_type" -> calls call_func on press, optional: ":name/theme_type" -> get_icon("name", "theme_type") PROPERTY_HINT_KEY_ACCEL, ///< hint_text= "length" (as integer) PROPERTY_HINT_FLAGS, ///< hint_text= "flag1,flag2,etc" (as bit flags) PROPERTY_HINT_LAYERS_2D_RENDER, diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index 02c73edd3..de2386d9f 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -1438,6 +1438,9 @@ Deprecated hint, unused. + + Use a button in the inspector for this property. The property's type has to be Variant::NIL. The hint string has to contain a method that you want called. [code]"my_method"[/code] will call [code]my_method()[/code] on press. Optionally it can also contain a theme icon like: [code]"my_method:name/theme_type"[/code] -> [code]get_icon("name", "theme_type")[/code] + Deprecated hint, unused. diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index 0d3946bb0..6e06d085d 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -44,6 +44,7 @@ #include "core/math/transform.h" #include "core/math/transform_2d.h" #include "core/math/vector3.h" +#include "core/object.h" #include "core/object_id.h" #include "core/os/input_event.h" #include "core/os/memory.h" @@ -642,6 +643,52 @@ EditorPropertyCheck::EditorPropertyCheck() { checkbox->connect("pressed", this, "_checkbox_pressed"); } +///////////////////// Button ///////////////////////// +void EditorPropertyButton::update_property() { + _button->set_disabled(is_read_only()); +} + +void EditorPropertyButton::setup(const String &method_name, const Vector &p_options) { + _method_name = method_name; + + if (p_options.size() >= 2) { + _icon_name = p_options[0]; + _icon_theme_type = p_options[1]; + } +} + +EditorPropertyButton::EditorPropertyButton() { + _button = memnew(Button); + _button->set_text(TTR("Run")); + add_child(_button); + add_focusable(_button); + _button->connect("pressed", this, "_button_pressed"); +} + +void EditorPropertyButton::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_THEME_CHANGED: + case NOTIFICATION_ENTER_TREE: { + if (_icon_name != "") { + _button->set_icon(get_icon(_icon_name, _icon_theme_type)); + } + } break; + } +} + +void EditorPropertyButton::_bind_methods() { + ClassDB::bind_method(D_METHOD("_button_pressed"), &EditorPropertyButton::_button_pressed); +} + +void EditorPropertyButton::_button_pressed() { + //emit_changed(get_edited_property(), _button->is_pressed()); + //get_edited_object()->get(get_edited_property()); + + if (_method_name != "") { + get_edited_object()->call(_method_name); + } +} + ///////////////////// ENUM ///////////////////////// void EditorPropertyEnum::_option_selected(int p_which) { @@ -3034,8 +3081,26 @@ bool EditorInspectorDefaultPlugin::parse_property(Object *p_object, Variant::Typ switch (p_type) { // atomic types case Variant::NIL: { - EditorPropertyNil *editor = memnew(EditorPropertyNil); - add_property_editor(p_path, editor); + if (p_hint == PROPERTY_HINT_BUTTON) { + EditorPropertyButton *editor = memnew(EditorPropertyButton); + + Vector nopts = p_hint_text.split(":"); + + if (nopts.size() > 0) { + Vector icon_opts; + + if (nopts.size() >= 2) { + icon_opts = nopts[1].split("/"); + } + + editor->setup(nopts[0], icon_opts); + } + + add_property_editor(p_path, editor); + } else { + EditorPropertyNil *editor = memnew(EditorPropertyNil); + add_property_editor(p_path, editor); + } } break; case Variant::BOOL: { EditorPropertyCheck *editor = memnew(EditorPropertyCheck); diff --git a/editor/editor_properties.h b/editor/editor_properties.h index a426d2283..b3ef30ed2 100644 --- a/editor/editor_properties.h +++ b/editor/editor_properties.h @@ -241,6 +241,29 @@ public: EditorPropertyCheck(); }; +class EditorPropertyButton : public EditorProperty { + GDCLASS(EditorPropertyButton, EditorProperty); + +public: + virtual void update_property(); + + void setup(const String &method_name, const Vector &p_options); + + EditorPropertyButton(); + +protected: + void _notification(int p_what); + static void _bind_methods(); + + void _button_pressed(); + + String _method_name; + String _icon_name; + String _icon_theme_type; + + Button *_button; +}; + class EditorPropertyEnum : public EditorProperty { GDCLASS(EditorPropertyEnum, EditorProperty); OptionButton *options; diff --git a/modules/web/http/csrf_token.cpp b/modules/web/http/csrf_token.cpp index 135958077..6ab06c0e5 100644 --- a/modules/web/http/csrf_token.cpp +++ b/modules/web/http/csrf_token.cpp @@ -1,6 +1,7 @@ #include "csrf_token.h" #include "core/crypto/crypto_core.h" +#include "core/object.h" #include "core/os/os.h" #include "http_server_enums.h" #include "http_session.h"