Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************/
2 /* skeleton_modification_2d_lookat.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_lookat.h"
32 #include "scene/2d/skeleton_2d.h"
33
34 bool SkeletonModification2DLookAt::_set(const StringName &p_path, const Variant &p_value) {
35 String path = p_path;
36
37 if (path.begins_with("enable_constraint")) {
38 set_enable_constraint(p_value);
39 } else if (path.begins_with("constraint_angle_min")) {
40 set_constraint_angle_min(Math::deg_to_rad(float(p_value)));
41 } else if (path.begins_with("constraint_angle_max")) {
42 set_constraint_angle_max(Math::deg_to_rad(float(p_value)));
43 } else if (path.begins_with("constraint_angle_invert")) {
44 set_constraint_angle_invert(p_value);
45 } else if (path.begins_with("constraint_in_localspace")) {
46 set_constraint_in_localspace(p_value);
47 } else if (path.begins_with("additional_rotation")) {
48 set_additional_rotation(Math::deg_to_rad(float(p_value)));
49 }
50 #ifdef TOOLS_ENABLED
51 else if (path.begins_with("editor/draw_gizmo")) {
52 set_editor_draw_gizmo(p_value);
53 }
54 #endif // TOOLS_ENABLED
55 else {
56 return false;
57 }
58
59 return true;
60 }
61
62 bool SkeletonModification2DLookAt::_get(const StringName &p_path, Variant &r_ret) const {
63 String path = p_path;
64
65 if (path.begins_with("enable_constraint")) {
66 r_ret = get_enable_constraint();
67 } else if (path.begins_with("constraint_angle_min")) {
68 r_ret = Math::rad_to_deg(get_constraint_angle_min());
69 } else if (path.begins_with("constraint_angle_max")) {
70 r_ret = Math::rad_to_deg(get_constraint_angle_max());
71 } else if (path.begins_with("constraint_angle_invert")) {
72 r_ret = get_constraint_angle_invert();
73 } else if (path.begins_with("constraint_in_localspace")) {
74 r_ret = get_constraint_in_localspace();
75 } else if (path.begins_with("additional_rotation")) {
76 r_ret = Math::rad_to_deg(get_additional_rotation());
77 }
78 #ifdef TOOLS_ENABLED
79 else if (path.begins_with("editor/draw_gizmo")) {
80 r_ret = get_editor_draw_gizmo();
81 }
82 #endif // TOOLS_ENABLED
83 else {
84 return false;
85 }
86
87 return true;
88 }
89
90 void SkeletonModification2DLookAt::_get_property_list(List<PropertyInfo> *p_list) const {
91 p_list->push_back(PropertyInfo(Variant::BOOL, "enable_constraint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
92 if (enable_constraint) {
93 p_list->push_back(PropertyInfo(Variant::FLOAT, "constraint_angle_min", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
94 p_list->push_back(PropertyInfo(Variant::FLOAT, "constraint_angle_max", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
95 p_list->push_back(PropertyInfo(Variant::BOOL, "constraint_angle_invert", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
96 p_list->push_back(PropertyInfo(Variant::BOOL, "constraint_in_localspace", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
97 }
98 p_list->push_back(PropertyInfo(Variant::FLOAT, "additional_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
99
100 #ifdef TOOLS_ENABLED
101 if (Engine::get_singleton()->is_editor_hint()) {
102 p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
103 }
104 #endif // TOOLS_ENABLED
105 }
106
107 void SkeletonModification2DLookAt::_execute(float p_delta) {
108 ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
109 "Modification is not setup and therefore cannot execute!");
110 if (!enabled) {
111 return;
112 }
113
114 if (target_node_cache.is_null()) {
115 WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
116 update_target_cache();
117 return;
118 }
119
120 if (bone2d_node_cache.is_null() && !bone2d_node.is_empty()) {
121 update_bone2d_cache();
122 WARN_PRINT_ONCE("Bone2D node cache is out of date. Attempting to update...");
123 return;
124 }
125
126 if (target_node_reference == nullptr) {
127 target_node_reference = ObjectDB::get_instance<Node2D>(target_node_cache);
128 }
129 if (!target_node_reference || !target_node_reference->is_inside_tree()) {
130 ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
131 return;
132 }
133 if (bone_idx <= -1) {
134 ERR_PRINT_ONCE("Bone index is invalid. Cannot execute modification!");
135 return;
136 }
137
138 Bone2D *operation_bone = stack->skeleton->get_bone(bone_idx);
139 if (operation_bone == nullptr) {
140 ERR_PRINT_ONCE("bone_idx for modification does not point to a valid bone! Cannot execute modification");
141 return;
142 }
143
144 real_t angle_to_target = operation_bone->get_angle_to(target_node_reference->get_global_position());
145
146 // Account for the direction the bone faces in
147 angle_to_target -= operation_bone->get_bone_angle();
148
149 // Apply additional rotation
150 angle_to_target += additional_rotation;
151
152 if (enable_constraint) {
153 real_t new_angle = angle_to_target;
154
155 if (constraint_in_localspace) {
156 new_angle += operation_bone->get_rotation();
157 new_angle = clamp_angle(new_angle, constraint_angle_min, constraint_angle_max, constraint_angle_invert);
158 operation_bone->set_rotation(new_angle);
159 } else {
160 new_angle += operation_bone->get_global_rotation();
161 new_angle = clamp_angle(new_angle, constraint_angle_min, constraint_angle_max, constraint_angle_invert);
162 operation_bone->set_global_rotation(new_angle);
163 }
164 } else {
165 operation_bone->rotate(angle_to_target);
166 }
167
168 // Set the local pose override, and to make sure child bones are also updated, set the transform of the bone.
169 stack->skeleton->set_bone_local_pose_override(bone_idx, operation_bone->get_transform(), stack->strength, true);
170 }
171
172 void SkeletonModification2DLookAt::_setup_modification(SkeletonModificationStack2D *p_stack) {
173 stack = p_stack;
174
175 if (stack != nullptr) {
176 is_setup = true;
177 update_target_cache();
178 update_bone2d_cache();
179 }
180 }
181
182 void SkeletonModification2DLookAt::_draw_editor_gizmo() {
183 if (!enabled || !is_setup || bone_idx < 0) {
184 return;
185 }
186
187 Bone2D *operation_bone = stack->skeleton->get_bone(bone_idx);
188 editor_draw_angle_constraints(operation_bone, constraint_angle_min, constraint_angle_max,
189 enable_constraint, constraint_in_localspace, constraint_angle_invert);
190 }
191
192 void SkeletonModification2DLookAt::update_bone2d_cache() {
193 if (!is_setup || !stack) {
194 if (is_setup) {
195 ERR_PRINT_ONCE("Cannot update Bone2D cache: modification is not properly setup!");
196 }
197 return;
198 }
199
200 bone2d_node_cache = ObjectID();
201 if (stack->skeleton) {
202 if (stack->skeleton->is_inside_tree()) {
203 if (stack->skeleton->has_node(bone2d_node)) {
204 Node *node = stack->skeleton->get_node(bone2d_node);
205 ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
206 "Cannot update Bone2D cache: node is this modification's skeleton or cannot be found!");
207 ERR_FAIL_COND_MSG(!node->is_inside_tree(),
208 "Cannot update Bone2D cache: node is not in the scene tree!");
209 bone2d_node_cache = node->get_instance_id();
210
211 Bone2D *bone = Object::cast_to<Bone2D>(node);
212 if (bone) {
213 bone_idx = bone->get_index_in_skeleton();
214 } else {
215 ERR_FAIL_MSG("Error Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
216 }
217
218 // Set this to null so we update it
219 target_node_reference = nullptr;
220 }
221 }
222 }
223 }
224
225 void SkeletonModification2DLookAt::set_bone2d_node(const NodePath &p_target_node) {
226 bone2d_node = p_target_node;
227 update_bone2d_cache();
228 }
229
230 NodePath SkeletonModification2DLookAt::get_bone2d_node() const {
231 return bone2d_node;
232 }
233
234 int SkeletonModification2DLookAt::get_bone_index() const {
235 return bone_idx;
236 }
237
238 void SkeletonModification2DLookAt::set_bone_index(int p_bone_idx) {
239 ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
240
241 if (is_setup && stack) {
242 if (stack->skeleton) {
243 ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
244 bone_idx = p_bone_idx;
245 bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
246 bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
247 } else {
248 WARN_PRINT("Cannot verify the bone index for this modification...");
249 bone_idx = p_bone_idx;
250 }
251 } else {
252 bone_idx = p_bone_idx;
253 }
254
255 notify_property_list_changed();
256 }
257
258 void SkeletonModification2DLookAt::update_target_cache() {
259 if (!is_setup || !stack) {
260 if (is_setup) {
261 ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
262 }
263 return;
264 }
265
266 target_node_cache = ObjectID();
267 if (stack->skeleton) {
268 if (stack->skeleton->is_inside_tree()) {
269 if (stack->skeleton->has_node(target_node)) {
270 Node *node = stack->skeleton->get_node(target_node);
271 ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
272 "Cannot update target cache: node is this modification's skeleton or cannot be found!");
273 ERR_FAIL_COND_MSG(!node->is_inside_tree(),
274 "Cannot update target cache: node is not in the scene tree!");
275 target_node_cache = node->get_instance_id();
276 }
277 }
278 }
279 }
280
281 void SkeletonModification2DLookAt::set_target_node(const NodePath &p_target_node) {
282 target_node = p_target_node;
283 update_target_cache();
284 }
285
286 NodePath SkeletonModification2DLookAt::get_target_node() const {
287 return target_node;
288 }
289
290 float SkeletonModification2DLookAt::get_additional_rotation() const {
291 return additional_rotation;
292 }
293
294 void SkeletonModification2DLookAt::set_additional_rotation(float p_rotation) {
295 additional_rotation = p_rotation;
296 }
297
298 void SkeletonModification2DLookAt::set_enable_constraint(bool p_constraint) {
299 enable_constraint = p_constraint;
300 notify_property_list_changed();
301 #ifdef TOOLS_ENABLED
302 if (stack && is_setup) {
303 stack->set_editor_gizmos_dirty(true);
304 }
305 #endif // TOOLS_ENABLED
306 }
307
308 bool SkeletonModification2DLookAt::get_enable_constraint() const {
309 return enable_constraint;
310 }
311
312 void SkeletonModification2DLookAt::set_constraint_angle_min(float p_angle_min) {
313 constraint_angle_min = p_angle_min;
314 #ifdef TOOLS_ENABLED
315 if (stack && is_setup) {
316 stack->set_editor_gizmos_dirty(true);
317 }
318 #endif // TOOLS_ENABLED
319 }
320
321 float SkeletonModification2DLookAt::get_constraint_angle_min() const {
322 return constraint_angle_min;
323 }
324
325 void SkeletonModification2DLookAt::set_constraint_angle_max(float p_angle_max) {
326 constraint_angle_max = p_angle_max;
327 #ifdef TOOLS_ENABLED
328 if (stack && is_setup) {
329 stack->set_editor_gizmos_dirty(true);
330 }
331 #endif // TOOLS_ENABLED
332 }
333
334 float SkeletonModification2DLookAt::get_constraint_angle_max() const {
335 return constraint_angle_max;
336 }
337
338 void SkeletonModification2DLookAt::set_constraint_angle_invert(bool p_invert) {
339 constraint_angle_invert = p_invert;
340 #ifdef TOOLS_ENABLED
341 if (stack && is_setup) {
342 stack->set_editor_gizmos_dirty(true);
343 }
344 #endif // TOOLS_ENABLED
345 }
346
347 bool SkeletonModification2DLookAt::get_constraint_angle_invert() const {
348 return constraint_angle_invert;
349 }
350
351 void SkeletonModification2DLookAt::set_constraint_in_localspace(bool p_constraint_in_localspace) {
352 constraint_in_localspace = p_constraint_in_localspace;
353 #ifdef TOOLS_ENABLED
354 if (stack && is_setup) {
355 stack->set_editor_gizmos_dirty(true);
356 }
357 #endif // TOOLS_ENABLED
358 }
359
360 bool SkeletonModification2DLookAt::get_constraint_in_localspace() const {
361 return constraint_in_localspace;
362 }
363
364 void SkeletonModification2DLookAt::_bind_methods() {
365 ClassDB::bind_method(D_METHOD("set_bone2d_node", "bone2d_nodepath"), &SkeletonModification2DLookAt::set_bone2d_node);
366 ClassDB::bind_method(D_METHOD("get_bone2d_node"), &SkeletonModification2DLookAt::get_bone2d_node);
367 ClassDB::bind_method(D_METHOD("set_bone_index", "bone_idx"), &SkeletonModification2DLookAt::set_bone_index);
368 ClassDB::bind_method(D_METHOD("get_bone_index"), &SkeletonModification2DLookAt::get_bone_index);
369
370 ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DLookAt::set_target_node);
371 ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DLookAt::get_target_node);
372
373 ClassDB::bind_method(D_METHOD("set_additional_rotation", "rotation"), &SkeletonModification2DLookAt::set_additional_rotation);
374 ClassDB::bind_method(D_METHOD("get_additional_rotation"), &SkeletonModification2DLookAt::get_additional_rotation);
375
376 ClassDB::bind_method(D_METHOD("set_enable_constraint", "enable_constraint"), &SkeletonModification2DLookAt::set_enable_constraint);
377 ClassDB::bind_method(D_METHOD("get_enable_constraint"), &SkeletonModification2DLookAt::get_enable_constraint);
378 ClassDB::bind_method(D_METHOD("set_constraint_angle_min", "angle_min"), &SkeletonModification2DLookAt::set_constraint_angle_min);
379 ClassDB::bind_method(D_METHOD("get_constraint_angle_min"), &SkeletonModification2DLookAt::get_constraint_angle_min);
380 ClassDB::bind_method(D_METHOD("set_constraint_angle_max", "angle_max"), &SkeletonModification2DLookAt::set_constraint_angle_max);
381 ClassDB::bind_method(D_METHOD("get_constraint_angle_max"), &SkeletonModification2DLookAt::get_constraint_angle_max);
382 ClassDB::bind_method(D_METHOD("set_constraint_angle_invert", "invert"), &SkeletonModification2DLookAt::set_constraint_angle_invert);
383 ClassDB::bind_method(D_METHOD("get_constraint_angle_invert"), &SkeletonModification2DLookAt::get_constraint_angle_invert);
384
385 ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_index"), "set_bone_index", "get_bone_index");
386 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D"), "set_bone2d_node", "get_bone2d_node");
387 ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
388 }
389
390 SkeletonModification2DLookAt::SkeletonModification2DLookAt() {
391 stack = nullptr;
392 is_setup = false;
393 bone_idx = -1;
394 additional_rotation = 0;
395 enable_constraint = false;
396 constraint_angle_min = 0;
397 constraint_angle_max = Math::PI * 2;
398 constraint_angle_invert = false;
399 enabled = true;
400
401 editor_draw_gizmo = true;
402 }
403
404 SkeletonModification2DLookAt::~SkeletonModification2DLookAt() {
405 }