property_utils.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**************************************************************************/
  2. /* property_utils.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_utils.h"
  31. #include "core/config/engine.h"
  32. #include "core/templates/local_vector.h"
  33. #include "scene/resources/packed_scene.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor/editor_node.h"
  36. #endif // TOOLS_ENABLED
  37. bool PropertyUtils::is_property_value_different(const Variant &p_a, const Variant &p_b) {
  38. if (p_a.get_type() == Variant::FLOAT && p_b.get_type() == Variant::FLOAT) {
  39. //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
  40. return !Math::is_equal_approx((float)p_a, (float)p_b);
  41. } else {
  42. // For our purposes, treating null object as NIL is the right thing to do
  43. const Variant &a = p_a.get_type() == Variant::OBJECT && (Object *)p_a == nullptr ? Variant() : p_a;
  44. const Variant &b = p_b.get_type() == Variant::OBJECT && (Object *)p_b == nullptr ? Variant() : p_b;
  45. return a != b;
  46. }
  47. }
  48. Variant PropertyUtils::get_property_default_value(const Object *p_object, const StringName &p_property, bool *r_is_valid, const Vector<SceneState::PackState> *p_states_stack_cache, bool p_update_exports, const Node *p_owner, bool *r_is_class_default) {
  49. // This function obeys the way property values are set when an object is instantiated,
  50. // which is the following (the latter wins):
  51. // 1. Default value from builtin class
  52. // 2. Default value from script exported variable (from the topmost script)
  53. // 3. Value overrides from the instantiation/inheritance stack
  54. if (r_is_class_default) {
  55. *r_is_class_default = false;
  56. }
  57. if (r_is_valid) {
  58. *r_is_valid = false;
  59. }
  60. Ref<Script> topmost_script;
  61. if (const Node *node = Object::cast_to<Node>(p_object)) {
  62. // Check inheritance/instantiation ancestors
  63. const Vector<SceneState::PackState> &states_stack = p_states_stack_cache ? *p_states_stack_cache : PropertyUtils::get_node_states_stack(node, p_owner);
  64. for (int i = 0; i < states_stack.size(); ++i) {
  65. const SceneState::PackState &ia = states_stack[i];
  66. bool found = false;
  67. Variant value_in_ancestor = ia.state->get_property_value(ia.node, p_property, found);
  68. if (found) {
  69. if (r_is_valid) {
  70. *r_is_valid = true;
  71. }
  72. return value_in_ancestor;
  73. }
  74. // Save script for later
  75. bool has_script = false;
  76. Variant script = ia.state->get_property_value(ia.node, SNAME("script"), has_script);
  77. if (has_script) {
  78. Ref<Script> scr = script;
  79. if (scr.is_valid()) {
  80. topmost_script = scr;
  81. }
  82. }
  83. }
  84. }
  85. // Let's see what default is set by the topmost script having a default, if any
  86. if (topmost_script.is_null()) {
  87. topmost_script = p_object->get_script();
  88. }
  89. if (topmost_script.is_valid()) {
  90. // Should be called in the editor only and not at runtime,
  91. // otherwise it can cause problems because of missing instance state support
  92. if (p_update_exports && Engine::get_singleton()->is_editor_hint()) {
  93. topmost_script->update_exports();
  94. }
  95. Variant default_value;
  96. if (topmost_script->get_property_default_value(p_property, default_value)) {
  97. if (r_is_valid) {
  98. *r_is_valid = true;
  99. }
  100. return default_value;
  101. }
  102. }
  103. // Fall back to the default from the native class
  104. {
  105. if (r_is_class_default) {
  106. *r_is_class_default = true;
  107. }
  108. bool valid = false;
  109. Variant value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property, &valid);
  110. if (valid) {
  111. if (r_is_valid) {
  112. *r_is_valid = true;
  113. }
  114. return value;
  115. } else {
  116. // Heuristically check if this is a synthetic property (whatever/0, whatever/1, etc.)
  117. // because they are not in the class DB yet must have a default (null).
  118. String prop_str = String(p_property);
  119. int p = prop_str.rfind("/");
  120. if (p != -1 && p < prop_str.length() - 1) {
  121. bool all_digits = true;
  122. for (int i = p + 1; i < prop_str.length(); i++) {
  123. if (!is_digit(prop_str[i])) {
  124. all_digits = false;
  125. break;
  126. }
  127. }
  128. if (r_is_valid) {
  129. *r_is_valid = all_digits;
  130. }
  131. }
  132. return Variant();
  133. }
  134. }
  135. }
  136. // Like SceneState::PackState, but using a raw pointer to avoid the cost of
  137. // updating the reference count during the internal work of the functions below
  138. namespace {
  139. struct _FastPackState {
  140. SceneState *state = nullptr;
  141. int node = -1;
  142. };
  143. } // namespace
  144. static bool _collect_inheritance_chain(const Ref<SceneState> &p_state, const NodePath &p_path, LocalVector<_FastPackState> &r_states_stack) {
  145. bool found = false;
  146. LocalVector<_FastPackState> inheritance_states;
  147. Ref<SceneState> state = p_state;
  148. while (state.is_valid()) {
  149. int node = state->find_node_by_path(p_path);
  150. if (node >= 0) {
  151. // This one has state for this node
  152. inheritance_states.push_back({ state.ptr(), node });
  153. found = true;
  154. }
  155. state = state->get_base_scene_state();
  156. }
  157. if (inheritance_states.size() > 0) {
  158. for (int i = inheritance_states.size() - 1; i >= 0; --i) {
  159. r_states_stack.push_back(inheritance_states[i]);
  160. }
  161. }
  162. return found;
  163. }
  164. Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p_node, const Node *p_owner, bool *r_instantiated_by_owner) {
  165. if (r_instantiated_by_owner) {
  166. *r_instantiated_by_owner = true;
  167. }
  168. LocalVector<_FastPackState> states_stack;
  169. {
  170. const Node *owner = p_owner;
  171. #ifdef TOOLS_ENABLED
  172. if (!p_owner && Engine::get_singleton()->is_editor_hint()) {
  173. owner = EditorNode::get_singleton()->get_edited_scene();
  174. }
  175. #endif
  176. const Node *n = p_node;
  177. while (n) {
  178. if (n == owner) {
  179. const Ref<SceneState> &state = n->get_scene_inherited_state();
  180. if (_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack)) {
  181. if (r_instantiated_by_owner) {
  182. *r_instantiated_by_owner = false;
  183. }
  184. }
  185. break;
  186. } else if (!n->get_scene_file_path().is_empty()) {
  187. const Ref<SceneState> &state = n->get_scene_instance_state();
  188. _collect_inheritance_chain(state, n->get_path_to(p_node), states_stack);
  189. }
  190. n = n->get_owner();
  191. }
  192. }
  193. // Convert to the proper type for returning, inverting the vector on the go
  194. // (it was more convenient to fill the vector in reverse order)
  195. Vector<SceneState::PackState> states_stack_ret;
  196. {
  197. states_stack_ret.resize(states_stack.size());
  198. _FastPackState *ps = states_stack.ptr();
  199. if (states_stack.size() > 0) {
  200. for (int i = states_stack.size() - 1; i >= 0; --i) {
  201. states_stack_ret.write[i].state.reference_ptr(ps->state);
  202. states_stack_ret.write[i].node = ps->node;
  203. ++ps;
  204. }
  205. }
  206. }
  207. return states_stack_ret;
  208. }