property_utils.cpp 8.5 KB

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