skeleton_modification_stack_3d.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*************************************************************************/
  2. /* skeleton_modification_stack_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "skeleton_modification_stack_3d.h"
  31. #include "scene/3d/skeleton_3d.h"
  32. ///////////////////////////////////////
  33. // ModificationStack3D
  34. ///////////////////////////////////////
  35. void SkeletonModificationStack3D::_get_property_list(List<PropertyInfo> *p_list) const {
  36. for (uint32_t i = 0; i < modifications.size(); i++) {
  37. p_list->push_back(
  38. PropertyInfo(Variant::OBJECT, "modifications/" + itos(i),
  39. PROPERTY_HINT_RESOURCE_TYPE,
  40. "SkeletonModification3D",
  41. PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_DEFERRED_SET_RESOURCE | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  42. }
  43. }
  44. bool SkeletonModificationStack3D::_set(const StringName &p_path, const Variant &p_value) {
  45. String path = p_path;
  46. if (path.begins_with("modifications/")) {
  47. int mod_idx = path.get_slicec('/', 1).to_int();
  48. set_modification(mod_idx, p_value);
  49. return true;
  50. }
  51. return true;
  52. }
  53. bool SkeletonModificationStack3D::_get(const StringName &p_path, Variant &r_ret) const {
  54. String path = p_path;
  55. if (path.begins_with("modifications/")) {
  56. int mod_idx = path.get_slicec('/', 1).to_int();
  57. r_ret = get_modification(mod_idx);
  58. return true;
  59. }
  60. return true;
  61. }
  62. void SkeletonModificationStack3D::setup() {
  63. if (is_setup) {
  64. return;
  65. }
  66. if (skeleton != nullptr) {
  67. is_setup = true;
  68. for (uint32_t i = 0; i < modifications.size(); i++) {
  69. if (!modifications[i].is_valid()) {
  70. continue;
  71. }
  72. modifications[i]->_setup_modification(this);
  73. }
  74. } else {
  75. WARN_PRINT("Cannot setup SkeletonModificationStack3D: no skeleton set!");
  76. }
  77. }
  78. void SkeletonModificationStack3D::execute(real_t p_delta, int p_execution_mode) {
  79. ERR_FAIL_COND_MSG(!is_setup || skeleton == nullptr || is_queued_for_deletion(),
  80. "Modification stack is not properly setup and therefore cannot execute!");
  81. if (!skeleton->is_inside_tree()) {
  82. ERR_PRINT_ONCE("Skeleton is not inside SceneTree! Cannot execute modification!");
  83. return;
  84. }
  85. if (!enabled) {
  86. return;
  87. }
  88. for (uint32_t i = 0; i < modifications.size(); i++) {
  89. if (!modifications[i].is_valid()) {
  90. continue;
  91. }
  92. if (modifications[i]->get_execution_mode() == p_execution_mode) {
  93. modifications[i]->_execute(p_delta);
  94. }
  95. }
  96. }
  97. void SkeletonModificationStack3D::enable_all_modifications(bool p_enabled) {
  98. for (uint32_t i = 0; i < modifications.size(); i++) {
  99. if (!modifications[i].is_valid()) {
  100. continue;
  101. }
  102. modifications[i]->set_enabled(p_enabled);
  103. }
  104. }
  105. Ref<SkeletonModification3D> SkeletonModificationStack3D::get_modification(int p_mod_idx) const {
  106. const int modifications_size = modifications.size();
  107. ERR_FAIL_INDEX_V(p_mod_idx, modifications_size, nullptr);
  108. return modifications[p_mod_idx];
  109. }
  110. void SkeletonModificationStack3D::add_modification(Ref<SkeletonModification3D> p_mod) {
  111. ERR_FAIL_NULL(p_mod);
  112. p_mod->_setup_modification(this);
  113. modifications.push_back(p_mod);
  114. }
  115. void SkeletonModificationStack3D::delete_modification(int p_mod_idx) {
  116. const int modifications_size = modifications.size();
  117. ERR_FAIL_INDEX(p_mod_idx, modifications_size);
  118. modifications.remove_at(p_mod_idx);
  119. }
  120. void SkeletonModificationStack3D::set_modification(int p_mod_idx, Ref<SkeletonModification3D> p_mod) {
  121. const int modifications_size = modifications.size();
  122. ERR_FAIL_INDEX(p_mod_idx, modifications_size);
  123. if (p_mod == nullptr) {
  124. modifications.remove_at(p_mod_idx);
  125. } else {
  126. p_mod->_setup_modification(this);
  127. modifications[p_mod_idx] = p_mod;
  128. }
  129. }
  130. void SkeletonModificationStack3D::set_modification_count(int p_count) {
  131. ERR_FAIL_COND_MSG(p_count < 0, "Modification count cannot be less than zero.");
  132. modifications.resize(p_count);
  133. notify_property_list_changed();
  134. }
  135. int SkeletonModificationStack3D::get_modification_count() const {
  136. return modifications.size();
  137. }
  138. void SkeletonModificationStack3D::set_skeleton(Skeleton3D *p_skeleton) {
  139. skeleton = p_skeleton;
  140. }
  141. Skeleton3D *SkeletonModificationStack3D::get_skeleton() const {
  142. return skeleton;
  143. }
  144. bool SkeletonModificationStack3D::get_is_setup() const {
  145. return is_setup;
  146. }
  147. void SkeletonModificationStack3D::set_enabled(bool p_enabled) {
  148. enabled = p_enabled;
  149. if (!enabled && is_setup && skeleton != nullptr) {
  150. skeleton->clear_bones_local_pose_override();
  151. }
  152. }
  153. bool SkeletonModificationStack3D::get_enabled() const {
  154. return enabled;
  155. }
  156. void SkeletonModificationStack3D::set_strength(real_t p_strength) {
  157. ERR_FAIL_COND_MSG(p_strength < 0, "Strength cannot be less than zero!");
  158. ERR_FAIL_COND_MSG(p_strength > 1, "Strength cannot be more than one!");
  159. strength = p_strength;
  160. }
  161. real_t SkeletonModificationStack3D::get_strength() const {
  162. return strength;
  163. }
  164. void SkeletonModificationStack3D::_bind_methods() {
  165. ClassDB::bind_method(D_METHOD("setup"), &SkeletonModificationStack3D::setup);
  166. ClassDB::bind_method(D_METHOD("execute", "delta", "execution_mode"), &SkeletonModificationStack3D::execute);
  167. ClassDB::bind_method(D_METHOD("enable_all_modifications", "enabled"), &SkeletonModificationStack3D::enable_all_modifications);
  168. ClassDB::bind_method(D_METHOD("get_modification", "mod_idx"), &SkeletonModificationStack3D::get_modification);
  169. ClassDB::bind_method(D_METHOD("add_modification", "modification"), &SkeletonModificationStack3D::add_modification);
  170. ClassDB::bind_method(D_METHOD("delete_modification", "mod_idx"), &SkeletonModificationStack3D::delete_modification);
  171. ClassDB::bind_method(D_METHOD("set_modification", "mod_idx", "modification"), &SkeletonModificationStack3D::set_modification);
  172. ClassDB::bind_method(D_METHOD("set_modification_count", "count"), &SkeletonModificationStack3D::set_modification_count);
  173. ClassDB::bind_method(D_METHOD("get_modification_count"), &SkeletonModificationStack3D::get_modification_count);
  174. ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModificationStack3D::get_is_setup);
  175. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModificationStack3D::set_enabled);
  176. ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModificationStack3D::get_enabled);
  177. ClassDB::bind_method(D_METHOD("set_strength", "strength"), &SkeletonModificationStack3D::set_strength);
  178. ClassDB::bind_method(D_METHOD("get_strength"), &SkeletonModificationStack3D::get_strength);
  179. ClassDB::bind_method(D_METHOD("get_skeleton"), &SkeletonModificationStack3D::get_skeleton);
  180. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");
  181. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "strength", PROPERTY_HINT_RANGE, "0, 1, 0.001"), "set_strength", "get_strength");
  182. ADD_PROPERTY(PropertyInfo(Variant::INT, "modification_count", PROPERTY_HINT_RANGE, "0, 100, 1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY, "Modifications,modifications/"), "set_modification_count", "get_modification_count");
  183. }
  184. SkeletonModificationStack3D::SkeletonModificationStack3D() {
  185. }