-
Notifications
You must be signed in to change notification settings - Fork 34
/
spine.cpp
1427 lines (1127 loc) · 43 KB
/
spine.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Spine Runtimes Software License v2.5
*
* Copyright (c) 2013-2016, Esoteric Software
* All rights reserved.
*
* You are granted a perpetual, non-exclusive, non-sublicensable, and
* non-transferable license to use, install, execute, and perform the Spine
* Runtimes software and derivative works solely for personal or internal
* use. Without the written permission of Esoteric Software (see Section 2 of
* the Spine Software License Agreement), you may not (a) modify, translate,
* adapt, or develop new applications using the Spine Runtimes or otherwise
* create derivative works or improvements of the Spine Runtimes or (b) remove,
* delete, alter, or obscure any trademarks or any copyright, trademark, patent,
* or other intellectual property or proprietary rights notices on or in the
* Software, including any copy thereof. Redistributions in binary or source
* form must include this license and terms.
*
* THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
* USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifdef MODULE_SPINE_ENABLED
#include "spine.h"
#include "core/io/resource_loader.h"
#include "scene/2d/collision_object_2d.h"
#include "scene/resources/convex_polygon_shape_2d.h"
#include "core/version.h"
#include <core/engine.h>
#include <spine/extension.h>
#include <spine/spine.h>
#include <core/method_bind_ext.gen.inc>
Spine::SpineResource::SpineResource() {
atlas = NULL;
data = NULL;
}
Spine::SpineResource::~SpineResource() {
if (atlas != NULL)
spAtlas_dispose(atlas);
if (data != NULL)
spSkeletonData_dispose(data);
}
Array *Spine::invalid_names = NULL;
Array Spine::get_invalid_names() {
if (invalid_names == NULL) {
invalid_names = memnew(Array());
}
return *invalid_names;
}
void Spine::spine_animation_callback(spAnimationState *p_state, spEventType p_type, spTrackEntry *p_track, spEvent *p_event) {
((Spine *)p_state->rendererObject)->_on_animation_state_event(p_track->trackIndex, p_type, p_event, 1);
}
void Spine::_on_animation_state_event(int p_track, spEventType p_type, spEvent *p_event, int p_loop_count) {
switch (p_type) {
case SP_ANIMATION_START:
emit_signal("animation_start", p_track);
break;
case SP_ANIMATION_COMPLETE:
emit_signal("animation_complete", p_track, p_loop_count);
break;
case SP_ANIMATION_EVENT: {
Dictionary event;
event["name"] = p_event->data->name;
event["int"] = p_event->intValue;
event["float"] = p_event->floatValue;
event["string"] = p_event->stringValue ? p_event->stringValue : "";
emit_signal("animation_event", p_track, event);
} break;
case SP_ANIMATION_END:
emit_signal("animation_end", p_track);
break;
}
}
void Spine::_spine_dispose() {
if (playing) {
// stop first
stop();
}
if (state) {
spAnimationStateData_dispose(state->data);
spAnimationState_dispose(state);
}
if (skeleton)
spSkeleton_dispose(skeleton);
state = NULL;
skeleton = NULL;
res = RES();
for (AttachmentNodes::Element *E = attachment_nodes.front(); E; E = E->next()) {
AttachmentNode &node = E->get();
memdelete(node.ref);
}
attachment_nodes.clear();
update();
}
static Ref<Texture> spine_get_texture(spRegionAttachment *attachment) {
if (Ref<Texture> *ref = static_cast<Ref<Texture> *>(((spAtlasRegion *)attachment->rendererObject)->page->rendererObject))
return *ref;
return NULL;
}
static Ref<Texture> spine_get_texture(spMeshAttachment *attachment) {
if (Ref<Texture> *ref = static_cast<Ref<Texture> *>(((spAtlasRegion *)attachment->rendererObject)->page->rendererObject))
return *ref;
return NULL;
}
void Spine::_on_fx_draw() {
if (skeleton == NULL)
return;
fx_batcher.reset();
RID eci = fx_node->get_canvas_item();
// VisualServer::get_singleton()->canvas_item_add_set_blend_mode(eci, VS::MaterialBlendMode(fx_node->get_blend_mode()));
fx_batcher.flush();
}
void Spine::_animation_draw() {
if (skeleton == NULL)
return;
spColor_setFromFloats(&skeleton->color, modulate.r, modulate.g, modulate.b, modulate.a);
int additive = 0;
int fx_additive = 0;
Color color;
const float *uvs = NULL;
int verties_count = 0;
unsigned short *triangles = NULL;
int triangles_count = 0;
float r = 0, g = 0, b = 0, a = 0;
RID ci = this->get_canvas_item();
batcher.reset();
// VisualServer::get_singleton()->canvas_item_add_set_blend_mode(ci, VS::MaterialBlendMode(get_blend_mode()));
const char *fx_prefix = fx_slot_prefix.get_data();
for (int i = 0, n = skeleton->slotsCount; i < n; i++) {
spSlot *slot = skeleton->drawOrder[i];
if (!slot->attachment) continue;
bool is_fx = false;
Ref<Texture> texture;
switch (slot->attachment->type) {
case SP_ATTACHMENT_REGION: {
spRegionAttachment *attachment = (spRegionAttachment *)slot->attachment;
is_fx = strstr(attachment->path, fx_prefix) != NULL;
spRegionAttachment_computeWorldVertices(attachment, slot->bone, world_verts.ptrw(), 0, 2);
texture = spine_get_texture(attachment);
uvs = attachment->uvs;
verties_count = 8;
static unsigned short quadTriangles[6] = { 0, 1, 2, 2, 3, 0 };
triangles = quadTriangles;
triangles_count = 6;
r = attachment->color.r;
g = attachment->color.g;
b = attachment->color.b;
a = attachment->color.a;
break;
}
case SP_ATTACHMENT_MESH: {
spMeshAttachment *attachment = (spMeshAttachment *)slot->attachment;
is_fx = strstr(attachment->path, fx_prefix) != NULL;
spVertexAttachment_computeWorldVertices(SUPER(attachment), slot, 0, attachment->super.worldVerticesLength, world_verts.ptrw(), 0, 2);
texture = spine_get_texture(attachment);
uvs = attachment->uvs;
verties_count = ((spVertexAttachment *)attachment)->worldVerticesLength;
triangles = attachment->triangles;
triangles_count = attachment->trianglesCount;
r = attachment->color.r;
g = attachment->color.g;
b = attachment->color.b;
a = attachment->color.a;
break;
}
case SP_ATTACHMENT_BOUNDING_BOX: {
continue;
}
}
if (texture.is_null())
continue;
/*
if (is_fx && slot->data->blendMode != fx_additive) {
fx_batcher.add_set_blender_mode(slot->data->additiveBlending
? VisualServer::MATERIAL_BLEND_MODE_ADD
: get_blend_mode()
);
fx_additive = slot->data->additiveBlending;
}
else if (slot->data->additiveBlending != additive) {
batcher.add_set_blender_mode(slot->data->additiveBlending
? VisualServer::MATERIAL_BLEND_MODE_ADD
: fx_node->get_blend_mode()
);
additive = slot->data->additiveBlending;
}
*/
color.a = skeleton->color.a * slot->color.a * a;
color.r = skeleton->color.r * slot->color.r * r;
color.g = skeleton->color.g * slot->color.g * g;
color.b = skeleton->color.b * slot->color.b * b;
if (is_fx)
fx_batcher.add(texture, world_verts.ptr(), uvs, verties_count, triangles, triangles_count, &color, flip_x, flip_y);
else
batcher.add(texture, world_verts.ptr(), uvs, verties_count, triangles, triangles_count, &color, flip_x, flip_y);
}
batcher.flush();
fx_node->update();
// Slots.
if (debug_attachment_region || debug_attachment_mesh || debug_attachment_skinned_mesh || debug_attachment_bounding_box) {
Color color(0, 0, 1, 1);
for (int i = 0, n = skeleton->slotsCount; i < n; i++) {
spSlot *slot = skeleton->drawOrder[i];
if (!slot->attachment)
continue;
switch (slot->attachment->type) {
case SP_ATTACHMENT_REGION: {
if (!debug_attachment_region)
continue;
spRegionAttachment *attachment = (spRegionAttachment *)slot->attachment;
verties_count = 8;
spRegionAttachment_computeWorldVertices(attachment, slot->bone, world_verts.ptrw(), 0, 2);
color = Color(0, 0, 1, 1);
triangles = NULL;
triangles_count = 0;
break;
}
case SP_ATTACHMENT_MESH: {
if (!debug_attachment_mesh)
continue;
spMeshAttachment *attachment = (spMeshAttachment *)slot->attachment;
spVertexAttachment_computeWorldVertices(SUPER(attachment), slot, 0, attachment->super.worldVerticesLength, world_verts.ptrw(), 0, 2);
verties_count = ((spVertexAttachment *)attachment)->verticesCount;
color = Color(0, 1, 1, 1);
triangles = attachment->triangles;
triangles_count = attachment->trianglesCount;
break;
}
case SP_ATTACHMENT_BOUNDING_BOX: {
if (!debug_attachment_bounding_box)
continue;
spBoundingBoxAttachment *attachment = (spBoundingBoxAttachment *)slot->attachment;
spVertexAttachment_computeWorldVertices(SUPER(attachment), slot, 0, ((spVertexAttachment *)attachment)->verticesCount, world_verts.ptrw(), 0, 2);
verties_count = ((spVertexAttachment *)attachment)->verticesCount;
color = Color(0, 1, 0, 1);
triangles = NULL;
triangles_count = 0;
break;
}
}
Point2 *points = (Point2 *)world_verts.ptr();
int points_size = verties_count / 2;
for (int idx = 0; idx < points_size; idx++) {
Point2 &pt = points[idx];
if (flip_x)
pt.x = -pt.x;
if (!flip_y)
pt.y = -pt.y;
}
if (triangles == NULL || triangles_count == 0) {
for (int idx = 0; idx < points_size; idx++) {
if (idx == points_size - 1)
draw_line(points[idx], points[0], color);
else
draw_line(points[idx], points[idx + 1], color);
}
} else {
for (int idx = 0; idx < triangles_count - 2; idx += 3) {
int a = triangles[idx];
int b = triangles[idx + 1];
int c = triangles[idx + 2];
draw_line(points[a], points[b], color);
draw_line(points[b], points[c], color);
draw_line(points[c], points[a], color);
}
}
}
}
if (debug_bones) {
// Bone lengths.
for (int i = 0; i < skeleton->bonesCount; i++) {
spBone *bone = skeleton->bones[i];
float x = bone->data->length * bone->a + bone->worldX;
float y = bone->data->length * bone->c + bone->worldY;
draw_line(Point2(flip_x ? -bone->worldX : bone->worldX,
flip_y ? bone->worldY : -bone->worldY),
Point2(flip_x ? -x : x, flip_y ? y : -y),
Color(1, 0, 0, 1),
2);
}
// Bone origins.
for (int i = 0, n = skeleton->bonesCount; i < n; i++) {
spBone *bone = skeleton->bones[i];
Rect2 rt = Rect2(flip_x ? -bone->worldX - 1 : bone->worldX - 1,
flip_y ? bone->worldY - 1 : -bone->worldY - 1,
3,
3);
draw_rect(rt, (i == 0) ? Color(0, 1, 0, 1) : Color(0, 0, 1, 1));
}
}
}
void Spine::_animation_process(float p_delta) {
if (speed_scale == 0)
return;
p_delta *= speed_scale;
process_delta += p_delta;
if (skip_frames) {
frames_to_skip--;
if (frames_to_skip >= 0) {
return;
} else {
frames_to_skip = skip_frames;
}
}
spAnimationState_update(state, forward ? process_delta : -process_delta);
spAnimationState_apply(state, skeleton);
spSkeleton_updateWorldTransform(skeleton);
for (AttachmentNodes::Element *E = attachment_nodes.front(); E; E = E->next()) {
AttachmentNode &info = E->get();
WeakRef *ref = info.ref;
Object *obj = ref->get_ref();
Node2D *node = (obj != NULL) ? Object::cast_to<Node2D>(obj) : NULL;
if (obj == NULL || node == NULL) {
AttachmentNodes::Element *NEXT = E->next();
attachment_nodes.erase(E);
E = NEXT;
if (E == NULL)
break;
continue;
}
spBone *bone = info.bone;
node->call("set_position", Vector2(bone->worldX + bone->skeleton->x, -bone->worldY + bone->skeleton->y) + info.ofs);
node->call("set_scale", Vector2(spBone_getWorldScaleX(bone), spBone_getWorldScaleY(bone)) * info.scale);
node->call("set_rotation", Math::atan2(bone->c, bone->d) + Math::deg2rad(info.rot));
}
update();
process_delta = 0;
}
void Spine::_set_process(bool p_process, bool p_force) {
if (processing == p_process && !p_force)
return;
switch (animation_process_mode) {
case ANIMATION_PROCESS_FIXED: set_physics_process(p_process && active); break;
case ANIMATION_PROCESS_IDLE: set_process(p_process && active); break;
}
processing = p_process;
}
bool Spine::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name == "playback/play") {
String which = p_value;
if (skeleton != NULL) {
if (which == "[stop]")
stop();
else if (has_animation(which)) {
reset();
play(which, 1, loop);
}
} else
current_animation = which;
if (current_animation == "[stop]")
actual_duration = 0.0;
else
actual_duration = get_animation_length(current_animation);
// Call this immediately to make the duration visible
set_duration(actual_duration);
} else if (name == "playback/loop") {
loop = p_value;
if (skeleton != NULL && has_animation(current_animation))
play(current_animation, 1, loop);
} else if (name == "playback/forward") {
forward = p_value;
} else if (name == "playback/skin") {
skin = p_value;
if (skeleton != NULL)
set_skin(skin);
} else if (name == "debug/region")
set_debug_attachment(DEBUG_ATTACHMENT_REGION, p_value);
else if (name == "debug/mesh")
set_debug_attachment(DEBUG_ATTACHMENT_MESH, p_value);
else if (name == "debug/skinned_mesh")
set_debug_attachment(DEBUG_ATTACHMENT_SKINNED_MESH, p_value);
else if (name == "debug/bounding_box")
set_debug_attachment(DEBUG_ATTACHMENT_BOUNDING_BOX, p_value);
return true;
}
bool Spine::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name;
if (name == "playback/play") {
r_ret = current_animation;
} else if (name == "playback/loop")
r_ret = loop;
else if (name == "playback/forward")
r_ret = forward;
else if (name == "playback/skin")
r_ret = skin;
else if (name == "debug/region")
r_ret = is_debug_attachment(DEBUG_ATTACHMENT_REGION);
else if (name == "debug/mesh")
r_ret = is_debug_attachment(DEBUG_ATTACHMENT_MESH);
else if (name == "debug/skinned_mesh")
r_ret = is_debug_attachment(DEBUG_ATTACHMENT_SKINNED_MESH);
else if (name == "debug/bounding_box")
r_ret = is_debug_attachment(DEBUG_ATTACHMENT_BOUNDING_BOX);
return true;
}
float Spine::get_animation_length(String p_animation) const {
if (state == NULL) return 0;
for (int i = 0; i < state->data->skeletonData->animationsCount; i++) {
spAnimation *anim = state->data->skeletonData->animations[i];
if (anim->name == p_animation) {
return anim->duration;
}
}
return 0;
}
void Spine::_get_property_list(List<PropertyInfo> *p_list) const {
List<String> names;
if (state != NULL) {
for (int i = 0; i < state->data->skeletonData->animationsCount; i++) {
names.push_back(state->data->skeletonData->animations[i]->name);
}
}
{
names.sort();
names.push_front("[stop]");
String hint;
for (List<String>::Element *E = names.front(); E; E = E->next()) {
if (E != names.front())
hint += ",";
hint += E->get();
}
p_list->push_back(PropertyInfo(Variant::STRING, "playback/play", PROPERTY_HINT_ENUM, hint));
p_list->push_back(PropertyInfo(Variant::BOOL, "playback/loop", PROPERTY_HINT_NONE));
p_list->push_back(PropertyInfo(Variant::BOOL, "playback/forward", PROPERTY_HINT_NONE));
}
names.clear();
{
if (state != NULL) {
for (int i = 0; i < state->data->skeletonData->skinsCount; i++) {
names.push_back(state->data->skeletonData->skins[i]->name);
}
}
String hint;
for (List<String>::Element *E = names.front(); E; E = E->next()) {
if (E != names.front())
hint += ",";
hint += E->get();
}
p_list->push_back(PropertyInfo(Variant::STRING, "playback/skin", PROPERTY_HINT_ENUM, hint));
}
p_list->push_back(PropertyInfo(Variant::BOOL, "debug/region", PROPERTY_HINT_NONE));
p_list->push_back(PropertyInfo(Variant::BOOL, "debug/mesh", PROPERTY_HINT_NONE));
p_list->push_back(PropertyInfo(Variant::BOOL, "debug/skinned_mesh", PROPERTY_HINT_NONE));
p_list->push_back(PropertyInfo(Variant::BOOL, "debug/bounding_box", PROPERTY_HINT_NONE));
}
void Spine::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
if (!processing) {
//make sure that a previous process state was not saved
//only process if "processing" is set
set_physics_process(false);
set_process(false);
}
} break;
case NOTIFICATION_READY: {
// add fx node as child
fx_node->connect("draw", this, "_on_fx_draw");
fx_node->set_z_index(1);
fx_node->set_z_as_relative(false);
add_child(fx_node);
if (!Engine::get_singleton()->is_editor_hint() && has_animation(autoplay)) {
play(autoplay);
}
} break;
case NOTIFICATION_PROCESS: {
if (animation_process_mode == ANIMATION_PROCESS_FIXED)
break;
if (processing)
_animation_process(get_process_delta_time());
} break;
case NOTIFICATION_PHYSICS_PROCESS: {
if (animation_process_mode == ANIMATION_PROCESS_IDLE)
break;
if (processing)
_animation_process(get_physics_process_delta_time());
} break;
case NOTIFICATION_DRAW: {
_animation_draw();
} break;
case NOTIFICATION_EXIT_TREE: {
stop_all();
} break;
}
}
void Spine::set_resource(Ref<Spine::SpineResource> p_data) {
if (res == p_data)
return;
_spine_dispose(); // cleanup
res = p_data;
if (res.is_null())
return;
ERR_FAIL_COND(!res->data);
skeleton = spSkeleton_create(res->data);
root_bone = skeleton->bones[0];
state = spAnimationState_create(spAnimationStateData_create(skeleton->data));
state->rendererObject = this;
state->listener = spine_animation_callback;
_update_verties_count();
if (skin != "")
set_skin(skin);
if (current_animation != "[stop]")
play(current_animation, 1, loop);
else
reset();
_change_notify();
}
Ref<Spine::SpineResource> Spine::get_resource() {
return res;
}
Array Spine::get_animation_names() const {
Array names;
if (state != NULL) {
for (int i = 0; i < state->data->skeletonData->animationsCount; i++) {
spAnimation *anim = state->data->skeletonData->animations[i];
names.push_back(anim->name);
}
}
return names;
}
bool Spine::has_animation(const String &p_name) {
if (skeleton == NULL) return false;
spAnimation *animation = spSkeletonData_findAnimation(skeleton->data, p_name.utf8().get_data());
return animation != NULL;
}
void Spine::set_default_mix(real_t p_duration) {
ERR_FAIL_COND(state == NULL);
ERR_FAIL_COND(p_duration <= 0.0f);
state->data->defaultMix = p_duration;
}
void Spine::mix(const String &p_from, const String &p_to, real_t p_duration) {
ERR_FAIL_COND(state == NULL);
spAnimationStateData_setMixByName(state->data, p_from.utf8().get_data(), p_to.utf8().get_data(), p_duration);
}
bool Spine::play(const String &p_name, bool p_loop, int p_track, int p_delay) {
ERR_FAIL_COND_V(skeleton == NULL, false);
spAnimation *animation = spSkeletonData_findAnimation(skeleton->data, p_name.utf8().get_data());
ERR_FAIL_COND_V(animation == NULL, false);
spTrackEntry *entry = spAnimationState_setAnimation(state, p_track, animation, p_loop);
entry->delay = p_delay;
current_animation = p_name;
if (skip_frames) {
frames_to_skip = 0;
}
_set_process(true);
playing = true;
// update frame
if (!is_active())
_animation_process(0);
return true;
}
bool Spine::add(const String &p_name, bool p_loop, int p_track, int p_delay) {
ERR_FAIL_COND_V(skeleton == NULL, false);
spAnimation *animation = spSkeletonData_findAnimation(skeleton->data, p_name.utf8().get_data());
ERR_FAIL_COND_V(animation == NULL, false);
spTrackEntry *entry = spAnimationState_addAnimation(state, p_track, animation, p_loop, p_delay);
_set_process(true);
playing = true;
return true;
}
void Spine::clear(int p_track) {
ERR_FAIL_COND(state == NULL);
if (p_track == -1)
spAnimationState_clearTracks(state);
else
spAnimationState_clearTrack(state, p_track);
}
void Spine::stop() {
_set_process(false);
playing = false;
current_animation = "[stop]";
reset();
}
bool Spine::is_playing(int p_track) const {
return playing && spAnimationState_getCurrent(state, p_track) != NULL;
}
void Spine::set_forward(bool p_forward) {
forward = p_forward;
}
bool Spine::is_forward() const {
return forward;
}
void Spine::set_skip_frames(int p_skip_frames) {
skip_frames = p_skip_frames;
frames_to_skip = 0;
}
int Spine::get_skip_frames() const {
return skip_frames;
}
String Spine::get_current_animation(int p_track = 0) {
ERR_FAIL_COND_V(state == NULL, "");
spTrackEntry *entry = spAnimationState_getCurrent(state, p_track);
if (entry == NULL || entry->animation == NULL)
return "";
return entry->animation->name;
}
void Spine::stop_all() {
stop();
_set_process(false); // always process when starting an animation
}
void Spine::reset() {
if (skeleton == NULL) {
return;
}
spSkeleton_setToSetupPose(skeleton);
spAnimationState_update(state, 0);
spAnimationState_apply(state, skeleton);
spSkeleton_updateWorldTransform(skeleton);
}
void Spine::seek(float p_pos) {
_animation_process(p_pos - current_pos);
}
float Spine::tell() const {
return current_pos;
}
void Spine::set_active(bool p_active) {
if (active == p_active)
return;
active = p_active;
_set_process(processing, true);
}
bool Spine::is_active() const {
return active;
}
void Spine::set_speed(float p_speed) {
speed_scale = p_speed;
}
float Spine::get_speed() const {
return speed_scale;
}
void Spine::set_autoplay(const String &p_name) {
autoplay = p_name;
}
String Spine::get_autoplay() const {
return autoplay;
}
void Spine::set_modulate(const Color &p_color) {
modulate = p_color;
update();
}
Color Spine::get_modulate() const {
return modulate;
}
void Spine::set_flip_x(bool p_flip) {
flip_x = p_flip;
update();
}
void Spine::set_flip_y(bool p_flip) {
flip_y = p_flip;
update();
}
bool Spine::is_flip_x() const {
return flip_x;
}
bool Spine::is_flip_y() const {
return flip_y;
}
bool Spine::set_skin(const String &p_name) {
ERR_FAIL_COND_V(skeleton == NULL, false);
return spSkeleton_setSkinByName(skeleton, p_name.utf8().get_data()) ? true : false;
}
void Spine::set_duration(float p_duration) {
// Ignore p_duration, because it can't actually be affected and this should be read-only
duration = actual_duration;
update();
_change_notify("playback/duration");
}
float Spine::get_duration() const {
return duration;
}
Dictionary Spine::get_skeleton() const {
ERR_FAIL_COND_V(skeleton == NULL, Variant());
Dictionary dict;
dict["bonesCount"] = skeleton->bonesCount;
dict["slotCount"] = skeleton->slotsCount;
dict["ikConstraintsCount"] = skeleton->ikConstraintsCount;
dict["time"] = skeleton->time;
dict["flipX"] = skeleton->flipX;
dict["flipY"] = skeleton->flipY;
dict["x"] = skeleton->x;
dict["y"] = skeleton->y;
return dict;
}
Dictionary Spine::get_attachment(const String &p_slot_name, const String &p_attachment_name) const {
ERR_FAIL_COND_V(skeleton == NULL, Variant());
spAttachment *attachment = spSkeleton_getAttachmentForSlotName(skeleton, p_slot_name.utf8().get_data(), p_attachment_name.utf8().get_data());
ERR_FAIL_COND_V(attachment == NULL, Variant());
Dictionary dict;
dict["name"] = attachment->name;
switch (attachment->type) {
case SP_ATTACHMENT_REGION: {
spRegionAttachment *info = (spRegionAttachment *)attachment;
dict["type"] = "region";
dict["path"] = info->path;
dict["x"] = info->x;
dict["y"] = info->y;
dict["scaleX"] = info->scaleX;
dict["scaleY"] = info->scaleY;
dict["rotation"] = info->rotation;
dict["width"] = info->width;
dict["height"] = info->height;
dict["color"] = Color(info->color.r, info->color.g, info->color.b, info->color.a);
dict["region"] = Rect2(info->regionOffsetX, info->regionOffsetY, info->regionWidth, info->regionHeight);
dict["region_original_size"] = Size2(info->regionOriginalWidth, info->regionOriginalHeight);
Vector<Vector2> offset, uvs;
for (int idx = 0; idx < 4; idx++) {
offset.push_back(Vector2(info->offset[idx * 2], info->offset[idx * 2 + 1]));
uvs.push_back(Vector2(info->uvs[idx * 2], info->uvs[idx * 2 + 1]));
}
dict["offset"] = offset;
dict["uvs"] = uvs;
} break;
case SP_ATTACHMENT_BOUNDING_BOX: {
spVertexAttachment *info = (spVertexAttachment *)attachment;
dict["type"] = "bounding_box";
Vector<Vector2> vertices;
for (int idx = 0; idx < info->verticesCount / 2; idx++)
vertices.push_back(Vector2(info->vertices[idx * 2], -info->vertices[idx * 2 + 1]));
dict["vertices"] = vertices;
} break;
case SP_ATTACHMENT_MESH: {
spMeshAttachment *info = (spMeshAttachment *)attachment;
dict["type"] = "mesh";
dict["path"] = info->path;
dict["color"] = Color(info->color.r, info->color.g, info->color.b, info->color.a);
} break;
}
return dict;
}
Dictionary Spine::get_bone(const String &p_bone_name) const {
ERR_FAIL_COND_V(skeleton == NULL, Variant());
spBone *bone = spSkeleton_findBone(skeleton, p_bone_name.utf8().get_data());
ERR_FAIL_COND_V(bone == NULL, Variant());
Dictionary dict;
dict["x"] = bone->x;
dict["y"] = bone->y;
dict["rotation"] = bone->rotation;
dict["rotationIK"] = 0; //bone->rotationIK;
dict["scaleX"] = bone->scaleX;
dict["scaleY"] = bone->scaleY;
dict["flipX"] = 0; //bone->flipX;
dict["flipY"] = 0; //bone->flipY;
dict["m00"] = bone->a; //m00;
dict["m01"] = bone->b; //m01;
dict["m10"] = bone->c; //m10;
dict["m11"] = bone->d; //m11;
dict["worldX"] = bone->worldX;
dict["worldY"] = bone->worldY;
dict["worldRotation"] = spBone_getWorldRotationX(bone); //->worldRotation;
dict["worldScaleX"] = spBone_getWorldScaleX(bone); //->worldScaleX;
dict["worldScaleY"] = spBone_getWorldScaleY(bone); //->worldScaleY;
dict["worldFlipX"] = 0; //bone->worldFlipX;
dict["worldFlipY"] = 0; //bone->worldFlipY;
return dict;
}
Dictionary Spine::get_slot(const String &p_slot_name) const {
ERR_FAIL_COND_V(skeleton == NULL, Variant());
spSlot *slot = spSkeleton_findSlot(skeleton, p_slot_name.utf8().get_data());
ERR_FAIL_COND_V(slot == NULL, Variant());
Dictionary dict;
dict["color"] = Color(slot->color.r, slot->color.g, slot->color.b, slot->color.a);
return dict;
}
bool Spine::set_attachment(const String &p_slot_name, const Variant &p_attachment) {
ERR_FAIL_COND_V(skeleton == NULL, false);
if (p_attachment.get_type() == Variant::STRING)
return spSkeleton_setAttachment(skeleton, p_slot_name.utf8().get_data(), ((const String)p_attachment).utf8().get_data()) != 0;
else
return spSkeleton_setAttachment(skeleton, p_slot_name.utf8().get_data(), NULL) != 0;
}
bool Spine::has_attachment_node(const String &p_bone_name, const Variant &p_node) {
return false;
}
bool Spine::add_attachment_node(const String &p_bone_name, const Variant &p_node, const Vector2 &p_ofs, const Vector2 &p_scale, const real_t p_rot) {
ERR_FAIL_COND_V(skeleton == NULL, false);