property_list_helper.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* property_list_helper.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "property_list_helper.h"
  31. Vector<PropertyListHelper *> PropertyListHelper::base_helpers; // static
  32. void PropertyListHelper::clear_base_helpers() { // static
  33. for (PropertyListHelper *helper : base_helpers) {
  34. helper->clear();
  35. }
  36. base_helpers.clear();
  37. }
  38. void PropertyListHelper::register_base_helper(PropertyListHelper *p_helper) { // static
  39. base_helpers.push_back(p_helper);
  40. }
  41. const PropertyListHelper::Property *PropertyListHelper::_get_property(const String &p_property, int *r_index) const {
  42. const Vector<String> components = p_property.rsplit("/", true, 1);
  43. if (components.size() < 2 || !components[0].begins_with(prefix)) {
  44. return nullptr;
  45. }
  46. const String index_string = components[0].trim_prefix(prefix);
  47. if (!index_string.is_valid_int()) {
  48. return nullptr;
  49. }
  50. int index = index_string.to_int();
  51. if (index < 0 || index >= _call_array_length_getter()) {
  52. return nullptr;
  53. }
  54. *r_index = index;
  55. return property_list.getptr(components[1]);
  56. }
  57. void PropertyListHelper::_call_setter(const MethodBind *p_setter, int p_index, const Variant &p_value) const {
  58. DEV_ASSERT(p_setter);
  59. Variant args[] = { p_index, p_value };
  60. const Variant *argptrs[] = { &args[0], &args[1] };
  61. Callable::CallError ce;
  62. p_setter->call(object, argptrs, 2, ce);
  63. }
  64. Variant PropertyListHelper::_call_getter(const Property *p_property, int p_index) const {
  65. if (!p_property->getter) {
  66. return object->get(prefix + itos(p_index) + "/" + p_property->info.name);
  67. }
  68. Callable::CallError ce;
  69. Variant args[] = { p_index };
  70. const Variant *argptrs[] = { &args[0] };
  71. return p_property->getter->call(object, argptrs, 1, ce);
  72. }
  73. int PropertyListHelper::_call_array_length_getter() const {
  74. Callable::CallError ce;
  75. return array_length_getter->call(object, nullptr, 0, ce);
  76. }
  77. void PropertyListHelper::set_prefix(const String &p_prefix) {
  78. prefix = p_prefix;
  79. }
  80. void PropertyListHelper::register_property(const PropertyInfo &p_info, const Variant &p_default) {
  81. Property property;
  82. property.info = p_info;
  83. property.default_value = p_default;
  84. property_list[p_info.name] = property;
  85. }
  86. bool PropertyListHelper::is_initialized() const {
  87. return !property_list.is_empty();
  88. }
  89. void PropertyListHelper::setup_for_instance(const PropertyListHelper &p_base, Object *p_object) {
  90. DEV_ASSERT(!p_base.prefix.is_empty());
  91. DEV_ASSERT(p_base.array_length_getter != nullptr);
  92. DEV_ASSERT(!p_base.property_list.is_empty());
  93. DEV_ASSERT(p_object != nullptr);
  94. prefix = p_base.prefix;
  95. array_length_getter = p_base.array_length_getter;
  96. property_list = p_base.property_list;
  97. object = p_object;
  98. }
  99. bool PropertyListHelper::is_property_valid(const String &p_property, int *r_index) const {
  100. const Vector<String> components = p_property.rsplit("/", true, 1);
  101. if (components.size() < 2 || !components[0].begins_with(prefix)) {
  102. return false;
  103. }
  104. {
  105. const String index_string = components[0].trim_prefix(prefix);
  106. if (!index_string.is_valid_int()) {
  107. return false;
  108. }
  109. if (r_index) {
  110. *r_index = index_string.to_int();
  111. }
  112. }
  113. return property_list.has(components[1]);
  114. }
  115. void PropertyListHelper::get_property_list(List<PropertyInfo> *p_list) const {
  116. const int property_count = _call_array_length_getter();
  117. for (int i = 0; i < property_count; i++) {
  118. for (const KeyValue<String, Property> &E : property_list) {
  119. const Property &property = E.value;
  120. PropertyInfo info = property.info;
  121. if (!(info.usage & PROPERTY_USAGE_STORE_IF_NULL) && _call_getter(&property, i) == property.default_value) {
  122. info.usage &= (~PROPERTY_USAGE_STORAGE);
  123. }
  124. info.name = vformat("%s%d/%s", prefix, i, info.name);
  125. p_list->push_back(info);
  126. }
  127. }
  128. }
  129. bool PropertyListHelper::property_get_value(const String &p_property, Variant &r_ret) const {
  130. int index;
  131. const Property *property = _get_property(p_property, &index);
  132. if (property) {
  133. r_ret = _call_getter(property, index);
  134. return true;
  135. }
  136. return false;
  137. }
  138. bool PropertyListHelper::property_set_value(const String &p_property, const Variant &p_value) const {
  139. int index;
  140. const Property *property = _get_property(p_property, &index);
  141. if (property) {
  142. _call_setter(property->setter, index, p_value);
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool PropertyListHelper::property_can_revert(const String &p_property) const {
  148. return is_property_valid(p_property);
  149. }
  150. bool PropertyListHelper::property_get_revert(const String &p_property, Variant &r_value) const {
  151. int index;
  152. const Property *property = _get_property(p_property, &index);
  153. if (property) {
  154. r_value = property->default_value;
  155. return true;
  156. }
  157. return false;
  158. }
  159. void PropertyListHelper::clear() {
  160. if (is_initialized()) {
  161. memdelete(array_length_getter);
  162. for (const KeyValue<String, Property> &E : property_list) {
  163. if (E.value.setter) {
  164. memdelete(E.value.setter);
  165. memdelete(E.value.getter);
  166. }
  167. }
  168. property_list.clear();
  169. }
  170. }