diff options
author | Pierre-Thomas Meisels <meisels27@yahoo.fr> | 2022-04-18 20:17:03 +0200 |
---|---|---|
committer | Pierre-Thomas Meisels <meisels27@yahoo.fr> | 2022-06-23 19:19:22 +0200 |
commit | c28936ba6d109b9880d34e9b1f41d409428d109b (patch) | |
tree | 867c8b07521918663cd43249a1e378e9dd03ad4e /core | |
parent | 462127eff08c8ca60a1e4a476153bdb60d63b890 (diff) |
Add core types enums description to extension api json
Diffstat (limited to 'core')
-rw-r--r-- | core/extension/extension_api_dump.cpp | 32 | ||||
-rw-r--r-- | core/variant/variant.h | 4 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 82 |
3 files changed, 118 insertions, 0 deletions
diff --git a/core/extension/extension_api_dump.cpp b/core/extension/extension_api_dump.cpp index edd48cf9cd..9d846f87c2 100644 --- a/core/extension/extension_api_dump.cpp +++ b/core/extension/extension_api_dump.cpp @@ -474,6 +474,38 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() { } } { + //enums + Array enums; + + List<StringName> enum_names; + Variant::get_enums_for_type(type, &enum_names); + for (const StringName &enum_name : enum_names) { + Dictionary enum_dict; + enum_dict["name"] = String(enum_name); + + List<StringName> enumeration_names; + Variant::get_enumerations_for_enum(type, enum_name, &enumeration_names); + + Array values; + + for (const StringName &enumeration : enumeration_names) { + Dictionary values_dict; + values_dict["name"] = String(enumeration); + values_dict["value"] = Variant::get_enum_value(type, enum_name, enumeration); + values.push_back(values_dict); + } + + if (values.size()) { + enum_dict["values"] = values; + } + enums.push_back(enum_dict); + } + + if (enums.size()) { + d["enums"] = enums; + } + } + { //operators Array operators; diff --git a/core/variant/variant.h b/core/variant/variant.h index 726ba120b5..992d9cad40 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -716,6 +716,10 @@ public: static bool has_constant(Variant::Type p_type, const StringName &p_value); static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr); + static void get_enums_for_type(Variant::Type p_type, List<StringName> *p_enums); + static void get_enumerations_for_enum(Variant::Type p_type, StringName p_enum_name, List<StringName> *p_enumerations); + static int get_enum_value(Variant::Type p_type, StringName p_enum_name, StringName p_enumeration, bool *r_valid = nullptr); + typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud); typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index cb9dfe478b..8e420ecf04 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -944,9 +944,20 @@ struct _VariantCall { constant_data[p_type].variant_value_ordered.push_back(p_constant_name); #endif } + + struct EnumData { + HashMap<StringName, HashMap<StringName, int>> value; + }; + + static EnumData *enum_data; + + static void add_enum_constant(int p_type, StringName p_enum_type_name, StringName p_enumeration_name, int p_enum_value) { + enum_data[p_type].value[p_enum_type_name][p_enumeration_name] = p_enum_value; + } }; _VariantCall::ConstantData *_VariantCall::constant_data = nullptr; +_VariantCall::EnumData *_VariantCall::enum_data = nullptr; struct VariantBuiltInMethodInfo { void (*call)(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector<Variant> &p_defvals, Callable::CallError &r_error) = nullptr; @@ -1300,6 +1311,54 @@ Variant Variant::get_constant_value(Variant::Type p_type, const StringName &p_va return E->value; } +void Variant::get_enums_for_type(Variant::Type p_type, List<StringName> *p_enums) { + ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX); + + _VariantCall::EnumData &enum_data = _VariantCall::enum_data[p_type]; + + for (const KeyValue<StringName, HashMap<StringName, int>> &E : enum_data.value) { + p_enums->push_back(E.key); + } +} + +void Variant::get_enumerations_for_enum(Variant::Type p_type, StringName p_enum_name, List<StringName> *p_enumerations) { + ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX); + + _VariantCall::EnumData &enum_data = _VariantCall::enum_data[p_type]; + + for (const KeyValue<StringName, HashMap<StringName, int>> &E : enum_data.value) { + for (const KeyValue<StringName, int> &V : E.value) { + p_enumerations->push_back(V.key); + } + } +} + +int Variant::get_enum_value(Variant::Type p_type, StringName p_enum_name, StringName p_enumeration, bool *r_valid) { + if (r_valid) { + *r_valid = false; + } + + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1); + + _VariantCall::EnumData &enum_data = _VariantCall::enum_data[p_type]; + + HashMap<StringName, HashMap<StringName, int>>::Iterator E = enum_data.value.find(p_enum_name); + if (!E) { + return -1; + } + + HashMap<StringName, int>::Iterator V = E->value.find(p_enumeration); + if (!V) { + return -1; + } + + if (r_valid) { + *r_valid = true; + } + + return V->value; +} + #ifdef DEBUG_METHODS_ENABLED #define bind_method(m_type, m_method, m_arg_names, m_default_args) \ METHOD_CLASS(m_type, m_method, &m_type::m_method); \ @@ -1360,6 +1419,7 @@ Variant Variant::get_constant_value(Variant::Type p_type, const StringName &p_va static void _register_variant_builtin_methods() { _VariantCall::constant_data = memnew_arr(_VariantCall::ConstantData, Variant::VARIANT_MAX); + _VariantCall::enum_data = memnew_arr(_VariantCall::EnumData, Variant::VARIANT_MAX); builtin_method_info = memnew_arr(BuiltinMethodMap, Variant::VARIANT_MAX); builtin_method_names = memnew_arr(List<StringName>, Variant::VARIANT_MAX); @@ -2124,6 +2184,10 @@ static void _register_variant_builtin_methods() { _VariantCall::add_constant(Variant::VECTOR3, "AXIS_Y", Vector3::AXIS_Y); _VariantCall::add_constant(Variant::VECTOR3, "AXIS_Z", Vector3::AXIS_Z); + _VariantCall::add_enum_constant(Variant::VECTOR3, "Axis", "AXIS_X", Vector3::AXIS_X); + _VariantCall::add_enum_constant(Variant::VECTOR3, "Axis", "AXIS_Y", Vector3::AXIS_Y); + _VariantCall::add_enum_constant(Variant::VECTOR3, "Axis", "AXIS_Z", Vector3::AXIS_Z); + _VariantCall::add_variant_constant(Variant::VECTOR3, "ZERO", Vector3(0, 0, 0)); _VariantCall::add_variant_constant(Variant::VECTOR3, "ONE", Vector3(1, 1, 1)); _VariantCall::add_variant_constant(Variant::VECTOR3, "INF", Vector3(INFINITY, INFINITY, INFINITY)); @@ -2138,6 +2202,10 @@ static void _register_variant_builtin_methods() { _VariantCall::add_constant(Variant::VECTOR3I, "AXIS_Y", Vector3i::AXIS_Y); _VariantCall::add_constant(Variant::VECTOR3I, "AXIS_Z", Vector3i::AXIS_Z); + _VariantCall::add_enum_constant(Variant::VECTOR3I, "Axis", "AXIS_X", Vector3i::AXIS_X); + _VariantCall::add_enum_constant(Variant::VECTOR3I, "Axis", "AXIS_Y", Vector3i::AXIS_Y); + _VariantCall::add_enum_constant(Variant::VECTOR3I, "Axis", "AXIS_Z", Vector3i::AXIS_Z); + _VariantCall::add_variant_constant(Variant::VECTOR3I, "ZERO", Vector3i(0, 0, 0)); _VariantCall::add_variant_constant(Variant::VECTOR3I, "ONE", Vector3i(1, 1, 1)); _VariantCall::add_variant_constant(Variant::VECTOR3I, "LEFT", Vector3i(-1, 0, 0)); @@ -2150,9 +2218,15 @@ static void _register_variant_builtin_methods() { _VariantCall::add_constant(Variant::VECTOR2, "AXIS_X", Vector2::AXIS_X); _VariantCall::add_constant(Variant::VECTOR2, "AXIS_Y", Vector2::AXIS_Y); + _VariantCall::add_enum_constant(Variant::VECTOR2, "Axis", "AXIS_X", Vector2::AXIS_X); + _VariantCall::add_enum_constant(Variant::VECTOR2, "Axis", "AXIS_Y", Vector2::AXIS_Y); + _VariantCall::add_constant(Variant::VECTOR2I, "AXIS_X", Vector2i::AXIS_X); _VariantCall::add_constant(Variant::VECTOR2I, "AXIS_Y", Vector2i::AXIS_Y); + _VariantCall::add_enum_constant(Variant::VECTOR2I, "Axis", "AXIS_X", Vector2i::AXIS_X); + _VariantCall::add_enum_constant(Variant::VECTOR2I, "Axis", "AXIS_Y", Vector2i::AXIS_Y); + _VariantCall::add_variant_constant(Variant::VECTOR2, "ZERO", Vector2(0, 0)); _VariantCall::add_variant_constant(Variant::VECTOR2, "ONE", Vector2(1, 1)); _VariantCall::add_variant_constant(Variant::VECTOR2, "INF", Vector2(INFINITY, INFINITY)); @@ -2175,6 +2249,13 @@ static void _register_variant_builtin_methods() { _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_ZXY", Basis::EULER_ORDER_ZXY); _VariantCall::add_constant(Variant::BASIS, "EULER_ORDER_ZYX", Basis::EULER_ORDER_ZYX); + _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_XYZ", Basis::EULER_ORDER_XYZ); + _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_XZY", Basis::EULER_ORDER_XZY); + _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_YXZ", Basis::EULER_ORDER_YXZ); + _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_YZX", Basis::EULER_ORDER_YZX); + _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_ZXY", Basis::EULER_ORDER_ZXY); + _VariantCall::add_enum_constant(Variant::BASIS, "EulerOrder", "EULER_ORDER_ZYX", Basis::EULER_ORDER_ZYX); + _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "IDENTITY", Transform2D()); _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "FLIP_X", Transform2D(-1, 0, 0, 1, 0, 0)); _VariantCall::add_variant_constant(Variant::TRANSFORM2D, "FLIP_Y", Transform2D(1, 0, 0, -1, 0, 0)); @@ -2213,4 +2294,5 @@ void Variant::_unregister_variant_methods() { memdelete_arr(builtin_method_names); memdelete_arr(builtin_method_info); memdelete_arr(_VariantCall::constant_data); + memdelete_arr(_VariantCall::enum_data); } |