Reused the old unused PROPERTY_HINT_SPRITE_FRAMES as PROPERTY_HINT_BUTTON. It allows you to add buttons to the inspectors.

This commit is contained in:
Relintai 2022-07-31 00:01:58 +02:00
parent 249dd4d661
commit c4e7bc1c45
6 changed files with 96 additions and 3 deletions

View File

@ -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);

View File

@ -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,

View File

@ -1438,6 +1438,9 @@
<constant name="PROPERTY_HINT_LENGTH" value="5" enum="PropertyHint">
Deprecated hint, unused.
</constant>
<constant name="PROPERTY_HINT_BUTTON" value="6" enum="PropertyHint">
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]
</constant>
<constant name="PROPERTY_HINT_KEY_ACCEL" value="7" enum="PropertyHint">
Deprecated hint, unused.
</constant>

View File

@ -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<String> &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<String> nopts = p_hint_text.split(":");
if (nopts.size() > 0) {
Vector<String> 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);

View File

@ -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<String> &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;

View File

@ -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"