/
spine_flutter.dart
4156 lines (3482 loc) · 157 KB
/
spine_flutter.dart
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 License Agreement
/// Last updated July 28, 2023. Replaces all prior versions.
///
/// Copyright (c) 2013-2023, Esoteric Software LLC
///
/// Integration of the Spine Runtimes into software or otherwise creating
/// derivative works of the Spine Runtimes is permitted under the terms and
/// conditions of Section 2 of the Spine Editor License Agreement:
/// http://esotericsoftware.com/spine-editor-license
///
/// Otherwise, it is permitted to integrate the Spine Runtimes into software or
/// otherwise create derivative works of the Spine Runtimes (collectively,
/// "Products"), provided that each user of the Products must obtain their own
/// Spine Editor license and redistribution of the Products in any form must
/// include this license and copyright notice.
///
/// THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "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 LLC 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 THE
/// SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///
import 'dart:convert' as convert;
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart' as material;
import 'package:flutter/rendering.dart' as rendering;
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:path/path.dart' as path;
import 'ffi_proxy.dart';
import 'init.dart' if (dart.library.html) 'init_web.dart';
import 'spine_flutter_bindings_generated.dart';
import 'raw_image_provider.dart';
export 'spine_widget.dart';
late SpineFlutterBindings _bindings;
late Allocator _allocator;
Future<void> initSpineFlutter({bool useStaticLinkage = false, bool enableMemoryDebugging = false}) async {
final ffi = await initSpineFlutterFFI(useStaticLinkage);
_bindings = SpineFlutterBindings(ffi.dylib);
_allocator = ffi.allocator;
if (enableMemoryDebugging) _bindings.spine_enable_debug_extension(-1);
return;
}
int majorVersion() => _bindings.spine_major_version();
int minorVersion() => _bindings.spine_minor_version();
void reportLeaks() => _bindings.spine_report_leaks();
/// A color made of red, green, blue, and alpha components,
/// ranging from 0-1.
class Color {
double r;
double g;
double b;
double a;
Color(this.r, this.g, this.b, this.a);
}
/// Bounds denoted by the top left corner coordinates [x] and [y]
/// and the [width] and [height].
class Bounds {
double x;
double y;
double width;
double height;
Bounds(this.x, this.y, this.width, this.height);
}
/// A two-dimensional vector with [x] and [y] components.
class Vec2 {
double x;
double y;
Vec2(this.x, this.y);
}
/// Atlas data loaded from a `.atlas` file and its corresponding `.png` files. For each atlas image,
/// a corresponding [Image] and [Paint] is constructed, which are used when rendering a skeleton
/// that uses this atlas.
///
/// Use the static methods [fromAsset], [fromFile], and [fromHttp] to load an atlas. Call [dispose]
/// when the atlas is no longer in use to release its resources.
class Atlas {
static FilterQuality filterQuality = FilterQuality.none;
final spine_atlas _atlas;
final List<Image> atlasPages;
final List<Map<BlendMode, Paint>> atlasPagePaints;
bool _disposed;
Atlas._(this._atlas, this.atlasPages, this.atlasPagePaints) : _disposed = false;
static Future<Atlas> _load(String atlasFileName, Future<Uint8List> Function(String name) loadFile) async {
final atlasBytes = await loadFile(atlasFileName);
final atlasData = convert.utf8.decode(atlasBytes);
final atlasDataNative = atlasData.toNativeUtf8(allocator: _allocator);
final atlas = _bindings.spine_atlas_load(atlasDataNative.cast());
_allocator.free(atlasDataNative);
if (_bindings.spine_atlas_get_error(atlas).address != nullptr.address) {
final Pointer<Utf8> error = _bindings.spine_atlas_get_error(atlas).cast();
final message = error.toDartString();
_bindings.spine_atlas_dispose(atlas);
throw Exception("Couldn't load atlas: $message");
}
final atlasDir = path.dirname(atlasFileName);
List<Image> atlasPages = [];
List<Map<BlendMode, Paint>> atlasPagePaints = [];
final numImagePaths = _bindings.spine_atlas_get_num_image_paths(atlas);
for (int i = 0; i < numImagePaths; i++) {
final Pointer<Utf8> atlasPageFile = _bindings.spine_atlas_get_image_path(atlas, i).cast();
final imagePath = atlasDir + "/" + atlasPageFile.toDartString();
var imageData = await loadFile(imagePath);
final Codec codec = await instantiateImageCodec(imageData);
final FrameInfo frameInfo = await codec.getNextFrame();
final Image image = frameInfo.image;
atlasPages.add(image);
Map<BlendMode, Paint> paints = {};
for (final blendMode in BlendMode.values) {
paints[blendMode] = Paint()
..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4
.identity()
.storage, filterQuality: Atlas.filterQuality)
..isAntiAlias = true
..blendMode = blendMode.canvasBlendMode;
}
atlasPagePaints.add(paints);
}
return Atlas._(atlas, atlasPages, atlasPagePaints);
}
/// Loads an [Atlas] from the file [atlasFileName] in the root bundle or the optionally provided [bundle].
///
/// Throws an [Exception] in case the atlas could not be loaded.
static Future<Atlas> fromAsset(String atlasFileName, {AssetBundle? bundle}) async {
bundle ??= rootBundle;
return _load(atlasFileName, (file) async => (await bundle!.load(file)).buffer.asUint8List());
}
/// Loads an [Atlas] from the file [atlasFileName].
///
/// Throws an [Exception] in case the atlas could not be loaded.
static Future<Atlas> fromFile(String atlasFileName) async {
return _load(atlasFileName, (file) => File(file).readAsBytes());
}
/// Loads an [Atlas] from the URL [atlasURL].
///
/// Throws an [Exception] in case the atlas could not be loaded.
static Future<Atlas> fromHttp(String atlasURL) async {
return _load(atlasURL, (file) async {
return (await http.get(Uri.parse(file))).bodyBytes;
});
}
/// Disposes the (native) resources of this atlas. The atlas can no longer be
/// used after calling this function. Only the first call to this method will
/// have an effect. Subsequent calls are ignored.
void dispose() {
if (_disposed) return;
_disposed = true;
_bindings.spine_atlas_dispose(_atlas);
for (final image in atlasPages) {
image.dispose();
}
atlasPagePaints.clear();
}
}
/// Skeleton data loaded from a skeleton `.json` or `.skel` file. Contains bones, slots, constraints,
/// skins, animations, and so on making up a skeleton. Also contains meta data such as the skeletons
/// setup pose bounding box, the Spine editor version it was exported from, and so on.
///
/// Skeleton data is stateless. Stateful [Skeleton] instances can be constructed from a [SkeletonData] instance.
/// A single [SkeletonData] instance can be shared by multiple [Skeleton] instances.
///
/// Use the static methods [fromJson], [fromBinary], [fromAsset], [fromFile], and [fromURL] to load
/// skeleton data. Call [dispose] when the skeleton data is no longer in use to free its resources.
///
/// See [Data objects](http://esotericsoftware.com/spine-runtime-architecture#Data-objects) in the Spine
/// Runtimes Guide.
class SkeletonData {
final spine_skeleton_data _data;
bool _disposed;
SkeletonData._(this._data) : _disposed = false;
/// Loads a [SkeletonData] from the [json] string, using the provided [atlas] to resolve attachment
/// images.
///
/// Throws an [Exception] in case the atlas could not be loaded.
static SkeletonData fromJson(Atlas atlas, String json) {
final jsonNative = json.toNativeUtf8(allocator: _allocator);
final result = _bindings.spine_skeleton_data_load_json(atlas._atlas, jsonNative.cast());
_allocator.free(jsonNative);
if (_bindings.spine_skeleton_data_result_get_error(result).address != nullptr.address) {
final Pointer<Utf8> error = _bindings.spine_skeleton_data_result_get_error(result).cast();
final message = error.toDartString();
_bindings.spine_skeleton_data_result_dispose(result);
throw Exception("Couldn't load skeleton data: $message");
}
var data = SkeletonData._(_bindings.spine_skeleton_data_result_get_data(result));
_bindings.spine_skeleton_data_result_dispose(result);
return data;
}
/// Loads a [SkeletonData] from the [binary] skeleton data, using the provided [atlas] to resolve attachment
/// images.
///
/// Throws an [Exception] in case the skeleton data could not be loaded.
static SkeletonData fromBinary(Atlas atlas, Uint8List binary) {
final Pointer<Uint8> binaryNative = _allocator.allocate(binary.lengthInBytes);
binaryNative.asTypedList(binary.lengthInBytes).setAll(0, binary);
final result = _bindings.spine_skeleton_data_load_binary(atlas._atlas, binaryNative.cast(), binary.lengthInBytes);
_allocator.free(binaryNative);
if (_bindings.spine_skeleton_data_result_get_error(result).address != nullptr.address) {
final Pointer<Utf8> error = _bindings.spine_skeleton_data_result_get_error(result).cast();
final message = error.toDartString();
_bindings.spine_skeleton_data_result_dispose(result);
throw Exception("Couldn't load skeleton data: $message");
}
var data = SkeletonData._(_bindings.spine_skeleton_data_result_get_data(result));
_bindings.spine_skeleton_data_result_dispose(result);
return data;
}
/// Loads a [SkeletonData] from the file [skeletonFile] in the root bundle or the optionally provided [bundle].
/// Uses the provided [atlas] to resolve attachment images.
///
/// Throws an [Exception] in case the skeleton data could not be loaded.
static Future<SkeletonData> fromAsset(Atlas atlas, String skeletonFile, {AssetBundle? bundle}) async {
bundle ??= rootBundle;
if (skeletonFile.endsWith(".json")) {
return fromJson(atlas, await bundle.loadString(skeletonFile));
} else {
return fromBinary(atlas, (await bundle.load(skeletonFile)).buffer.asUint8List());
}
}
/// Loads a [SkeletonData] from the file [skeletonFile]. Uses the provided [atlas] to resolve attachment images.
///
/// Throws an [Exception] in case the skeleton data could not be loaded.
static Future<SkeletonData> fromFile(Atlas atlas, String skeletonFile) async {
if (skeletonFile.endsWith(".json")) {
return fromJson(atlas, convert.utf8.decode(await File(skeletonFile).readAsBytes()));
} else {
return fromBinary(atlas, await File(skeletonFile).readAsBytes());
}
}
/// Loads a [SkeletonData] from the URL [skeletonURL]. Uses the provided [atlas] to resolve attachment images.
///
/// Throws an [Exception] in case the skeleton data could not be loaded.
static Future<SkeletonData> fromHttp(Atlas atlas, String skeletonURL) async {
if (skeletonURL.endsWith(".json")) {
return fromJson(atlas, convert.utf8.decode((await http.get(Uri.parse(skeletonURL))).bodyBytes));
} else {
return fromBinary(atlas, (await http.get(Uri.parse(skeletonURL))).bodyBytes);
}
}
/// The skeleton's bones, sorted parent first. The root bone is always the first bone.
List<BoneData> getBones() {
final List<BoneData> bones = [];
final numBones = _bindings.spine_skeleton_data_get_num_bones(_data);
final nativeBones = _bindings.spine_skeleton_data_get_bones(_data);
for (int i = 0; i < numBones; i++) {
bones.add(BoneData._(nativeBones[i]));
}
return bones;
}
/// Finds a bone by comparing each bone's name. It is more efficient to cache the results of this method than to call it multiple times.
BoneData? findBone(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final bone = _bindings.spine_skeleton_data_find_bone(_data, nativeName.cast());
_allocator.free(nativeName);
if (bone.address == nullptr.address) return null;
return BoneData._(bone);
}
/// The skeleton's slots.
List<SlotData> getSlots() {
final List<SlotData> slots = [];
final numSlots = _bindings.spine_skeleton_data_get_num_slots(_data);
final nativeSlots = _bindings.spine_skeleton_data_get_slots(_data);
for (int i = 0; i < numSlots; i++) {
slots.add(SlotData._(nativeSlots[i]));
}
return slots;
}
/// Finds a slot by comparing each slot's name. It is more efficient to cache the results of this method than to call it multiple times.
SlotData? findSlot(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final slot = _bindings.spine_skeleton_data_find_slot(_data, nativeName.cast());
_allocator.free(nativeName);
if (slot.address == nullptr.address) return null;
return SlotData._(slot);
}
/// All skins, including the default skin.
List<Skin> getSkins() {
final List<Skin> skins = [];
final numSkins = _bindings.spine_skeleton_data_get_num_skins(_data);
final nativeSkins = _bindings.spine_skeleton_data_get_skins(_data);
for (int i = 0; i < numSkins; i++) {
skins.add(Skin._(nativeSkins[i]));
}
return skins;
}
/// The skeleton's default skin. By default this skin contains all attachments that were not in a skin in Spine.
Skin? getDefaultSkin() {
final skin = _bindings.spine_skeleton_data_get_default_skin(_data);
if (skin.address == nullptr.address) return null;
return Skin._(skin);
}
void setDefaultSkin(Skin? skin) {
if (skin == null) {
_bindings.spine_skeleton_data_set_default_skin(_data, nullptr);
} else {
_bindings.spine_skeleton_data_set_default_skin(_data, skin._skin);
}
}
/// Finds a skin by comparing each skin's name. It is more efficient to cache the results of this method than to call it
/// multiple times.
Skin? findSkin(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final skin = _bindings.spine_skeleton_data_find_skin(_data, nativeName.cast());
_allocator.free(nativeName);
if (skin.address == nullptr.address) return null;
return Skin._(skin);
}
/// The skeleton's events.
List<EventData> getEvents() {
final List<EventData> events = [];
final numEvents = _bindings.spine_skeleton_data_get_num_events(_data);
final nativeEvents = _bindings.spine_skeleton_data_get_events(_data);
for (int i = 0; i < numEvents; i++) {
events.add(EventData._(nativeEvents[i]));
}
return events;
}
/// Finds an event by comparing each events's name. It is more efficient to cache the results of this method than to call it
/// multiple times.
EventData? findEvent(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final event = _bindings.spine_skeleton_data_find_event(_data, nativeName.cast());
_allocator.free(nativeName);
if (event.address == nullptr.address) return null;
return EventData._(event);
}
/// The skeleton's animations.
List<Animation> getAnimations() {
final List<Animation> events = [];
final numAnimation = _bindings.spine_skeleton_data_get_num_animations(_data);
final nativeAnimations = _bindings.spine_skeleton_data_get_animations(_data);
for (int i = 0; i < numAnimation; i++) {
events.add(Animation._(nativeAnimations[i]));
}
return events;
}
/// Finds an animation by comparing each animation's name. It is more efficient to cache the results of this method than to
/// call it multiple times.
Animation? findAnimation(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final animation = _bindings.spine_skeleton_data_find_animation(_data, nativeName.cast());
_allocator.free(nativeName);
if (animation.address == nullptr.address) return null;
return Animation._(animation);
}
/// The skeleton's IK constraints.
List<IkConstraintData> getIkConstraints() {
final List<IkConstraintData> constraints = [];
final numConstraints = _bindings.spine_skeleton_data_get_num_ik_constraints(_data);
final nativeConstraints = _bindings.spine_skeleton_data_get_ik_constraints(_data);
for (int i = 0; i < numConstraints; i++) {
constraints.add(IkConstraintData._(nativeConstraints[i]));
}
return constraints;
}
/// Finds an IK constraint by comparing each IK constraint's name. It is more efficient to cache the results of this method
/// than to call it multiple times.
IkConstraintData? findIkConstraint(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final constraint = _bindings.spine_skeleton_data_find_ik_constraint(_data, nativeName.cast());
_allocator.free(nativeName);
if (constraint.address == nullptr.address) return null;
return IkConstraintData._(constraint);
}
/// The skeleton's transform constraints.
List<TransformConstraint> getTransformConstraints() {
final List<TransformConstraint> constraints = [];
final numConstraints = _bindings.spine_skeleton_data_get_num_transform_constraints(_data);
final nativeConstraints = _bindings.spine_skeleton_data_get_transform_constraints(_data);
for (int i = 0; i < numConstraints; i++) {
constraints.add(TransformConstraint._(nativeConstraints[i].cast()));
}
return constraints;
}
/// Finds a transform constraint by comparing each transform constraint's name. It is more efficient to cache the results of
/// this method than to call it multiple times.
TransformConstraintData? findTransformConstraint(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final constraint = _bindings.spine_skeleton_data_find_transform_constraint(_data, nativeName.cast());
_allocator.free(nativeName);
if (constraint.address == nullptr.address) return null;
return TransformConstraintData._(constraint);
}
/// The skeleton's path constraints.
List<PathConstraintData> getPathConstraints() {
final List<PathConstraintData> constraints = [];
final numConstraints = _bindings.spine_skeleton_data_get_num_path_constraints(_data);
final nativeConstraints = _bindings.spine_skeleton_data_get_path_constraints(_data);
for (int i = 0; i < numConstraints; i++) {
constraints.add(PathConstraintData._(nativeConstraints[i]));
}
return constraints;
}
/// Finds a path constraint by comparing each path constraint's name. It is more efficient to cache the results of this method
/// than to call it multiple times.
PathConstraintData? findPathConstraint(String name) {
final nativeName = name.toNativeUtf8(allocator: _allocator);
final constraint = _bindings.spine_skeleton_data_find_path_constraint(_data, nativeName.cast());
_allocator.free(nativeName);
if (constraint.address == nullptr.address) return null;
return PathConstraintData._(constraint);
}
/// The skeleton's name, which by default is the name of the skeleton data file when possible, or null when a name hasn't been
/// set.
String? getName() {
Pointer<Utf8> name = _bindings.spine_skeleton_data_get_name(_data).cast();
if (name.address == nullptr.address) return null;
return name.toDartString();
}
/// The X coordinate of the skeleton's axis aligned bounding box in the setup pose.
double getX() {
return _bindings.spine_skeleton_data_get_x(_data);
}
void setX(double x) {
_bindings.spine_skeleton_data_set_x(_data, x);
}
/// The Y coordinate of the skeleton's axis aligned bounding box in the setup pose.
double getY() {
return _bindings.spine_skeleton_data_get_y(_data);
}
void setY(double y) {
_bindings.spine_skeleton_data_set_x(_data, y);
}
/// The width of the skeleton's axis aligned bounding box in the setup pose.
double getWidth() {
return _bindings.spine_skeleton_data_get_width(_data);
}
void setWidth(double width) {
_bindings.spine_skeleton_data_set_width(_data, width);
}
/// The height of the skeleton's axis aligned bounding box in the setup pose.
double getHeight() {
return _bindings.spine_skeleton_data_get_height(_data);
}
void setHeight(double height) {
_bindings.spine_skeleton_data_set_height(_data, height);
}
/// The Spine version used to export the skeleton data.
String? getVersion() {
Pointer<Utf8> name = _bindings.spine_skeleton_data_get_version(_data).cast();
if (name.address == nullptr.address) return null;
return name.toDartString();
}
/// The skeleton data hash. This value will change if any of the skeleton data has changed.
String? getHash() {
Pointer<Utf8> name = _bindings.spine_skeleton_data_get_hash(_data).cast();
if (name.address == nullptr.address) return null;
return name.toDartString();
}
/// The path to the images directory as defined in Spine, or null if nonessential data was not exported.
String? getImagesPath() {
Pointer<Utf8> name = _bindings.spine_skeleton_data_get_images_path(_data).cast();
if (name.address == nullptr.address) return null;
return name.toDartString();
}
/// The path to the audio directory as defined in Spine, or null if nonessential data was not exported.
String? getAudioPath() {
Pointer<Utf8> name = _bindings.spine_skeleton_data_get_audio_path(_data).cast();
if (name.address == nullptr.address) return null;
return name.toDartString();
}
/// The dopesheet FPS in Spine, or zero if nonessential data was not exported.
double getFps() {
return _bindings.spine_skeleton_data_get_fps(_data);
}
/// Disposes the (native) resources of this skeleton data. The skeleton data can no longer be
/// used after calling this function. Only the first call to this method will
/// have an effect. Subsequent calls are ignored.
void dispose() {
if (_disposed) return;
_disposed = true;
_bindings.spine_skeleton_data_dispose(_data);
}
}
/// Determines how images are blended with existing pixels when drawn. See [Blending](http://esotericsoftware.com/spine-slots#Blending) in
/// the Spine User Guide.
enum BlendMode {
normal(0, rendering.BlendMode.srcOver),
additive(1, rendering.BlendMode.plus),
multiply(2, rendering.BlendMode.multiply),
screen(3, rendering.BlendMode.screen);
final int value;
final rendering.BlendMode canvasBlendMode;
const BlendMode(this.value, this.canvasBlendMode);
}
/// Determines how a bone inherits world transforms from parent bones. See [Transform inheritance](esotericsoftware.com/spine-bones#Transform-inheritance)
/// in the Spine User Guide.
enum Inherit {
normal(0),
onlyTranslation(1),
noRotationOrReflection(2),
noScale(3),
noScaleOrReflection(4);
final int value;
const Inherit(this.value);
}
/// Determines how physics and other non-deterministic updates are applied.
enum Physics {
none(0),
reset(1),
update(2),
pose(3);
final int value;
const Physics(this.value);
}
/// Controls how the first bone is positioned along the path.
///
/// See [Position mode](http://esotericsoftware.com/spine-path-constraints#Position-mode) in the Spine User Guide.
enum PositionMode {
fixed(0),
percent(1);
final int value;
const PositionMode(this.value);
}
/// Controls how bones after the first bone are positioned along the path.
///
/// See [Spacing mode](http://esotericsoftware.com/spine-path-constraints#Spacing-mode) in the Spine User Guide.
enum SpacingMode {
length(0),
fixed(1),
percent(2),
proportional(3);
final int value;
const SpacingMode(this.value);
}
/// Controls how bones are rotated, translated, and scaled to match the path.
///
/// See [Rotate mode](http://esotericsoftware.com/spine-path-constraints#Rotate-mode) in the Spine User Guide.
enum RotateMode {
tangent(0),
chain(1),
chainScale(2);
final int value;
const RotateMode(this.value);
}
/// Stores the setup pose for a [Bone].
class BoneData {
final spine_bone_data _data;
BoneData._(this._data);
/// The index of the bone in [Skeleton.getBones].
int getIndex() {
return _bindings.spine_bone_data_get_index(_data);
}
/// The name of the bone, which is unique across all bones in the skeleton.
String getName() {
Pointer<Utf8> name = _bindings.spine_bone_data_get_name(_data).cast();
return name.toDartString();
}
/// The parent bone or `null` if this is the root bone.
BoneData? getParent() {
final parent = _bindings.spine_bone_data_get_parent(_data);
if (parent.address == nullptr.address) return null;
return BoneData._(parent);
}
/// The bone's length.
double getLength() {
return _bindings.spine_bone_data_get_length(_data);
}
void setLength(double length) {
_bindings.spine_bone_data_set_length(_data, length);
}
/// The local x translation.
double getX() {
return _bindings.spine_bone_data_get_x(_data);
}
void setX(double x) {
_bindings.spine_bone_data_set_x(_data, x);
}
/// The local y translation.
double getY() {
return _bindings.spine_bone_data_get_y(_data);
}
void setY(double y) {
_bindings.spine_bone_data_set_y(_data, y);
}
/// The local rotation in degrees.
double getRotation() {
return _bindings.spine_bone_data_get_rotation(_data);
}
void setRotation(double rotation) {
_bindings.spine_bone_data_set_rotation(_data, rotation);
}
/// The local scaleX.
double getScaleX() {
return _bindings.spine_bone_data_get_scale_x(_data);
}
void setScaleX(double scaleX) {
_bindings.spine_bone_data_set_scale_x(_data, scaleX);
}
/// The local scaleY.
double getScaleY() {
return _bindings.spine_bone_data_get_scale_y(_data);
}
void setScaleY(double scaleY) {
_bindings.spine_bone_data_set_scale_y(_data, scaleY);
}
/// The local shearX.
double getShearX() {
return _bindings.spine_bone_data_get_shear_x(_data);
}
void setShearX(double shearX) {
_bindings.spine_bone_data_set_shear_x(_data, shearX);
}
/// The local shearY.
double getShearY() {
return _bindings.spine_bone_data_get_shear_y(_data);
}
void setShearY(double shearY) {
_bindings.spine_bone_data_set_shear_y(_data, shearY);
}
/// The [Inherit] for how parent world transforms affect this bone.
Inherit getInherit() {
final nativeMode = _bindings.spine_bone_data_get_inherit(_data);
return Inherit.values[nativeMode];
}
void setInherit(Inherit inherit) {
_bindings.spine_bone_data_set_inherit(_data, inherit.value);
}
/// When true, [Skeleton.updateWorldTransform] only updates this bone if the [Skeleton.getSkin] contains this bone.
///
/// See [Skin.getBones].
bool isSkinRequired() {
return _bindings.spine_bone_data_is_skin_required(_data) == -1;
}
void setIsSkinRequired(bool isSkinRequired) {
_bindings.spine_bone_data_set_is_skin_required(_data, isSkinRequired ? -1 : 0);
}
/// The [Color] of the bone as it was in Spine, or a default color if nonessential data was not exported. Bones are not usually
/// rendered at runtime.
Color getColor() {
final color = _bindings.spine_bone_data_get_color(_data);
return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
_bindings.spine_color_get_a(color));
}
void setColor(double r, double g, double b, double a) {
_bindings.spine_bone_data_set_color(_data, r, g, b, a);
}
@override
String toString() {
return getName();
}
}
/// Stores a bone's current pose.
///
/// A bone has a local transform which is used to compute its world transform. A bone also has an applied transform, which is a
/// local transform that can be applied to compute the world transform. The local transform and applied transform may differ if a
/// constraint or application code modifies the world transform after it was computed from the local transform.
class Bone {
final spine_bone _bone;
Bone._(this._bone);
/// Assume y-axis pointing down for all calculations.
static void setIsYDown(bool isYDown) {
_bindings.spine_bone_set_is_y_down(isYDown ? -1 : 0);
}
static bool getIsYDown() {
return _bindings.spine_bone_get_is_y_down() == 1;
}
/// Computes the world transform using the parent bone and this bone's local applied transform.
void update() {
_bindings.spine_bone_update(_bone);
}
/// Computes the world transform using the parent bone and this bone's local transform.
///
/// See [updateWorldTransformWith].
void updateWorldTransform() {
_bindings.spine_bone_update_world_transform(_bone);
}
/// Computes the world transform using the parent bone and the specified local transform. The applied transform is set to the
/// specified local transform. Child bones are not updated.
///
/// See [World transform](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms) in the Spine
/// Runtimes Guide.
void updateWorldTransformWith(double x, double y, double rotation, double scaleX, double scaleY, double shearX, double shearY) {
_bindings.spine_bone_update_world_transform_with(_bone, x, y, rotation, scaleX, scaleY, shearX, shearY);
}
/// Computes the applied transform values from the world transform.
///
/// If the world transform is modified (by a constraint, [rotateWorld], etc) then this method should be called so
/// the applied transform matches the world transform. The applied transform may be needed by other code (eg to apply another
/// constraint).
///
/// Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. The applied transform after
/// calling this method is equivalent to the local transform used to compute the world transform, but may not be identical.
void updateAppliedTransform() {
_bindings.spine_bone_update_applied_transform(_bone);
}
/// Sets this bone's local transform to the setup pose.
void setToSetupPose() {
_bindings.spine_bone_set_to_setup_pose(_bone);
}
/// Transforms a point from world coordinates to the bone's local coordinates.
Vec2 worldToLocal(double worldX, double worldY) {
final local = _bindings.spine_bone_world_to_local(_bone, worldX, worldY);
final result = Vec2(_bindings.spine_vector_get_x(local), _bindings.spine_vector_get_y(local));
return result;
}
/// Transforms a point from the bone's local coordinates to world coordinates.
Vec2 localToWorld(double localX, double localY) {
final world = _bindings.spine_bone_local_to_world(_bone, localX, localY);
final result = Vec2(_bindings.spine_vector_get_x(world), _bindings.spine_vector_get_y(world));
return result;
}
/// Transforms a world rotation to a local rotation.
double worldToLocalRotation(double worldRotation) {
return _bindings.spine_bone_world_to_local_rotation(_bone, worldRotation);
}
/// Transforms a local rotation to a world rotation.
double localToWorldRotation(double localRotation) {
return _bindings.spine_bone_local_to_world_rotation(_bone, localRotation);
}
/// Rotates the world transform the specified amount.
///
/// After changes are made to the world transform, [updateAppliedTransform] should be called and [update] will
/// need to be called on any child bones, recursively.
void rotateWorld(double degrees) {
_bindings.spine_bone_rotate_world(_bone, degrees);
}
double getWorldToLocalRotationX() {
return _bindings.spine_bone_get_world_rotation_x(_bone);
}
double getWorldToLocalRotationY() {
return _bindings.spine_bone_get_world_to_local_rotation_y(_bone);
}
/// The bone's setup pose data.
BoneData getData() {
return BoneData._(_bindings.spine_bone_get_data(_bone));
}
/// The skeleton this bone belongs to.
Skeleton getSkeleton() {
return Skeleton._(_bindings.spine_bone_get_skeleton(_bone));
}
/// The parent bone, or null if this is the root bone.
Bone? getParent() {
final parent = _bindings.spine_bone_get_parent(_bone);
if (parent.address == nullptr.address) return null;
return Bone._(parent);
}
/// The immediate children of this bone.
List<Bone> getChildren() {
List<Bone> children = [];
final numChildren = _bindings.spine_bone_get_num_children(_bone);
final nativeChildren = _bindings.spine_bone_get_children(_bone);
for (int i = 0; i < numChildren; i++) {
children.add(Bone._(nativeChildren[i]));
}
return children;
}
/// The local x translation.
double getX() {
return _bindings.spine_bone_get_x(_bone);
}
void setX(double x) {
_bindings.spine_bone_set_x(_bone, x);
}
/// The local y translation.
double getY() {
return _bindings.spine_bone_get_y(_bone);
}
void setY(double y) {
_bindings.spine_bone_set_y(_bone, y);
}
/// The local rotation in degrees, counter clockwise.
double getRotation() {
return _bindings.spine_bone_get_rotation(_bone);
}
void setRotation(double rotation) {
_bindings.spine_bone_set_rotation(_bone, rotation);
}
/// The local scaleX.
double getScaleX() {
return _bindings.spine_bone_get_scale_x(_bone);
}
void setScaleX(double scaleX) {
_bindings.spine_bone_set_scale_x(_bone, scaleX);
}
/// The local scaleY.
double getScaleY() {
return _bindings.spine_bone_get_scale_y(_bone);
}
void setScaleY(double scaleY) {
_bindings.spine_bone_set_scale_y(_bone, scaleY);
}
/// The local shearX.
double getShearX() {
return _bindings.spine_bone_get_shear_x(_bone);
}
void setShearX(double shearX) {
_bindings.spine_bone_set_shear_x(_bone, shearX);
}
/// The local shearY.
double getShearY() {
return _bindings.spine_bone_get_shear_y(_bone);
}
void setShearY(double shearY) {
_bindings.spine_bone_set_shear_y(_bone, shearY);
}
/// The applied local x translation.
double getAX() {
return _bindings.spine_bone_get_a_x(_bone);
}
void setAX(double x) {
_bindings.spine_bone_set_a_x(_bone, x);
}
/// The applied local y translation.
double getAY() {
return _bindings.spine_bone_get_a_y(_bone);
}
void setAY(double y) {
_bindings.spine_bone_set_a_y(_bone, y);
}
/// The applied local rotation in degrees, counter clockwise.
double getAppliedRotation() {
return _bindings.spine_bone_get_applied_rotation(_bone);
}
void setAppliedRotation(double rotation) {
_bindings.spine_bone_set_applied_rotation(_bone, rotation);
}
/// The applied local scaleX.
double getAScaleX() {
return _bindings.spine_bone_get_a_scale_x(_bone);
}
void setAScaleX(double scaleX) {
_bindings.spine_bone_set_a_scale_x(_bone, scaleX);
}
/// The applied local scaleY.
double getAScaleY() {
return _bindings.spine_bone_get_a_scale_y(_bone);
}
void setAScaleY(double scaleY) {
_bindings.spine_bone_set_a_scale_y(_bone, scaleY);
}
/// The applied local shearX.
double getAShearX() {
return _bindings.spine_bone_get_a_shear_x(_bone);
}