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