-
Notifications
You must be signed in to change notification settings - Fork 1
/
ybone_attachment_3d.cpp
386 lines (325 loc) · 11.6 KB
/
ybone_attachment_3d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//
// Created by Daniel on 2024-06-14.
//
#include "ybone_attachment_3d.h"
void YBoneAttachment3D::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "bone_name") {
// Because it is a constant function, we cannot use the _get_skeleton_3d function.
const Skeleton3D *parent = nullptr;
if (use_external_skeleton) {
if (external_skeleton_node_cache.is_valid()) {
parent = Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
}
} else {
parent = Object::cast_to<Skeleton3D>(get_parent());
}
if (parent) {
String names;
for (int i = 0; i < parent->get_bone_count(); i++) {
if (i > 0) {
names += ",";
}
names += parent->get_bone_name(i);
}
p_property.hint = PROPERTY_HINT_ENUM;
p_property.hint_string = names;
} else {
p_property.hint = PROPERTY_HINT_NONE;
p_property.hint_string = "";
}
}
}
bool YBoneAttachment3D::_set(const StringName &p_path, const Variant &p_value) {
if (p_path == SNAME("use_external_skeleton")) {
set_use_external_skeleton(p_value);
} else if (p_path == SNAME("external_skeleton")) {
set_external_skeleton(p_value);
}
return true;
}
bool YBoneAttachment3D::_get(const StringName &p_path, Variant &r_ret) const {
if (p_path == SNAME("use_external_skeleton")) {
r_ret = get_use_external_skeleton();
} else if (p_path == SNAME("external_skeleton")) {
r_ret = get_external_skeleton();
}
return true;
}
void YBoneAttachment3D::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::BOOL, "use_external_skeleton", PROPERTY_HINT_NONE, ""));
if (use_external_skeleton) {
p_list->push_back(PropertyInfo(Variant::NODE_PATH, "external_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"));
}
}
PackedStringArray YBoneAttachment3D::get_configuration_warnings() const {
PackedStringArray warnings = Node3D::get_configuration_warnings();
if (use_external_skeleton) {
if (external_skeleton_node_cache.is_null()) {
warnings.push_back(RTR("External Skeleton3D node not set! Please set a path to an external Skeleton3D node."));
}
} else {
Skeleton3D *parent = Object::cast_to<Skeleton3D>(get_parent());
if (!parent) {
warnings.push_back(RTR("Parent node is not a Skeleton3D node! Please use an external Skeleton3D if you intend to use the YBoneAttachment3D without it being a child of a Skeleton3D node."));
}
}
if (bone_idx == -1) {
warnings.push_back(RTR("YBoneAttachment3D node is not bound to any bones! Please select a bone to attach this node."));
}
return warnings;
}
void YBoneAttachment3D::_update_external_skeleton_cache() {
external_skeleton_node_cache = ObjectID();
if (has_node(external_skeleton_node)) {
Node *node = get_node(external_skeleton_node);
ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Node cannot be found!");
// Make sure it's a skeleton3D
Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Skeleton3D Nodepath does not point to a Skeleton3D node!");
external_skeleton_node_cache = node->get_instance_id();
} else {
if (external_skeleton_node.is_empty()) {
YBoneAttachment3D *parent_attachment = Object::cast_to<YBoneAttachment3D>(get_parent());
if (parent_attachment) {
parent_attachment->_update_external_skeleton_cache();
if (parent_attachment->has_node(parent_attachment->external_skeleton_node)) {
Node *node = parent_attachment->get_node(parent_attachment->external_skeleton_node);
ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Parent's Skeleton3D node cannot be found!");
// Make sure it's a skeleton3D
Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Parent Skeleton3D Nodepath does not point to a Skeleton3D node!");
external_skeleton_node_cache = node->get_instance_id();
external_skeleton_node = get_path_to(node);
}
}
}
}
}
void YBoneAttachment3D::_check_bind() {
Skeleton3D *sk = _get_skeleton3d();
if (sk && !bound) {
if (bone_idx <= -1) {
bone_idx = sk->find_bone(bone_name);
}
if (bone_idx != -1) {
sk->connect(SNAME("skeleton_updated"), callable_mp(this, &YBoneAttachment3D::on_skeleton_update));
bound = true;
callable_mp(this, &YBoneAttachment3D::on_skeleton_update);
}
}
}
Skeleton3D *YBoneAttachment3D::_get_skeleton3d() {
if (use_external_skeleton) {
if (external_skeleton_node_cache.is_valid()) {
return Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
} else {
_update_external_skeleton_cache();
if (external_skeleton_node_cache.is_valid()) {
return Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
}
}
} else {
return Object::cast_to<Skeleton3D>(get_parent());
}
return nullptr;
}
void YBoneAttachment3D::_check_unbind() {
if (bound) {
Skeleton3D *sk = _get_skeleton3d();
if (sk) {
sk->disconnect(SNAME("skeleton_updated"), callable_mp(this, &YBoneAttachment3D::on_skeleton_update));
}
bound = false;
}
}
void YBoneAttachment3D::_transform_changed() {
if (!is_inside_tree()) {
return;
}
if (override_pose && !overriding) {
Skeleton3D *sk = _get_skeleton3d();
ERR_FAIL_NULL_MSG(sk, "Cannot override pose: Skeleton not found!");
ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!");
Transform3D our_trans = get_transform();
if (use_external_skeleton) {
our_trans = sk->get_global_transform().affine_inverse() * get_global_transform();
}
overriding = true;
sk->set_bone_global_pose(bone_idx, our_trans);
sk->force_update_all_dirty_bones();
}
overriding = false;
}
void YBoneAttachment3D::set_bone_name(const String &p_name) {
bone_name = p_name;
Skeleton3D *sk = _get_skeleton3d();
if (sk) {
set_bone_idx(sk->find_bone(bone_name));
}
}
String YBoneAttachment3D::get_bone_name() const {
return bone_name;
}
void YBoneAttachment3D::set_bone_idx(const int &p_idx) {
if (is_inside_tree()) {
_check_unbind();
}
bone_idx = p_idx;
Skeleton3D *sk = _get_skeleton3d();
if (sk) {
if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) {
WARN_PRINT("Bone index out of range! Cannot connect BoneAttachment to node!");
bone_idx = -1;
} else {
bone_name = sk->get_bone_name(bone_idx);
}
}
if (is_inside_tree()) {
_check_bind();
}
notify_property_list_changed();
}
int YBoneAttachment3D::get_bone_idx() const {
return bone_idx;
}
void YBoneAttachment3D::set_override_pose(bool p_override) {
override_pose = p_override;
set_notify_transform(override_pose);
set_process_internal(override_pose);
notify_property_list_changed();
}
bool YBoneAttachment3D::get_override_pose() const {
return override_pose;
}
void YBoneAttachment3D::set_ignore_scale(bool p_ignore_scale) {
ignore_scale = p_ignore_scale;
}
bool YBoneAttachment3D::get_ignore_scale() const {
return ignore_scale;
}
void YBoneAttachment3D::set_use_external_skeleton(bool p_use_external) {
use_external_skeleton = p_use_external;
if (use_external_skeleton) {
_check_unbind();
_update_external_skeleton_cache();
_check_bind();
_transform_changed();
}
notify_property_list_changed();
}
bool YBoneAttachment3D::get_use_external_skeleton() const {
return use_external_skeleton;
}
void YBoneAttachment3D::set_external_skeleton(NodePath p_path) {
external_skeleton_node = p_path;
_update_external_skeleton_cache();
notify_property_list_changed();
}
NodePath YBoneAttachment3D::get_external_skeleton() const {
return external_skeleton_node;
}
void YBoneAttachment3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
if (use_external_skeleton) {
_update_external_skeleton_cache();
}
_check_bind();
} break;
case NOTIFICATION_EXIT_TREE: {
_check_unbind();
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
_transform_changed();
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
if (_override_dirty) {
_override_dirty = false;
}
} break;
}
}
void YBoneAttachment3D::on_skeleton_update() {
if (updating) {
return;
}
updating = true;
if (bone_idx >= 0) {
Skeleton3D *sk = _get_skeleton3d();
if (sk) {
if (!override_pose) {
if (ignore_scale) {
if (use_external_skeleton) {
const auto found_transform = sk->get_global_transform() * sk->get_bone_global_pose(bone_idx);
set_global_basis(found_transform.basis.get_rotation_quaternion());
set_global_position(found_transform.get_origin());
} else {
const auto found_transform = sk->get_bone_global_pose(bone_idx);
set_global_basis(found_transform.basis.get_rotation_quaternion());
set_global_position(found_transform.get_origin());
}
} else {
if (use_external_skeleton) {
set_global_transform(sk->get_global_transform() * sk->get_bone_global_pose(bone_idx));
} else {
set_transform(sk->get_bone_global_pose(bone_idx));
}
}
} else {
if (!_override_dirty) {
_transform_changed();
_override_dirty = true;
}
}
}
}
updating = false;
}
#ifdef TOOLS_ENABLED
void YBoneAttachment3D::notify_skeleton_bones_renamed(Node *p_base_scene, Skeleton3D *p_skeleton, Dictionary p_rename_map) {
const Skeleton3D *parent = nullptr;
if (use_external_skeleton) {
if (external_skeleton_node_cache.is_valid()) {
parent = Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
}
} else {
parent = Object::cast_to<Skeleton3D>(get_parent());
}
if (parent && parent == p_skeleton) {
StringName bn = p_rename_map[bone_name];
if (bn) {
set_bone_name(bn);
}
}
}
void YBoneAttachment3D::notify_rebind_required() {
// Ensures bindings are properly updated after a scene reload.
_check_unbind();
if (use_external_skeleton) {
_update_external_skeleton_cache();
}
bone_idx = -1;
_check_bind();
}
#endif // TOOLS_ENABLED
YBoneAttachment3D::YBoneAttachment3D() {
}
void YBoneAttachment3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &YBoneAttachment3D::set_bone_name);
ClassDB::bind_method(D_METHOD("get_bone_name"), &YBoneAttachment3D::get_bone_name);
ClassDB::bind_method(D_METHOD("set_bone_idx", "bone_idx"), &YBoneAttachment3D::set_bone_idx);
ClassDB::bind_method(D_METHOD("get_bone_idx"), &YBoneAttachment3D::get_bone_idx);
ClassDB::bind_method(D_METHOD("on_skeleton_update"), &YBoneAttachment3D::on_skeleton_update);
ClassDB::bind_method(D_METHOD("set_override_pose", "override_pose"), &YBoneAttachment3D::set_override_pose);
ClassDB::bind_method(D_METHOD("get_override_pose"), &YBoneAttachment3D::get_override_pose);
ClassDB::bind_method(D_METHOD("set_ignore_scale", "ignore_scale"), &YBoneAttachment3D::set_ignore_scale);
ClassDB::bind_method(D_METHOD("get_ignore_scale"), &YBoneAttachment3D::get_ignore_scale);
ClassDB::bind_method(D_METHOD("set_use_external_skeleton", "use_external_skeleton"), &YBoneAttachment3D::set_use_external_skeleton);
ClassDB::bind_method(D_METHOD("get_use_external_skeleton"), &YBoneAttachment3D::get_use_external_skeleton);
ClassDB::bind_method(D_METHOD("set_external_skeleton", "external_skeleton"), &YBoneAttachment3D::set_external_skeleton);
ClassDB::bind_method(D_METHOD("get_external_skeleton"), &YBoneAttachment3D::get_external_skeleton);
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_idx"), "set_bone_idx", "get_bone_idx");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_pose"), "set_override_pose", "get_override_pose");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_scale"), "set_ignore_scale", "get_ignore_scale");
}