|
24 | 24 | #include "LuaScriptInstance.hpp" |
25 | 25 |
|
26 | 26 | #include "../LuaCoroutine.hpp" |
| 27 | +#include "../utils/Class.hpp" |
27 | 28 | #include "../utils/VariantArguments.hpp" |
28 | 29 | #include "../utils/VariantType.hpp" |
29 | 30 | #include "../utils/VariantTypedArray.hpp" |
30 | 31 | #include "../utils/convert_godot_lua.hpp" |
31 | 32 |
|
| 33 | +#include <godot_cpp/classes/node.hpp> |
| 34 | +#include <godot_cpp/classes/resource.hpp> |
| 35 | + |
32 | 36 | namespace luagdextension { |
33 | 37 |
|
34 | | -static LuaScriptProperty lua_property(sol::stack_object value) { |
| 38 | +static LuaScriptProperty lua_property(sol::this_state L, sol::stack_object value) { |
35 | 39 | LuaScriptProperty property; |
36 | 40 | property.usage = PROPERTY_USAGE_STORAGE; |
37 | 41 |
|
38 | | - if (auto table = value.as<sol::optional<sol::stack_table>>()) { |
39 | | - // index 1: either a Variant type or the default value |
40 | | - if (auto type = table->get<sol::optional<VariantType>>(1)) { |
41 | | - property.type = type->get_type(); |
42 | | - } |
43 | | - else if (auto typed_array = table->get<sol::optional<VariantTypedArray>>(1)) { |
44 | | - property.type = Variant::Type::ARRAY; |
45 | | - property.hint = PROPERTY_HINT_TYPE_STRING; |
46 | | - property.hint_string = typed_array->get_hint_string(); |
47 | | - } |
48 | | - else if (auto default_value = table->get<sol::optional<sol::object>>(1)) { |
49 | | - property.default_value = to_variant(*default_value); |
50 | | - } |
| 42 | + sol::stack_table table; |
| 43 | + if (value.get_type() == sol::type::table) { |
| 44 | + table = value; |
| 45 | + } |
| 46 | + else { |
| 47 | + lua_createtable(L, 1, 0); |
| 48 | + lua_pushvalue(L, value.stack_index()); |
| 49 | + lua_rawseti(L, -2, 1); |
| 50 | + table = sol::stack_table(L, -1); |
| 51 | + } |
51 | 52 |
|
52 | | - // PropertyInfo fields |
53 | | - if (auto type = table->get<sol::optional<VariantType>>("type")) { |
54 | | - property.type = type->get_type(); |
55 | | - } |
56 | | - else if (auto typed_array = table->get<sol::optional<VariantTypedArray>>("type")) { |
57 | | - property.type = Variant::Type::ARRAY; |
58 | | - property.hint = PROPERTY_HINT_TYPE_STRING; |
59 | | - property.hint_string = typed_array->get_hint_string(); |
60 | | - } |
61 | | - if (auto hint = table->get<sol::optional<uint32_t>>("hint")) { |
62 | | - property.hint = *hint; |
63 | | - } |
64 | | - if (auto hint_string = table->get<sol::optional<String>>("hint_string")) { |
65 | | - property.hint_string = *hint_string; |
66 | | - } |
67 | | - if (auto usage = table->get<sol::optional<uint32_t>>("usage")) { |
68 | | - property.usage = *usage; |
| 53 | + // index 1: either a Variant type or the default value |
| 54 | + if (auto type = table.get<sol::optional<VariantType>>(1)) { |
| 55 | + property.type = type->get_type(); |
| 56 | + property.default_value = type->construct_default(); |
| 57 | + } |
| 58 | + else if (auto typed_array = table.get<sol::optional<VariantTypedArray>>(1)) { |
| 59 | + property.type = Variant::Type::ARRAY; |
| 60 | + property.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 61 | + property.hint_string = typed_array->get_hint_string(); |
| 62 | + property.default_value = typed_array->construct_default(); |
| 63 | + } |
| 64 | + else if (auto cls = table.get<sol::optional<Class>>(1)) { |
| 65 | + property.type = Variant::Type::OBJECT; |
| 66 | + property.class_name = cls->get_name(); |
| 67 | + if (ClassDB::is_parent_class(property.class_name, Node::get_class_static())) { |
| 68 | + property.hint = PROPERTY_HINT_NODE_TYPE; |
| 69 | + property.hint_string = property.class_name; |
69 | 70 | } |
70 | | - if (auto class_name = table->get<sol::optional<StringName>>("class_name")) { |
71 | | - property.class_name = *class_name; |
| 71 | + else if (ClassDB::is_parent_class(property.class_name, Resource::get_class_static())) { |
| 72 | + property.hint = PROPERTY_HINT_RESOURCE_TYPE; |
| 73 | + property.hint_string = property.class_name; |
72 | 74 | } |
| 75 | + } |
| 76 | + else if (auto default_value = table.get<sol::optional<sol::object>>(1)) { |
| 77 | + property.default_value = to_variant(*default_value); |
| 78 | + } |
73 | 79 |
|
74 | | - // Extra LuaScriptProperty fields |
75 | | - if (auto default_value = table->get<sol::optional<sol::object>>("default")) { |
76 | | - property.default_value = to_variant(*default_value); |
77 | | - } |
78 | | - if (auto getter_name = table->get<sol::optional<StringName>>("get")) { |
79 | | - property.getter_name = *getter_name; |
80 | | - property.usage &= ~PROPERTY_USAGE_STORAGE; |
81 | | - } |
82 | | - else if (auto getter = table->get<sol::optional<sol::protected_function>>("get")) { |
83 | | - property.getter = *getter; |
84 | | - property.usage &= ~PROPERTY_USAGE_STORAGE; |
85 | | - } |
86 | | - if (auto setter_name = table->get<sol::optional<StringName>>("set")) { |
87 | | - property.setter_name = *setter_name; |
| 80 | + // PropertyInfo fields |
| 81 | + if (auto type = table.get<sol::optional<VariantType>>("type")) { |
| 82 | + property.type = type->get_type(); |
| 83 | + if (property.default_value.get_type() == Variant::Type::NIL) { |
| 84 | + property.default_value = type->construct_default(); |
88 | 85 | } |
89 | | - else if (auto setter = table->get<sol::optional<sol::protected_function>>("set")) { |
90 | | - property.setter = *setter; |
| 86 | + } |
| 87 | + else if (auto typed_array = table.get<sol::optional<VariantTypedArray>>("type")) { |
| 88 | + property.type = Variant::Type::ARRAY; |
| 89 | + property.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 90 | + property.hint_string = typed_array->get_hint_string(); |
| 91 | + if (property.default_value.get_type() == Variant::Type::NIL) { |
| 92 | + property.default_value = typed_array->construct_default(); |
91 | 93 | } |
92 | 94 | } |
93 | | - else if (auto type = value.as<sol::optional<VariantType>>()) { |
94 | | - property.type = type->get_type(); |
| 95 | + if (auto hint = table.get<sol::optional<uint32_t>>("hint")) { |
| 96 | + property.hint = *hint; |
95 | 97 | } |
96 | | - else { |
97 | | - property.default_value = to_variant(value); |
| 98 | + if (auto hint_string = table.get<sol::optional<String>>("hint_string")) { |
| 99 | + property.hint_string = *hint_string; |
| 100 | + } |
| 101 | + if (auto usage = table.get<sol::optional<uint32_t>>("usage")) { |
| 102 | + property.usage = *usage; |
| 103 | + } |
| 104 | + if (auto class_name = table.get<sol::optional<StringName>>("class_name")) { |
| 105 | + property.class_name = *class_name; |
| 106 | + } |
| 107 | + |
| 108 | + // Extra LuaScriptProperty fields |
| 109 | + if (auto default_value = table.get<sol::optional<sol::object>>("default")) { |
| 110 | + property.default_value = to_variant(*default_value); |
| 111 | + } |
| 112 | + if (auto getter_name = table.get<sol::optional<StringName>>("get")) { |
| 113 | + property.getter_name = *getter_name; |
| 114 | + property.usage &= ~PROPERTY_USAGE_STORAGE; |
| 115 | + } |
| 116 | + else if (auto getter = table.get<sol::optional<sol::protected_function>>("get")) { |
| 117 | + property.getter = *getter; |
| 118 | + property.usage &= ~PROPERTY_USAGE_STORAGE; |
| 119 | + } |
| 120 | + if (auto setter_name = table.get<sol::optional<StringName>>("set")) { |
| 121 | + property.setter_name = *setter_name; |
| 122 | + } |
| 123 | + else if (auto setter = table.get<sol::optional<sol::protected_function>>("set")) { |
| 124 | + property.setter = *setter; |
98 | 125 | } |
99 | 126 |
|
100 | 127 | if (property.type == 0) { |
101 | 128 | property.type = property.default_value.get_type(); |
| 129 | + if (property.default_value.get_type() == Variant::Type::ARRAY && Array(property.default_value).is_typed()) { |
| 130 | + property.hint = PROPERTY_HINT_ARRAY_TYPE; |
| 131 | + property.hint_string = VariantTypedArray::of(property.default_value).get_hint_string(); |
| 132 | + } |
102 | 133 | } |
103 | 134 | property.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE; |
104 | 135 | return property; |
105 | 136 | } |
106 | 137 |
|
107 | | -static LuaScriptProperty lua_export(sol::stack_object value) { |
108 | | - LuaScriptProperty property = lua_property(value); |
| 138 | +static LuaScriptProperty lua_export(sol::this_state L, sol::stack_object value) { |
| 139 | + LuaScriptProperty property = lua_property(L, value); |
109 | 140 | property.usage |= PROPERTY_USAGE_EDITOR; |
110 | 141 | return property; |
111 | 142 | } |
|
0 commit comments