skeleton_modification_2d.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**************************************************************************/
  2. /* skeleton_modification_2d.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_modification_2d.h"
  31. #include "scene/2d/skeleton_2d.h"
  32. #include "scene/2d/collision_object_2d.h"
  33. #include "scene/2d/collision_shape_2d.h"
  34. #include "scene/2d/physical_bone_2d.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/editor_settings.h"
  37. #endif // TOOLS_ENABLED
  38. ///////////////////////////////////////
  39. // Modification2D
  40. ///////////////////////////////////////
  41. void SkeletonModification2D::_execute(float p_delta) {
  42. GDVIRTUAL_CALL(_execute, p_delta);
  43. if (!enabled) {
  44. return;
  45. }
  46. }
  47. void SkeletonModification2D::_setup_modification(SkeletonModificationStack2D *p_stack) {
  48. stack = p_stack;
  49. if (stack) {
  50. is_setup = true;
  51. } else {
  52. WARN_PRINT("Could not setup modification with name " + get_name());
  53. }
  54. GDVIRTUAL_CALL(_setup_modification, Ref<SkeletonModificationStack2D>(p_stack));
  55. }
  56. void SkeletonModification2D::_draw_editor_gizmo() {
  57. GDVIRTUAL_CALL(_draw_editor_gizmo);
  58. }
  59. void SkeletonModification2D::set_enabled(bool p_enabled) {
  60. enabled = p_enabled;
  61. #ifdef TOOLS_ENABLED
  62. if (editor_draw_gizmo) {
  63. if (stack) {
  64. stack->set_editor_gizmos_dirty(true);
  65. }
  66. }
  67. #endif // TOOLS_ENABLED
  68. }
  69. bool SkeletonModification2D::get_enabled() {
  70. return enabled;
  71. }
  72. float SkeletonModification2D::clamp_angle(float p_angle, float p_min_bound, float p_max_bound, bool p_invert) {
  73. // Map to the 0 to 360 range (in radians though) instead of the -180 to 180 range.
  74. if (p_angle < 0) {
  75. p_angle = Math_TAU + p_angle;
  76. }
  77. // Make min and max in the range of 0 to 360 (in radians), and make sure they are in the right order
  78. if (p_min_bound < 0) {
  79. p_min_bound = Math_TAU + p_min_bound;
  80. }
  81. if (p_max_bound < 0) {
  82. p_max_bound = Math_TAU + p_max_bound;
  83. }
  84. if (p_min_bound > p_max_bound) {
  85. SWAP(p_min_bound, p_max_bound);
  86. }
  87. bool is_beyond_bounds = (p_angle < p_min_bound || p_angle > p_max_bound);
  88. bool is_within_bounds = (p_angle > p_min_bound && p_angle < p_max_bound);
  89. // Note: May not be the most optimal way to clamp, but it always constraints to the nearest angle.
  90. if ((!p_invert && is_beyond_bounds) || (p_invert && is_within_bounds)) {
  91. Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
  92. Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
  93. Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));
  94. if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
  95. p_angle = p_min_bound;
  96. } else {
  97. p_angle = p_max_bound;
  98. }
  99. }
  100. return p_angle;
  101. }
  102. void SkeletonModification2D::editor_draw_angle_constraints(Bone2D *p_operation_bone, float p_min_bound, float p_max_bound,
  103. bool p_constraint_enabled, bool p_constraint_in_localspace, bool p_constraint_inverted) {
  104. if (!p_operation_bone) {
  105. return;
  106. }
  107. Color bone_ik_color = Color(1.0, 0.65, 0.0, 0.4);
  108. #ifdef TOOLS_ENABLED
  109. if (Engine::get_singleton()->is_editor_hint()) {
  110. bone_ik_color = EDITOR_GET("editors/2d/bone_ik_color");
  111. }
  112. #endif // TOOLS_ENABLED
  113. float arc_angle_min = p_min_bound;
  114. float arc_angle_max = p_max_bound;
  115. if (arc_angle_min < 0) {
  116. arc_angle_min = (Math_PI * 2) + arc_angle_min;
  117. }
  118. if (arc_angle_max < 0) {
  119. arc_angle_max = (Math_PI * 2) + arc_angle_max;
  120. }
  121. if (arc_angle_min > arc_angle_max) {
  122. SWAP(arc_angle_min, arc_angle_max);
  123. }
  124. arc_angle_min += p_operation_bone->get_bone_angle();
  125. arc_angle_max += p_operation_bone->get_bone_angle();
  126. if (p_constraint_enabled) {
  127. if (p_constraint_in_localspace) {
  128. Node *operation_bone_parent = p_operation_bone->get_parent();
  129. Bone2D *operation_bone_parent_bone = Object::cast_to<Bone2D>(operation_bone_parent);
  130. if (operation_bone_parent_bone) {
  131. stack->skeleton->draw_set_transform(
  132. stack->skeleton->to_local(p_operation_bone->get_global_position()),
  133. operation_bone_parent_bone->get_global_rotation() - stack->skeleton->get_global_rotation());
  134. } else {
  135. stack->skeleton->draw_set_transform(stack->skeleton->to_local(p_operation_bone->get_global_position()));
  136. }
  137. } else {
  138. stack->skeleton->draw_set_transform(stack->skeleton->to_local(p_operation_bone->get_global_position()));
  139. }
  140. if (p_constraint_inverted) {
  141. stack->skeleton->draw_arc(Vector2(0, 0), p_operation_bone->get_length(),
  142. arc_angle_min + (Math_PI * 2), arc_angle_max, 32, bone_ik_color, 1.0);
  143. } else {
  144. stack->skeleton->draw_arc(Vector2(0, 0), p_operation_bone->get_length(),
  145. arc_angle_min, arc_angle_max, 32, bone_ik_color, 1.0);
  146. }
  147. stack->skeleton->draw_line(Vector2(0, 0), Vector2(Math::cos(arc_angle_min), Math::sin(arc_angle_min)) * p_operation_bone->get_length(), bone_ik_color, 1.0);
  148. stack->skeleton->draw_line(Vector2(0, 0), Vector2(Math::cos(arc_angle_max), Math::sin(arc_angle_max)) * p_operation_bone->get_length(), bone_ik_color, 1.0);
  149. } else {
  150. stack->skeleton->draw_set_transform(stack->skeleton->to_local(p_operation_bone->get_global_position()));
  151. stack->skeleton->draw_arc(Vector2(0, 0), p_operation_bone->get_length(), 0, Math_PI * 2, 32, bone_ik_color, 1.0);
  152. stack->skeleton->draw_line(Vector2(0, 0), Vector2(1, 0) * p_operation_bone->get_length(), bone_ik_color, 1.0);
  153. }
  154. }
  155. Ref<SkeletonModificationStack2D> SkeletonModification2D::get_modification_stack() {
  156. return stack;
  157. }
  158. void SkeletonModification2D::set_is_setup(bool p_setup) {
  159. is_setup = p_setup;
  160. }
  161. bool SkeletonModification2D::get_is_setup() const {
  162. return is_setup;
  163. }
  164. void SkeletonModification2D::set_execution_mode(int p_mode) {
  165. execution_mode = p_mode;
  166. }
  167. int SkeletonModification2D::get_execution_mode() const {
  168. return execution_mode;
  169. }
  170. void SkeletonModification2D::set_editor_draw_gizmo(bool p_draw_gizmo) {
  171. editor_draw_gizmo = p_draw_gizmo;
  172. #ifdef TOOLS_ENABLED
  173. if (is_setup) {
  174. if (stack) {
  175. stack->set_editor_gizmos_dirty(true);
  176. }
  177. }
  178. #endif // TOOLS_ENABLED
  179. }
  180. bool SkeletonModification2D::get_editor_draw_gizmo() const {
  181. return editor_draw_gizmo;
  182. }
  183. void SkeletonModification2D::_bind_methods() {
  184. GDVIRTUAL_BIND(_execute, "delta");
  185. GDVIRTUAL_BIND(_setup_modification, "modification_stack")
  186. GDVIRTUAL_BIND(_draw_editor_gizmo)
  187. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModification2D::set_enabled);
  188. ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModification2D::get_enabled);
  189. ClassDB::bind_method(D_METHOD("get_modification_stack"), &SkeletonModification2D::get_modification_stack);
  190. ClassDB::bind_method(D_METHOD("set_is_setup", "is_setup"), &SkeletonModification2D::set_is_setup);
  191. ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModification2D::get_is_setup);
  192. ClassDB::bind_method(D_METHOD("set_execution_mode", "execution_mode"), &SkeletonModification2D::set_execution_mode);
  193. ClassDB::bind_method(D_METHOD("get_execution_mode"), &SkeletonModification2D::get_execution_mode);
  194. ClassDB::bind_method(D_METHOD("clamp_angle", "angle", "min", "max", "invert"), &SkeletonModification2D::clamp_angle);
  195. ClassDB::bind_method(D_METHOD("set_editor_draw_gizmo", "draw_gizmo"), &SkeletonModification2D::set_editor_draw_gizmo);
  196. ClassDB::bind_method(D_METHOD("get_editor_draw_gizmo"), &SkeletonModification2D::get_editor_draw_gizmo);
  197. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");
  198. ADD_PROPERTY(PropertyInfo(Variant::INT, "execution_mode", PROPERTY_HINT_ENUM, "process,physics_process"), "set_execution_mode", "get_execution_mode");
  199. }
  200. SkeletonModification2D::SkeletonModification2D() {
  201. stack = nullptr;
  202. is_setup = false;
  203. }