property_utils.cpp 7.5 KB

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