Skip to content

Commit

Permalink
Expose various property info for configuring the VariantResource
Browse files Browse the repository at this point in the history
Represents `PropertyInfo` in the engine.
  • Loading branch information
Xrayez committed Oct 30, 2020
1 parent 04418ad commit f0966ce
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
65 changes: 54 additions & 11 deletions core/types/variant_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Variant VariantResource::convert(const Variant &p_value, const Variant::Type &p_

bool VariantResource::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name.operator String();
if (name == "value") {
if (name == pi.name) {
value = p_value;
type = p_value.get_type();
emit_changed();
Expand All @@ -39,28 +39,71 @@ bool VariantResource::_set(const StringName &p_name, const Variant &p_value) {

bool VariantResource::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name.operator String();
if (name == "value") {
if (name == pi.name) {
r_ret = value;
} else {
return false;
}
return true;
}

void VariantResource::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(type, "value"));
void VariantResource::set_property_name(const String &p_property_name) {
pi.name = p_property_name;
_change_notify();
}

void VariantResource::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_type", "type"), &VariantResource::set_type);
ClassDB::bind_method(D_METHOD("get_type"), &VariantResource::get_type);
void VariantResource::set_property_hint(PropertyHint p_property_hint) {
pi.hint = p_property_hint;
_change_notify();
}

String hint_types;
void VariantResource::set_property_hint_string(const String &p_property_hint_string) {
pi.hint_string = p_property_hint_string;
_change_notify();
}

void VariantResource::set_property_usage(PropertyUsageFlags p_property_usage) {
pi.usage = p_property_usage;
_change_notify();
}

void VariantResource::_get_property_list(List<PropertyInfo> *p_list) const {
// This property is changed dynamically from other properties.
p_list->push_back(PropertyInfo(type, pi.name, pi.hint, pi.hint_string, pi.usage));
}

String VariantResource::get_type_hints() {
String type_hints;
for (int i = 0; i < Variant::VARIANT_MAX; ++i) {
hint_types += Variant::get_type_name(Variant::Type(i));
type_hints += Variant::get_type_name(Variant::Type(i));
if (i < Variant::VARIANT_MAX - 1) {
hint_types += ",";
type_hints += ",";
}
}
ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, hint_types), "set_type", "get_type");
return type_hints;
}

void VariantResource::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_type", "type"), &VariantResource::set_type);
ClassDB::bind_method(D_METHOD("get_type"), &VariantResource::get_type);

ClassDB::bind_method(D_METHOD("set_property_name", "name"), &VariantResource::set_property_name);
ClassDB::bind_method(D_METHOD("get_property_name"), &VariantResource::get_property_name);

ClassDB::bind_method(D_METHOD("set_property_hint", "hint"), &VariantResource::set_property_hint);
ClassDB::bind_method(D_METHOD("get_property_hint"), &VariantResource::get_property_hint);

ClassDB::bind_method(D_METHOD("set_property_hint_string", "hint_string"), &VariantResource::set_property_hint_string);
ClassDB::bind_method(D_METHOD("get_property_hint_string"), &VariantResource::get_property_hint_string);

ClassDB::bind_method(D_METHOD("set_property_usage", "usage"), &VariantResource::set_property_usage);
ClassDB::bind_method(D_METHOD("get_property_usage"), &VariantResource::get_property_usage);

ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, get_type_hints()), "set_type", "get_type");

ADD_GROUP("Property", "property_");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "property_name"), "set_property_name", "get_property_name");
ADD_PROPERTY(PropertyInfo(Variant::INT, "property_hint"), "set_property_hint", "get_property_hint");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "property_hint_string"), "set_property_hint_string", "get_property_hint_string");
ADD_PROPERTY(PropertyInfo(Variant::INT, "property_usage"), "set_property_usage", "get_property_usage");
}
19 changes: 19 additions & 0 deletions core/types/variant_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,35 @@ class VariantResource : public Resource {

private:
Variant::Type type = Variant::NIL;
PropertyInfo pi;
Variant value;

public:
void set_type(Variant::Type p_type);
int get_type() const { return type; }

void set_property_name(const String &p_property_name);
String get_property_name() const { return pi.name; };

void set_property_hint(PropertyHint p_property_hint);
PropertyHint get_property_hint() const { return pi.hint; };

void set_property_hint_string(const String &p_property_hint_string);
String get_property_hint_string() const { return pi.hint_string; };

void set_property_usage(PropertyUsageFlags p_property_usage);
PropertyUsageFlags get_property_usage() const { return PropertyUsageFlags(pi.usage); };

static Variant create(const Variant::Type &p_type);
static Variant convert(const Variant &p_value, const Variant::Type &p_to_type);
static String get_type_hints();

virtual String to_string() { return String(value); }

VariantResource() {
// Default, but can be configured with `property_name` property.
pi.name = "value";
}
};

#endif // GOOST_VARIANT_RESOURCE_H
12 changes: 12 additions & 0 deletions doc/VariantResource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
<methods>
</methods>
<members>
<member name="property_hint" type="int" setter="set_property_hint" getter="get_property_hint" enum="PropertyHint" default="0">
Specifies how the value is represented in the editor, one of [enum @GlobalScope.PropertyHint] values.
</member>
<member name="property_hint_string" type="String" setter="set_property_hint_string" getter="get_property_hint_string" default="&quot;&quot;">
Configures [member property_hint].
</member>
<member name="property_name" type="String" setter="set_property_name" getter="get_property_name" default="&quot;value&quot;">
Specifies the value's property name. By default, the data can be fetched via code by referencing the [code]value[/code] property, but this can be customized.
</member>
<member name="property_usage" type="int" setter="set_property_usage" getter="get_property_usage" enum="PropertyUsageFlags" default="7">
Specifies how the value should be used throughout the editor and code, a combination of [enum @GlobalScope.PropertyUsageFlags] values.
</member>
<member name="type" type="int" setter="set_type" getter="get_type" default="0">
Sets the type of the [Variant], one of the [code]TYPE_*[/code] constants available at [@GlobalScope], such as [constant @GlobalScope.TYPE_INT].
Once the type is set, an implicit [Variant] [code]value[/code] property is constructed. The [code]value[/code] property can be changed dynamically anytime, and this emits the [signal changed] signal, which can be connected to other script or engine methods:
Expand Down

0 comments on commit f0966ce

Please sign in to comment.