Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/2d/skeleton/skeleton_modification_2d_lookat.cpp
9904 views
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
Transform2D operation_transform = operation_bone->get_global_transform();
145
Transform2D target_trans = target_node_reference->get_global_transform();
146
147
// Look at the target!
148
operation_transform = operation_transform.looking_at(target_trans.get_origin());
149
// Apply whatever scale it had prior to looking_at
150
operation_transform.set_scale(operation_bone->get_global_scale());
151
152
// Account for the direction the bone faces in:
153
operation_transform.set_rotation(operation_transform.get_rotation() - operation_bone->get_bone_angle());
154
155
// Apply additional rotation
156
operation_transform.set_rotation(operation_transform.get_rotation() + additional_rotation);
157
158
// Apply constraints in globalspace:
159
if (enable_constraint && !constraint_in_localspace) {
160
operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), constraint_angle_min, constraint_angle_max, constraint_angle_invert));
161
}
162
163
// Convert from a global transform to a local transform via the Bone2D node
164
operation_bone->set_global_transform(operation_transform);
165
operation_transform = operation_bone->get_transform();
166
167
// Apply constraints in localspace:
168
if (enable_constraint && constraint_in_localspace) {
169
operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), constraint_angle_min, constraint_angle_max, constraint_angle_invert));
170
}
171
172
// Set the local pose override, and to make sure child bones are also updated, set the transform of the bone.
173
stack->skeleton->set_bone_local_pose_override(bone_idx, operation_transform, stack->strength, true);
174
operation_bone->set_transform(operation_transform);
175
}
176
177
void SkeletonModification2DLookAt::_setup_modification(SkeletonModificationStack2D *p_stack) {
178
stack = p_stack;
179
180
if (stack != nullptr) {
181
is_setup = true;
182
update_target_cache();
183
update_bone2d_cache();
184
}
185
}
186
187
void SkeletonModification2DLookAt::_draw_editor_gizmo() {
188
if (!enabled || !is_setup) {
189
return;
190
}
191
192
Bone2D *operation_bone = stack->skeleton->get_bone(bone_idx);
193
editor_draw_angle_constraints(operation_bone, constraint_angle_min, constraint_angle_max,
194
enable_constraint, constraint_in_localspace, constraint_angle_invert);
195
}
196
197
void SkeletonModification2DLookAt::update_bone2d_cache() {
198
if (!is_setup || !stack) {
199
if (is_setup) {
200
ERR_PRINT_ONCE("Cannot update Bone2D cache: modification is not properly setup!");
201
}
202
return;
203
}
204
205
bone2d_node_cache = ObjectID();
206
if (stack->skeleton) {
207
if (stack->skeleton->is_inside_tree()) {
208
if (stack->skeleton->has_node(bone2d_node)) {
209
Node *node = stack->skeleton->get_node(bone2d_node);
210
ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
211
"Cannot update Bone2D cache: node is this modification's skeleton or cannot be found!");
212
ERR_FAIL_COND_MSG(!node->is_inside_tree(),
213
"Cannot update Bone2D cache: node is not in the scene tree!");
214
bone2d_node_cache = node->get_instance_id();
215
216
Bone2D *bone = Object::cast_to<Bone2D>(node);
217
if (bone) {
218
bone_idx = bone->get_index_in_skeleton();
219
} else {
220
ERR_FAIL_MSG("Error Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
221
}
222
223
// Set this to null so we update it
224
target_node_reference = nullptr;
225
}
226
}
227
}
228
}
229
230
void SkeletonModification2DLookAt::set_bone2d_node(const NodePath &p_target_node) {
231
bone2d_node = p_target_node;
232
update_bone2d_cache();
233
}
234
235
NodePath SkeletonModification2DLookAt::get_bone2d_node() const {
236
return bone2d_node;
237
}
238
239
int SkeletonModification2DLookAt::get_bone_index() const {
240
return bone_idx;
241
}
242
243
void SkeletonModification2DLookAt::set_bone_index(int p_bone_idx) {
244
ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
245
246
if (is_setup && stack) {
247
if (stack->skeleton) {
248
ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
249
bone_idx = p_bone_idx;
250
bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
251
bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
252
} else {
253
WARN_PRINT("Cannot verify the bone index for this modification...");
254
bone_idx = p_bone_idx;
255
}
256
} else {
257
bone_idx = p_bone_idx;
258
}
259
260
notify_property_list_changed();
261
}
262
263
void SkeletonModification2DLookAt::update_target_cache() {
264
if (!is_setup || !stack) {
265
if (is_setup) {
266
ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
267
}
268
return;
269
}
270
271
target_node_cache = ObjectID();
272
if (stack->skeleton) {
273
if (stack->skeleton->is_inside_tree()) {
274
if (stack->skeleton->has_node(target_node)) {
275
Node *node = stack->skeleton->get_node(target_node);
276
ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
277
"Cannot update target cache: node is this modification's skeleton or cannot be found!");
278
ERR_FAIL_COND_MSG(!node->is_inside_tree(),
279
"Cannot update target cache: node is not in the scene tree!");
280
target_node_cache = node->get_instance_id();
281
}
282
}
283
}
284
}
285
286
void SkeletonModification2DLookAt::set_target_node(const NodePath &p_target_node) {
287
target_node = p_target_node;
288
update_target_cache();
289
}
290
291
NodePath SkeletonModification2DLookAt::get_target_node() const {
292
return target_node;
293
}
294
295
float SkeletonModification2DLookAt::get_additional_rotation() const {
296
return additional_rotation;
297
}
298
299
void SkeletonModification2DLookAt::set_additional_rotation(float p_rotation) {
300
additional_rotation = p_rotation;
301
}
302
303
void SkeletonModification2DLookAt::set_enable_constraint(bool p_constraint) {
304
enable_constraint = p_constraint;
305
notify_property_list_changed();
306
#ifdef TOOLS_ENABLED
307
if (stack && is_setup) {
308
stack->set_editor_gizmos_dirty(true);
309
}
310
#endif // TOOLS_ENABLED
311
}
312
313
bool SkeletonModification2DLookAt::get_enable_constraint() const {
314
return enable_constraint;
315
}
316
317
void SkeletonModification2DLookAt::set_constraint_angle_min(float p_angle_min) {
318
constraint_angle_min = p_angle_min;
319
#ifdef TOOLS_ENABLED
320
if (stack && is_setup) {
321
stack->set_editor_gizmos_dirty(true);
322
}
323
#endif // TOOLS_ENABLED
324
}
325
326
float SkeletonModification2DLookAt::get_constraint_angle_min() const {
327
return constraint_angle_min;
328
}
329
330
void SkeletonModification2DLookAt::set_constraint_angle_max(float p_angle_max) {
331
constraint_angle_max = p_angle_max;
332
#ifdef TOOLS_ENABLED
333
if (stack && is_setup) {
334
stack->set_editor_gizmos_dirty(true);
335
}
336
#endif // TOOLS_ENABLED
337
}
338
339
float SkeletonModification2DLookAt::get_constraint_angle_max() const {
340
return constraint_angle_max;
341
}
342
343
void SkeletonModification2DLookAt::set_constraint_angle_invert(bool p_invert) {
344
constraint_angle_invert = p_invert;
345
#ifdef TOOLS_ENABLED
346
if (stack && is_setup) {
347
stack->set_editor_gizmos_dirty(true);
348
}
349
#endif // TOOLS_ENABLED
350
}
351
352
bool SkeletonModification2DLookAt::get_constraint_angle_invert() const {
353
return constraint_angle_invert;
354
}
355
356
void SkeletonModification2DLookAt::set_constraint_in_localspace(bool p_constraint_in_localspace) {
357
constraint_in_localspace = p_constraint_in_localspace;
358
#ifdef TOOLS_ENABLED
359
if (stack && is_setup) {
360
stack->set_editor_gizmos_dirty(true);
361
}
362
#endif // TOOLS_ENABLED
363
}
364
365
bool SkeletonModification2DLookAt::get_constraint_in_localspace() const {
366
return constraint_in_localspace;
367
}
368
369
void SkeletonModification2DLookAt::_bind_methods() {
370
ClassDB::bind_method(D_METHOD("set_bone2d_node", "bone2d_nodepath"), &SkeletonModification2DLookAt::set_bone2d_node);
371
ClassDB::bind_method(D_METHOD("get_bone2d_node"), &SkeletonModification2DLookAt::get_bone2d_node);
372
ClassDB::bind_method(D_METHOD("set_bone_index", "bone_idx"), &SkeletonModification2DLookAt::set_bone_index);
373
ClassDB::bind_method(D_METHOD("get_bone_index"), &SkeletonModification2DLookAt::get_bone_index);
374
375
ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DLookAt::set_target_node);
376
ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DLookAt::get_target_node);
377
378
ClassDB::bind_method(D_METHOD("set_additional_rotation", "rotation"), &SkeletonModification2DLookAt::set_additional_rotation);
379
ClassDB::bind_method(D_METHOD("get_additional_rotation"), &SkeletonModification2DLookAt::get_additional_rotation);
380
381
ClassDB::bind_method(D_METHOD("set_enable_constraint", "enable_constraint"), &SkeletonModification2DLookAt::set_enable_constraint);
382
ClassDB::bind_method(D_METHOD("get_enable_constraint"), &SkeletonModification2DLookAt::get_enable_constraint);
383
ClassDB::bind_method(D_METHOD("set_constraint_angle_min", "angle_min"), &SkeletonModification2DLookAt::set_constraint_angle_min);
384
ClassDB::bind_method(D_METHOD("get_constraint_angle_min"), &SkeletonModification2DLookAt::get_constraint_angle_min);
385
ClassDB::bind_method(D_METHOD("set_constraint_angle_max", "angle_max"), &SkeletonModification2DLookAt::set_constraint_angle_max);
386
ClassDB::bind_method(D_METHOD("get_constraint_angle_max"), &SkeletonModification2DLookAt::get_constraint_angle_max);
387
ClassDB::bind_method(D_METHOD("set_constraint_angle_invert", "invert"), &SkeletonModification2DLookAt::set_constraint_angle_invert);
388
ClassDB::bind_method(D_METHOD("get_constraint_angle_invert"), &SkeletonModification2DLookAt::get_constraint_angle_invert);
389
390
ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_index"), "set_bone_index", "get_bone_index");
391
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D"), "set_bone2d_node", "get_bone2d_node");
392
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
393
}
394
395
SkeletonModification2DLookAt::SkeletonModification2DLookAt() {
396
stack = nullptr;
397
is_setup = false;
398
bone_idx = -1;
399
additional_rotation = 0;
400
enable_constraint = false;
401
constraint_angle_min = 0;
402
constraint_angle_max = Math::PI * 2;
403
constraint_angle_invert = false;
404
enabled = true;
405
406
editor_draw_gizmo = true;
407
}
408
409
SkeletonModification2DLookAt::~SkeletonModification2DLookAt() {
410
}
411
412