skeleton_2d.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**************************************************************************/
  2. /* skeleton_2d.h */
  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. #ifndef SKELETON_2D_H
  31. #define SKELETON_2D_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/resources/2d/skeleton/skeleton_modification_2d.h"
  34. class Skeleton2D;
  35. class Bone2D : public Node2D {
  36. GDCLASS(Bone2D, Node2D);
  37. friend class Skeleton2D;
  38. #ifdef TOOLS_ENABLED
  39. friend class AnimatedValuesBackup;
  40. #endif
  41. Bone2D *parent_bone = nullptr;
  42. Skeleton2D *skeleton = nullptr;
  43. Transform2D rest;
  44. bool autocalculate_length_and_angle = true;
  45. real_t length = 16;
  46. real_t bone_angle = 0;
  47. int skeleton_index = -1;
  48. void calculate_length_and_rotation();
  49. #ifdef TOOLS_ENABLED
  50. RID editor_gizmo_rid;
  51. bool _editor_get_bone_shape(Vector<Vector2> *p_shape, Vector<Vector2> *p_outline_shape, Bone2D *p_other_bone);
  52. bool _editor_show_bone_gizmo = true;
  53. #endif // TOOLS ENABLED
  54. protected:
  55. void _notification(int p_what);
  56. static void _bind_methods();
  57. bool _set(const StringName &p_path, const Variant &p_value);
  58. bool _get(const StringName &p_path, Variant &r_ret) const;
  59. void _get_property_list(List<PropertyInfo> *p_list) const;
  60. public:
  61. Transform2D cache_transform;
  62. bool copy_transform_to_cache = true;
  63. void set_rest(const Transform2D &p_rest);
  64. Transform2D get_rest() const;
  65. void apply_rest();
  66. Transform2D get_skeleton_rest() const;
  67. PackedStringArray get_configuration_warnings() const override;
  68. void set_autocalculate_length_and_angle(bool p_autocalculate);
  69. bool get_autocalculate_length_and_angle() const;
  70. void set_length(real_t p_length);
  71. real_t get_length() const;
  72. void set_bone_angle(real_t p_angle);
  73. real_t get_bone_angle() const;
  74. int get_index_in_skeleton() const;
  75. #ifdef TOOLS_ENABLED
  76. void _editor_set_show_bone_gizmo(bool p_show_gizmo);
  77. bool _editor_get_show_bone_gizmo() const;
  78. #endif // TOOLS_ENABLED
  79. Bone2D();
  80. ~Bone2D();
  81. };
  82. class SkeletonModificationStack2D;
  83. class Skeleton2D : public Node2D {
  84. GDCLASS(Skeleton2D, Node2D);
  85. friend class Bone2D;
  86. #ifdef TOOLS_ENABLED
  87. friend class AnimatedValuesBackup;
  88. #endif
  89. struct Bone {
  90. bool operator<(const Bone &p_bone) const {
  91. return p_bone.bone->is_greater_than(bone);
  92. }
  93. Bone2D *bone = nullptr;
  94. int parent_index = 0;
  95. Transform2D accum_transform;
  96. Transform2D rest_inverse;
  97. //Transform2D local_pose_cache;
  98. Transform2D local_pose_override;
  99. real_t local_pose_override_amount = 0;
  100. bool local_pose_override_persistent = false;
  101. };
  102. LocalVector<Bone> bones;
  103. bool bone_setup_dirty = true;
  104. void _make_bone_setup_dirty();
  105. void _update_bone_setup();
  106. bool transform_dirty = true;
  107. void _make_transform_dirty();
  108. void _update_transform();
  109. RID skeleton;
  110. Ref<SkeletonModificationStack2D> modification_stack;
  111. ///////////////////////////////////////////////////////
  112. // INTERPOLATION
  113. struct InterpolationData {
  114. Transform2D xform_curr;
  115. Transform2D xform_prev;
  116. uint32_t last_update_physics_tick = UINT32_MAX; // Ensure tick 0 is detected as a change.
  117. } _interpolation_data;
  118. void _update_process_mode();
  119. void _ensure_update_interpolation_data();
  120. protected:
  121. virtual void _physics_interpolated_changed() override;
  122. ///////////////////////////////////////////////////////
  123. void _notification(int p_what);
  124. static void _bind_methods();
  125. bool _set(const StringName &p_path, const Variant &p_value);
  126. bool _get(const StringName &p_path, Variant &r_ret) const;
  127. void _get_property_list(List<PropertyInfo> *p_list) const;
  128. public:
  129. int get_bone_count() const;
  130. Bone2D *get_bone(int p_idx);
  131. RID get_skeleton() const;
  132. void set_bone_local_pose_override(int p_bone_idx, Transform2D p_override, real_t p_amount, bool p_persistent = true);
  133. Transform2D get_bone_local_pose_override(int p_bone_idx);
  134. Ref<SkeletonModificationStack2D> get_modification_stack() const;
  135. void set_modification_stack(Ref<SkeletonModificationStack2D> p_stack);
  136. void execute_modifications(real_t p_delta, int p_execution_mode);
  137. Skeleton2D();
  138. ~Skeleton2D();
  139. };
  140. #endif // SKELETON_2D_H