spine_flutter.dart 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164
  1. ///
  2. /// Spine Runtimes License Agreement
  3. /// Last updated April 5, 2025. Replaces all prior versions.
  4. ///
  5. /// Copyright (c) 2013-2025, Esoteric Software LLC
  6. ///
  7. /// Integration of the Spine Runtimes into software or otherwise creating
  8. /// derivative works of the Spine Runtimes is permitted under the terms and
  9. /// conditions of Section 2 of the Spine Editor License Agreement:
  10. /// http://esotericsoftware.com/spine-editor-license
  11. ///
  12. /// Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. /// or otherwise create derivative works of the Spine Runtimes (collectively,
  14. /// "Products"), provided that each user of the Products must obtain their own
  15. /// Spine Editor license and redistribution of the Products in any form must
  16. /// include this license and copyright notice.
  17. ///
  18. /// THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. /// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. /// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. /// DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. /// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. /// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. /// BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. /// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. /// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. /// THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. ///
  29. import 'dart:convert' as convert;
  30. import 'dart:io';
  31. import 'dart:typed_data';
  32. import 'dart:ui';
  33. import 'package:flutter/foundation.dart' show kIsWeb;
  34. import 'package:flutter/material.dart' as material;
  35. import 'package:flutter/rendering.dart' as rendering;
  36. import 'package:flutter/services.dart';
  37. import 'package:http/http.dart' as http;
  38. import 'package:path/path.dart' as path;
  39. import 'ffi_proxy.dart';
  40. import 'init.dart' if (dart.library.html) 'init_web.dart';
  41. import 'spine_flutter_bindings_generated.dart';
  42. import 'raw_image_provider.dart';
  43. export 'spine_widget.dart';
  44. late SpineFlutterBindings _bindings;
  45. late Allocator _allocator;
  46. Future<void> initSpineFlutter({bool useStaticLinkage = false, bool enableMemoryDebugging = false}) async {
  47. final ffi = await initSpineFlutterFFI(useStaticLinkage);
  48. _bindings = SpineFlutterBindings(ffi.dylib);
  49. _allocator = ffi.allocator;
  50. if (enableMemoryDebugging) _bindings.spine_enable_debug_extension(-1);
  51. return;
  52. }
  53. int majorVersion() => _bindings.spine_major_version();
  54. int minorVersion() => _bindings.spine_minor_version();
  55. void reportLeaks() => _bindings.spine_report_leaks();
  56. /// A color made of red, green, blue, and alpha components,
  57. /// ranging from 0-1.
  58. class Color {
  59. double r;
  60. double g;
  61. double b;
  62. double a;
  63. Color(this.r, this.g, this.b, this.a);
  64. }
  65. /// Bounds denoted by the top left corner coordinates [x] and [y]
  66. /// and the [width] and [height].
  67. class Bounds {
  68. double x;
  69. double y;
  70. double width;
  71. double height;
  72. Bounds(this.x, this.y, this.width, this.height);
  73. }
  74. /// A two-dimensional vector with [x] and [y] components.
  75. class Vec2 {
  76. double x;
  77. double y;
  78. Vec2(this.x, this.y);
  79. }
  80. /// Atlas data loaded from a `.atlas` file and its corresponding `.png` files. For each atlas image,
  81. /// a corresponding [Image] and [Paint] is constructed, which are used when rendering a skeleton
  82. /// that uses this atlas.
  83. ///
  84. /// Use the static methods [fromAsset], [fromFile], and [fromHttp] to load an atlas. Call [dispose]
  85. /// when the atlas is no longer in use to release its resources.
  86. class Atlas {
  87. static FilterQuality filterQuality = FilterQuality.none;
  88. final spine_atlas _atlas;
  89. final List<Image> atlasPages;
  90. final List<Map<BlendMode, Paint>> atlasPagePaints;
  91. bool _disposed;
  92. Atlas._(this._atlas, this.atlasPages, this.atlasPagePaints) : _disposed = false;
  93. static Future<Atlas> _load(String atlasFileName, Future<Uint8List> Function(String name) loadFile) async {
  94. final atlasBytes = await loadFile(atlasFileName);
  95. final atlasData = convert.utf8.decode(atlasBytes);
  96. final atlasDataNative = atlasData.toNativeUtf8(allocator: _allocator);
  97. final atlas = _bindings.spine_atlas_load(atlasDataNative.cast());
  98. _allocator.free(atlasDataNative);
  99. if (_bindings.spine_atlas_get_error(atlas).address != nullptr.address) {
  100. final Pointer<Utf8> error = _bindings.spine_atlas_get_error(atlas).cast();
  101. final message = error.toDartString();
  102. _bindings.spine_atlas_dispose(atlas);
  103. throw Exception("Couldn't load atlas: $message");
  104. }
  105. final atlasDir = path.dirname(atlasFileName);
  106. List<Image> atlasPages = [];
  107. List<Map<BlendMode, Paint>> atlasPagePaints = [];
  108. final numImagePaths = _bindings.spine_atlas_get_num_image_paths(atlas);
  109. for (int i = 0; i < numImagePaths; i++) {
  110. final Pointer<Utf8> atlasPageFile = _bindings.spine_atlas_get_image_path(atlas, i).cast();
  111. final imagePath = "$atlasDir/${atlasPageFile.toDartString()}";
  112. var imageData = await loadFile(imagePath);
  113. final Codec codec = await instantiateImageCodec(imageData);
  114. final FrameInfo frameInfo = await codec.getNextFrame();
  115. final Image image = frameInfo.image;
  116. atlasPages.add(image);
  117. Map<BlendMode, Paint> paints = {};
  118. for (final blendMode in BlendMode.values) {
  119. paints[blendMode] = Paint()
  120. ..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4
  121. .identity()
  122. .storage, filterQuality: Atlas.filterQuality)
  123. ..isAntiAlias = true
  124. ..blendMode = blendMode.canvasBlendMode;
  125. }
  126. atlasPagePaints.add(paints);
  127. }
  128. return Atlas._(atlas, atlasPages, atlasPagePaints);
  129. }
  130. /// Loads an [Atlas] from the file [atlasFileName] in the root bundle or the optionally provided [bundle].
  131. ///
  132. /// Throws an [Exception] in case the atlas could not be loaded.
  133. static Future<Atlas> fromAsset(String atlasFileName, {AssetBundle? bundle}) async {
  134. bundle ??= rootBundle;
  135. return _load(atlasFileName, (file) async => (await bundle!.load(file)).buffer.asUint8List());
  136. }
  137. /// Loads an [Atlas] from the file [atlasFileName].
  138. ///
  139. /// Throws an [Exception] in case the atlas could not be loaded.
  140. static Future<Atlas> fromFile(String atlasFileName) async {
  141. return _load(atlasFileName, (file) => File(file).readAsBytes());
  142. }
  143. /// Loads an [Atlas] from the URL [atlasURL].
  144. ///
  145. /// Throws an [Exception] in case the atlas could not be loaded.
  146. static Future<Atlas> fromHttp(String atlasURL) async {
  147. return _load(atlasURL, (file) async {
  148. return (await http.get(Uri.parse(file))).bodyBytes;
  149. });
  150. }
  151. /// Disposes the (native) resources of this atlas. The atlas can no longer be
  152. /// used after calling this function. Only the first call to this method will
  153. /// have an effect. Subsequent calls are ignored.
  154. void dispose() {
  155. if (_disposed) return;
  156. _disposed = true;
  157. _bindings.spine_atlas_dispose(_atlas);
  158. for (final image in atlasPages) {
  159. image.dispose();
  160. }
  161. atlasPagePaints.clear();
  162. }
  163. }
  164. /// Skeleton data loaded from a skeleton `.json` or `.skel` file. Contains bones, slots, constraints,
  165. /// skins, animations, and so on making up a skeleton. Also contains meta data such as the skeletons
  166. /// setup pose bounding box, the Spine editor version it was exported from, and so on.
  167. ///
  168. /// Skeleton data is stateless. Stateful [Skeleton] instances can be constructed from a [SkeletonData] instance.
  169. /// A single [SkeletonData] instance can be shared by multiple [Skeleton] instances.
  170. ///
  171. /// Use the static methods [fromJson], [fromBinary], [fromAsset], [fromFile], and [fromURL] to load
  172. /// skeleton data. Call [dispose] when the skeleton data is no longer in use to free its resources.
  173. ///
  174. /// See [Data objects](http://esotericsoftware.com/spine-runtime-architecture#Data-objects) in the Spine
  175. /// Runtimes Guide.
  176. class SkeletonData {
  177. final spine_skeleton_data _data;
  178. bool _disposed;
  179. SkeletonData._(this._data) : _disposed = false;
  180. /// Loads a [SkeletonData] from the [json] string, using the provided [atlas] to resolve attachment
  181. /// images.
  182. ///
  183. /// Throws an [Exception] in case the atlas could not be loaded.
  184. static SkeletonData fromJson(Atlas atlas, String json) {
  185. final jsonNative = json.toNativeUtf8(allocator: _allocator);
  186. final result = _bindings.spine_skeleton_data_load_json(atlas._atlas, jsonNative.cast());
  187. _allocator.free(jsonNative);
  188. if (_bindings.spine_skeleton_data_result_get_error(result).address != nullptr.address) {
  189. final Pointer<Utf8> error = _bindings.spine_skeleton_data_result_get_error(result).cast();
  190. final message = error.toDartString();
  191. _bindings.spine_skeleton_data_result_dispose(result);
  192. throw Exception("Couldn't load skeleton data: $message");
  193. }
  194. var data = SkeletonData._(_bindings.spine_skeleton_data_result_get_data(result));
  195. _bindings.spine_skeleton_data_result_dispose(result);
  196. return data;
  197. }
  198. /// Loads a [SkeletonData] from the [binary] skeleton data, using the provided [atlas] to resolve attachment
  199. /// images.
  200. ///
  201. /// Throws an [Exception] in case the skeleton data could not be loaded.
  202. static SkeletonData fromBinary(Atlas atlas, Uint8List binary) {
  203. final Pointer<Uint8> binaryNative = _allocator.allocate(binary.lengthInBytes);
  204. binaryNative.asTypedList(binary.lengthInBytes).setAll(0, binary);
  205. final result = _bindings.spine_skeleton_data_load_binary(atlas._atlas, binaryNative.cast(), binary.lengthInBytes);
  206. _allocator.free(binaryNative);
  207. if (_bindings.spine_skeleton_data_result_get_error(result).address != nullptr.address) {
  208. final Pointer<Utf8> error = _bindings.spine_skeleton_data_result_get_error(result).cast();
  209. final message = error.toDartString();
  210. _bindings.spine_skeleton_data_result_dispose(result);
  211. throw Exception("Couldn't load skeleton data: $message");
  212. }
  213. var data = SkeletonData._(_bindings.spine_skeleton_data_result_get_data(result));
  214. _bindings.spine_skeleton_data_result_dispose(result);
  215. return data;
  216. }
  217. /// Loads a [SkeletonData] from the file [skeletonFile] in the root bundle or the optionally provided [bundle].
  218. /// Uses the provided [atlas] to resolve attachment images.
  219. ///
  220. /// Throws an [Exception] in case the skeleton data could not be loaded.
  221. static Future<SkeletonData> fromAsset(Atlas atlas, String skeletonFile, {AssetBundle? bundle}) async {
  222. bundle ??= rootBundle;
  223. if (skeletonFile.endsWith(".json")) {
  224. return fromJson(atlas, await bundle.loadString(skeletonFile));
  225. } else {
  226. return fromBinary(atlas, (await bundle.load(skeletonFile)).buffer.asUint8List());
  227. }
  228. }
  229. /// Loads a [SkeletonData] from the file [skeletonFile]. Uses the provided [atlas] to resolve attachment images.
  230. ///
  231. /// Throws an [Exception] in case the skeleton data could not be loaded.
  232. static Future<SkeletonData> fromFile(Atlas atlas, String skeletonFile) async {
  233. if (skeletonFile.endsWith(".json")) {
  234. return fromJson(atlas, convert.utf8.decode(await File(skeletonFile).readAsBytes()));
  235. } else {
  236. return fromBinary(atlas, await File(skeletonFile).readAsBytes());
  237. }
  238. }
  239. /// Loads a [SkeletonData] from the URL [skeletonURL]. Uses the provided [atlas] to resolve attachment images.
  240. ///
  241. /// Throws an [Exception] in case the skeleton data could not be loaded.
  242. static Future<SkeletonData> fromHttp(Atlas atlas, String skeletonURL) async {
  243. if (skeletonURL.endsWith(".json")) {
  244. return fromJson(atlas, convert.utf8.decode((await http.get(Uri.parse(skeletonURL))).bodyBytes));
  245. } else {
  246. return fromBinary(atlas, (await http.get(Uri.parse(skeletonURL))).bodyBytes);
  247. }
  248. }
  249. /// The skeleton's bones, sorted parent first. The root bone is always the first bone.
  250. List<BoneData> getBones() {
  251. final List<BoneData> bones = [];
  252. final numBones = _bindings.spine_skeleton_data_get_num_bones(_data);
  253. final nativeBones = _bindings.spine_skeleton_data_get_bones(_data);
  254. for (int i = 0; i < numBones; i++) {
  255. bones.add(BoneData._(nativeBones[i]));
  256. }
  257. return bones;
  258. }
  259. /// 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.
  260. BoneData? findBone(String name) {
  261. final nativeName = name.toNativeUtf8(allocator: _allocator);
  262. final bone = _bindings.spine_skeleton_data_find_bone(_data, nativeName.cast());
  263. _allocator.free(nativeName);
  264. if (bone.address == nullptr.address) return null;
  265. return BoneData._(bone);
  266. }
  267. /// The skeleton's slots.
  268. List<SlotData> getSlots() {
  269. final List<SlotData> slots = [];
  270. final numSlots = _bindings.spine_skeleton_data_get_num_slots(_data);
  271. final nativeSlots = _bindings.spine_skeleton_data_get_slots(_data);
  272. for (int i = 0; i < numSlots; i++) {
  273. slots.add(SlotData._(nativeSlots[i]));
  274. }
  275. return slots;
  276. }
  277. /// 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.
  278. SlotData? findSlot(String name) {
  279. final nativeName = name.toNativeUtf8(allocator: _allocator);
  280. final slot = _bindings.spine_skeleton_data_find_slot(_data, nativeName.cast());
  281. _allocator.free(nativeName);
  282. if (slot.address == nullptr.address) return null;
  283. return SlotData._(slot);
  284. }
  285. /// All skins, including the default skin.
  286. List<Skin> getSkins() {
  287. final List<Skin> skins = [];
  288. final numSkins = _bindings.spine_skeleton_data_get_num_skins(_data);
  289. final nativeSkins = _bindings.spine_skeleton_data_get_skins(_data);
  290. for (int i = 0; i < numSkins; i++) {
  291. skins.add(Skin._(nativeSkins[i]));
  292. }
  293. return skins;
  294. }
  295. /// The skeleton's default skin. By default this skin contains all attachments that were not in a skin in Spine.
  296. Skin? getDefaultSkin() {
  297. final skin = _bindings.spine_skeleton_data_get_default_skin(_data);
  298. if (skin.address == nullptr.address) return null;
  299. return Skin._(skin);
  300. }
  301. void setDefaultSkin(Skin? skin) {
  302. if (skin == null) {
  303. _bindings.spine_skeleton_data_set_default_skin(_data, nullptr);
  304. } else {
  305. _bindings.spine_skeleton_data_set_default_skin(_data, skin._skin);
  306. }
  307. }
  308. /// Finds a skin by comparing each skin's name. It is more efficient to cache the results of this method than to call it
  309. /// multiple times.
  310. Skin? findSkin(String name) {
  311. final nativeName = name.toNativeUtf8(allocator: _allocator);
  312. final skin = _bindings.spine_skeleton_data_find_skin(_data, nativeName.cast());
  313. _allocator.free(nativeName);
  314. if (skin.address == nullptr.address) return null;
  315. return Skin._(skin);
  316. }
  317. /// The skeleton's events.
  318. List<EventData> getEvents() {
  319. final List<EventData> events = [];
  320. final numEvents = _bindings.spine_skeleton_data_get_num_events(_data);
  321. final nativeEvents = _bindings.spine_skeleton_data_get_events(_data);
  322. for (int i = 0; i < numEvents; i++) {
  323. events.add(EventData._(nativeEvents[i]));
  324. }
  325. return events;
  326. }
  327. /// Finds an event by comparing each events's name. It is more efficient to cache the results of this method than to call it
  328. /// multiple times.
  329. EventData? findEvent(String name) {
  330. final nativeName = name.toNativeUtf8(allocator: _allocator);
  331. final event = _bindings.spine_skeleton_data_find_event(_data, nativeName.cast());
  332. _allocator.free(nativeName);
  333. if (event.address == nullptr.address) return null;
  334. return EventData._(event);
  335. }
  336. /// The skeleton's animations.
  337. List<Animation> getAnimations() {
  338. final List<Animation> events = [];
  339. final numAnimation = _bindings.spine_skeleton_data_get_num_animations(_data);
  340. final nativeAnimations = _bindings.spine_skeleton_data_get_animations(_data);
  341. for (int i = 0; i < numAnimation; i++) {
  342. events.add(Animation._(nativeAnimations[i]));
  343. }
  344. return events;
  345. }
  346. /// Finds an animation by comparing each animation's name. It is more efficient to cache the results of this method than to
  347. /// call it multiple times.
  348. Animation? findAnimation(String name) {
  349. final nativeName = name.toNativeUtf8(allocator: _allocator);
  350. final animation = _bindings.spine_skeleton_data_find_animation(_data, nativeName.cast());
  351. _allocator.free(nativeName);
  352. if (animation.address == nullptr.address) return null;
  353. return Animation._(animation);
  354. }
  355. /// The skeleton's IK constraints.
  356. List<IkConstraintData> getIkConstraints() {
  357. final List<IkConstraintData> constraints = [];
  358. final numConstraints = _bindings.spine_skeleton_data_get_num_ik_constraints(_data);
  359. final nativeConstraints = _bindings.spine_skeleton_data_get_ik_constraints(_data);
  360. for (int i = 0; i < numConstraints; i++) {
  361. constraints.add(IkConstraintData._(nativeConstraints[i]));
  362. }
  363. return constraints;
  364. }
  365. /// Finds an IK constraint by comparing each IK constraint's name. It is more efficient to cache the results of this method
  366. /// than to call it multiple times.
  367. IkConstraintData? findIkConstraint(String name) {
  368. final nativeName = name.toNativeUtf8(allocator: _allocator);
  369. final constraint = _bindings.spine_skeleton_data_find_ik_constraint(_data, nativeName.cast());
  370. _allocator.free(nativeName);
  371. if (constraint.address == nullptr.address) return null;
  372. return IkConstraintData._(constraint);
  373. }
  374. /// The skeleton's transform constraints.
  375. List<TransformConstraint> getTransformConstraints() {
  376. final List<TransformConstraint> constraints = [];
  377. final numConstraints = _bindings.spine_skeleton_data_get_num_transform_constraints(_data);
  378. final nativeConstraints = _bindings.spine_skeleton_data_get_transform_constraints(_data);
  379. for (int i = 0; i < numConstraints; i++) {
  380. constraints.add(TransformConstraint._(nativeConstraints[i].cast()));
  381. }
  382. return constraints;
  383. }
  384. /// Finds a transform constraint by comparing each transform constraint's name. It is more efficient to cache the results of
  385. /// this method than to call it multiple times.
  386. TransformConstraintData? findTransformConstraint(String name) {
  387. final nativeName = name.toNativeUtf8(allocator: _allocator);
  388. final constraint = _bindings.spine_skeleton_data_find_transform_constraint(_data, nativeName.cast());
  389. _allocator.free(nativeName);
  390. if (constraint.address == nullptr.address) return null;
  391. return TransformConstraintData._(constraint);
  392. }
  393. /// The skeleton's path constraints.
  394. List<PathConstraintData> getPathConstraints() {
  395. final List<PathConstraintData> constraints = [];
  396. final numConstraints = _bindings.spine_skeleton_data_get_num_path_constraints(_data);
  397. final nativeConstraints = _bindings.spine_skeleton_data_get_path_constraints(_data);
  398. for (int i = 0; i < numConstraints; i++) {
  399. constraints.add(PathConstraintData._(nativeConstraints[i]));
  400. }
  401. return constraints;
  402. }
  403. /// Finds a path constraint by comparing each path constraint's name. It is more efficient to cache the results of this method
  404. /// than to call it multiple times.
  405. PathConstraintData? findPathConstraint(String name) {
  406. final nativeName = name.toNativeUtf8(allocator: _allocator);
  407. final constraint = _bindings.spine_skeleton_data_find_path_constraint(_data, nativeName.cast());
  408. _allocator.free(nativeName);
  409. if (constraint.address == nullptr.address) return null;
  410. return PathConstraintData._(constraint);
  411. }
  412. /// 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
  413. /// set.
  414. String? getName() {
  415. Pointer<Utf8> name = _bindings.spine_skeleton_data_get_name(_data).cast();
  416. if (name.address == nullptr.address) return null;
  417. return name.toDartString();
  418. }
  419. /// The X coordinate of the skeleton's axis aligned bounding box in the setup pose.
  420. double getX() {
  421. return _bindings.spine_skeleton_data_get_x(_data);
  422. }
  423. void setX(double x) {
  424. _bindings.spine_skeleton_data_set_x(_data, x);
  425. }
  426. /// The Y coordinate of the skeleton's axis aligned bounding box in the setup pose.
  427. double getY() {
  428. return _bindings.spine_skeleton_data_get_y(_data);
  429. }
  430. void setY(double y) {
  431. _bindings.spine_skeleton_data_set_x(_data, y);
  432. }
  433. /// The width of the skeleton's axis aligned bounding box in the setup pose.
  434. double getWidth() {
  435. return _bindings.spine_skeleton_data_get_width(_data);
  436. }
  437. void setWidth(double width) {
  438. _bindings.spine_skeleton_data_set_width(_data, width);
  439. }
  440. /// The height of the skeleton's axis aligned bounding box in the setup pose.
  441. double getHeight() {
  442. return _bindings.spine_skeleton_data_get_height(_data);
  443. }
  444. void setHeight(double height) {
  445. _bindings.spine_skeleton_data_set_height(_data, height);
  446. }
  447. /// The Spine version used to export the skeleton data.
  448. String? getVersion() {
  449. Pointer<Utf8> name = _bindings.spine_skeleton_data_get_version(_data).cast();
  450. if (name.address == nullptr.address) return null;
  451. return name.toDartString();
  452. }
  453. /// The skeleton data hash. This value will change if any of the skeleton data has changed.
  454. String? getHash() {
  455. Pointer<Utf8> name = _bindings.spine_skeleton_data_get_hash(_data).cast();
  456. if (name.address == nullptr.address) return null;
  457. return name.toDartString();
  458. }
  459. /// The path to the images directory as defined in Spine, or null if nonessential data was not exported.
  460. String? getImagesPath() {
  461. Pointer<Utf8> name = _bindings.spine_skeleton_data_get_images_path(_data).cast();
  462. if (name.address == nullptr.address) return null;
  463. return name.toDartString();
  464. }
  465. /// The path to the audio directory as defined in Spine, or null if nonessential data was not exported.
  466. String? getAudioPath() {
  467. Pointer<Utf8> name = _bindings.spine_skeleton_data_get_audio_path(_data).cast();
  468. if (name.address == nullptr.address) return null;
  469. return name.toDartString();
  470. }
  471. /// The dopesheet FPS in Spine, or zero if nonessential data was not exported.
  472. double getFps() {
  473. return _bindings.spine_skeleton_data_get_fps(_data);
  474. }
  475. /// Disposes the (native) resources of this skeleton data. The skeleton data can no longer be
  476. /// used after calling this function. Only the first call to this method will
  477. /// have an effect. Subsequent calls are ignored.
  478. void dispose() {
  479. if (_disposed) return;
  480. _disposed = true;
  481. _bindings.spine_skeleton_data_dispose(_data);
  482. }
  483. }
  484. /// Determines how images are blended with existing pixels when drawn. See [Blending](http://esotericsoftware.com/spine-slots#Blending) in
  485. /// the Spine User Guide.
  486. enum BlendMode {
  487. normal(0, rendering.BlendMode.srcOver),
  488. additive(1, rendering.BlendMode.plus),
  489. multiply(2, rendering.BlendMode.multiply),
  490. screen(3, rendering.BlendMode.screen);
  491. final int value;
  492. final rendering.BlendMode canvasBlendMode;
  493. const BlendMode(this.value, this.canvasBlendMode);
  494. }
  495. /// Determines how a bone inherits world transforms from parent bones. See [Transform inheritance](esotericsoftware.com/spine-bones#Transform-inheritance)
  496. /// in the Spine User Guide.
  497. enum Inherit {
  498. normal(0),
  499. onlyTranslation(1),
  500. noRotationOrReflection(2),
  501. noScale(3),
  502. noScaleOrReflection(4);
  503. final int value;
  504. const Inherit(this.value);
  505. }
  506. /// Determines how physics and other non-deterministic updates are applied.
  507. enum Physics {
  508. none(0),
  509. reset(1),
  510. update(2),
  511. pose(3);
  512. final int value;
  513. const Physics(this.value);
  514. }
  515. /// Controls how the first bone is positioned along the path.
  516. ///
  517. /// See [Position mode](http://esotericsoftware.com/spine-path-constraints#Position-mode) in the Spine User Guide.
  518. enum PositionMode {
  519. fixed(0),
  520. percent(1);
  521. final int value;
  522. const PositionMode(this.value);
  523. }
  524. /// Controls how bones after the first bone are positioned along the path.
  525. ///
  526. /// See [Spacing mode](http://esotericsoftware.com/spine-path-constraints#Spacing-mode) in the Spine User Guide.
  527. enum SpacingMode {
  528. length(0),
  529. fixed(1),
  530. percent(2),
  531. proportional(3);
  532. final int value;
  533. const SpacingMode(this.value);
  534. }
  535. /// Controls how bones are rotated, translated, and scaled to match the path.
  536. ///
  537. /// See [Rotate mode](http://esotericsoftware.com/spine-path-constraints#Rotate-mode) in the Spine User Guide.
  538. enum RotateMode {
  539. tangent(0),
  540. chain(1),
  541. chainScale(2);
  542. final int value;
  543. const RotateMode(this.value);
  544. }
  545. /// Stores the setup pose for a [Bone].
  546. class BoneData {
  547. final spine_bone_data _data;
  548. BoneData._(this._data);
  549. /// The index of the bone in [Skeleton.getBones].
  550. int getIndex() {
  551. return _bindings.spine_bone_data_get_index(_data);
  552. }
  553. /// The name of the bone, which is unique across all bones in the skeleton.
  554. String getName() {
  555. Pointer<Utf8> name = _bindings.spine_bone_data_get_name(_data).cast();
  556. return name.toDartString();
  557. }
  558. /// The parent bone or `null` if this is the root bone.
  559. BoneData? getParent() {
  560. final parent = _bindings.spine_bone_data_get_parent(_data);
  561. if (parent.address == nullptr.address) return null;
  562. return BoneData._(parent);
  563. }
  564. /// The bone's length.
  565. double getLength() {
  566. return _bindings.spine_bone_data_get_length(_data);
  567. }
  568. void setLength(double length) {
  569. _bindings.spine_bone_data_set_length(_data, length);
  570. }
  571. /// The local x translation.
  572. double getX() {
  573. return _bindings.spine_bone_data_get_x(_data);
  574. }
  575. void setX(double x) {
  576. _bindings.spine_bone_data_set_x(_data, x);
  577. }
  578. /// The local y translation.
  579. double getY() {
  580. return _bindings.spine_bone_data_get_y(_data);
  581. }
  582. void setY(double y) {
  583. _bindings.spine_bone_data_set_y(_data, y);
  584. }
  585. /// The local rotation in degrees.
  586. double getRotation() {
  587. return _bindings.spine_bone_data_get_rotation(_data);
  588. }
  589. void setRotation(double rotation) {
  590. _bindings.spine_bone_data_set_rotation(_data, rotation);
  591. }
  592. /// The local scaleX.
  593. double getScaleX() {
  594. return _bindings.spine_bone_data_get_scale_x(_data);
  595. }
  596. void setScaleX(double scaleX) {
  597. _bindings.spine_bone_data_set_scale_x(_data, scaleX);
  598. }
  599. /// The local scaleY.
  600. double getScaleY() {
  601. return _bindings.spine_bone_data_get_scale_y(_data);
  602. }
  603. void setScaleY(double scaleY) {
  604. _bindings.spine_bone_data_set_scale_y(_data, scaleY);
  605. }
  606. /// The local shearX.
  607. double getShearX() {
  608. return _bindings.spine_bone_data_get_shear_x(_data);
  609. }
  610. void setShearX(double shearX) {
  611. _bindings.spine_bone_data_set_shear_x(_data, shearX);
  612. }
  613. /// The local shearY.
  614. double getShearY() {
  615. return _bindings.spine_bone_data_get_shear_y(_data);
  616. }
  617. void setShearY(double shearY) {
  618. _bindings.spine_bone_data_set_shear_y(_data, shearY);
  619. }
  620. /// The [Inherit] for how parent world transforms affect this bone.
  621. Inherit getInherit() {
  622. final nativeMode = _bindings.spine_bone_data_get_inherit(_data);
  623. return Inherit.values[nativeMode];
  624. }
  625. void setInherit(Inherit inherit) {
  626. _bindings.spine_bone_data_set_inherit(_data, inherit.value);
  627. }
  628. /// When true, [Skeleton.updateWorldTransform] only updates this bone if the [Skeleton.getSkin] contains this bone.
  629. ///
  630. /// See [Skin.getBones].
  631. bool isSkinRequired() {
  632. return _bindings.spine_bone_data_is_skin_required(_data) == -1;
  633. }
  634. void setIsSkinRequired(bool isSkinRequired) {
  635. _bindings.spine_bone_data_set_is_skin_required(_data, isSkinRequired ? -1 : 0);
  636. }
  637. /// The [Color] of the bone as it was in Spine, or a default color if nonessential data was not exported. Bones are not usually
  638. /// rendered at runtime.
  639. Color getColor() {
  640. final color = _bindings.spine_bone_data_get_color(_data);
  641. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  642. _bindings.spine_color_get_a(color));
  643. }
  644. void setColor(double r, double g, double b, double a) {
  645. _bindings.spine_bone_data_set_color(_data, r, g, b, a);
  646. }
  647. @override
  648. String toString() {
  649. return getName();
  650. }
  651. }
  652. /// Stores a bone's current pose.
  653. ///
  654. /// A bone has a local transform which is used to compute its world transform. A bone also has an applied transform, which is a
  655. /// local transform that can be applied to compute the world transform. The local transform and applied transform may differ if a
  656. /// constraint or application code modifies the world transform after it was computed from the local transform.
  657. class Bone {
  658. final spine_bone _bone;
  659. Bone._(this._bone);
  660. /// Assume y-axis pointing down for all calculations.
  661. static void setIsYDown(bool isYDown) {
  662. _bindings.spine_bone_set_is_y_down(isYDown ? -1 : 0);
  663. }
  664. static bool getIsYDown() {
  665. return _bindings.spine_bone_get_is_y_down() == 1;
  666. }
  667. /// Computes the world transform using the parent bone and this bone's local applied transform.
  668. void update() {
  669. _bindings.spine_bone_update(_bone);
  670. }
  671. /// Computes the world transform using the parent bone and this bone's local transform.
  672. ///
  673. /// See [updateWorldTransformWith].
  674. void updateWorldTransform() {
  675. _bindings.spine_bone_update_world_transform(_bone);
  676. }
  677. /// Computes the world transform using the parent bone and the specified local transform. The applied transform is set to the
  678. /// specified local transform. Child bones are not updated.
  679. ///
  680. /// See [World transform](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms) in the Spine
  681. /// Runtimes Guide.
  682. void updateWorldTransformWith(double x, double y, double rotation, double scaleX, double scaleY, double shearX, double shearY) {
  683. _bindings.spine_bone_update_world_transform_with(_bone, x, y, rotation, scaleX, scaleY, shearX, shearY);
  684. }
  685. /// Computes the applied transform values from the world transform.
  686. ///
  687. /// If the world transform is modified (by a constraint, [rotateWorld], etc) then this method should be called so
  688. /// the applied transform matches the world transform. The applied transform may be needed by other code (eg to apply another
  689. /// constraint).
  690. ///
  691. /// Some information is ambiguous in the world transform, such as -1,-1 scale versus 180 rotation. The applied transform after
  692. /// calling this method is equivalent to the local transform used to compute the world transform, but may not be identical.
  693. void updateAppliedTransform() {
  694. _bindings.spine_bone_update_applied_transform(_bone);
  695. }
  696. /// Sets this bone's local transform to the setup pose.
  697. void setToSetupPose() {
  698. _bindings.spine_bone_set_to_setup_pose(_bone);
  699. }
  700. /// Transforms a point from world coordinates to the bone's local coordinates.
  701. Vec2 worldToLocal(double worldX, double worldY) {
  702. final local = _bindings.spine_bone_world_to_local(_bone, worldX, worldY);
  703. final result = Vec2(_bindings.spine_vector_get_x(local), _bindings.spine_vector_get_y(local));
  704. return result;
  705. }
  706. /// Transforms a point from the bone's local coordinates to world coordinates.
  707. Vec2 localToWorld(double localX, double localY) {
  708. final world = _bindings.spine_bone_local_to_world(_bone, localX, localY);
  709. final result = Vec2(_bindings.spine_vector_get_x(world), _bindings.spine_vector_get_y(world));
  710. return result;
  711. }
  712. /// Transforms a world rotation to a local rotation.
  713. double worldToLocalRotation(double worldRotation) {
  714. return _bindings.spine_bone_world_to_local_rotation(_bone, worldRotation);
  715. }
  716. /// Transforms a local rotation to a world rotation.
  717. double localToWorldRotation(double localRotation) {
  718. return _bindings.spine_bone_local_to_world_rotation(_bone, localRotation);
  719. }
  720. /// Rotates the world transform the specified amount.
  721. ///
  722. /// After changes are made to the world transform, [updateAppliedTransform] should be called and [update] will
  723. /// need to be called on any child bones, recursively.
  724. void rotateWorld(double degrees) {
  725. _bindings.spine_bone_rotate_world(_bone, degrees);
  726. }
  727. double getWorldToLocalRotationX() {
  728. return _bindings.spine_bone_get_world_rotation_x(_bone);
  729. }
  730. double getWorldToLocalRotationY() {
  731. return _bindings.spine_bone_get_world_to_local_rotation_y(_bone);
  732. }
  733. /// The bone's setup pose data.
  734. BoneData getData() {
  735. return BoneData._(_bindings.spine_bone_get_data(_bone));
  736. }
  737. /// The skeleton this bone belongs to.
  738. Skeleton getSkeleton() {
  739. return Skeleton._(_bindings.spine_bone_get_skeleton(_bone));
  740. }
  741. /// The parent bone, or null if this is the root bone.
  742. Bone? getParent() {
  743. final parent = _bindings.spine_bone_get_parent(_bone);
  744. if (parent.address == nullptr.address) return null;
  745. return Bone._(parent);
  746. }
  747. /// The immediate children of this bone.
  748. List<Bone> getChildren() {
  749. List<Bone> children = [];
  750. final numChildren = _bindings.spine_bone_get_num_children(_bone);
  751. final nativeChildren = _bindings.spine_bone_get_children(_bone);
  752. for (int i = 0; i < numChildren; i++) {
  753. children.add(Bone._(nativeChildren[i]));
  754. }
  755. return children;
  756. }
  757. /// The local x translation.
  758. double getX() {
  759. return _bindings.spine_bone_get_x(_bone);
  760. }
  761. void setX(double x) {
  762. _bindings.spine_bone_set_x(_bone, x);
  763. }
  764. /// The local y translation.
  765. double getY() {
  766. return _bindings.spine_bone_get_y(_bone);
  767. }
  768. void setY(double y) {
  769. _bindings.spine_bone_set_y(_bone, y);
  770. }
  771. /// The local rotation in degrees, counter clockwise.
  772. double getRotation() {
  773. return _bindings.spine_bone_get_rotation(_bone);
  774. }
  775. void setRotation(double rotation) {
  776. _bindings.spine_bone_set_rotation(_bone, rotation);
  777. }
  778. /// The local scaleX.
  779. double getScaleX() {
  780. return _bindings.spine_bone_get_scale_x(_bone);
  781. }
  782. void setScaleX(double scaleX) {
  783. _bindings.spine_bone_set_scale_x(_bone, scaleX);
  784. }
  785. /// The local scaleY.
  786. double getScaleY() {
  787. return _bindings.spine_bone_get_scale_y(_bone);
  788. }
  789. void setScaleY(double scaleY) {
  790. _bindings.spine_bone_set_scale_y(_bone, scaleY);
  791. }
  792. /// The local shearX.
  793. double getShearX() {
  794. return _bindings.spine_bone_get_shear_x(_bone);
  795. }
  796. void setShearX(double shearX) {
  797. _bindings.spine_bone_set_shear_x(_bone, shearX);
  798. }
  799. /// The local shearY.
  800. double getShearY() {
  801. return _bindings.spine_bone_get_shear_y(_bone);
  802. }
  803. void setShearY(double shearY) {
  804. _bindings.spine_bone_set_shear_y(_bone, shearY);
  805. }
  806. /// The applied local x translation.
  807. double getAX() {
  808. return _bindings.spine_bone_get_a_x(_bone);
  809. }
  810. void setAX(double x) {
  811. _bindings.spine_bone_set_a_x(_bone, x);
  812. }
  813. /// The applied local y translation.
  814. double getAY() {
  815. return _bindings.spine_bone_get_a_y(_bone);
  816. }
  817. void setAY(double y) {
  818. _bindings.spine_bone_set_a_y(_bone, y);
  819. }
  820. /// The applied local rotation in degrees, counter clockwise.
  821. double getAppliedRotation() {
  822. return _bindings.spine_bone_get_applied_rotation(_bone);
  823. }
  824. void setAppliedRotation(double rotation) {
  825. _bindings.spine_bone_set_applied_rotation(_bone, rotation);
  826. }
  827. /// The applied local scaleX.
  828. double getAScaleX() {
  829. return _bindings.spine_bone_get_a_scale_x(_bone);
  830. }
  831. void setAScaleX(double scaleX) {
  832. _bindings.spine_bone_set_a_scale_x(_bone, scaleX);
  833. }
  834. /// The applied local scaleY.
  835. double getAScaleY() {
  836. return _bindings.spine_bone_get_a_scale_y(_bone);
  837. }
  838. void setAScaleY(double scaleY) {
  839. _bindings.spine_bone_set_a_scale_y(_bone, scaleY);
  840. }
  841. /// The applied local shearX.
  842. double getAShearX() {
  843. return _bindings.spine_bone_get_a_shear_x(_bone);
  844. }
  845. void setAShearX(double shearX) {
  846. _bindings.spine_bone_set_a_shear_x(_bone, shearX);
  847. }
  848. /// The applied local shearY.
  849. double getAShearY() {
  850. return _bindings.spine_bone_get_a_shear_y(_bone);
  851. }
  852. void setAShearY(double shearY) {
  853. _bindings.spine_bone_set_a_shear_y(_bone, shearY);
  854. }
  855. /// Part of the world transform matrix for the X axis. If changed, [updateAppliedTransform] should be called.
  856. double getA() {
  857. return _bindings.spine_bone_get_a(_bone);
  858. }
  859. void setA(double a) {
  860. _bindings.spine_bone_set_a(_bone, a);
  861. }
  862. /// Part of the world transform matrix for the Y axis. If changed, [updateAppliedTransform] should be called.
  863. double getB() {
  864. return _bindings.spine_bone_get_b(_bone);
  865. }
  866. void setB(double b) {
  867. _bindings.spine_bone_set_b(_bone, b);
  868. }
  869. /// Part of the world transform matrix for the X axis. If changed, [updateAppliedTransform] should be called.
  870. double getC() {
  871. return _bindings.spine_bone_get_c(_bone);
  872. }
  873. void setC(double c) {
  874. _bindings.spine_bone_set_c(_bone, c);
  875. }
  876. /// Part of the world transform matrix for the Y axis. If changed, [updateAppliedTransform] should be called.
  877. double getD() {
  878. return _bindings.spine_bone_get_d(_bone);
  879. }
  880. void setD(double d) {
  881. _bindings.spine_bone_set_a(_bone, d);
  882. }
  883. /// The world X position. If changed, [updateAppliedTransform] should be called.
  884. double getWorldX() {
  885. return _bindings.spine_bone_get_world_x(_bone);
  886. }
  887. void setWorldX(double worldX) {
  888. _bindings.spine_bone_set_world_x(_bone, worldX);
  889. }
  890. /// The world Y position. If changed, [updateAppliedTransform] should be called.
  891. double getWorldY() {
  892. return _bindings.spine_bone_get_world_y(_bone);
  893. }
  894. void setWorldY(double worldY) {
  895. _bindings.spine_bone_set_world_y(_bone, worldY);
  896. }
  897. /// The world rotation for the X axis, calculated using [getA] and [getC].
  898. double getWorldRotationX() {
  899. return _bindings.spine_bone_get_world_rotation_x(_bone);
  900. }
  901. /// The world rotation for the Y axis, calculated using [getB] and [getD].
  902. double getWorldRotationY() {
  903. return _bindings.spine_bone_get_world_rotation_y(_bone);
  904. }
  905. /// The magnitude (always positive) of the world scale X, calculated using [getA] and [getC].
  906. double getWorldScaleX() {
  907. return _bindings.spine_bone_get_world_scale_x(_bone);
  908. }
  909. /// The magnitude (always positive) of the world scale Y, calculated using [getB] and [getD].
  910. double getWorldScaleY() {
  911. return _bindings.spine_bone_get_world_scale_y(_bone);
  912. }
  913. /// Returns false when the bone has not been computed because [BoneData.getSkinRequired] is true and the
  914. /// active skin (see [Skeleton.getSkin]) does not contain this bone (see [Skin.getBones]).
  915. bool isActive() {
  916. return _bindings.spine_bone_get_is_active(_bone) == -1;
  917. }
  918. void setIsActive(bool isActive) {
  919. _bindings.spine_bone_set_is_active(_bone, isActive ? -1 : 0);
  920. }
  921. }
  922. /// Stores the setup pose for a [Slot].
  923. class SlotData {
  924. final spine_slot_data _data;
  925. SlotData._(this._data);
  926. /// The index of the slot in [Skeleton.getSlots].
  927. int getIndex() {
  928. return _bindings.spine_slot_data_get_index(_data);
  929. }
  930. /// The name of the slot, which is unique across all slots in the skeleton.
  931. String getName() {
  932. final Pointer<Utf8> value = _bindings.spine_slot_data_get_name(_data).cast();
  933. return value.toDartString();
  934. }
  935. /// The bone this slot belongs to.
  936. BoneData getBoneData() {
  937. return BoneData._(_bindings.spine_slot_data_get_bone_data(_data));
  938. }
  939. /// The [Color] used to tint the slot's attachment. If [hasDarkColor] is true, this is used as the light color for two
  940. /// color tinting.
  941. Color getColor() {
  942. final color = _bindings.spine_slot_data_get_color(_data);
  943. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  944. _bindings.spine_color_get_a(color));
  945. }
  946. void setColor(double r, double g, double b, double a) {
  947. _bindings.spine_slot_data_set_color(_data, r, g, b, a);
  948. }
  949. /// The dark color used to tint the slot's attachment for two color tinting, if [hasDarkColor] is true. The dark
  950. /// color's alpha is not used.
  951. Color getDarkColor() {
  952. final color = _bindings.spine_slot_data_get_dark_color(_data);
  953. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  954. _bindings.spine_color_get_a(color));
  955. }
  956. void setDarkColor(double r, double g, double b, double a) {
  957. _bindings.spine_slot_data_set_dark_color(_data, r, g, b, a);
  958. }
  959. /// Returns whether this slot has a dark color set for two color tinting.
  960. bool hasDarkColor() {
  961. return _bindings.spine_slot_data_has_dark_color(_data) == -1;
  962. }
  963. void setHasDarkColor(bool hasDarkColor) {
  964. _bindings.spine_slot_data_set_has_dark_color(_data, hasDarkColor ? -1 : 0);
  965. }
  966. /// The name of the attachment that is visible for this slot in the setup pose, or null if no attachment is visible.
  967. String getAttachmentName() {
  968. final Pointer<Utf8> value = _bindings.spine_slot_data_get_attachment_name(_data).cast();
  969. return value.toDartString();
  970. }
  971. void setAttachmentName(String attachmentName) {
  972. final nativeName = attachmentName.toNativeUtf8(allocator: _allocator);
  973. _bindings.spine_slot_data_set_attachment_name(_data, nativeName.cast());
  974. _allocator.free(nativeName);
  975. }
  976. /// The [BlendMode] for drawing the slot's attachment.
  977. BlendMode getBlendMode() {
  978. return BlendMode.values[_bindings.spine_slot_data_get_blend_mode(_data)];
  979. }
  980. void setBlendMode(BlendMode mode) {
  981. _bindings.spine_slot_data_set_blend_mode(_data, mode.value);
  982. }
  983. @override
  984. String toString() {
  985. return getName();
  986. }
  987. }
  988. /// Stores a slot's current pose. Slots organize attachments for [Skeleton.getDrawOrder] purposes and provide a place to store
  989. /// state for an attachment. State cannot be stored in an attachment itself because attachments are stateless and may be shared
  990. /// across multiple skeletons.
  991. class Slot {
  992. final spine_slot _slot;
  993. Slot._(this._slot);
  994. /// Sets this slot to the setup pose.
  995. void setToSetupPose() {
  996. _bindings.spine_slot_set_to_setup_pose(_slot);
  997. }
  998. /// The slot's setup pose data.
  999. SlotData getData() {
  1000. return SlotData._(_bindings.spine_slot_get_data(_slot));
  1001. }
  1002. /// The bone this slot belongs to.
  1003. Bone getBone() {
  1004. return Bone._(_bindings.spine_slot_get_bone(_slot));
  1005. }
  1006. /// The skeleton this slot belongs to.
  1007. Skeleton getSkeleton() {
  1008. return Skeleton._(_bindings.spine_slot_get_skeleton(_slot));
  1009. }
  1010. /// The color used to tint the slot's attachment. If [hasDarkColor] is true, this is used as the light color for two
  1011. /// color tinting.
  1012. Color getColor() {
  1013. final color = _bindings.spine_slot_get_color(_slot);
  1014. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1015. _bindings.spine_color_get_a(color));
  1016. }
  1017. void setColor(Color color) {
  1018. _bindings.spine_slot_set_color(_slot, color.r, color.g, color.b, color.a);
  1019. }
  1020. /// The dark color used to tint the slot's attachment for two color tinting, if [hasDarkColor] is true. The dark
  1021. /// color's alpha is not used.
  1022. Color getDarkColor() {
  1023. final color = _bindings.spine_slot_get_dark_color(_slot);
  1024. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1025. _bindings.spine_color_get_a(color));
  1026. }
  1027. void setDarkColor(Color color) {
  1028. _bindings.spine_slot_set_dark_color(_slot, color.r, color.g, color.b, color.a);
  1029. }
  1030. /// Returns whether this slot has a dark color set for two color tinting.
  1031. bool hasDarkColor() {
  1032. return _bindings.spine_slot_has_dark_color(_slot) == -1;
  1033. }
  1034. /// The current attachment for the slot, or null if the slot has no attachment.
  1035. Attachment? getAttachment() {
  1036. final attachment = _bindings.spine_slot_get_attachment(_slot);
  1037. if (attachment.address == nullptr.address) return null;
  1038. return Attachment._toSubclass(attachment);
  1039. }
  1040. void setAttachment(Attachment? attachment) {
  1041. _bindings.spine_slot_set_attachment(_slot, attachment != null ? attachment._attachment.cast() : nullptr);
  1042. }
  1043. @override
  1044. String toString() {
  1045. return getData().getName();
  1046. }
  1047. /// The index of the texture region to display when the slot's attachment has a [Sequence]. -1 represents the
  1048. /// [Sequence.getSetupIndex].
  1049. int getSequenceIndex() {
  1050. return _bindings.spine_slot_get_sequence_index(_slot);
  1051. }
  1052. void setSequenceIndex(int sequenceIndex) {
  1053. _bindings.spine_slot_set_sequence_index(_slot, sequenceIndex);
  1054. }
  1055. }
  1056. /// A region within a texture, given in normalized texture coordinates of the top left ([getU], [getV]) and
  1057. /// bottom left ([getU2], [getV2]) corner of the region within the texture.
  1058. class TextureRegion {
  1059. final spine_texture_region _region;
  1060. TextureRegion._(this._region);
  1061. Pointer<Void> getTexture() {
  1062. return _bindings.spine_texture_region_get_texture(_region);
  1063. }
  1064. void setTexture(Pointer<Void> texture) {
  1065. _bindings.spine_texture_region_set_texture(_region, texture);
  1066. }
  1067. double getU() {
  1068. return _bindings.spine_texture_region_get_u(_region);
  1069. }
  1070. void setU(double u) {
  1071. _bindings.spine_texture_region_set_u(_region, u);
  1072. }
  1073. double getV() {
  1074. return _bindings.spine_texture_region_get_v(_region);
  1075. }
  1076. void setV(double v) {
  1077. _bindings.spine_texture_region_set_v(_region, v);
  1078. }
  1079. double getU2() {
  1080. return _bindings.spine_texture_region_get_u2(_region);
  1081. }
  1082. void setU2(double u2) {
  1083. _bindings.spine_texture_region_set_u2(_region, u2);
  1084. }
  1085. double getV2() {
  1086. return _bindings.spine_texture_region_get_v2(_region);
  1087. }
  1088. void setV2(double v2) {
  1089. _bindings.spine_texture_region_set_v2(_region, v2);
  1090. }
  1091. int getDegrees() {
  1092. return _bindings.spine_texture_region_get_degrees(_region);
  1093. }
  1094. void setDegrees(int degrees) {
  1095. _bindings.spine_texture_region_set_degrees(_region, degrees);
  1096. }
  1097. double getOffsetX() {
  1098. return _bindings.spine_texture_region_get_offset_x(_region);
  1099. }
  1100. void setOffsetX(double offsetX) {
  1101. _bindings.spine_texture_region_set_offset_x(_region, offsetX);
  1102. }
  1103. double getOffsetY() {
  1104. return _bindings.spine_texture_region_get_offset_x(_region);
  1105. }
  1106. void setOffsetY(double offsetX) {
  1107. _bindings.spine_texture_region_set_offset_x(_region, offsetX);
  1108. }
  1109. int getWidth() {
  1110. return _bindings.spine_texture_region_get_width(_region);
  1111. }
  1112. void setWidth(int width) {
  1113. _bindings.spine_texture_region_set_width(_region, width);
  1114. }
  1115. int getHeight() {
  1116. return _bindings.spine_texture_region_get_height(_region);
  1117. }
  1118. void setHeight(int height) {
  1119. _bindings.spine_texture_region_set_height(_region, height);
  1120. }
  1121. int getOriginalWidth() {
  1122. return _bindings.spine_texture_region_get_original_width(_region);
  1123. }
  1124. void setOriginalWidth(int originalWidth) {
  1125. _bindings.spine_texture_region_set_original_width(_region, originalWidth);
  1126. }
  1127. int getOriginalHeight() {
  1128. return _bindings.spine_texture_region_get_original_height(_region);
  1129. }
  1130. void setOriginalHeight(int originalHeight) {
  1131. _bindings.spine_texture_region_set_original_height(_region, originalHeight);
  1132. }
  1133. }
  1134. /// Stores a sequence of [TextureRegion] instances that will switched through when set on an attachment.
  1135. class Sequence {
  1136. final spine_sequence _sequence;
  1137. Sequence._(this._sequence);
  1138. void apply(Slot slot, Attachment attachment) {
  1139. _bindings.spine_sequence_apply(_sequence, slot._slot, attachment._attachment.cast());
  1140. }
  1141. String getPath(String basePath, int index) {
  1142. final nativeBasePath = basePath.toNativeUtf8(allocator: _allocator);
  1143. final Pointer<Utf8> path = _bindings.spine_sequence_get_path(_sequence, nativeBasePath.cast(), index).cast();
  1144. final result = path.toDartString();
  1145. _allocator.free(nativeBasePath);
  1146. _allocator.free(path);
  1147. return result;
  1148. }
  1149. int getId() {
  1150. return _bindings.spine_sequence_get_id(_sequence);
  1151. }
  1152. void setId(int id) {
  1153. _bindings.spine_sequence_set_id(_sequence, id);
  1154. }
  1155. int getStart() {
  1156. return _bindings.spine_sequence_get_start(_sequence);
  1157. }
  1158. void setStart(int start) {
  1159. _bindings.spine_sequence_set_start(_sequence, start);
  1160. }
  1161. int getDigits() {
  1162. return _bindings.spine_sequence_get_digits(_sequence);
  1163. }
  1164. void setDigits(int digits) {
  1165. _bindings.spine_sequence_set_digits(_sequence, digits);
  1166. }
  1167. int getSetupIndex() {
  1168. return _bindings.spine_sequence_get_setup_index(_sequence);
  1169. }
  1170. void setSetupIndex(int setupIndex) {
  1171. _bindings.spine_sequence_set_setup_index(_sequence, setupIndex);
  1172. }
  1173. List<TextureRegion> getRegions() {
  1174. List<TextureRegion> result = [];
  1175. final num = _bindings.spine_sequence_get_num_regions(_sequence);
  1176. final nativeRegions = _bindings.spine_sequence_get_regions(_sequence);
  1177. for (int i = 0; i < num; i++) {
  1178. result.add(TextureRegion._(nativeRegions[i]));
  1179. }
  1180. return result;
  1181. }
  1182. }
  1183. /// Attachment types.
  1184. enum AttachmentType {
  1185. region(0),
  1186. mesh(1),
  1187. clipping(2),
  1188. boundingBox(3),
  1189. path(4),
  1190. point(5);
  1191. final int value;
  1192. const AttachmentType(this.value);
  1193. }
  1194. /// The base class for all attachments.
  1195. abstract class Attachment<T extends Pointer> {
  1196. final T _attachment;
  1197. Attachment._(this._attachment);
  1198. /// The attachment's name.
  1199. String getName() {
  1200. Pointer<Utf8> name = _bindings.spine_attachment_get_name(_attachment.cast()).cast();
  1201. return name.toString();
  1202. }
  1203. /// The attachment's type.
  1204. AttachmentType getType() {
  1205. final type = _bindings.spine_attachment_get_type(_attachment.cast());
  1206. return AttachmentType.values[type];
  1207. }
  1208. static Attachment _toSubclass(spine_attachment attachment) {
  1209. final type = AttachmentType.values[_bindings.spine_attachment_get_type(attachment)];
  1210. switch (type) {
  1211. case AttachmentType.region:
  1212. return RegionAttachment._(attachment.cast());
  1213. case AttachmentType.mesh:
  1214. return MeshAttachment._(attachment.cast());
  1215. case AttachmentType.clipping:
  1216. return ClippingAttachment._(attachment.cast());
  1217. case AttachmentType.boundingBox:
  1218. return BoundingBoxAttachment._(attachment.cast());
  1219. case AttachmentType.path:
  1220. return PathAttachment._(attachment.cast());
  1221. case AttachmentType.point:
  1222. return PointAttachment._(attachment.cast());
  1223. }
  1224. }
  1225. /// Returns a copy of the attachment. Copied attachments need to be disposed manually
  1226. /// when no longer in use via the [dispose] method.
  1227. Attachment copy() {
  1228. return _toSubclass(_bindings.spine_attachment_copy(_attachment.cast()));
  1229. }
  1230. void dispose() {
  1231. _bindings.spine_attachment_dispose(_attachment.cast());
  1232. }
  1233. }
  1234. /// An attachment that displays a textured quadrilateral.
  1235. ///
  1236. /// See [Region attachments](http://esotericsoftware.com/spine-regions) in the Spine User Guide.
  1237. class RegionAttachment extends Attachment<spine_region_attachment> {
  1238. RegionAttachment._(super.attachment) : super._();
  1239. /// Transforms and returns the attachment's four vertices to world coordinates. If the attachment has a [Sequence], the region may
  1240. /// be changed.
  1241. ///
  1242. /// See [World transforms](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms) in the Spine
  1243. /// Runtimes Guide.
  1244. List<double> computeWorldVertices(Slot slot) {
  1245. Pointer<Float> vertices = _allocator.allocate(4 * 8).cast();
  1246. _bindings.spine_region_attachment_compute_world_vertices(_attachment, slot._slot, vertices);
  1247. final result = vertices.asTypedList(8).toList();
  1248. _allocator.free(vertices);
  1249. return result;
  1250. }
  1251. /// The local x translation.
  1252. double getX() {
  1253. return _bindings.spine_region_attachment_get_x(_attachment);
  1254. }
  1255. void setX(double x) {
  1256. _bindings.spine_region_attachment_set_x(_attachment, x);
  1257. }
  1258. /// The local y translation.
  1259. double getY() {
  1260. return _bindings.spine_region_attachment_get_y(_attachment);
  1261. }
  1262. void setY(double y) {
  1263. _bindings.spine_region_attachment_set_y(_attachment, y);
  1264. }
  1265. /// The local rotation.
  1266. double getRotation() {
  1267. return _bindings.spine_region_attachment_get_rotation(_attachment);
  1268. }
  1269. void setRotation(double rotation) {
  1270. _bindings.spine_region_attachment_set_rotation(_attachment, rotation);
  1271. }
  1272. /// The local scaleX.
  1273. double getScaleX() {
  1274. return _bindings.spine_region_attachment_get_scale_x(_attachment);
  1275. }
  1276. void setScaleX(double scaleX) {
  1277. _bindings.spine_region_attachment_set_scale_x(_attachment, scaleX);
  1278. }
  1279. /// The local scaleY.
  1280. double getScaleY() {
  1281. return _bindings.spine_region_attachment_get_scale_y(_attachment);
  1282. }
  1283. void setScaleY(double scaleY) {
  1284. _bindings.spine_region_attachment_set_scale_x(_attachment, scaleY);
  1285. }
  1286. /// The width of the region attachment in Spine.
  1287. double getWidth() {
  1288. return _bindings.spine_region_attachment_get_width(_attachment);
  1289. }
  1290. void setWidth(double width) {
  1291. _bindings.spine_region_attachment_set_width(_attachment, width);
  1292. }
  1293. /// The height of the region attachment in Spine.
  1294. double getHeight() {
  1295. return _bindings.spine_region_attachment_get_height(_attachment);
  1296. }
  1297. void setHeight(double height) {
  1298. _bindings.spine_region_attachment_set_height(_attachment, height);
  1299. }
  1300. Color getColor() {
  1301. final color = _bindings.spine_region_attachment_get_color(_attachment);
  1302. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1303. _bindings.spine_color_get_a(color));
  1304. }
  1305. void setColor(double r, double g, double b, double a) {
  1306. _bindings.spine_region_attachment_set_color(_attachment, r, g, b, a);
  1307. }
  1308. String getPath() {
  1309. Pointer<Utf8> path = _bindings.spine_region_attachment_get_path(_attachment).cast();
  1310. return path.toDartString();
  1311. }
  1312. TextureRegion? getRegion() {
  1313. final region = _bindings.spine_region_attachment_get_region(_attachment);
  1314. if (region.address == nullptr.address) return null;
  1315. return TextureRegion._(region);
  1316. }
  1317. Sequence? getSequence() {
  1318. final sequence = _bindings.spine_region_attachment_get_sequence(_attachment);
  1319. if (sequence.address == nullptr.address) return null;
  1320. return Sequence._(sequence);
  1321. }
  1322. /// For each of the 4 vertices, a pair of `x,y` values that is the local position of the vertex.
  1323. ///
  1324. /// See [updateRegion].
  1325. Float32List getOffset() {
  1326. final num = _bindings.spine_region_attachment_get_num_offset(_attachment);
  1327. final offset = _bindings.spine_region_attachment_get_offset(_attachment);
  1328. return offset.asTypedList(num);
  1329. }
  1330. Float32List getUVs() {
  1331. final num = _bindings.spine_region_attachment_get_num_uvs(_attachment);
  1332. final uvs = _bindings.spine_region_attachment_get_uvs(_attachment);
  1333. return uvs.asTypedList(num);
  1334. }
  1335. }
  1336. /// Base class for an attachment with vertices that are transformed by one or more bones and can be deformed by a slot's
  1337. /// [Slot.getDeform].
  1338. class VertexAttachment<T extends Pointer> extends Attachment<T> {
  1339. VertexAttachment._(super.attachment) : super._();
  1340. /// Transforms and returns the attachment's local [getVertices] to world coordinates. If the slot's [Slot.getDeform] is
  1341. /// not empty, it is used to deform the vertices.
  1342. /// See [World transforms](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms) in the Spine
  1343. /// Runtimes Guide.
  1344. List<double> computeWorldVertices(Slot slot) {
  1345. final worldVerticesLength = _bindings.spine_vertex_attachment_get_world_vertices_length(_attachment.cast());
  1346. Pointer<Float> vertices = _allocator.allocate(4 * worldVerticesLength).cast();
  1347. _bindings.spine_vertex_attachment_compute_world_vertices(_attachment.cast(), slot._slot, vertices);
  1348. final result = vertices.asTypedList(worldVerticesLength).toList();
  1349. _allocator.free(vertices);
  1350. return result;
  1351. }
  1352. /// The bones which affect the [getVertices]. The array entries are, for each vertex, the number of bones affecting
  1353. /// the vertex followed by that many bone indices, which is the index of the bone in [Skeleton.getBones]. Will be null
  1354. /// if this attachment has no weights.
  1355. Int32List getBones() {
  1356. final num = _bindings.spine_vertex_attachment_get_num_bones(_attachment.cast());
  1357. final bones = _bindings.spine_vertex_attachment_get_bones(_attachment.cast());
  1358. return bones.asTypedList(num);
  1359. }
  1360. /// The vertex positions in the bone's coordinate system. For a non-weighted attachment, the values are `x,y`
  1361. /// entries for each vertex. For a weighted attachment, the values are `x,y,weight` entries for each bone affecting
  1362. /// each vertex.
  1363. Float32List getVertices() {
  1364. final num = _bindings.spine_vertex_attachment_get_num_vertices(_attachment.cast());
  1365. final vertices = _bindings.spine_vertex_attachment_get_vertices(_attachment.cast());
  1366. return vertices.asTypedList(num);
  1367. }
  1368. /// Timelines for the timeline attachment are also applied to this attachment. May return `null` if not
  1369. /// attachment-specific timelines should be applied.
  1370. Attachment? getTimelineAttachment() {
  1371. final attachment = _bindings.spine_vertex_attachment_get_timeline_attachment(_attachment.cast());
  1372. if (_attachment.address == nullptr.address) return null;
  1373. return Attachment._toSubclass(attachment);
  1374. }
  1375. void setTimelineAttachment(Attachment? attachment) {
  1376. _bindings.spine_vertex_attachment_set_timeline_attachment(
  1377. _attachment.cast(), attachment == null ? nullptr : attachment._attachment.cast());
  1378. }
  1379. }
  1380. /// An attachment that displays a textured mesh. A mesh has hull vertices and internal vertices within the hull. Holes are not
  1381. /// supported. Each vertex has UVs (texture coordinates) and triangles are used to map an image on to the mesh.
  1382. ///
  1383. /// See [Mesh attachments](http://esotericsoftware.com/spine-meshes) in the Spine User Guide.
  1384. class MeshAttachment extends VertexAttachment<spine_mesh_attachment> {
  1385. MeshAttachment._(spine_mesh_attachment attachment) : super._(attachment.cast());
  1386. /// Calculates texture coordinates returned by [getUVs] using the coordinates returned by [getRegionUVs] and region. Must be called if
  1387. /// the region, the region's properties, or the [getRegionUVs] are changed.
  1388. void updateRegion() {
  1389. _bindings.spine_mesh_attachment_update_region(_attachment);
  1390. }
  1391. /// The number of entries at the beginning of {@link #vertices} that make up the mesh hull.
  1392. int getHullLength() {
  1393. return _bindings.spine_mesh_attachment_get_hull_length(_attachment);
  1394. }
  1395. void setHullLength(int hullLength) {
  1396. _bindings.spine_mesh_attachment_set_hull_length(_attachment, hullLength);
  1397. }
  1398. /// The UV pair for each vertex, normalized within the texture region.
  1399. Float32List getRegionUVs() {
  1400. final num = _bindings.spine_mesh_attachment_get_num_region_uvs(_attachment);
  1401. final uvs = _bindings.spine_mesh_attachment_get_region_uvs(_attachment);
  1402. return uvs.asTypedList(num);
  1403. }
  1404. /// The UV pair for each vertex, normalized within the entire texture.
  1405. ///
  1406. /// See [updateRegion].
  1407. Float32List getUVs() {
  1408. final num = _bindings.spine_mesh_attachment_get_num_uvs(_attachment);
  1409. final uvs = _bindings.spine_mesh_attachment_get_uvs(_attachment);
  1410. return uvs.asTypedList(num);
  1411. }
  1412. /// Triplets of vertex indices which describe the mesh's triangulation.
  1413. Uint16List getTriangles() {
  1414. final num = _bindings.spine_mesh_attachment_get_num_triangles(_attachment);
  1415. final triangles = _bindings.spine_mesh_attachment_get_triangles(_attachment);
  1416. return triangles.asTypedList(num);
  1417. }
  1418. Color getColor() {
  1419. final color = _bindings.spine_mesh_attachment_get_color(_attachment);
  1420. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1421. _bindings.spine_color_get_a(color));
  1422. }
  1423. void setColor(double r, double g, double b, double a) {
  1424. _bindings.spine_mesh_attachment_set_color(_attachment, r, g, b, a);
  1425. }
  1426. String getPath() {
  1427. Pointer<Utf8> path = _bindings.spine_mesh_attachment_get_path(_attachment).cast();
  1428. return path.toDartString();
  1429. }
  1430. TextureRegion? getRegion() {
  1431. final region = _bindings.spine_mesh_attachment_get_region(_attachment);
  1432. if (region.address == nullptr.address) return null;
  1433. return TextureRegion._(region);
  1434. }
  1435. Sequence? getSequence() {
  1436. final sequence = _bindings.spine_mesh_attachment_get_sequence(_attachment);
  1437. if (sequence.address == nullptr.address) return null;
  1438. return Sequence._(sequence);
  1439. }
  1440. /// The parent mesh if this is a linked mesh, else null. A linked mesh shares the bones, vertices,
  1441. /// region UVs, triangles, hull length, edges, width, and height with the
  1442. /// parent mesh, but may have a different name or path (and therefore a different texture).
  1443. MeshAttachment? getParentMesh() {
  1444. final parent = _bindings.spine_mesh_attachment_get_parent_mesh(_attachment);
  1445. if (parent.address == nullptr.address) return null;
  1446. return MeshAttachment._(parent);
  1447. }
  1448. void setParentMesh(MeshAttachment? parentMesh) {
  1449. _bindings.spine_mesh_attachment_set_parent_mesh(_attachment, parentMesh == null ? nullptr : parentMesh._attachment);
  1450. }
  1451. /// Vertex index pairs describing edges for controlling triangulation, or be null if nonessential data was not exported. Mesh
  1452. /// triangles will never cross edges. Triangulation is not performed at runtime.
  1453. Uint16List getEdges() {
  1454. final num = _bindings.spine_mesh_attachment_get_num_edges(_attachment);
  1455. final edges = _bindings.spine_mesh_attachment_get_edges(_attachment);
  1456. return edges.asTypedList(num);
  1457. }
  1458. /// The width of the mesh's image, or zero if nonessential data was not exported.
  1459. double getWidth() {
  1460. return _bindings.spine_mesh_attachment_get_width(_attachment);
  1461. }
  1462. void setWidth(double width) {
  1463. _bindings.spine_mesh_attachment_set_width(_attachment, width);
  1464. }
  1465. /// The height of the mesh's image, or zero if nonessential data was not exported.
  1466. double getHeight() {
  1467. return _bindings.spine_mesh_attachment_get_height(_attachment);
  1468. }
  1469. void setHeight(double height) {
  1470. _bindings.spine_mesh_attachment_set_height(_attachment, height);
  1471. }
  1472. }
  1473. /// An attachment with vertices that make up a polygon used for clipping the rendering of other attachments.
  1474. class ClippingAttachment extends VertexAttachment<spine_clipping_attachment> {
  1475. ClippingAttachment._(spine_clipping_attachment attachment) : super._(attachment.cast());
  1476. /// Clipping is performed between the clipping attachment's slot and the end slot. If null clipping is done until the end of
  1477. /// the skeleton's rendering.
  1478. SlotData? getEndSlot() {
  1479. final endSlot = _bindings.spine_clipping_attachment_get_end_slot(_attachment);
  1480. if (endSlot.address == nullptr.address) return null;
  1481. return SlotData._(endSlot);
  1482. }
  1483. void setEndSlot(SlotData? endSlot) {
  1484. _bindings.spine_clipping_attachment_set_end_slot(_attachment, endSlot == null ? nullptr : endSlot._data);
  1485. }
  1486. /// The color of the clipping attachment as it was in Spine, or a default color if nonessential data was not exported. Clipping
  1487. /// attachments are not usually rendered at runtime.
  1488. Color getColor() {
  1489. final color = _bindings.spine_clipping_attachment_get_color(_attachment);
  1490. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1491. _bindings.spine_color_get_a(color));
  1492. }
  1493. void setColor(double r, double g, double b, double a) {
  1494. _bindings.spine_clipping_attachment_set_color(_attachment, r, g, b, a);
  1495. }
  1496. }
  1497. /// An attachment with vertices that make up a polygon. Can be used for hit detection, creating physics bodies, spawning particle
  1498. /// effects, and more.
  1499. ///
  1500. /// See [SkeletonBounds] and [Bounding boxes](http://esotericsoftware.com/spine-bounding-boxes) in the Spine User
  1501. /// Guide.
  1502. class BoundingBoxAttachment extends VertexAttachment<spine_bounding_box_attachment> {
  1503. BoundingBoxAttachment._(super.attachment) : super._();
  1504. /// The color of the bounding box as it was in Spine, or a default color if nonessential data was not exported. Bounding boxes
  1505. /// are not usually rendered at runtime.
  1506. Color getColor() {
  1507. final color = _bindings.spine_bounding_box_attachment_get_color(_attachment);
  1508. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1509. _bindings.spine_color_get_a(color));
  1510. }
  1511. void setColor(double r, double g, double b, double a) {
  1512. _bindings.spine_bounding_box_attachment_set_color(_attachment, r, g, b, a);
  1513. }
  1514. }
  1515. /// An attachment whose vertices make up a composite Bezier curve.
  1516. ///
  1517. /// See [PathConstraint] and [Paths](http://esotericsoftware.com/spine-paths) in the Spine User Guide.
  1518. class PathAttachment extends VertexAttachment<spine_path_attachment> {
  1519. PathAttachment._(super.attachment) : super._();
  1520. /// The lengths along the path in the setup pose from the start of the path to the end of each Bezier curve.
  1521. Float32List getLengths() {
  1522. final num = _bindings.spine_path_attachment_get_num_lengths(_attachment);
  1523. final lengths = _bindings.spine_path_attachment_get_lengths(_attachment);
  1524. return lengths.asTypedList(num);
  1525. }
  1526. /// If true, the start and end knots are connected.
  1527. bool isClosed() {
  1528. return _bindings.spine_path_attachment_get_is_closed(_attachment) == -1;
  1529. }
  1530. void setIsClosed(bool isClosed) {
  1531. _bindings.spine_path_attachment_set_is_closed(_attachment, isClosed ? -1 : 0);
  1532. }
  1533. /// If true, additional calculations are performed to make computing positions along the path more accurate and movement along
  1534. /// the path have a constant speed.
  1535. bool isConstantSpeed() {
  1536. return _bindings.spine_path_attachment_get_is_constant_speed(_attachment) == -1;
  1537. }
  1538. void setIsConstantSpeed(bool isClosed) {
  1539. _bindings.spine_path_attachment_set_is_constant_speed(_attachment, isClosed ? -1 : 0);
  1540. }
  1541. /// The color of the path as it was in Spine, or a default color if nonessential data was not exported. Paths are not usually
  1542. /// rendered at runtime.
  1543. Color getColor() {
  1544. final color = _bindings.spine_path_attachment_get_color(_attachment);
  1545. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1546. _bindings.spine_color_get_a(color));
  1547. }
  1548. void setColor(double r, double g, double b, double a) {
  1549. _bindings.spine_path_attachment_set_color(_attachment, r, g, b, a);
  1550. }
  1551. }
  1552. /// An attachment which is a single point and a rotation. This can be used to spawn projectiles, particles, etc. A bone can be
  1553. /// used in similar ways, but a PointAttachment is slightly less expensive to compute and can be hidden, shown, and placed in a
  1554. /// skin.
  1555. ///
  1556. /// See [Point Attachments](http://esotericsoftware.com/spine-point-attachments) in the Spine User Guide.
  1557. class PointAttachment extends Attachment<spine_point_attachment> {
  1558. PointAttachment._(super.attachment) : super._();
  1559. Vec2 computeWorldPosition(Bone bone) {
  1560. final position = _bindings.spine_point_attachment_compute_world_position(_attachment, bone._bone);
  1561. final result = Vec2(_bindings.spine_vector_get_x(position), _bindings.spine_vector_get_y(position));
  1562. return result;
  1563. }
  1564. double computeWorldRotation(Bone bone) {
  1565. return _bindings.spine_point_attachment_compute_world_rotation(_attachment, bone._bone);
  1566. }
  1567. double getX() {
  1568. return _bindings.spine_point_attachment_get_x(_attachment);
  1569. }
  1570. void setX(double x) {
  1571. _bindings.spine_point_attachment_set_x(_attachment, x);
  1572. }
  1573. double getY() {
  1574. return _bindings.spine_point_attachment_get_y(_attachment);
  1575. }
  1576. void setY(double y) {
  1577. _bindings.spine_point_attachment_set_y(_attachment, y);
  1578. }
  1579. double getRotation() {
  1580. return _bindings.spine_point_attachment_get_rotation(_attachment);
  1581. }
  1582. void setRotation(double rotation) {
  1583. _bindings.spine_point_attachment_set_x(_attachment, rotation);
  1584. }
  1585. /// The color of the point attachment as it was in Spine, or a default clor if nonessential data was not exported. Point
  1586. /// attachments are not usually rendered at runtime.
  1587. Color getColor() {
  1588. final color = _bindings.spine_point_attachment_get_color(_attachment);
  1589. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  1590. _bindings.spine_color_get_a(color));
  1591. }
  1592. void setColor(double r, double g, double b, double a) {
  1593. _bindings.spine_point_attachment_set_color(_attachment, r, g, b, a);
  1594. }
  1595. }
  1596. /// An entry storing the attachment to be used for a specific slot within [Skin].
  1597. class SkinEntry {
  1598. final int slotIndex;
  1599. final String name;
  1600. final Attachment? attachment;
  1601. SkinEntry(this.slotIndex, this.name, this.attachment);
  1602. }
  1603. /// Stores attachments by slot index and attachment name.
  1604. ///
  1605. /// Skins constructed manually via the `Skin(String name)` constructor must be manually disposed via the [dipose] method if they
  1606. /// are no longer used.
  1607. ///
  1608. /// See [SkeletonData.defaultSkin], [Skeleton.getSkin}, and [Runtime skins](http://esotericsoftware.com/spine-runtime-skins) in the
  1609. /// Spine Runtimes Guide.
  1610. class Skin {
  1611. late final bool _isCustomSkin;
  1612. late final spine_skin _skin;
  1613. Skin._(this._skin) : _isCustomSkin = false;
  1614. /// Constructs a new empty skin using the given [name]. Skins constructed this way must be manually disposed via the [dispose] method
  1615. /// if they are no longer used.
  1616. Skin(String name) {
  1617. final nativeName = name.toNativeUtf8(allocator: _allocator);
  1618. _skin = _bindings.spine_skin_create(nativeName.cast());
  1619. _allocator.free(nativeName);
  1620. _isCustomSkin = true;
  1621. }
  1622. /// Diposes this skin.
  1623. void dispose() {
  1624. if (!_isCustomSkin) return;
  1625. _bindings.spine_skin_dispose(_skin);
  1626. }
  1627. /// Adds an attachment to the skin for the specified slot index and name.
  1628. void setAttachment(int slotIndex, String name, Attachment? attachment) {
  1629. final nativeName = name.toNativeUtf8(allocator: _allocator);
  1630. _bindings.spine_skin_set_attachment(_skin, slotIndex, nativeName.cast(), attachment == null ? nullptr : attachment._attachment.cast());
  1631. _allocator.free(nativeName);
  1632. }
  1633. /// Returns the attachment for the specified slot index and name, or null.
  1634. Attachment? getAttachment(int slotIndex, String name) {
  1635. final nativeName = name.toNativeUtf8(allocator: _allocator);
  1636. final attachment = _bindings.spine_skin_get_attachment(_skin, slotIndex, nativeName.cast());
  1637. _allocator.free(nativeName);
  1638. if (attachment.address == nullptr.address) return null;
  1639. return Attachment._toSubclass(attachment);
  1640. }
  1641. /// Removes the attachment in the skin for the specified slot index and name, if any.
  1642. void removeAttachment(int slotIndex, String name) {
  1643. final nativeName = name.toNativeUtf8(allocator: _allocator);
  1644. _bindings.spine_skin_remove_attachment(_skin, slotIndex, nativeName.cast());
  1645. _allocator.free(nativeName);
  1646. }
  1647. /// The skin's name, which is unique across all skins in the skeleton.
  1648. String getName() {
  1649. Pointer<Utf8> name = _bindings.spine_skin_get_name(_skin).cast();
  1650. return name.toDartString();
  1651. }
  1652. /// Adds all attachments, bones, and constraints from the specified skin to this skin.
  1653. void addSkin(Skin other) {
  1654. _bindings.spine_skin_add_skin(_skin, other._skin);
  1655. }
  1656. /// Returns all entries in this skin.
  1657. List<SkinEntry> getEntries() {
  1658. List<SkinEntry> result = [];
  1659. final entries = _bindings.spine_skin_get_entries(_skin);
  1660. int numEntries = _bindings.spine_skin_entries_get_num_entries(entries);
  1661. for (int i = 0; i < numEntries; i++) {
  1662. final entry = _bindings.spine_skin_entries_get_entry(entries, i);
  1663. Pointer<Utf8> name = _bindings.spine_skin_entry_get_name(entry).cast();
  1664. result.add(SkinEntry(
  1665. _bindings.spine_skin_entry_get_slot_index(entry),
  1666. name.toDartString(),
  1667. _bindings.spine_skin_entry_get_attachment(entry).address == nullptr.address
  1668. ? null
  1669. : Attachment._toSubclass(_bindings.spine_skin_entry_get_attachment(entry))));
  1670. }
  1671. return result;
  1672. }
  1673. List<BoneData> getBones() {
  1674. List<BoneData> bones = [];
  1675. final numBones = _bindings.spine_skin_get_num_bones(_skin);
  1676. final nativeBones = _bindings.spine_skin_get_bones(_skin);
  1677. for (int i = 0; i < numBones; i++) {
  1678. bones.add(BoneData._(nativeBones[i]));
  1679. }
  1680. return bones;
  1681. }
  1682. List<ConstraintData> getConstraints() {
  1683. List<ConstraintData> constraints = [];
  1684. final numConstraints = _bindings.spine_skin_get_num_constraints(_skin);
  1685. final nativeConstraints = _bindings.spine_skin_get_constraints(_skin);
  1686. for (int i = 0; i < numConstraints; i++) {
  1687. final nativeConstraint = nativeConstraints[i];
  1688. final type = _bindings.spine_constraint_data_get_type(nativeConstraint);
  1689. switch (type) {
  1690. case spine_constraint_type.SPINE_CONSTRAINT_IK:
  1691. constraints.add(IkConstraintData._(nativeConstraint.cast()));
  1692. break;
  1693. case spine_constraint_type.SPINE_CONSTRAINT_TRANSFORM:
  1694. constraints.add(TransformConstraintData._(nativeConstraint.cast()));
  1695. break;
  1696. case spine_constraint_type.SPINE_CONSTRAINT_PATH:
  1697. constraints.add(PathConstraintData._(nativeConstraint.cast()));
  1698. break;
  1699. }
  1700. }
  1701. return constraints;
  1702. }
  1703. /// Adds all bones and constraints and copies of all attachments from the specified skin to this skin. Mesh attachments are not
  1704. /// copied, instead a new linked mesh is created. The attachment copies can be modified without affecting the originals.
  1705. void copy(Skin other) {
  1706. _bindings.spine_skin_copy_skin(_skin, other._skin);
  1707. }
  1708. }
  1709. /// The base class for all constraint datas.
  1710. class ConstraintData<T extends Pointer> {
  1711. final T _data;
  1712. ConstraintData._(this._data);
  1713. /// The constraint's name, which is unique across all constraints in the skeleton of the same type.
  1714. String getName() {
  1715. final Pointer<Utf8> name = _bindings.spine_constraint_data_get_name(_data.cast()).cast();
  1716. return name.toDartString();
  1717. }
  1718. /// The ordinal of this constraint for the order a skeleton's constraints will be applied by
  1719. /// [Skeleton.updateWorldTransform].
  1720. int getOrder() {
  1721. return _bindings.spine_constraint_data_get_order(_data.cast());
  1722. }
  1723. void setOrder(int order) {
  1724. _bindings.spine_constraint_data_set_order(_data.cast(), order);
  1725. }
  1726. /// When true, [Skeleton.updateWorldTransform] only updates this constraint if the skin returned by [Skeleton.getSkin] contains
  1727. /// this constraint.
  1728. ///
  1729. /// See [Skin.getConstraints].
  1730. bool isSkinRequired() {
  1731. return _bindings.spine_constraint_data_get_is_skin_required(_data.cast()) == 1;
  1732. }
  1733. void setIsSkinRequired(bool isSkinRequired) {
  1734. _bindings.spine_constraint_data_set_is_skin_required(_data.cast(), isSkinRequired ? -1 : 0);
  1735. }
  1736. }
  1737. /// Stores the setup pose for an [IkConstraint].
  1738. ///
  1739. /// See [IK constraints](http://esotericsoftware.com/spine-ik-constraints) in the Spine User Guide.
  1740. class IkConstraintData extends ConstraintData<spine_ik_constraint_data> {
  1741. IkConstraintData._(super.data) : super._();
  1742. /// The bones that are constrained by this IK constraint.
  1743. List<BoneData> getBones() {
  1744. final List<BoneData> result = [];
  1745. final numBones = _bindings.spine_ik_constraint_data_get_num_bones(_data);
  1746. final nativeBones = _bindings.spine_ik_constraint_data_get_bones(_data);
  1747. for (int i = 0; i < numBones; i++) {
  1748. result.add(BoneData._(nativeBones[i]));
  1749. }
  1750. return result;
  1751. }
  1752. /// The bone that is the IK target.
  1753. BoneData getTarget() {
  1754. return BoneData._(_bindings.spine_ik_constraint_data_get_target(_data));
  1755. }
  1756. void setTarget(BoneData target) {
  1757. _bindings.spine_ik_constraint_data_set_target(_data, target._data);
  1758. }
  1759. /// For two bone IK, controls the bend direction of the IK bones, either 1 or -1.
  1760. int getBendDirection() {
  1761. return _bindings.spine_ik_constraint_data_get_bend_direction(_data);
  1762. }
  1763. void setBendDirection(int bendDirection) {
  1764. _bindings.spine_ik_constraint_data_set_bend_direction(_data, bendDirection);
  1765. }
  1766. /// For one bone IK, when true and the target is too close, the bone is scaled to reach it.
  1767. bool getCompress() {
  1768. return _bindings.spine_ik_constraint_data_get_compress(_data) == -1;
  1769. }
  1770. void setCompress(bool compress) {
  1771. _bindings.spine_ik_constraint_data_set_compress(_data, compress ? -1 : 0);
  1772. }
  1773. /// When true and the target is out of range, the parent bone is scaled to reach it.
  1774. ///
  1775. /// For two bone IK: 1) the child bone's local Y translation is set to 0, 2) stretch is not applied if [getSoftness] is
  1776. /// > 0, and 3) if the parent bone has local nonuniform scale, stretch is not applied.
  1777. bool getStretch() {
  1778. return _bindings.spine_ik_constraint_data_get_stretch(_data) == -1;
  1779. }
  1780. void setStretch(bool stretch) {
  1781. _bindings.spine_ik_constraint_data_set_stretch(_data, stretch ? -1 : 0);
  1782. }
  1783. /// When true and [getCompress] or [getStretch] is used, the bone is scaled on both the X and Y axes.
  1784. bool getUniform() {
  1785. return _bindings.spine_ik_constraint_data_get_uniform(_data) == -1;
  1786. }
  1787. void setUniform(bool uniform) {
  1788. _bindings.spine_ik_constraint_data_set_uniform(_data, uniform ? -1 : 0);
  1789. }
  1790. /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.
  1791. ///
  1792. /// For two bone IK: if the parent bone has local nonuniform scale, the child bone's local Y translation is set to 0.
  1793. double getMix() {
  1794. return _bindings.spine_ik_constraint_data_get_mix(_data);
  1795. }
  1796. void setMix(double mix) {
  1797. _bindings.spine_ik_constraint_data_set_mix(_data, mix);
  1798. }
  1799. /// For two bone IK, the target bone's distance from the maximum reach of the bones where rotation begins to slow. The bones
  1800. /// will not straighten completely until the target is this far out of range.
  1801. double getSoftness() {
  1802. return _bindings.spine_ik_constraint_data_get_softness(_data);
  1803. }
  1804. void setSoftness(double softness) {
  1805. _bindings.spine_ik_constraint_data_set_softness(_data, softness);
  1806. }
  1807. }
  1808. /// Stores the current pose for an IK constraint. An IK constraint adjusts the rotation of 1 or 2 constrained bones so the tip of
  1809. /// the last bone is as close to the target bone as possible.
  1810. /// <p>
  1811. /// See <a href="http://esotericsoftware.com/spine-ik-constraints">IK constraints</a> in the Spine User Guide.
  1812. class IkConstraint {
  1813. final spine_ik_constraint _constraint;
  1814. IkConstraint._(this._constraint);
  1815. /// Applies the constraint to the constrained bones.
  1816. void update() {
  1817. _bindings.spine_ik_constraint_update(_constraint);
  1818. }
  1819. int getOrder() {
  1820. return _bindings.spine_ik_constraint_get_order(_constraint);
  1821. }
  1822. /// The IK constraint's setup pose data.
  1823. IkConstraintData getData() {
  1824. return IkConstraintData._(_bindings.spine_ik_constraint_get_data(_constraint));
  1825. }
  1826. /// The bones that will be modified by this IK constraint.
  1827. List<Bone> getBones() {
  1828. List<Bone> result = [];
  1829. final num = _bindings.spine_ik_constraint_get_num_bones(_constraint);
  1830. final nativeBones = _bindings.spine_ik_constraint_get_bones(_constraint);
  1831. for (int i = 0; i < num; i++) {
  1832. result.add(Bone._(nativeBones[i]));
  1833. }
  1834. return result;
  1835. }
  1836. /// The bone that is the IK target.
  1837. Bone getTarget() {
  1838. return Bone._(_bindings.spine_ik_constraint_get_target(_constraint));
  1839. }
  1840. void setTarget(Bone target) {
  1841. _bindings.spine_ik_constraint_set_target(_constraint, target._bone);
  1842. }
  1843. /// For two bone IK, controls the bend direction of the IK bones, either 1 or -1.
  1844. int getBendDirection() {
  1845. return _bindings.spine_ik_constraint_get_bend_direction(_constraint);
  1846. }
  1847. void setBendDirection(int bendDirection) {
  1848. _bindings.spine_ik_constraint_set_bend_direction(_constraint, bendDirection);
  1849. }
  1850. /// For one bone IK, when true and the target is too close, the bone is scaled to reach it.
  1851. bool getCompress() {
  1852. return _bindings.spine_ik_constraint_get_compress(_constraint) == -1;
  1853. }
  1854. void setCompress(bool compress) {
  1855. _bindings.spine_ik_constraint_set_compress(_constraint, compress ? -1 : 0);
  1856. }
  1857. /// When true and the target is out of range, the parent bone is scaled to reach it.
  1858. ///
  1859. /// For two bone IK: 1) the child bone's local Y translation is set to 0, 2) stretch is not applied if [getSoftness] is
  1860. /// > 0, and 3) if the parent bone has local nonuniform scale, stretch is not applied.
  1861. bool getStretch() {
  1862. return _bindings.spine_ik_constraint_get_stretch(_constraint) == -1;
  1863. }
  1864. void setStretch(bool stretch) {
  1865. _bindings.spine_ik_constraint_set_stretch(_constraint, stretch ? -1 : 0);
  1866. }
  1867. /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.
  1868. ///
  1869. /// For two bone IK: if the parent bone has local nonuniform scale, the child bone's local Y translation is set to 0.
  1870. double getMix() {
  1871. return _bindings.spine_ik_constraint_get_mix(_constraint);
  1872. }
  1873. void setMix(double mix) {
  1874. _bindings.spine_ik_constraint_set_mix(_constraint, mix);
  1875. }
  1876. /// For two bone IK, the target bone's distance from the maximum reach of the bones where rotation begins to slow. The bones
  1877. /// will not straighten completely until the target is this far out of range.
  1878. double getSoftness() {
  1879. return _bindings.spine_ik_constraint_get_softness(_constraint);
  1880. }
  1881. void setSoftness(double softness) {
  1882. _bindings.spine_ik_constraint_set_softness(_constraint, softness);
  1883. }
  1884. bool isActive() {
  1885. return _bindings.spine_ik_constraint_get_is_active(_constraint) == -1;
  1886. }
  1887. void setIsActive(bool isActive) {
  1888. _bindings.spine_ik_constraint_set_is_active(_constraint, isActive ? -1 : 0);
  1889. }
  1890. }
  1891. /// Stores the setup pose for a {@link TransformConstraint}.
  1892. ///
  1893. /// See [Transform constraints](http://esotericsoftware.com/spine-transform-constraints) in the Spine User Guide.
  1894. class TransformConstraintData extends ConstraintData<spine_transform_constraint_data> {
  1895. TransformConstraintData._(super.data) : super._();
  1896. /// The bones that will be modified by this transform constraint.
  1897. List<BoneData> getBones() {
  1898. final List<BoneData> result = [];
  1899. final numBones = _bindings.spine_transform_constraint_data_get_num_bones(_data);
  1900. final nativeBones = _bindings.spine_transform_constraint_data_get_bones(_data);
  1901. for (int i = 0; i < numBones; i++) {
  1902. result.add(BoneData._(nativeBones[i]));
  1903. }
  1904. return result;
  1905. }
  1906. /// The target bone whose world transform will be copied to the constrained bones.
  1907. BoneData getTarget() {
  1908. return BoneData._(_bindings.spine_transform_constraint_data_get_target(_data));
  1909. }
  1910. void setTarget(BoneData target) {
  1911. _bindings.spine_transform_constraint_data_set_target(_data, target._data);
  1912. }
  1913. /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.
  1914. double getMixRotate() {
  1915. return _bindings.spine_transform_constraint_data_get_mix_rotate(_data);
  1916. }
  1917. void setMixRotate(double mixRotate) {
  1918. _bindings.spine_transform_constraint_data_set_mix_rotate(_data, mixRotate);
  1919. }
  1920. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation X.
  1921. double getMixX() {
  1922. return _bindings.spine_transform_constraint_data_get_mix_x(_data);
  1923. }
  1924. void setMixX(double mixX) {
  1925. _bindings.spine_transform_constraint_data_set_mix_x(_data, mixX);
  1926. }
  1927. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation Y.
  1928. double getMixY() {
  1929. return _bindings.spine_transform_constraint_data_get_mix_y(_data);
  1930. }
  1931. void setMixY(double mixY) {
  1932. _bindings.spine_transform_constraint_data_set_mix_y(_data, mixY);
  1933. }
  1934. /// A percentage (0-1) that controls the mix between the constrained and unconstrained scale X.
  1935. double getMixScaleX() {
  1936. return _bindings.spine_transform_constraint_data_get_mix_scale_x(_data);
  1937. }
  1938. void setMixScaleX(double mixScaleX) {
  1939. _bindings.spine_transform_constraint_data_set_mix_scale_x(_data, mixScaleX);
  1940. }
  1941. /// A percentage (0-1) that controls the mix between the constrained and unconstrained scale Y.
  1942. double getMixScaleY() {
  1943. return _bindings.spine_transform_constraint_data_get_mix_scale_y(_data);
  1944. }
  1945. void setMixScaleY(double mixScaleY) {
  1946. _bindings.spine_transform_constraint_data_set_mix_scale_y(_data, mixScaleY);
  1947. }
  1948. /// A percentage (0-1) that controls the mix between the constrained and unconstrained shear Y.
  1949. double getMixShearY() {
  1950. return _bindings.spine_transform_constraint_data_get_mix_shear_y(_data);
  1951. }
  1952. void setMixShearY(double mixShearY) {
  1953. _bindings.spine_transform_constraint_data_set_mix_shear_y(_data, mixShearY);
  1954. }
  1955. /// An offset added to the constrained bone rotation.
  1956. double getOffsetRotation() {
  1957. return _bindings.spine_transform_constraint_data_get_offset_rotation(_data);
  1958. }
  1959. void setOffsetRotation(double offsetRotation) {
  1960. _bindings.spine_transform_constraint_data_set_offset_rotation(_data, offsetRotation);
  1961. }
  1962. /// An offset added to the constrained bone X translation.
  1963. double getOffsetX() {
  1964. return _bindings.spine_transform_constraint_data_get_offset_x(_data);
  1965. }
  1966. void setOffsetX(double offsetX) {
  1967. _bindings.spine_transform_constraint_data_set_offset_x(_data, offsetX);
  1968. }
  1969. /// An offset added to the constrained bone Y translation.
  1970. double getOffsetY() {
  1971. return _bindings.spine_transform_constraint_data_get_offset_y(_data);
  1972. }
  1973. /// An offset added to the constrained bone scaleX.
  1974. void setOffsetY(double offsetY) {
  1975. _bindings.spine_transform_constraint_data_set_offset_y(_data, offsetY);
  1976. }
  1977. /// An offset added to the constrained bone scaleX.
  1978. double getOffsetScaleX() {
  1979. return _bindings.spine_transform_constraint_data_get_offset_scale_x(_data);
  1980. }
  1981. void setOffsetScaleX(double offsetScaleX) {
  1982. _bindings.spine_transform_constraint_data_set_offset_x(_data, offsetScaleX);
  1983. }
  1984. /// An offset added to the constrained bone scaleY.
  1985. double getOffsetScaleY() {
  1986. return _bindings.spine_transform_constraint_data_get_offset_scale_y(_data);
  1987. }
  1988. void setOffsetScaleY(double offsetScaleY) {
  1989. _bindings.spine_transform_constraint_data_set_offset_scale_y(_data, offsetScaleY);
  1990. }
  1991. /// An offset added to the constrained bone shearY.
  1992. double getOffsetShearY() {
  1993. return _bindings.spine_transform_constraint_data_get_offset_shear_y(_data);
  1994. }
  1995. void setOffsetShearY(double offsetShearY) {
  1996. _bindings.spine_transform_constraint_data_set_offset_shear_y(_data, offsetShearY);
  1997. }
  1998. bool isRelative() {
  1999. return _bindings.spine_transform_constraint_data_get_is_relative(_data) == -1;
  2000. }
  2001. void setIsRelative(bool isRelative) {
  2002. _bindings.spine_transform_constraint_data_set_is_relative(_data, isRelative ? -1 : 0);
  2003. }
  2004. bool isLocal() {
  2005. return _bindings.spine_transform_constraint_data_get_is_local(_data) == -1;
  2006. }
  2007. void setIsLocal(bool isLocal) {
  2008. _bindings.spine_transform_constraint_data_set_is_local(_data, isLocal ? -1 : 0);
  2009. }
  2010. }
  2011. /// Stores the current pose for a transform constraint. A transform constraint adjusts the world transform of the constrained
  2012. /// bones to match that of the target bone.
  2013. ///
  2014. /// See [Transform constraints](http://esotericsoftware.com/spine-transform-constraints) in the Spine User Guide.
  2015. class TransformConstraint {
  2016. final spine_transform_constraint _constraint;
  2017. TransformConstraint._(this._constraint);
  2018. /// Applies the constraint to the constrained bones.
  2019. void update() {
  2020. _bindings.spine_transform_constraint_update(_constraint);
  2021. }
  2022. int getOrder() {
  2023. return _bindings.spine_transform_constraint_get_order(_constraint);
  2024. }
  2025. /// The transform constraint's setup pose data.
  2026. TransformConstraintData getData() {
  2027. return TransformConstraintData._(_bindings.spine_transform_constraint_get_data(_constraint));
  2028. }
  2029. /// The bones that will be modified by this transform constraint.
  2030. List<Bone> getBones() {
  2031. List<Bone> result = [];
  2032. final num = _bindings.spine_transform_constraint_get_num_bones(_constraint);
  2033. final nativeBones = _bindings.spine_transform_constraint_get_bones(_constraint);
  2034. for (int i = 0; i < num; i++) {
  2035. result.add(Bone._(nativeBones[i]));
  2036. }
  2037. return result;
  2038. }
  2039. /// The target bone whose world transform will be copied to the constrained bones.
  2040. Bone getTarget() {
  2041. return Bone._(_bindings.spine_transform_constraint_get_target(_constraint));
  2042. }
  2043. void setTarget(Bone target) {
  2044. _bindings.spine_transform_constraint_set_target(_constraint, target._bone);
  2045. }
  2046. /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.
  2047. double getMixRotate() {
  2048. return _bindings.spine_transform_constraint_get_mix_rotate(_constraint);
  2049. }
  2050. void setMixRotate(double mixRotate) {
  2051. _bindings.spine_transform_constraint_set_mix_rotate(_constraint, mixRotate);
  2052. }
  2053. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation X.
  2054. double getMixX() {
  2055. return _bindings.spine_transform_constraint_get_mix_x(_constraint);
  2056. }
  2057. void setMixX(double mixX) {
  2058. _bindings.spine_transform_constraint_set_mix_x(_constraint, mixX);
  2059. }
  2060. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation Y.
  2061. double getMixY() {
  2062. return _bindings.spine_transform_constraint_get_mix_y(_constraint);
  2063. }
  2064. void setMixY(double mixY) {
  2065. _bindings.spine_transform_constraint_set_mix_y(_constraint, mixY);
  2066. }
  2067. /// A percentage (0-1) that controls the mix between the constrained and unconstrained scale X.
  2068. double getMixScaleX() {
  2069. return _bindings.spine_transform_constraint_get_mix_scale_x(_constraint);
  2070. }
  2071. void setMixScaleX(double mixScaleX) {
  2072. _bindings.spine_transform_constraint_set_mix_scale_x(_constraint, mixScaleX);
  2073. }
  2074. /// A percentage (0-1) that controls the mix between the constrained and unconstrained scale X.
  2075. double getMixScaleY() {
  2076. return _bindings.spine_transform_constraint_get_mix_scale_y(_constraint);
  2077. }
  2078. void setMixScaleY(double mixScaleY) {
  2079. _bindings.spine_transform_constraint_set_mix_scale_y(_constraint, mixScaleY);
  2080. }
  2081. /// A percentage (0-1) that controls the mix between the constrained and unconstrained shear Y.
  2082. double getMixShearY() {
  2083. return _bindings.spine_transform_constraint_get_mix_shear_y(_constraint);
  2084. }
  2085. void setMixShearY(double mixShearY) {
  2086. _bindings.spine_transform_constraint_set_mix_shear_y(_constraint, mixShearY);
  2087. }
  2088. bool isActive() {
  2089. return _bindings.spine_transform_constraint_get_is_active(_constraint) == -1;
  2090. }
  2091. void setIsActive(bool isActive) {
  2092. _bindings.spine_transform_constraint_set_is_active(_constraint, isActive ? -1 : 0);
  2093. }
  2094. }
  2095. /// Stores the setup pose for a [PathConstraint].
  2096. ///
  2097. /// See [Path constraints](http://esotericsoftware.com/spine-path-constraints) in the Spine User Guide.
  2098. class PathConstraintData extends ConstraintData<spine_path_constraint_data> {
  2099. PathConstraintData._(super.data) : super._();
  2100. /// The bones that will be modified by this path constraint.
  2101. List<BoneData> getBones() {
  2102. final List<BoneData> result = [];
  2103. final numBones = _bindings.spine_path_constraint_data_get_num_bones(_data);
  2104. final nativeBones = _bindings.spine_path_constraint_data_get_bones(_data);
  2105. for (int i = 0; i < numBones; i++) {
  2106. result.add(BoneData._(nativeBones[i]));
  2107. }
  2108. return result;
  2109. }
  2110. /// The slot whose path attachment will be used to constrained the bones.
  2111. SlotData getTarget() {
  2112. return SlotData._(_bindings.spine_path_constraint_data_get_target(_data));
  2113. }
  2114. void setTarget(SlotData target) {
  2115. _bindings.spine_path_constraint_data_set_target(_data, target._data);
  2116. }
  2117. /// The mode for positioning the first bone on the path.
  2118. PositionMode getPositionMode() {
  2119. return PositionMode.values[_bindings.spine_path_constraint_data_get_position_mode(_data)];
  2120. }
  2121. void setPositionMode(PositionMode positionMode) {
  2122. _bindings.spine_path_constraint_data_set_position_mode(_data, positionMode.value);
  2123. }
  2124. /// The mode for positioning the bones after the first bone on the path.
  2125. SpacingMode getSpacingMode() {
  2126. return SpacingMode.values[_bindings.spine_path_constraint_data_get_spacing_mode(_data)];
  2127. }
  2128. void setSpacingMode(SpacingMode spacingMode) {
  2129. _bindings.spine_path_constraint_data_set_spacing_mode(_data, spacingMode.value);
  2130. }
  2131. /// The mode for adjusting the rotation of the bones.
  2132. RotateMode getRotateMode() {
  2133. return RotateMode.values[_bindings.spine_path_constraint_data_get_rotate_mode(_data)];
  2134. }
  2135. void setRotateMode(RotateMode rotateMode) {
  2136. _bindings.spine_path_constraint_data_set_rotate_mode(_data, rotateMode.value);
  2137. }
  2138. /// An offset added to the constrained bone rotation.
  2139. double getOffsetRotation() {
  2140. return _bindings.spine_path_constraint_data_get_offset_rotation(_data);
  2141. }
  2142. void setOffsetRotation(double offsetRotation) {
  2143. _bindings.spine_path_constraint_data_set_offset_rotation(_data, offsetRotation);
  2144. }
  2145. /// The position along the path.
  2146. double getPosition() {
  2147. return _bindings.spine_path_constraint_data_get_position(_data);
  2148. }
  2149. void setPosition(double position) {
  2150. _bindings.spine_path_constraint_data_set_position(_data, position);
  2151. }
  2152. /// The spacing between bones.
  2153. double getSpacing() {
  2154. return _bindings.spine_path_constraint_data_get_spacing(_data);
  2155. }
  2156. void setSpacing(double spacing) {
  2157. _bindings.spine_path_constraint_data_set_spacing(_data, spacing);
  2158. }
  2159. /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.
  2160. double getMixRotate() {
  2161. return _bindings.spine_path_constraint_data_get_mix_rotate(_data);
  2162. }
  2163. void setMixRotate(double mixRotate) {
  2164. _bindings.spine_path_constraint_data_set_mix_rotate(_data, mixRotate);
  2165. }
  2166. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation X.
  2167. double getMixX() {
  2168. return _bindings.spine_path_constraint_data_get_mix_x(_data);
  2169. }
  2170. void setMixX(double mixX) {
  2171. _bindings.spine_path_constraint_data_set_mix_x(_data, mixX);
  2172. }
  2173. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation Y.
  2174. double getMixY() {
  2175. return _bindings.spine_path_constraint_data_get_mix_x(_data);
  2176. }
  2177. void setMixY(double mixY) {
  2178. _bindings.spine_path_constraint_data_set_mix_y(_data, mixY);
  2179. }
  2180. }
  2181. /// Stores the current pose for a path constraint. A path constraint adjusts the rotation, translation, and scale of the
  2182. /// constrained bones so they follow a [PathAttachment].
  2183. ///
  2184. /// See [Path constraints](http://esotericsoftware.com/spine-path-constraints) in the Spine User Guide.
  2185. class PathConstraint {
  2186. final spine_path_constraint _constraint;
  2187. PathConstraint._(this._constraint);
  2188. /// Applies the constraint to the constrained bones.
  2189. void update() {
  2190. _bindings.spine_path_constraint_update(_constraint);
  2191. }
  2192. int getOrder() {
  2193. return _bindings.spine_path_constraint_get_order(_constraint);
  2194. }
  2195. /// The bones that will be modified by this path constraint.
  2196. List<Bone> getBones() {
  2197. List<Bone> result = [];
  2198. final num = _bindings.spine_path_constraint_get_num_bones(_constraint);
  2199. final nativeBones = _bindings.spine_path_constraint_get_bones(_constraint);
  2200. for (int i = 0; i < num; i++) {
  2201. result.add(Bone._(nativeBones[i]));
  2202. }
  2203. return result;
  2204. }
  2205. /// The slot whose path attachment will be used to constrained the bones.
  2206. Slot getTarget() {
  2207. return Slot._(_bindings.spine_path_constraint_get_target(_constraint));
  2208. }
  2209. void setTarget(Slot target) {
  2210. _bindings.spine_path_constraint_set_target(_constraint, target._slot);
  2211. }
  2212. /// The position along the path.
  2213. double getPosition() {
  2214. return _bindings.spine_path_constraint_get_position(_constraint);
  2215. }
  2216. void setPosition(double position) {
  2217. _bindings.spine_path_constraint_set_position(_constraint, position);
  2218. }
  2219. /// The spacing between bones.
  2220. double getSpacing() {
  2221. return _bindings.spine_path_constraint_get_spacing(_constraint);
  2222. }
  2223. void setSpacing(double spacing) {
  2224. _bindings.spine_path_constraint_set_spacing(_constraint, spacing);
  2225. }
  2226. /// A percentage (0-1) that controls the mix between the constrained and unconstrained rotation.
  2227. double getMixRotate() {
  2228. return _bindings.spine_path_constraint_get_mix_rotate(_constraint);
  2229. }
  2230. void setMixRotate(double mixRotate) {
  2231. _bindings.spine_path_constraint_set_mix_rotate(_constraint, mixRotate);
  2232. }
  2233. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation X.
  2234. double getMixX() {
  2235. return _bindings.spine_path_constraint_get_mix_x(_constraint);
  2236. }
  2237. void setMixX(double mixX) {
  2238. _bindings.spine_path_constraint_set_mix_x(_constraint, mixX);
  2239. }
  2240. /// A percentage (0-1) that controls the mix between the constrained and unconstrained translation Y.
  2241. double getMixY() {
  2242. return _bindings.spine_path_constraint_get_mix_y(_constraint);
  2243. }
  2244. void setMixY(double mixY) {
  2245. _bindings.spine_path_constraint_set_mix_y(_constraint, mixY);
  2246. }
  2247. bool isActive() {
  2248. return _bindings.spine_path_constraint_get_is_active(_constraint) == -1;
  2249. }
  2250. void setIsActive(bool isActive) {
  2251. _bindings.spine_path_constraint_set_is_active(_constraint, isActive ? -1 : 0);
  2252. }
  2253. }
  2254. /// Stores the current pose for a skeleton.
  2255. ///
  2256. /// See [Instance objects](http://esotericsoftware.com/spine-runtime-architecture#Instance-objects) in the Spine
  2257. /// Runtimes Guide.
  2258. class Skeleton {
  2259. final spine_skeleton _skeleton;
  2260. Skeleton._(this._skeleton);
  2261. /// Caches information about bones and constraints. Must be called if the [getSkin] is modified or if bones,
  2262. /// constraints, or weighted path attachments are added or removed.
  2263. void updateCache() {
  2264. _bindings.spine_skeleton_update_cache(_skeleton);
  2265. }
  2266. /// Updates the world transform for each bone and applies all constraints.
  2267. ///
  2268. /// See [World transforms](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms) in the Spine
  2269. /// Runtimes Guide.
  2270. void updateWorldTransform(Physics physics) {
  2271. _bindings.spine_skeleton_update_world_transform(_skeleton, physics.value);
  2272. }
  2273. /// Temporarily sets the root bone as a child of the specified bone, then updates the world transform for each bone and applies
  2274. /// all constraints.
  2275. ///
  2276. /// See [World transforms](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms) in the Spine
  2277. /// Runtimes Guide.
  2278. void updateWorldTransformBone(Physics physics, Bone parent) {
  2279. _bindings.spine_skeleton_update_world_transform_bone(_skeleton, physics.value, parent._bone);
  2280. }
  2281. /// Sets the bones, constraints, slots, and draw order to their setup pose values.
  2282. void setToSetupPose() {
  2283. _bindings.spine_skeleton_set_to_setup_pose(_skeleton);
  2284. }
  2285. /// Sets the bones and constraints to their setup pose values.
  2286. void setBonesToSetupPose() {
  2287. _bindings.spine_skeleton_set_bones_to_setup_pose(_skeleton);
  2288. }
  2289. /// Sets the slots and draw order to their setup pose values.
  2290. void setSlotsToSetupPose() {
  2291. _bindings.spine_skeleton_set_slots_to_setup_pose(_skeleton);
  2292. }
  2293. /// Finds a bone by comparing each bone's name. It is more efficient to cache the results of this method than to call it
  2294. /// repeatedly.
  2295. Bone? findBone(String boneName) {
  2296. final nameNative = boneName.toNativeUtf8(allocator: _allocator);
  2297. final bone = _bindings.spine_skeleton_find_bone(_skeleton, nameNative.cast());
  2298. _allocator.free(nameNative);
  2299. if (bone.address == nullptr.address) return null;
  2300. return Bone._(bone);
  2301. }
  2302. /// Finds a slot by comparing each slot's name. It is more efficient to cache the results of this method than to call it
  2303. /// repeatedly.
  2304. Slot? findSlot(String slotName) {
  2305. final nameNative = slotName.toNativeUtf8(allocator: _allocator);
  2306. final slot = _bindings.spine_skeleton_find_slot(_skeleton, nameNative.cast());
  2307. _allocator.free(nameNative);
  2308. if (slot.address == nullptr.address) return null;
  2309. return Slot._(slot);
  2310. }
  2311. /// Sets a skin by name.
  2312. ///
  2313. /// See [setSkin].
  2314. void setSkinByName(String skinName) {
  2315. final nameNative = skinName.toNativeUtf8(allocator: _allocator);
  2316. _bindings.spine_skeleton_set_skin_by_name(_skeleton, nameNative.cast());
  2317. _allocator.free(nameNative);
  2318. }
  2319. /// Sets the skin used to look up attachments before looking in the default skin (see [SkeletonData.getDefaultSkin]). If the
  2320. /// skin is changed, [updateCache] is called.
  2321. ///
  2322. /// Attachments from the new skin are attached if the corresponding attachment from the old skin was attached. If there was no
  2323. /// old skin, each slot's setup mode attachment is attached from the new skin.
  2324. ///
  2325. /// After changing the skin, the visible attachments can be reset to those attached in the setup pose by calling
  2326. /// [setSlotsToSetupPose]. Also, often [AnimationState.apply] is called before the next time the
  2327. /// skeleton is rendered to allow any attachment keys in the current animation(s) to hide or show attachments from the new
  2328. /// skin.
  2329. void setSkin(Skin skin) {
  2330. _bindings.spine_skeleton_set_skin(_skeleton, skin._skin);
  2331. }
  2332. /// Finds an attachment by looking in the currently set skin (see [getSkin]) and default skin (see [SkeletonData.getDefaultSkin]) using
  2333. /// the slot name and attachment name.
  2334. ///
  2335. /// See [getAttachment].
  2336. Attachment? getAttachmentByName(String slotName, String attachmentName) {
  2337. final slotNameNative = slotName.toNativeUtf8(allocator: _allocator);
  2338. final attachmentNameNative = attachmentName.toNativeUtf8(allocator: _allocator);
  2339. final attachment = _bindings.spine_skeleton_get_attachment_by_name(_skeleton, slotNameNative.cast(), attachmentNameNative.cast());
  2340. _allocator.free(slotNameNative);
  2341. _allocator.free(attachmentNameNative);
  2342. if (attachment.address == nullptr.address) return null;
  2343. return Attachment._toSubclass(attachment);
  2344. }
  2345. /// Finds an attachment by looking in the currently set skin (see [getSkin]) and default skin (see [SkeletonData.getDefaultSkin]) using the
  2346. /// slot index and attachment name. First the skin is checked and if the attachment was not found, the default skin is checked.
  2347. ///
  2348. /// See [Runtime skins](http://esotericsoftware.com/spine-runtime-skins) in the Spine Runtimes Guide.
  2349. Attachment? getAttachment(int slotIndex, String attachmentName) {
  2350. final attachmentNameNative = attachmentName.toNativeUtf8(allocator: _allocator);
  2351. final attachment = _bindings.spine_skeleton_get_attachment(_skeleton, slotIndex, attachmentNameNative.cast());
  2352. _allocator.free(attachmentNameNative);
  2353. if (attachment.address == nullptr.address) return null;
  2354. return Attachment._toSubclass(attachment);
  2355. }
  2356. /// A convenience method to set an attachment by finding the slot with [findSlot], finding the attachment with
  2357. /// [getAttachment], then setting the slot's attachment. The [attachmentName] may be an empty string to clear the slot's attachment.
  2358. void setAttachment(String slotName, String attachmentName) {
  2359. final slotNameNative = slotName.toNativeUtf8(allocator: _allocator);
  2360. final attachmentNameNative = attachmentName.toNativeUtf8(allocator: _allocator);
  2361. _bindings.spine_skeleton_set_attachment(_skeleton, slotNameNative.cast(), attachmentNameNative.cast());
  2362. _allocator.free(slotNameNative);
  2363. _allocator.free(attachmentNameNative);
  2364. }
  2365. /// Finds an IK constraint by comparing each IK constraint's name. It is more efficient to cache the results of this method
  2366. /// than to call it repeatedly.
  2367. IkConstraint? findIkConstraint(String constraintName) {
  2368. final nameNative = constraintName.toNativeUtf8(allocator: _allocator);
  2369. final constraint = _bindings.spine_skeleton_find_ik_constraint(_skeleton, nameNative.cast());
  2370. _allocator.free(nameNative);
  2371. if (constraint.address == nullptr.address) return null;
  2372. return IkConstraint._(constraint);
  2373. }
  2374. /// Finds a transform constraint by comparing each transform constraint's name. It is more efficient to cache the results of
  2375. /// this method than to call it repeatedly.
  2376. TransformConstraint? findTransformConstraint(String constraintName) {
  2377. final nameNative = constraintName.toNativeUtf8(allocator: _allocator);
  2378. final constraint = _bindings.spine_skeleton_find_transform_constraint(_skeleton, nameNative.cast());
  2379. _allocator.free(nameNative);
  2380. if (constraint.address == nullptr.address) return null;
  2381. return TransformConstraint._(constraint);
  2382. }
  2383. /// Finds a path constraint by comparing each path constraint's name. It is more efficient to cache the results of this method
  2384. /// than to call it repeatedly.
  2385. PathConstraint? findPathConstraint(String constraintName) {
  2386. final nameNative = constraintName.toNativeUtf8(allocator: _allocator);
  2387. final constraint = _bindings.spine_skeleton_find_path_constraint(_skeleton, nameNative.cast());
  2388. _allocator.free(nameNative);
  2389. if (constraint.address == nullptr.address) return null;
  2390. return PathConstraint._(constraint);
  2391. }
  2392. /// Returns the axis aligned bounding box (AABB) of the region and mesh attachments for the current pose.
  2393. Bounds getBounds() {
  2394. final nativeBounds = _bindings.spine_skeleton_get_bounds(_skeleton);
  2395. final bounds = Bounds(_bindings.spine_bounds_get_x(nativeBounds), _bindings.spine_bounds_get_y(nativeBounds),
  2396. _bindings.spine_bounds_get_width(nativeBounds), _bindings.spine_bounds_get_height(nativeBounds));
  2397. return bounds;
  2398. }
  2399. /// Returns the root bone, or null if the skeleton has no bones.
  2400. Bone? getRootBone() {
  2401. final bone = _bindings.spine_skeleton_get_root_bone(_skeleton);
  2402. if (bone.address == nullptr.address) return null;
  2403. return Bone._(bone);
  2404. }
  2405. /// The skeleton's setup pose data.
  2406. SkeletonData? getData() {
  2407. final data = _bindings.spine_skeleton_get_data(_skeleton);
  2408. if (data.address == nullptr.address) return null;
  2409. return SkeletonData._(data);
  2410. }
  2411. /// The skeleton's bones, sorted parent first. The root bone is always the first bone.
  2412. List<Bone> getBones() {
  2413. final List<Bone> bones = [];
  2414. final numBones = _bindings.spine_skeleton_get_num_bones(_skeleton);
  2415. final nativeBones = _bindings.spine_skeleton_get_bones(_skeleton);
  2416. for (int i = 0; i < numBones; i++) {
  2417. bones.add(Bone._(nativeBones[i]));
  2418. }
  2419. return bones;
  2420. }
  2421. /// The skeleton's slots.
  2422. List<Slot> getSlots() {
  2423. final List<Slot> slots = [];
  2424. final numSlots = _bindings.spine_skeleton_get_num_slots(_skeleton);
  2425. final nativeSlots = _bindings.spine_skeleton_get_slots(_skeleton);
  2426. for (int i = 0; i < numSlots; i++) {
  2427. slots.add(Slot._(nativeSlots[i]));
  2428. }
  2429. return slots;
  2430. }
  2431. /// The skeleton's slots in the order they should be drawn. The returned array may be modified to change the draw order.
  2432. List<Slot> getDrawOrder() {
  2433. final List<Slot> slots = [];
  2434. final numSlots = _bindings.spine_skeleton_get_num_draw_order(_skeleton);
  2435. final nativeDrawOrder = _bindings.spine_skeleton_get_draw_order(_skeleton);
  2436. for (int i = 0; i < numSlots; i++) {
  2437. slots.add(Slot._(nativeDrawOrder[i]));
  2438. }
  2439. return slots;
  2440. }
  2441. /// The skeleton's IK constraints.
  2442. List<IkConstraint> getIkConstraints() {
  2443. final List<IkConstraint> constraints = [];
  2444. final numConstraints = _bindings.spine_skeleton_get_num_ik_constraints(_skeleton);
  2445. final nativeConstraints = _bindings.spine_skeleton_get_ik_constraints(_skeleton);
  2446. for (int i = 0; i < numConstraints; i++) {
  2447. constraints.add(IkConstraint._(nativeConstraints[i]));
  2448. }
  2449. return constraints;
  2450. }
  2451. /// The skeleton's path constraints.
  2452. List<PathConstraint> getPathConstraints() {
  2453. final List<PathConstraint> constraints = [];
  2454. final numConstraints = _bindings.spine_skeleton_get_num_path_constraints(_skeleton);
  2455. final nativeConstraints = _bindings.spine_skeleton_get_path_constraints(_skeleton);
  2456. for (int i = 0; i < numConstraints; i++) {
  2457. constraints.add(PathConstraint._(nativeConstraints[i]));
  2458. }
  2459. return constraints;
  2460. }
  2461. /// The skeleton's transform constraints.
  2462. List<TransformConstraint> getTransformConstraints() {
  2463. final List<TransformConstraint> constraints = [];
  2464. final numConstraints = _bindings.spine_skeleton_get_num_transform_constraints(_skeleton);
  2465. final nativeConstraints = _bindings.spine_skeleton_get_transform_constraints(_skeleton);
  2466. for (int i = 0; i < numConstraints; i++) {
  2467. constraints.add(TransformConstraint._(nativeConstraints[i]));
  2468. }
  2469. return constraints;
  2470. }
  2471. /// The skeleton's current skin.
  2472. Skin? getSkin() {
  2473. final skin = _bindings.spine_skeleton_get_skin(_skeleton);
  2474. if (skin.address == nullptr.address) return null;
  2475. return Skin._(skin);
  2476. }
  2477. /// The color to tint all the skeleton's attachments.
  2478. Color getColor() {
  2479. final color = _bindings.spine_skeleton_get_color(_skeleton);
  2480. return Color(_bindings.spine_color_get_r(color), _bindings.spine_color_get_g(color), _bindings.spine_color_get_b(color),
  2481. _bindings.spine_color_get_a(color));
  2482. }
  2483. void setColor(Color color) {
  2484. _bindings.spine_skeleton_set_color(_skeleton, color.r, color.g, color.b, color.a);
  2485. }
  2486. /// Sets the skeleton X and Y position, which is added to the root bone worldX and worldY position.
  2487. ///
  2488. /// Bones that do not inherit translation are still affected by this property.
  2489. void setPosition(double x, double y) {
  2490. _bindings.spine_skeleton_set_position(_skeleton, x, y);
  2491. }
  2492. Vec2 getPosition() {
  2493. final x = _bindings.spine_skeleton_get_x(_skeleton);
  2494. final y = _bindings.spine_skeleton_get_y(_skeleton);
  2495. return Vec2(x, y);
  2496. }
  2497. /// Sets the skeleton X position, which is added to the root bone worldX position.
  2498. ///
  2499. /// Bones that do not inherit translation are still affected by this property.
  2500. double getX() {
  2501. return _bindings.spine_skeleton_get_x(_skeleton);
  2502. }
  2503. void setX(double x) {
  2504. _bindings.spine_skeleton_set_x(_skeleton, x);
  2505. }
  2506. /// Sets the skeleton Y position, which is added to the root bone worldY position.
  2507. /// <p>
  2508. /// Bones that do not inherit translation are still affected by this property.
  2509. double getY() {
  2510. return _bindings.spine_skeleton_get_y(_skeleton);
  2511. }
  2512. void setY(double y) {
  2513. _bindings.spine_skeleton_set_y(_skeleton, y);
  2514. }
  2515. /// Scales the entire skeleton on the X axis.
  2516. ///
  2517. /// Bones that do not inherit scale are still affected by this property.
  2518. double getScaleX() {
  2519. return _bindings.spine_skeleton_get_scale_x(_skeleton);
  2520. }
  2521. void setScaleX(double scaleX) {
  2522. _bindings.spine_skeleton_set_scale_x(_skeleton, scaleX);
  2523. }
  2524. /// Scales the entire skeleton on the Y axis.
  2525. ///
  2526. /// Bones that do not inherit scale are still affected by this property.
  2527. double getScaleY() {
  2528. return _bindings.spine_skeleton_get_scale_y(_skeleton);
  2529. }
  2530. void setScaleY(double scaleY) {
  2531. _bindings.spine_skeleton_set_scale_y(_skeleton, scaleY);
  2532. }
  2533. double getTime() {
  2534. return _bindings.spine_skeleton_get_time(_skeleton);
  2535. }
  2536. void setTime(double time) {
  2537. return _bindings.spine_skeleton_set_time(_skeleton, time);
  2538. }
  2539. void update(double delta) {
  2540. _bindings.spine_skeleton_update(_skeleton, delta);
  2541. }
  2542. }
  2543. /// Stores a list of timelines to animate a skeleton's pose over time.
  2544. class Animation {
  2545. final spine_animation _animation;
  2546. Animation._(this._animation);
  2547. /// The animation's name, which is unique across all animations in the skeleton.
  2548. String getName() {
  2549. final Pointer<Utf8> value = _bindings.spine_animation_get_name(_animation).cast();
  2550. return value.toDartString();
  2551. }
  2552. /// The duration of the animation in seconds, which is usually the highest time of all frames in the timeline. The duration is
  2553. /// used to know when it has completed and when it should loop back to the start.
  2554. double getDuration() {
  2555. return _bindings.spine_animation_get_duration(_animation);
  2556. }
  2557. }
  2558. /// Controls how timeline values are mixed with setup pose values or current pose values when a timeline is applied with
  2559. /// <code>alpha</code> < 1.
  2560. enum MixBlend {
  2561. /// Transitions from the setup value to the timeline value (the current value is not used). Before the first frame, the
  2562. /// setup value is set.
  2563. setup(0),
  2564. /// Transitions from the current value to the timeline value. Before the first frame, transitions from the current value to
  2565. /// the setup value. Timelines which perform instant transitions, such as {@link DrawOrderTimeline} or
  2566. /// {link AttachmentTimeline}, use the setup value before the first frame.
  2567. /// <p>
  2568. /// <code>first</code> is intended for the first animations applied, not for animations layered on top of those.
  2569. first(1),
  2570. /// Transitions from the current value to the timeline value. No change is made before the first frame (the current value is
  2571. /// kept until the first frame).
  2572. /// <p>
  2573. /// <code>replace</code> is intended for animations layered on top of others, not for the first animations applied.
  2574. replace(2),
  2575. /// Transitions from the current value to the current value plus the timeline value. No change is made before the first
  2576. /// frame (the current value is kept until the first frame).
  2577. /// <p>
  2578. /// <code>add</code> is intended for animations layered on top of others, not for the first animations applied. Properties
  2579. /// set by additive animations must be set manually or by another animation before applying the additive animations, else the
  2580. /// property values will increase each time the additive animations are applied.
  2581. add(3);
  2582. final int value;
  2583. const MixBlend(this.value);
  2584. }
  2585. /// Stores settings and other state for the playback of an animation on an [AnimationState] track.
  2586. ///
  2587. /// References to a track entry must not be kept after the dispose [EventType] is reported to [AnimationStateListener].
  2588. class TrackEntry {
  2589. final spine_track_entry _entry;
  2590. final AnimationState _state;
  2591. TrackEntry._(this._entry, this._state);
  2592. /// The index of the track where this track entry is either current or queued.
  2593. ///
  2594. /// See [AnimationState.getCurrent].
  2595. int getTtrackIndex() {
  2596. return _bindings.spine_track_entry_get_track_index(_entry);
  2597. }
  2598. /// The animation to apply for this track entry.
  2599. Animation getAnimation() {
  2600. return Animation._(_bindings.spine_track_entry_get_animation(_entry));
  2601. }
  2602. /// If true, the animation will repeat. If false it will not, instead its last frame is applied if played beyond its
  2603. /// duration.
  2604. bool getLoop() {
  2605. return _bindings.spine_track_entry_get_loop(_entry) == -1;
  2606. }
  2607. void setLoop(bool loop) {
  2608. _bindings.spine_track_entry_set_loop(_entry, loop ? -1 : 0);
  2609. }
  2610. /// Seconds to postpone playing the animation. When this track entry is the current track entry, <code>delay</code>
  2611. /// postpones incrementing the [getTrackTime]. When this track entry is queued, <code>delay</code> is the time from
  2612. /// the start of the previous animation to when this track entry will become the current track entry (ie when the previous
  2613. /// track entry [getTrackTime] >= this track entry's <code>delay</code>).
  2614. ///
  2615. /// [getTimeScale] affects the delay.
  2616. ///
  2617. /// When using [AnimationState.addAnimation] with a <code>delay</code> <= 0, the delay
  2618. /// is set using the mix duration from the [AnimationStateData]. If [getMixDuration] is set afterward, the delay
  2619. /// may need to be adjusted.
  2620. bool getHoldPrevious() {
  2621. return _bindings.spine_track_entry_get_hold_previous(_entry) == -1;
  2622. }
  2623. void setHoldPrevious(bool holdPrevious) {
  2624. _bindings.spine_track_entry_set_hold_previous(_entry, holdPrevious ? -1 : 0);
  2625. }
  2626. /// If true, the animation will be applied in reverse. Events are not fired when an animation is applied in reverse.
  2627. bool getReverse() {
  2628. return _bindings.spine_track_entry_get_reverse(_entry) == -1;
  2629. }
  2630. void setReverse(bool reverse) {
  2631. _bindings.spine_track_entry_set_reverse(_entry, reverse ? -1 : 0);
  2632. }
  2633. /// If true, mixing rotation between tracks always uses the shortest rotation direction. If the rotation is animated, the
  2634. /// shortest rotation direction may change during the mix.
  2635. ///
  2636. /// If false, the shortest rotation direction is remembered when the mix starts and the same direction is used for the rest
  2637. /// of the mix. Defaults to false.
  2638. bool getShortestRotation() {
  2639. return _bindings.spine_track_entry_get_shortest_rotation(_entry) == 1;
  2640. }
  2641. void setShortestRotation(bool shortestRotation) {
  2642. _bindings.spine_track_entry_set_shortest_rotation(_entry, shortestRotation ? -1 : 0);
  2643. }
  2644. /// Seconds to postpone playing the animation. When this track entry is the current track entry, <code>delay</code>
  2645. /// postpones incrementing the [getTrackTime]. When this track entry is queued, <code>delay</code> is the time from
  2646. /// the start of the previous animation to when this track entry will become the current track entry (ie when the previous
  2647. /// track entry [getTrackTime] >= this track entry's <code>delay</code>).
  2648. ///
  2649. /// [getTimeScale] affects the delay.
  2650. ///
  2651. /// When using [AnimationState.addAnimation] with a <code>delay</code> <= 0, the delay
  2652. /// is set using the mix duration from the [AnimationStateData]. If [getMixDuration] is set afterward, the delay
  2653. /// may need to be adjusted.
  2654. double getDelay() {
  2655. return _bindings.spine_track_entry_get_delay(_entry);
  2656. }
  2657. void setDelay(double delay) {
  2658. _bindings.spine_track_entry_set_delay(_entry, delay);
  2659. }
  2660. /// Current time in seconds this track entry has been the current track entry. The track time determines
  2661. /// [getAnimationTime]. The track time can be set to start the animation at a time other than 0, without affecting
  2662. /// looping.
  2663. double getTrackTime() {
  2664. return _bindings.spine_track_entry_get_track_time(_entry);
  2665. }
  2666. void setTrackTime(double trackTime) {
  2667. _bindings.spine_track_entry_set_track_time(_entry, trackTime);
  2668. }
  2669. /// The track time in seconds when this animation will be removed from the track. Defaults to the highest possible float
  2670. /// value, meaning the animation will be applied until a new animation is set or the track is cleared. If the track end time
  2671. /// is reached, no other animations are queued for playback, and mixing from any previous animations is complete, then the
  2672. /// properties keyed by the animation are set to the setup pose and the track is cleared.
  2673. ///
  2674. /// It may be desired to use [AnimationState.addEmptyAnimation] rather than have the animation
  2675. /// abruptly cease being applied.
  2676. double getTrackEnd() {
  2677. return _bindings.spine_track_entry_get_track_end(_entry);
  2678. }
  2679. void setTrackEnd(double trackEnd) {
  2680. _bindings.spine_track_entry_set_track_end(_entry, trackEnd);
  2681. }
  2682. /// Seconds when this animation starts, both initially and after looping. Defaults to 0.
  2683. ///
  2684. /// When changing the <code>animationStart</code> time, it often makes sense to set [getAnimationLast] to the same
  2685. /// value to prevent timeline keys before the start time from triggering.
  2686. double getAnimationStart() {
  2687. return _bindings.spine_track_entry_get_animation_start(_entry);
  2688. }
  2689. void setAnimationStart(double animationStart) {
  2690. _bindings.spine_track_entry_set_animation_start(_entry, animationStart);
  2691. }
  2692. /// Seconds for the last frame of this animation. Non-looping animations won't play past this time. Looping animations will
  2693. /// loop back to [getAnimationStart] at this time. Defaults to the animation [Animation.getDuration].
  2694. double getAnimationEnd() {
  2695. return _bindings.spine_track_entry_get_animation_end(_entry);
  2696. }
  2697. void setAnimationEnd(double animationEnd) {
  2698. _bindings.spine_track_entry_set_animation_end(_entry, animationEnd);
  2699. }
  2700. /// The time in seconds this animation was last applied. Some timelines use this for one-time triggers. Eg, when this
  2701. /// animation is applied, event timelines will fire all events between the <code>animationLast</code> time (exclusive) and
  2702. /// <code>animationTime</code> (inclusive). Defaults to -1 to ensure triggers on frame 0 happen the first time this animation
  2703. /// is applied.
  2704. double getAnimationLast() {
  2705. return _bindings.spine_track_entry_get_animation_last(_entry);
  2706. }
  2707. void setAnimationLast(double animationLast) {
  2708. _bindings.spine_track_entry_set_animation_last(_entry, animationLast);
  2709. }
  2710. /// Uses [getTrackTime] to compute the <code>animationTime</code>. When the <code>trackTime</code> is 0, the
  2711. /// <code>animationTime</code> is equal to the <code>animationStart</code> time.
  2712. /// <p>
  2713. /// The <code>animationTime</code> is between [getAnimationStart] and [getAnimationEnd], except if this
  2714. /// track entry is non-looping and [getAnimationEnd] is >= to the animation [Animation.getDuration], then
  2715. /// <code>animationTime</code> continues to increase past [getAnimationEnd].
  2716. double getAnimationTime() {
  2717. return _bindings.spine_track_entry_get_animation_time(_entry);
  2718. }
  2719. /// Multiplier for the delta time when this track entry is updated, causing time for this animation to pass slower or
  2720. /// faster. Defaults to 1.
  2721. ///
  2722. /// Values < 0 are not supported. To play an animation in reverse, use [getReverse].
  2723. ///
  2724. /// [getMixTime] is not affected by track entry time scale, so [getMixDuration] may need to be adjusted to
  2725. /// match the animation speed.
  2726. ///
  2727. /// When using [AnimationState.addAnimation] with a <code>delay</code> <= 0, the
  2728. /// [getDelay] is set using the mix duration from the [AnimationStateData], assuming time scale to be 1. If
  2729. /// the time scale is not 1, the delay may need to be adjusted.
  2730. ///
  2731. /// See [AnimationState.getTimeScale] for affecting all animations.
  2732. double getTimeScale() {
  2733. return _bindings.spine_track_entry_get_time_scale(_entry);
  2734. }
  2735. void setTimeScale(double timeScale) {
  2736. _bindings.spine_track_entry_set_time_scale(_entry, timeScale);
  2737. }
  2738. /// Values < 1 mix this animation with the skeleton's current pose (usually the pose resulting from lower tracks). Defaults
  2739. /// to 1, which overwrites the skeleton's current pose with this animation.
  2740. ///
  2741. /// Typically track 0 is used to completely pose the skeleton, then alpha is used on higher tracks. It doesn't make sense to
  2742. /// use alpha on track 0 if the skeleton pose is from the last frame render.
  2743. Future<double> getAlpha() async {
  2744. return _bindings.spine_track_entry_get_alpha(_entry);
  2745. }
  2746. void setAlpha(double alpha) {
  2747. _bindings.spine_track_entry_set_alpha(_entry, alpha);
  2748. }
  2749. /// When the mix percentage ([getMixTime] / [getMixDuration]) is less than the
  2750. /// <code>eventThreshold</code>, event timelines are applied while this animation is being mixed out. Defaults to 0, so event
  2751. /// timelines are not applied while this animation is being mixed out.
  2752. double getEventThreshold() {
  2753. return _bindings.spine_track_entry_get_event_threshold(_entry);
  2754. }
  2755. void setEventThreshold(double eventThreshold) {
  2756. _bindings.spine_track_entry_set_event_threshold(_entry, eventThreshold);
  2757. }
  2758. /// Values less than 1 mix this animation with the last skeleton pose. Defaults to 1, which overwrites the last skeleton pose with
  2759. /// this animation.
  2760. ///
  2761. /// Typically track 0 is used to completely pose the skeleton, then alpha can be used on higher tracks. It doesn't make sense
  2762. /// to use alpha on track 0 if the skeleton pose is from the last frame render.
  2763. double getAlphaAttachmentThreshold() {
  2764. return _bindings.spine_track_entry_get_alpha_attachment_threshold(_entry);
  2765. }
  2766. void setAlphaAttachmentThreshold(double attachmentThreshold) {
  2767. _bindings.spine_track_entry_set_alpha_attachment_threshold(_entry, attachmentThreshold);
  2768. }
  2769. /// When the mix percentage ([getMixTime] / [getMixDuration]) is less than the
  2770. /// <code>attachmentThreshold</code>, attachment timelines are applied while this animation is being mixed out. Defaults to
  2771. /// 0, so attachment timelines are not applied while this animation is being mixed out.
  2772. double getMixAttachmentThreshold() {
  2773. return _bindings.spine_track_entry_get_mix_attachment_threshold(_entry);
  2774. }
  2775. void setMixAttachmentThreshold(double attachmentThreshold) {
  2776. _bindings.spine_track_entry_set_mix_attachment_threshold(_entry, attachmentThreshold);
  2777. }
  2778. /// When the mix percentage ([getMixTime] / [getMixDuration]) is less than the
  2779. /// <code>drawOrderThreshold</code>, draw order timelines are applied while this animation is being mixed out. Defaults to 0,
  2780. /// so draw order timelines are not applied while this animation is being mixed out.
  2781. double getMixDrawOrderThreshold() {
  2782. return _bindings.spine_track_entry_get_mix_draw_order_threshold(_entry);
  2783. }
  2784. void setMixDrawOrderThreshold(double drawOrderThreshold) {
  2785. _bindings.spine_track_entry_set_mix_draw_order_threshold(_entry, drawOrderThreshold);
  2786. }
  2787. /// The animation queued to start after this animation, or null if there is none. <code>next</code> makes up a doubly linked
  2788. /// list.
  2789. ///
  2790. /// See [AnimationState.clearNext] to truncate the list.
  2791. TrackEntry? getNext() {
  2792. final next = _bindings.spine_track_entry_get_next(_entry);
  2793. if (next.address == nullptr.address) return null;
  2794. return TrackEntry._(next, _state);
  2795. }
  2796. /// Returns true if at least one loop has been completed.
  2797. bool isComplete() {
  2798. return _bindings.spine_track_entry_is_complete(_entry) == -1;
  2799. }
  2800. /// Seconds from 0 to the [getMixDuration] when mixing from the previous animation to this animation. May be
  2801. /// slightly more than <code>mixDuration</code> when the mix is complete.
  2802. double getMixTime() {
  2803. return _bindings.spine_track_entry_get_mix_time(_entry);
  2804. }
  2805. void setMixTime(double mixTime) {
  2806. _bindings.spine_track_entry_set_mix_time(_entry, mixTime);
  2807. }
  2808. /// Seconds for mixing from the previous animation to this animation. Defaults to the value provided by
  2809. /// [AnimationStateData.getMix] based on the animation before this animation (if any).
  2810. ///
  2811. /// A mix duration of 0 still mixes out over one frame to provide the track entry being mixed out a chance to revert the
  2812. /// properties it was animating. A mix duration of 0 can be set at any time to end the mix on the next
  2813. /// [AnimationState.update].
  2814. ///
  2815. /// The <code>mixDuration</code> can be set manually rather than use the value from
  2816. /// [AnimationStateData.getMix]. In that case, the <code>mixDuration</code> can be set for a new
  2817. /// track entry only before [AnimationState.update] is first called.
  2818. /// <p>
  2819. /// When using [AnimationState.addAnimation] with a <code>delay</code> <= 0, the
  2820. /// [getDelay] is set using the mix duration from the [AnimationStateData]. If <code>mixDuration</code> is set
  2821. /// afterward, the delay may need to be adjusted. For example:
  2822. /// <code>entry.delay = entry.previous.getTrackComplete() - entry.mixDuration;</code>
  2823. double getMixDuration() {
  2824. return _bindings.spine_track_entry_get_mix_duration(_entry);
  2825. }
  2826. void setMixDuration(double mixDuration) {
  2827. _bindings.spine_track_entry_set_mix_duration(_entry, mixDuration);
  2828. }
  2829. /// Controls how properties keyed in the animation are mixed with lower tracks. Defaults to [MixBlend.replace].
  2830. ///
  2831. /// Track entries on track 0 ignore this setting and always use {@link MixBlend#first}.
  2832. ///
  2833. /// The <code>mixBlend</code> can be set for a new track entry only before [AnimationState.apply] is first
  2834. /// called.
  2835. MixBlend getMixBlend() {
  2836. return MixBlend.values[_bindings.spine_track_entry_get_mix_blend(_entry)];
  2837. }
  2838. void setMixBlend(MixBlend mixBlend) {
  2839. _bindings.spine_track_entry_set_mix_blend(_entry, mixBlend.value);
  2840. }
  2841. /// The track entry for the previous animation when mixing from the previous animation to this animation, or null if no
  2842. /// mixing is currently occurring. When mixing from multiple animations, <code>mixingFrom</code> makes up a linked list.
  2843. TrackEntry? getMixingFrom() {
  2844. final from = _bindings.spine_track_entry_get_mixing_from(_entry);
  2845. if (from.address == nullptr.address) return null;
  2846. return TrackEntry._(from, _state);
  2847. }
  2848. /// The track entry for the next animation when mixing from this animation to the next animation, or null if no mixing is
  2849. /// currently occurring. When mixing to multiple animations, <code>mixingTo</code> makes up a linked list.
  2850. TrackEntry? getMixingTo() {
  2851. final to = _bindings.spine_track_entry_get_mixing_to(_entry);
  2852. if (to.address == nullptr.address) return null;
  2853. return TrackEntry._(to, _state);
  2854. }
  2855. /// Resets the rotation directions for mixing this entry's rotate timelines. This can be useful to avoid bones rotating the
  2856. /// long way around when using alpha and starting animations on other tracks.
  2857. ///
  2858. /// Mixing involves finding a rotation between two others, which has two possible solutions: the short way or the long way around.
  2859. /// The two rotations likely change over time, so which direction is the short or long way also changes.
  2860. /// If the short way was always chosen, bones would flip to the other side when that direction became the long way.
  2861. /// TrackEntry chooses the short way the first time it is applied and remembers that direction.
  2862. void resetRotationDirections() {
  2863. _bindings.spine_track_entry_reset_rotation_directions(_entry);
  2864. }
  2865. /// If this track entry is non-looping, the track time in seconds when [getAnimationEnd] is reached, or the current
  2866. /// [getTrackTime] if it has already been reached. If this track entry is looping, the track time when this
  2867. /// animation will reach its next [getAnimationEnd] (the next loop completion).
  2868. double getTrackComplete() {
  2869. return _bindings.spine_track_entry_get_track_complete(_entry);
  2870. }
  2871. bool wasApplied() {
  2872. return _bindings.spine_track_entry_was_applied(_entry) == -1;
  2873. }
  2874. bool isNextReady() {
  2875. return _bindings.spine_track_entry_is_next_ready(_entry) == -1;
  2876. }
  2877. /// The listener for events generated by this track entry, or null.
  2878. ///
  2879. /// A track entry returned from [AnimationState.setAnimation] is already the current animation
  2880. /// for the track, so the track entry listener will not be called for [EventType.start].
  2881. void setListener(AnimationStateListener? listener) {
  2882. _state._setTrackEntryListener(_entry, listener);
  2883. }
  2884. }
  2885. /// The event type passed to [AnimationStateListener]
  2886. enum EventType {
  2887. /// Emitted when [TrackEntry] has been set as the current entry. [EventType.end] will occur when this entry will no
  2888. /// longer be applied.
  2889. start,
  2890. /// Emitted when another entry has replaced the current entry. This entry may continue being applied for
  2891. /// mixing.
  2892. interrupt,
  2893. /// Emitted when this entry will never be applied again. This only occurs if this entry has previously been set as the
  2894. /// current entry ([EventType.start] was emitted).
  2895. end,
  2896. /// Emitted every time the current entry's animation completes a loop. This may occur during mixing (after
  2897. /// [EventType.interrupted] is emitted).
  2898. ///
  2899. /// If [TrackEntry.getMixingTo] of the entry reported by the event is not null, the entry is mixing out (it is not the current entry).
  2900. ///
  2901. /// Because this event is triggered at the end of [AnimationState.apply], any animations set in response to
  2902. /// the event won't be applied until the next time the [AnimationState] is applied.
  2903. complete,
  2904. /// Emitted when this entry will be disposed. This may occur without the entry ever being set as the current entry.
  2905. ///
  2906. /// References to the entry should not be kept after <code>dispose</code> is called, as it may be destroyed or reused.
  2907. dispose,
  2908. /// Invoked when the current entry's animation triggers an event. This may occur during mixing (after
  2909. /// [EventType.interrupt] is emitted), see [TrackEntry.getEventThreshold].
  2910. ///
  2911. /// Because this event is triggered at the end of [AnimationState.apply], any animations set in response to
  2912. /// the event won't be applied until the next time the [AnimationState] is applied.
  2913. event
  2914. }
  2915. /// Stores the setup pose values for an [Event].
  2916. ///
  2917. /// See <a href="http://esotericsoftware.com/spine-events">Events</a> in the Spine User Guide.
  2918. class EventData {
  2919. final spine_event_data _data;
  2920. EventData._(this._data);
  2921. /// The name of the event, which is unique across all events in the skeleton.
  2922. String getName() {
  2923. final Pointer<Utf8> value = _bindings.spine_event_data_get_name(_data).cast();
  2924. return value.toDartString();
  2925. }
  2926. int getIntValue() {
  2927. return _bindings.spine_event_data_get_int_value(_data);
  2928. }
  2929. void setIntValue(int value) {
  2930. _bindings.spine_event_data_set_int_value(_data, value);
  2931. }
  2932. double getFloatValue() {
  2933. return _bindings.spine_event_data_get_float_value(_data);
  2934. }
  2935. void setFloatValue(double value) {
  2936. _bindings.spine_event_data_set_float_value(_data, value);
  2937. }
  2938. String getStringValue() {
  2939. final Pointer<Utf8> value = _bindings.spine_event_data_get_string_value(_data).cast();
  2940. return value.toDartString();
  2941. }
  2942. void setStringValue(String value) {
  2943. final nativeString = value.toNativeUtf8(allocator: _allocator);
  2944. _bindings.spine_event_data_set_string_value(_data, nativeString.cast());
  2945. _allocator.free(nativeString);
  2946. }
  2947. String getAudioPath() {
  2948. final Pointer<Utf8> value = _bindings.spine_event_data_get_audio_path(_data).cast();
  2949. return value.toDartString();
  2950. }
  2951. double getVolume() {
  2952. return _bindings.spine_event_data_get_volume(_data);
  2953. }
  2954. void setVolume(double volume) {
  2955. _bindings.spine_event_data_set_volume(_data, volume);
  2956. }
  2957. double getBalance() {
  2958. return _bindings.spine_event_data_get_balance(_data);
  2959. }
  2960. void setBalance(double value) {
  2961. _bindings.spine_event_data_set_balance(_data, value);
  2962. }
  2963. }
  2964. /// Stores the current pose values for an {@link Event}.
  2965. ///
  2966. /// See [AnimationStateListener], [EventType.event], and
  2967. /// <a href="http://esotericsoftware.com/spine-events">Events</a> in the Spine User Guide.
  2968. class Event {
  2969. final spine_event _event;
  2970. Event._(this._event);
  2971. /// The events's setup pose data.
  2972. EventData getData() {
  2973. return EventData._(_bindings.spine_event_get_data(_event));
  2974. }
  2975. /// The animation time this event was keyed.
  2976. double getTime() {
  2977. return _bindings.spine_event_get_time(_event);
  2978. }
  2979. int getIntValue() {
  2980. return _bindings.spine_event_get_int_value(_event);
  2981. }
  2982. void setIntValue(int value) {
  2983. _bindings.spine_event_set_int_value(_event, value);
  2984. }
  2985. double getFloatValue() {
  2986. return _bindings.spine_event_get_float_value(_event);
  2987. }
  2988. void setFloatValue(double value) {
  2989. _bindings.spine_event_set_float_value(_event, value);
  2990. }
  2991. String getStringValue() {
  2992. final Pointer<Utf8> value = _bindings.spine_event_get_string_value(_event).cast();
  2993. return value.toDartString();
  2994. }
  2995. void setStringValue(String value) {
  2996. final nativeString = value.toNativeUtf8(allocator: _allocator);
  2997. _bindings.spine_event_set_string_value(_event, nativeString.cast());
  2998. _allocator.free(nativeString);
  2999. }
  3000. double getVolume() {
  3001. return _bindings.spine_event_get_volume(_event);
  3002. }
  3003. void setVolume(double volume) {
  3004. _bindings.spine_event_set_volume(_event, volume);
  3005. }
  3006. double getBalance() {
  3007. return _bindings.spine_event_get_balance(_event);
  3008. }
  3009. void setBalance(double balance) {
  3010. _bindings.spine_event_set_balance(_event, balance);
  3011. }
  3012. }
  3013. /// The callback to implement for receiving [TrackEntry] events. It is always safe to call [AnimationState] methods when receiving
  3014. /// events.
  3015. ///
  3016. /// TrackEntry events are collected during [AnimationState.update] and [AnimationState.apply] and
  3017. /// fired only after those methods are finished.
  3018. ///
  3019. /// See [TrackEntry.setListener] and [AnimationState.setListener].
  3020. typedef AnimationStateListener = void Function(EventType type, TrackEntry entry, Event? event);
  3021. /// Stores mix (crossfade) durations to be applied when {@link AnimationState} animations are changed.
  3022. class AnimationStateData {
  3023. final spine_animation_state_data _data;
  3024. AnimationStateData._(this._data);
  3025. /// The SkeletonData to look up animations when they are specified by name.
  3026. SkeletonData getSkeletonData() {
  3027. return SkeletonData._(_bindings.spine_animation_state_data_get_skeleton_data(_data));
  3028. }
  3029. double getDefaultMix() {
  3030. return _bindings.spine_animation_state_data_get_default_mix(_data);
  3031. }
  3032. void setDefaultMix(double defaultMix) {
  3033. _bindings.spine_animation_state_data_set_default_mix(_data, defaultMix);
  3034. }
  3035. /// Sets a mix duration by animation name.
  3036. ///
  3037. /// See [setMix].
  3038. void setMixByName(String fromName, String toName, double duration) {
  3039. final fromNative = fromName.toNativeUtf8(allocator: _allocator);
  3040. final toNative = toName.toNativeUtf8(allocator: _allocator);
  3041. _bindings.spine_animation_state_data_set_mix_by_name(_data, fromNative.cast(), toNative.cast(), duration);
  3042. _allocator.free(fromNative);
  3043. _allocator.free(toNative);
  3044. }
  3045. /// Returns the mix duration to use when changing from the specified animation to the other, or the [getDefaultMix] if
  3046. /// no mix duration has been set.
  3047. double getMixByName(String fromName, String toName) {
  3048. final fromNative = fromName.toNativeUtf8(allocator: _allocator);
  3049. final toNative = toName.toNativeUtf8(allocator: _allocator);
  3050. final duration = _bindings.spine_animation_state_data_get_mix_by_name(_data, fromNative.cast(), toNative.cast());
  3051. _allocator.free(fromNative);
  3052. _allocator.free(toNative);
  3053. return duration;
  3054. }
  3055. /// Sets the mix duration when changing from the specified animation to the other.
  3056. ///
  3057. /// See [TrackEntry.mixDuration].
  3058. Future<void> setMix(Animation from, Animation to, double duration) async {
  3059. _bindings.spine_animation_state_data_set_mix(_data, from._animation, to._animation, duration);
  3060. }
  3061. /// Returns the mix duration to use when changing from the specified animation to the other, or the [getDefaultMix] if
  3062. /// no mix duration has been set.
  3063. double getMix(Animation from, Animation to) {
  3064. return _bindings.spine_animation_state_data_get_mix(_data, from._animation, to._animation);
  3065. }
  3066. /// Removes all mix durations.
  3067. void clear() {
  3068. _bindings.spine_animation_state_data_clear(_data);
  3069. }
  3070. }
  3071. /// Applies animations over time, queues animations for later playback, mixes (crossfading) between animations, and applies
  3072. /// multiple animations on top of each other (layering).
  3073. ///
  3074. /// See <a href='http://esotericsoftware.com/spine-applying-animations/'>Applying Animations</a> in the Spine Runtimes Guide.
  3075. class AnimationState {
  3076. final spine_animation_state _state;
  3077. final spine_animation_state_events _events;
  3078. final Map<spine_track_entry, AnimationStateListener> _trackEntryListeners;
  3079. AnimationStateListener? _stateListener;
  3080. AnimationState._(this._state, this._events) : _trackEntryListeners = {};
  3081. void _setTrackEntryListener(spine_track_entry entry, AnimationStateListener? listener) {
  3082. if (listener == null) {
  3083. _trackEntryListeners.remove(entry);
  3084. } else {
  3085. _trackEntryListeners[entry] = listener;
  3086. }
  3087. }
  3088. /// Increments each track entry [TrackEntry.getTrackTime], setting queued animations as current if needed.
  3089. void update(double delta) {
  3090. _bindings.spine_animation_state_update(_state, delta);
  3091. final numEvents = _bindings.spine_animation_state_events_get_num_events(_events);
  3092. if (numEvents > 0) {
  3093. for (int i = 0; i < numEvents; i++) {
  3094. late final EventType type;
  3095. switch (_bindings.spine_animation_state_events_get_event_type(_events, i)) {
  3096. case 0:
  3097. type = EventType.start;
  3098. break;
  3099. case 1:
  3100. type = EventType.interrupt;
  3101. break;
  3102. case 2:
  3103. type = EventType.end;
  3104. break;
  3105. case 3:
  3106. type = EventType.complete;
  3107. break;
  3108. case 4:
  3109. type = EventType.dispose;
  3110. break;
  3111. case 5:
  3112. type = EventType.event;
  3113. break;
  3114. }
  3115. final nativeEntry = _bindings.spine_animation_state_events_get_track_entry(_events, i);
  3116. final entry = TrackEntry._(nativeEntry, this);
  3117. final nativeEvent = _bindings.spine_animation_state_events_get_event(_events, i);
  3118. final event = nativeEvent.address == nullptr.address ? null : Event._(nativeEvent);
  3119. if (_trackEntryListeners.containsKey(nativeEntry)) {
  3120. _trackEntryListeners[nativeEntry]?.call(type, entry, event);
  3121. }
  3122. if (_stateListener != null) {
  3123. _stateListener?.call(type, entry, event);
  3124. }
  3125. if (type == EventType.dispose) {
  3126. _bindings.spine_animation_state_dispose_track_entry(_state, nativeEntry);
  3127. }
  3128. }
  3129. }
  3130. _bindings.spine_animation_state_events_reset(_events);
  3131. }
  3132. /// Poses the skeleton using the track entry animations. The animation state is not changed, so can be applied to multiple
  3133. /// skeletons to pose them identically.
  3134. ///
  3135. /// Returns true if any animations were applied.
  3136. void apply(Skeleton skeleton) {
  3137. _bindings.spine_animation_state_apply(_state, skeleton._skeleton);
  3138. }
  3139. /// Removes all animations from all tracks, leaving skeletons in their current pose.
  3140. ///
  3141. /// It may be desired to use [setEmptyAnimations] to mix the skeletons back to the setup pose,
  3142. /// rather than leaving them in their current pose.
  3143. void clearTracks() {
  3144. _bindings.spine_animation_state_clear_tracks(_state);
  3145. }
  3146. /// Removes all animations from the track, leaving skeletons in their current pose.
  3147. ///
  3148. /// It may be desired to use [setEmptyAnimations] to mix the skeletons back to the setup pose,
  3149. /// rather than leaving them in their current pose.
  3150. void clearTrack(int trackIndex) {
  3151. _bindings.spine_animation_state_clear_track(_state, trackIndex);
  3152. }
  3153. /// Sets an animation by name.
  3154. ///
  3155. /// See [setAnimation].
  3156. TrackEntry setAnimationByName(int trackIndex, String animationName, bool loop) {
  3157. final animation = animationName.toNativeUtf8(allocator: _allocator);
  3158. final entry = _bindings.spine_animation_state_set_animation_by_name(_state, trackIndex, animation.cast(), loop ? -1 : 0);
  3159. _allocator.free(animation);
  3160. if (entry.address == nullptr.address) throw Exception("Couldn't set animation $animationName");
  3161. return TrackEntry._(entry, this);
  3162. }
  3163. /// Sets the current [animation] for a track at [trackIndex], discarding any queued animations. If the formerly current track entry was never
  3164. /// applied to a skeleton, it is replaced (not mixed from).
  3165. ///
  3166. /// If [loop] is true, the animation will repeat. If false it will not, instead its last frame is applied if played beyond its
  3167. /// duration. In either case [TrackEntry.getTrackEnd] determines when the track is cleared.
  3168. ///
  3169. /// Returns a track entry to allow further customization of animation playback. References to the track entry must not be kept
  3170. /// after the [EventType.dispose] event occurs.
  3171. TrackEntry setAnimation(int trackIndex, Animation animation, bool loop) {
  3172. final entry = _bindings.spine_animation_state_set_animation(_state, trackIndex, animation._animation, loop ? -1 : 0);
  3173. if (entry.address == nullptr.address) throw Exception("Couldn't set animation ${animation.getName()}");
  3174. return TrackEntry._(entry, this);
  3175. }
  3176. /// Queues an animation by name.
  3177. ///
  3178. /// See [addAnimation].
  3179. TrackEntry addAnimationByName(int trackIndex, String animationName, bool loop, double delay) {
  3180. final animation = animationName.toNativeUtf8(allocator: _allocator);
  3181. final entry = _bindings.spine_animation_state_add_animation_by_name(_state, trackIndex, animation.cast(), loop ? -1 : 0, delay);
  3182. _allocator.free(animation);
  3183. if (entry.address == nullptr.address) throw Exception("Couldn't add animation $animationName");
  3184. return TrackEntry._(entry, this);
  3185. }
  3186. /// Adds an [animation] to be played after the current or last queued animation for a track at [trackIndex]. If the track is empty, it is
  3187. /// equivalent to calling [setAnimation].
  3188. ///
  3189. /// If [delay] > 0, sets [TrackEntry.getDelay]. If [delay] <= 0, the delay set is the duration of the previous track entry
  3190. /// minus any mix duration (from the [AnimationStateData]) plus the specified <code>delay</code> (ie the mix
  3191. /// ends at (<code>delay</code> = 0) or before (<code>delay</code> < 0) the previous track entry duration). If the
  3192. /// previous entry is looping, its next loop completion is used instead of its duration.
  3193. ///
  3194. /// Returns a track entry to allow further customization of animation playback. References to the track entry must not be kept
  3195. /// after the [EventType.dispose] event occurs.
  3196. TrackEntry addAnimation(int trackIndex, Animation animation, bool loop, double delay) {
  3197. final entry = _bindings.spine_animation_state_add_animation(_state, trackIndex, animation._animation, loop ? -1 : 0, delay);
  3198. if (entry.address == nullptr.address) throw Exception("Couldn't add animation ${animation.getName()}");
  3199. return TrackEntry._(entry, this);
  3200. }
  3201. /// Sets an empty animation for a track at [trackIndex], discarding any queued animations, and sets the track entry's
  3202. /// [TrackEntry.getMixDuration] to [mixDuration]. An empty animation has no timelines and serves as a placeholder for mixing in or out.
  3203. ///
  3204. /// Mixing out is done by setting an empty animation with a mix duration using either [setEmptyAnimation],
  3205. /// [setEmptyAnimations], or [addEmptyAnimation]. Mixing to an empty animation causes
  3206. /// the previous animation to be applied less and less over the mix duration. Properties keyed in the previous animation
  3207. /// transition to the value from lower tracks or to the setup pose value if no lower tracks key the property. A mix duration of
  3208. /// 0 still mixes out over one frame.
  3209. ///
  3210. /// Mixing in is done by first setting an empty animation, then adding an animation using
  3211. /// [addAnimation] with the desired delay (an empty animation has a duration of 0) and on
  3212. /// the returned track entry, set the [TrackEntry.setMixDuration]. Mixing from an empty animation causes the new
  3213. /// animation to be applied more and more over the mix duration. Properties keyed in the new animation transition from the value
  3214. /// from lower tracks or from the setup pose value if no lower tracks key the property to the value keyed in the new
  3215. /// animation.
  3216. TrackEntry setEmptyAnimation(int trackIndex, double mixDuration) {
  3217. final entry = _bindings.spine_animation_state_set_empty_animation(_state, trackIndex, mixDuration);
  3218. return TrackEntry._(entry, this);
  3219. }
  3220. /// Adds an empty animation to be played after the current or last queued animation for a track, and sets the track entry's
  3221. /// [TrackEntry.getMixDuration]. If the track is empty, it is equivalent to calling
  3222. /// [setEmptyAnimation].
  3223. ///
  3224. /// See [setEmptyAnimation].
  3225. ///
  3226. /// If [delay] > 0, sets [TrackEntry.getDelay]. If <= 0, the delay set is the duration of the previous track entry
  3227. /// minus any mix duration plus the specified <code>delay</code> (ie the mix ends at (<code>delay</code> = 0) or
  3228. /// before (<code>delay</code> < 0) the previous track entry duration). If the previous entry is looping, its next
  3229. /// loop completion is used instead of its duration.
  3230. ///
  3231. /// Returns a track entry to allow further customization of animation playback. References to the track entry must not be kept
  3232. /// after the [EventType.dispose] event occurs.
  3233. TrackEntry addEmptyAnimation(int trackIndex, double mixDuration, double delay) {
  3234. final entry = _bindings.spine_animation_state_add_empty_animation(_state, trackIndex, mixDuration, delay);
  3235. return TrackEntry._(entry, this);
  3236. }
  3237. /// Returns the track entry for the animation currently playing on the track, or null if no animation is currently playing.
  3238. TrackEntry? getCurrent(int trackIndex) {
  3239. final entry = _bindings.spine_animation_state_get_current(_state, trackIndex);
  3240. if (entry.address == nullptr.address) return null;
  3241. return TrackEntry._(entry, this);
  3242. }
  3243. /// Returns the number of tracks that have animations queued.
  3244. int getNumTracks() {
  3245. return _bindings.spine_animation_state_get_num_tracks(_state);
  3246. }
  3247. /// Sets an empty animation for every track, discarding any queued animations, and mixes to it over the specified mix
  3248. /// duration.
  3249. void setEmptyAnimations(double mixDuration) {
  3250. _bindings.spine_animation_state_set_empty_animations(_state, mixDuration);
  3251. }
  3252. /// Multiplier for the delta time when the animation state is updated, causing time for all animations and mixes to play slower
  3253. /// or faster. Defaults to 1.
  3254. ///
  3255. /// See [TrackEntry.getTimeScale] for affecting a single animation.
  3256. double getTimeScale() {
  3257. return _bindings.spine_animation_state_get_time_scale(_state);
  3258. }
  3259. void setTimeScale(double timeScale) {
  3260. _bindings.spine_animation_state_set_time_scale(_state, timeScale);
  3261. }
  3262. /// The [AnimationStateData] to look up mix durations.
  3263. AnimationStateData getData() {
  3264. return AnimationStateData._(_bindings.spine_animation_state_get_data(_state));
  3265. }
  3266. /// The listener for events generated for all tracks managed by the AnimationState, or null.
  3267. ///
  3268. /// A track entry returned from [setAnimation] is already the current animation
  3269. /// for the track, so the track entry listener will not be called for [EventType.start].
  3270. void setListener(AnimationStateListener? listener) {
  3271. _stateListener = listener;
  3272. }
  3273. }
  3274. /// A SkeletonDrawable bundles loading, updating, and rendering an [Atlas], [Skeleton], and [AnimationState]
  3275. /// into a single easy to use class.
  3276. ///
  3277. /// Use the [fromAsset], [fromFile], or [fromHttp] methods to construct a SkeletonDrawable. To have
  3278. /// multiple skeleton drawable instances share the same [Atlas] and [SkeletonData], use the constructor.
  3279. ///
  3280. /// You can then directly access the [atlas], [skeletonData], [skeleton], [animationStateData], and [animationState]
  3281. /// to query and animate the skeleton. Use the [AnimationState] to queue animations on one or more tracks
  3282. /// via [AnimationState.setAnimation] or [AnimationState.addAnimation].
  3283. ///
  3284. /// To update the [AnimationState] and apply it to the [Skeleton] call the [update] function, providing it
  3285. /// a delta time in seconds to advance the animations.
  3286. ///
  3287. /// To render the current pose of the [Skeleton], use the rendering methods [render], [renderToCanvas], [renderToPictureRecorder],
  3288. /// [renderToPng], or [renderToRawImageData], depending on your needs.
  3289. ///
  3290. /// When the skeleton drawable is no longer needed, call the [dispose] method to release its resources. If
  3291. /// the skeleton drawable was constructed from a shared [Atlas] and [SkeletonData], make sure to dispose the
  3292. /// atlas and skeleton data as well, if no skeleton drawable references them anymore.
  3293. class SkeletonDrawable {
  3294. final Atlas atlas;
  3295. final SkeletonData skeletonData;
  3296. late final spine_skeleton_drawable _drawable;
  3297. late final Skeleton skeleton;
  3298. late final AnimationStateData animationStateData;
  3299. late final AnimationState animationState;
  3300. final bool _ownsAtlasAndSkeletonData;
  3301. bool _disposed;
  3302. /// Constructs a new skeleton drawable from the given (possibly shared) [Atlas] and [SkeletonData]. If
  3303. /// the atlas and skeleton data are not shared, the drawable can take ownership by passing true for [_ownsAtlasAndSkeletonData].
  3304. /// In that case a call to [dispose] will also dispose the atlas and skeleton data.
  3305. SkeletonDrawable(this.atlas, this.skeletonData, this._ownsAtlasAndSkeletonData) : _disposed = false {
  3306. _drawable = _bindings.spine_skeleton_drawable_create(skeletonData._data);
  3307. skeleton = Skeleton._(_bindings.spine_skeleton_drawable_get_skeleton(_drawable));
  3308. animationStateData = AnimationStateData._(_bindings.spine_skeleton_drawable_get_animation_state_data(_drawable));
  3309. animationState = AnimationState._(_bindings.spine_skeleton_drawable_get_animation_state(_drawable),
  3310. _bindings.spine_skeleton_drawable_get_animation_state_events(_drawable));
  3311. skeleton.updateWorldTransform(Physics.none);
  3312. }
  3313. /// Constructs a new skeleton drawable from the [atlasFile] and [skeletonFile] from the root asset bundle
  3314. /// or the optionally provided [bundle].
  3315. ///
  3316. /// Throws an exception in case the data could not be loaded.
  3317. static Future<SkeletonDrawable> fromAsset(String atlasFile, String skeletonFile, {AssetBundle? bundle}) async {
  3318. bundle ??= rootBundle;
  3319. var atlas = await Atlas.fromAsset(atlasFile, bundle: bundle);
  3320. var skeletonData = await SkeletonData.fromAsset(atlas, skeletonFile, bundle: bundle);
  3321. return SkeletonDrawable(atlas, skeletonData, true);
  3322. }
  3323. /// Constructs a new skeleton drawable from the [atlasFile] and [skeletonFile].
  3324. ///
  3325. /// Throws an exception in case the data could not be loaded.
  3326. static Future<SkeletonDrawable> fromFile(String atlasFile, String skeletonFile) async {
  3327. var atlas = await Atlas.fromFile(atlasFile);
  3328. var skeletonData = await SkeletonData.fromFile(atlas, skeletonFile);
  3329. return SkeletonDrawable(atlas, skeletonData, true);
  3330. }
  3331. /// Constructs a new skeleton drawable from the [atlasUrl] and [skeletonUrl].
  3332. ///
  3333. /// Throws an exception in case the data could not be loaded.
  3334. static Future<SkeletonDrawable> fromHttp(String atlasUrl, String skeletonUrl) async {
  3335. var atlas = await Atlas.fromHttp(atlasUrl);
  3336. var skeletonData = await SkeletonData.fromHttp(atlas, skeletonUrl);
  3337. return SkeletonDrawable(atlas, skeletonData, true);
  3338. }
  3339. /// Updates the [AnimationState] using the [delta] time given in seconds, applies the
  3340. /// animation state to the [Skeleton] and updates the world transforms of the skeleton
  3341. /// to calculate its current pose.
  3342. void update(double delta) {
  3343. if (_disposed) return;
  3344. animationState.update(delta);
  3345. animationState.apply(skeleton);
  3346. skeleton.update(delta);
  3347. skeleton.updateWorldTransform(Physics.update);
  3348. }
  3349. /// Renders to current skeleton pose to a list of [RenderCommand] instances. The render commands
  3350. /// can be rendered via [Canvas.drawVertices].
  3351. List<RenderCommand> render() {
  3352. if (_disposed) return [];
  3353. spine_render_command nativeCmd = _bindings.spine_skeleton_drawable_render(_drawable);
  3354. List<RenderCommand> commands = [];
  3355. while (nativeCmd.address != nullptr.address) {
  3356. final atlasPage = atlas.atlasPages[_bindings.spine_render_command_get_atlas_page(nativeCmd)];
  3357. commands.add(RenderCommand._(nativeCmd, atlasPage.width.toDouble(), atlasPage.height.toDouble()));
  3358. nativeCmd = _bindings.spine_render_command_get_next(nativeCmd);
  3359. }
  3360. return commands;
  3361. }
  3362. /// Renders the skeleton drawable's current pose to the given [canvas]. Does not perform any
  3363. /// scaling or fitting.
  3364. List<RenderCommand> renderToCanvas(Canvas canvas) {
  3365. var commands = render();
  3366. for (final cmd in commands) {
  3367. canvas.drawVertices(cmd.vertices, rendering.BlendMode.modulate, atlas.atlasPagePaints[cmd.atlasPageIndex][cmd.blendMode]!);
  3368. }
  3369. return commands;
  3370. }
  3371. /// Renders the skeleton drawable's current pose to a [PictureRecorder] with the given [width] and [height].
  3372. /// Uses [bgColor], a 32-bit ARGB color value, to paint the background.
  3373. /// Scales and centers the skeleton to fit the within the bounds of [width] and [height].
  3374. PictureRecorder renderToPictureRecorder(double width, double height, int bgColor) {
  3375. var bounds = skeleton.getBounds();
  3376. var scale = 1 / (bounds.width > bounds.height ? bounds.width / width : bounds.height / height);
  3377. var recorder = PictureRecorder();
  3378. var canvas = Canvas(recorder);
  3379. var paint = Paint()
  3380. ..color = material.Color(bgColor)
  3381. ..style = PaintingStyle.fill;
  3382. canvas.drawRect(Rect.fromLTWH(0, 0, width, height), paint);
  3383. canvas.translate(width / 2, height / 2);
  3384. canvas.scale(scale, scale);
  3385. canvas.translate(-(bounds.x + bounds.width / 2), -(bounds.y + bounds.height / 2));
  3386. canvas.drawRect(const Rect.fromLTRB(-5, -5, 5, -5), paint..color = material.Colors.red);
  3387. renderToCanvas(canvas);
  3388. return recorder;
  3389. }
  3390. /// Renders the skeleton drawable's current pose to a PNG encoded in a [Uint8List], with the given [width] and [height].
  3391. /// Uses [bgColor], a 32-bit ARGB color value, to paint the background.
  3392. /// Scales and centers the skeleton to fit the within the bounds of [width] and [height].
  3393. Future<Uint8List> renderToPng(double width, double height, int bgColor) async {
  3394. final recorder = renderToPictureRecorder(width, height, bgColor);
  3395. final image = await recorder.endRecording().toImage(width.toInt(), height.toInt());
  3396. return (await image.toByteData(format: ImageByteFormat.png))!.buffer.asUint8List();
  3397. }
  3398. /// Renders the skeleton drawable's current pose to a [RawImageData], with the given [width] and [height].
  3399. /// Uses [bgColor], a 32-bit ARGB color value, to paint the background.
  3400. /// Scales and centers the skeleton to fit the within the bounds of [width] and [height].
  3401. Future<RawImageData> renderToRawImageData(double width, double height, int bgColor) async {
  3402. final recorder = renderToPictureRecorder(width, height, bgColor);
  3403. var rawImageData =
  3404. (await (await recorder.endRecording().toImage(width.toInt(), height.toInt())).toByteData(format: ImageByteFormat.rawRgba))!
  3405. .buffer
  3406. .asUint8List();
  3407. return RawImageData(rawImageData, width.toInt(), height.toInt());
  3408. }
  3409. /// Disposes the skeleton drawable's resources. If the skeleton drawable owns the atlas
  3410. /// and skeleton data, they are disposed as well. Must be called when the skeleton drawable
  3411. /// is no longer in use.
  3412. void dispose() {
  3413. if (_disposed) return;
  3414. _disposed = true;
  3415. if (_ownsAtlasAndSkeletonData) {
  3416. atlas.dispose();
  3417. skeletonData.dispose();
  3418. }
  3419. _bindings.spine_skeleton_drawable_dispose(_drawable);
  3420. }
  3421. }
  3422. /// Stores the vertices, indices, and atlas page index to be used for rendering one or more attachments
  3423. /// of a [Skeleton] to a [Canvas]. See the implementation of [SkeletonDrawable.renderToCanvas] on how to use this data to render it to a
  3424. /// [Canvas].
  3425. class RenderCommand {
  3426. late final Vertices vertices;
  3427. late final int atlasPageIndex;
  3428. late final Float32List positions;
  3429. late final Float32List uvs;
  3430. late final Int32List colors;
  3431. late final Uint16List indices;
  3432. late final BlendMode blendMode;
  3433. RenderCommand._(spine_render_command nativeCmd, double pageWidth, double pageHeight) {
  3434. atlasPageIndex = _bindings.spine_render_command_get_atlas_page(nativeCmd);
  3435. int numVertices = _bindings.spine_render_command_get_num_vertices(nativeCmd);
  3436. int numIndices = _bindings.spine_render_command_get_num_indices(nativeCmd);
  3437. positions = _bindings.spine_render_command_get_positions(nativeCmd).asTypedList(numVertices * 2);
  3438. uvs = _bindings.spine_render_command_get_uvs(nativeCmd).asTypedList(numVertices * 2);
  3439. for (int i = 0; i < numVertices * 2; i += 2) {
  3440. uvs[i] *= pageWidth;
  3441. uvs[i + 1] *= pageHeight;
  3442. }
  3443. colors = _bindings.spine_render_command_get_colors(nativeCmd).asTypedList(numVertices);
  3444. indices = _bindings.spine_render_command_get_indices(nativeCmd).asTypedList(numIndices);
  3445. blendMode = BlendMode.values[_bindings.spine_render_command_get_blend_mode(nativeCmd)];
  3446. if (!kIsWeb) {
  3447. // We pass the native data as views directly to Vertices.raw. According to the sources, the data
  3448. // is copied, so it doesn't matter that we free up the underlying memory on the next
  3449. // render call. See the implementation of Vertices.raw() here:
  3450. // https://github.com/flutter/engine/blob/5c60785b802ad2c8b8899608d949342d5c624952/lib/ui/painting/vertices.cc#L21
  3451. //
  3452. // Impeller is currently using a slow path when using vertex colors.
  3453. // See https://github.com/flutter/flutter/issues/127486
  3454. //
  3455. // We thus batch all meshes not only by atlas page and blend mode, but also vertex color.
  3456. // See spine_flutter.cpp, batch_commands().
  3457. //
  3458. // If the vertex color equals (1, 1, 1, 1), we do not store
  3459. // colors, which will trigger the fast path in Impeller. Otherwise we have to go the slow path, which
  3460. // has to render to an offscreen surface.
  3461. if (colors.isNotEmpty && colors[0] == -1) {
  3462. vertices = Vertices.raw(VertexMode.triangles, positions, textureCoordinates: uvs, indices: indices);
  3463. } else {
  3464. vertices = Vertices.raw(VertexMode.triangles, positions, textureCoordinates: uvs, colors: colors, indices: indices);
  3465. }
  3466. } else {
  3467. // On the web, rendering is done through CanvasKit, which requires copies of the native data.
  3468. final positionsCopy = Float32List.fromList(positions);
  3469. final uvsCopy = Float32List.fromList(uvs);
  3470. final colorsCopy = Int32List.fromList(colors);
  3471. final indicesCopy = Uint16List.fromList(indices);
  3472. vertices = Vertices.raw(VertexMode.triangles, positionsCopy, textureCoordinates: uvsCopy, colors: colorsCopy, indices: indicesCopy);
  3473. }
  3474. }
  3475. }
  3476. /// Renders debug information for a [SkeletonDrawable], like bone locations, to a [Canvas].
  3477. /// See [DebugRenderer.render].
  3478. class DebugRenderer {
  3479. const DebugRenderer();
  3480. void render(SkeletonDrawable drawable, Canvas canvas, List<RenderCommand> commands) {
  3481. final bonePaint = Paint()
  3482. ..color = material.Colors.blue
  3483. ..style = PaintingStyle.fill;
  3484. for (final bone in drawable.skeleton.getBones()) {
  3485. canvas.drawRect(Rect.fromCenter(center: Offset(bone.getWorldX(), bone.getWorldY()), width: 5, height: 5), bonePaint);
  3486. }
  3487. }
  3488. }