property_utils.cpp 8.5 KB

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