Debian Codegraph @

scene/resources/2d/skeleton/skeleton_modification_2d.cpp

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