property_utils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 Object *p_object, 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 if (p_a.get_type() == Variant::NODE_PATH && p_b.get_type() == Variant::OBJECT) {
  43. // With properties of type Node, left side is NodePath, while right side is Node.
  44. const Node *base_node = Object::cast_to<Node>(p_object);
  45. const Node *target_node = Object::cast_to<Node>(p_b);
  46. if (base_node && target_node) {
  47. return p_a != base_node->get_path_to(target_node);
  48. }
  49. }
  50. if (p_a.get_type() == Variant::ARRAY && p_b.get_type() == Variant::ARRAY) {
  51. const Node *base_node = Object::cast_to<Node>(p_object);
  52. Array array1 = p_a;
  53. Array array2 = p_b;
  54. if (base_node && !array1.is_empty() && array2.size() == array1.size() && array1[0].get_type() == Variant::NODE_PATH && array2[0].get_type() == Variant::OBJECT) {
  55. // Like above, but NodePaths/Nodes are inside arrays.
  56. for (int i = 0; i < array1.size(); i++) {
  57. const Node *target_node = Object::cast_to<Node>(array2[i]);
  58. if (!target_node || array1[i] != base_node->get_path_to(target_node)) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. }
  65. // For our purposes, treating null object as NIL is the right thing to do
  66. const Variant &a = p_a.get_type() == Variant::OBJECT && (Object *)p_a == nullptr ? Variant() : p_a;
  67. const Variant &b = p_b.get_type() == Variant::OBJECT && (Object *)p_b == nullptr ? Variant() : p_b;
  68. return a != b;
  69. }
  70. 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) {
  71. // This function obeys the way property values are set when an object is instantiated,
  72. // which is the following (the latter wins):
  73. // 1. Default value from builtin class
  74. // 2. Default value from script exported variable (from the topmost script)
  75. // 3. Value overrides from the instantiation/inheritance stack
  76. if (r_is_class_default) {
  77. *r_is_class_default = false;
  78. }
  79. if (r_is_valid) {
  80. *r_is_valid = false;
  81. }
  82. // Handle special case "script" property, where the default value is either null or the custom type script.
  83. // Do this only if there's no states stack cache to trace for default values.
  84. if (!p_states_stack_cache && p_property == CoreStringName(script) && p_object->has_meta(SceneStringName(_custom_type_script))) {
  85. Ref<Script> ct_scr = p_object->get_meta(SceneStringName(_custom_type_script));
  86. if (r_is_valid) {
  87. *r_is_valid = true;
  88. }
  89. return ct_scr;
  90. }
  91. Ref<Script> topmost_script;
  92. if (const Node *node = Object::cast_to<Node>(p_object)) {
  93. // Check inheritance/instantiation ancestors
  94. const Vector<SceneState::PackState> &states_stack = p_states_stack_cache ? *p_states_stack_cache : PropertyUtils::get_node_states_stack(node, p_owner);
  95. for (int i = 0; i < states_stack.size(); ++i) {
  96. const SceneState::PackState &ia = states_stack[i];
  97. bool found = false;
  98. bool node_deferred = false;
  99. Variant value_in_ancestor = ia.state->get_property_value(ia.node, p_property, found, node_deferred);
  100. if (found) {
  101. if (r_is_valid) {
  102. *r_is_valid = true;
  103. }
  104. // Replace properties stored as NodePaths with actual Nodes.
  105. // Otherwise, the property value would be considered as overridden.
  106. if (node_deferred) {
  107. if (value_in_ancestor.get_type() == Variant::ARRAY) {
  108. Array paths = value_in_ancestor;
  109. bool valid = false;
  110. Array array = node->get(p_property, &valid);
  111. ERR_CONTINUE(!valid);
  112. array = array.duplicate();
  113. array.resize(paths.size());
  114. for (int j = 0; j < array.size(); j++) {
  115. array.set(j, node->get_node_or_null(paths[j]));
  116. }
  117. value_in_ancestor = array;
  118. } else {
  119. value_in_ancestor = node->get_node_or_null(value_in_ancestor);
  120. }
  121. }
  122. return value_in_ancestor;
  123. }
  124. // Save script for later
  125. bool has_script = false;
  126. Variant script = ia.state->get_property_value(ia.node, SNAME("script"), has_script, node_deferred);
  127. if (has_script) {
  128. Ref<Script> scr = script;
  129. if (scr.is_valid()) {
  130. topmost_script = scr;
  131. }
  132. }
  133. }
  134. }
  135. // Let's see what default is set by the topmost script having a default, if any
  136. if (topmost_script.is_null()) {
  137. topmost_script = p_object->get_script();
  138. }
  139. if (topmost_script.is_valid()) {
  140. // Should be called in the editor only and not at runtime,
  141. // otherwise it can cause problems because of missing instance state support
  142. if (p_update_exports && Engine::get_singleton()->is_editor_hint()) {
  143. topmost_script->update_exports();
  144. }
  145. Variant default_value;
  146. if (topmost_script->get_property_default_value(p_property, default_value)) {
  147. if (r_is_valid) {
  148. *r_is_valid = true;
  149. }
  150. return default_value;
  151. }
  152. }
  153. // Fall back to the default from the native class
  154. {
  155. if (r_is_class_default) {
  156. *r_is_class_default = true;
  157. }
  158. bool valid = false;
  159. Variant value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property, &valid);
  160. if (valid) {
  161. if (r_is_valid) {
  162. *r_is_valid = true;
  163. }
  164. return value;
  165. } else {
  166. // Heuristically check if this is a synthetic property (whatever/0, whatever/1, etc.)
  167. // because they are not in the class DB yet must have a default (null).
  168. String prop_str = String(p_property);
  169. int p = prop_str.rfind_char('/');
  170. if (p != -1 && p < prop_str.length() - 1) {
  171. bool all_digits = true;
  172. for (int i = p + 1; i < prop_str.length(); i++) {
  173. if (!is_digit(prop_str[i])) {
  174. all_digits = false;
  175. break;
  176. }
  177. }
  178. if (r_is_valid) {
  179. *r_is_valid = all_digits;
  180. }
  181. }
  182. return Variant();
  183. }
  184. }
  185. }
  186. // Like SceneState::PackState, but using a raw pointer to avoid the cost of
  187. // updating the reference count during the internal work of the functions below
  188. namespace {
  189. struct _FastPackState {
  190. SceneState *state = nullptr;
  191. int node = -1;
  192. };
  193. } // namespace
  194. static bool _collect_inheritance_chain(const Ref<SceneState> &p_state, const NodePath &p_path, LocalVector<_FastPackState> &r_states_stack) {
  195. bool found = false;
  196. LocalVector<_FastPackState> inheritance_states;
  197. Ref<SceneState> state = p_state;
  198. while (state.is_valid()) {
  199. int node = state->find_node_by_path(p_path);
  200. if (node >= 0) {
  201. // This one has state for this node
  202. inheritance_states.push_back({ state.ptr(), node });
  203. found = true;
  204. }
  205. state = state->get_base_scene_state();
  206. }
  207. if (inheritance_states.size() > 0) {
  208. for (int i = inheritance_states.size() - 1; i >= 0; --i) {
  209. r_states_stack.push_back(inheritance_states[i]);
  210. }
  211. }
  212. return found;
  213. }
  214. Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p_node, const Node *p_owner, bool *r_instantiated_by_owner) {
  215. if (r_instantiated_by_owner) {
  216. *r_instantiated_by_owner = true;
  217. }
  218. LocalVector<_FastPackState> states_stack;
  219. {
  220. const Node *owner = p_owner;
  221. #ifdef TOOLS_ENABLED
  222. if (!p_owner && Engine::get_singleton()->is_editor_hint()) {
  223. owner = EditorNode::get_singleton()->get_edited_scene();
  224. }
  225. #endif
  226. const Node *n = p_node;
  227. while (n) {
  228. if (n == owner) {
  229. const Ref<SceneState> &state = n->get_scene_inherited_state();
  230. if (_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack)) {
  231. if (r_instantiated_by_owner) {
  232. *r_instantiated_by_owner = false;
  233. }
  234. }
  235. break;
  236. } else if (!n->get_scene_file_path().is_empty()) {
  237. const Ref<SceneState> &state = n->get_scene_instance_state();
  238. _collect_inheritance_chain(state, n->get_path_to(p_node), states_stack);
  239. }
  240. n = n->get_owner();
  241. }
  242. }
  243. // Convert to the proper type for returning, inverting the vector on the go
  244. // (it was more convenient to fill the vector in reverse order)
  245. Vector<SceneState::PackState> states_stack_ret;
  246. {
  247. states_stack_ret.resize(states_stack.size());
  248. _FastPackState *ps = states_stack.ptr();
  249. if (states_stack.size() > 0) {
  250. for (int i = states_stack.size() - 1; i >= 0; --i) {
  251. states_stack_ret.write[i].state.reference_ptr(ps->state);
  252. states_stack_ret.write[i].node = ps->node;
  253. ++ps;
  254. }
  255. }
  256. }
  257. return states_stack_ret;
  258. }