skeleton_modifier_3d.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**************************************************************************/
  2. /* skeleton_modifier_3d.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 "skeleton_modifier_3d.h"
  31. void SkeletonModifier3D::_validate_property(PropertyInfo &p_property) const {
  32. //
  33. }
  34. PackedStringArray SkeletonModifier3D::get_configuration_warnings() const {
  35. PackedStringArray warnings = Node3D::get_configuration_warnings();
  36. if (skeleton_id.is_null()) {
  37. warnings.push_back(RTR("Skeleton3D node not set! SkeletonModifier3D must be child of Skeleton3D."));
  38. }
  39. return warnings;
  40. }
  41. /* Skeleton3D */
  42. Skeleton3D *SkeletonModifier3D::get_skeleton() const {
  43. return ObjectDB::get_instance<Skeleton3D>(skeleton_id);
  44. }
  45. void SkeletonModifier3D::_update_skeleton_path() {
  46. skeleton_id = ObjectID();
  47. // Make sure parent is a Skeleton3D.
  48. Skeleton3D *sk = Object::cast_to<Skeleton3D>(get_parent());
  49. if (sk) {
  50. skeleton_id = sk->get_instance_id();
  51. }
  52. }
  53. void SkeletonModifier3D::_update_skeleton() {
  54. if (!is_inside_tree()) {
  55. return;
  56. }
  57. Skeleton3D *old_sk = get_skeleton();
  58. _update_skeleton_path();
  59. Skeleton3D *new_sk = get_skeleton();
  60. if (old_sk != new_sk) {
  61. _skeleton_changed(old_sk, new_sk);
  62. }
  63. update_configuration_warnings();
  64. }
  65. void SkeletonModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
  66. //
  67. }
  68. void SkeletonModifier3D::_force_update_skeleton_skin() {
  69. if (!is_inside_tree()) {
  70. return;
  71. }
  72. Skeleton3D *skeleton = get_skeleton();
  73. if (!skeleton) {
  74. return;
  75. }
  76. skeleton->force_update_deferred();
  77. }
  78. /* Process */
  79. void SkeletonModifier3D::set_active(bool p_active) {
  80. if (active == p_active) {
  81. return;
  82. }
  83. active = p_active;
  84. _set_active(active);
  85. _force_update_skeleton_skin();
  86. }
  87. bool SkeletonModifier3D::is_active() const {
  88. return active;
  89. }
  90. void SkeletonModifier3D::_set_active(bool p_active) {
  91. //
  92. }
  93. void SkeletonModifier3D::set_influence(real_t p_influence) {
  94. influence = p_influence;
  95. }
  96. real_t SkeletonModifier3D::get_influence() const {
  97. return influence;
  98. }
  99. void SkeletonModifier3D::process_modification(double p_delta) {
  100. if (!active) {
  101. return;
  102. }
  103. _process_modification(p_delta);
  104. emit_signal(SNAME("modification_processed"));
  105. }
  106. void SkeletonModifier3D::_process_modification(double p_delta) {
  107. if (GDVIRTUAL_CALL(_process_modification_with_delta, p_delta)) {
  108. return;
  109. }
  110. #ifndef DISABLE_DEPRECATED
  111. if (GDVIRTUAL_CALL(_process_modification)) {
  112. return;
  113. }
  114. #endif // DISABLE_DEPRECATED
  115. }
  116. void SkeletonModifier3D::_notification(int p_what) {
  117. switch (p_what) {
  118. case NOTIFICATION_ENTER_TREE:
  119. case NOTIFICATION_PARENTED: {
  120. _update_skeleton();
  121. } break;
  122. case NOTIFICATION_EXIT_TREE:
  123. case NOTIFICATION_UNPARENTED: {
  124. _force_update_skeleton_skin();
  125. } break;
  126. }
  127. }
  128. void SkeletonModifier3D::_bind_methods() {
  129. ClassDB::bind_method(D_METHOD("get_skeleton"), &SkeletonModifier3D::get_skeleton);
  130. ClassDB::bind_method(D_METHOD("set_active", "active"), &SkeletonModifier3D::set_active);
  131. ClassDB::bind_method(D_METHOD("is_active"), &SkeletonModifier3D::is_active);
  132. ClassDB::bind_method(D_METHOD("set_influence", "influence"), &SkeletonModifier3D::set_influence);
  133. ClassDB::bind_method(D_METHOD("get_influence"), &SkeletonModifier3D::get_influence);
  134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  135. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "influence", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_influence", "get_influence");
  136. ADD_SIGNAL(MethodInfo("modification_processed"));
  137. GDVIRTUAL_BIND(_process_modification_with_delta, "delta");
  138. #ifndef DISABLE_DEPRECATED
  139. GDVIRTUAL_BIND(_process_modification);
  140. #endif
  141. BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_X);
  142. BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_X);
  143. BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_Y);
  144. BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_Y);
  145. BIND_ENUM_CONSTANT(BONE_AXIS_PLUS_Z);
  146. BIND_ENUM_CONSTANT(BONE_AXIS_MINUS_Z);
  147. }
  148. Vector3 SkeletonModifier3D::get_vector_from_bone_axis(BoneAxis p_axis) {
  149. Vector3 ret;
  150. switch (p_axis) {
  151. case BONE_AXIS_PLUS_X: {
  152. ret = Vector3(1, 0, 0);
  153. } break;
  154. case BONE_AXIS_MINUS_X: {
  155. ret = Vector3(-1, 0, 0);
  156. } break;
  157. case BONE_AXIS_PLUS_Y: {
  158. ret = Vector3(0, 1, 0);
  159. } break;
  160. case BONE_AXIS_MINUS_Y: {
  161. ret = Vector3(0, -1, 0);
  162. } break;
  163. case BONE_AXIS_PLUS_Z: {
  164. ret = Vector3(0, 0, 1);
  165. } break;
  166. case BONE_AXIS_MINUS_Z: {
  167. ret = Vector3(0, 0, -1);
  168. } break;
  169. }
  170. return ret;
  171. }
  172. Vector3 SkeletonModifier3D::get_vector_from_axis(Vector3::Axis p_axis) {
  173. Vector3 ret;
  174. switch (p_axis) {
  175. case Vector3::AXIS_X: {
  176. ret = Vector3(1, 0, 0);
  177. } break;
  178. case Vector3::AXIS_Y: {
  179. ret = Vector3(0, 1, 0);
  180. } break;
  181. case Vector3::AXIS_Z: {
  182. ret = Vector3(0, 0, 1);
  183. } break;
  184. }
  185. return ret;
  186. }
  187. Vector3::Axis SkeletonModifier3D::get_axis_from_bone_axis(BoneAxis p_axis) {
  188. Vector3::Axis ret = Vector3::AXIS_X;
  189. switch (p_axis) {
  190. case BONE_AXIS_PLUS_X:
  191. case BONE_AXIS_MINUS_X: {
  192. ret = Vector3::AXIS_X;
  193. } break;
  194. case BONE_AXIS_PLUS_Y:
  195. case BONE_AXIS_MINUS_Y: {
  196. ret = Vector3::AXIS_Y;
  197. } break;
  198. case BONE_AXIS_PLUS_Z:
  199. case BONE_AXIS_MINUS_Z: {
  200. ret = Vector3::AXIS_Z;
  201. } break;
  202. }
  203. return ret;
  204. }
  205. SkeletonModifier3D::SkeletonModifier3D() {
  206. }