Formatting.

This commit is contained in:
Mario Zechner 2022-09-09 19:36:50 +02:00
parent 82f2a26d29
commit 8ffde33fa9
8 changed files with 1975 additions and 3074 deletions

View File

@ -3,17 +3,21 @@ import 'dart:ffi';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:ui'; import 'dart:ui';
import 'package:ffi/ffi.dart'; import 'package:ffi/ffi.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'spine_flutter_bindings_generated.dart'; import 'package:http/http.dart' as http;
export 'spine_widget.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
import 'spine_flutter_bindings_generated.dart';
export 'spine_widget.dart';
int majorVersion() => _bindings.spine_major_version(); int majorVersion() => _bindings.spine_major_version();
int minorVersion() => _bindings.spine_minor_version(); int minorVersion() => _bindings.spine_minor_version();
void reportLeaks() => _bindings.spine_report_leaks(); void reportLeaks() => _bindings.spine_report_leaks();
class Color { class Color {
@ -47,7 +51,7 @@ class Atlas {
final List<Paint> atlasPagePaints; final List<Paint> atlasPagePaints;
bool _disposed; bool _disposed;
Atlas._(this._atlas, this.atlasPages, this.atlasPagePaints): _disposed = false; Atlas._(this._atlas, this.atlasPages, this.atlasPagePaints) : _disposed = false;
static Future<Atlas> _load(String atlasFileName, Future<Uint8List> Function(String name) loadFile) async { static Future<Atlas> _load(String atlasFileName, Future<Uint8List> Function(String name) loadFile) async {
final atlasBytes = await loadFile(atlasFileName); final atlasBytes = await loadFile(atlasFileName);
@ -74,9 +78,9 @@ class Atlas {
final Image image = frameInfo.image; final Image image = frameInfo.image;
atlasPages.add(image); atlasPages.add(image);
atlasPagePaints.add(Paint() atlasPagePaints.add(Paint()
..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage, filterQuality: FilterQuality.high) ..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage,
..isAntiAlias = true filterQuality: FilterQuality.high)
); ..isAntiAlias = true);
} }
return Atlas._(atlas, atlasPages, atlasPagePaints); return Atlas._(atlas, atlasPages, atlasPagePaints);
@ -110,7 +114,7 @@ class SkeletonData {
final spine_skeleton_data _data; final spine_skeleton_data _data;
bool _disposed; bool _disposed;
SkeletonData._(this._data): _disposed = false; SkeletonData._(this._data) : _disposed = false;
static SkeletonData fromJson(Atlas atlas, String json) { static SkeletonData fromJson(Atlas atlas, String json) {
final jsonNative = json.toNativeUtf8(); final jsonNative = json.toNativeUtf8();
@ -284,7 +288,6 @@ class SkeletonData {
return events; return events;
} }
/// The skeleton's animations. /// The skeleton's animations.
List<Animation> getAnimations() { List<Animation> getAnimations() {
final List<Animation> events = []; final List<Animation> events = [];
@ -412,6 +415,7 @@ enum BlendMode {
Screen(3); Screen(3);
final int value; final int value;
const BlendMode(this.value); const BlendMode(this.value);
} }
@ -423,6 +427,7 @@ enum TransformMode {
NoScaleOrReflection(4); NoScaleOrReflection(4);
final int value; final int value;
const TransformMode(this.value); const TransformMode(this.value);
} }
@ -431,6 +436,7 @@ enum PositionMode {
Percent(1); Percent(1);
final int value; final int value;
const PositionMode(this.value); const PositionMode(this.value);
} }
@ -441,6 +447,7 @@ enum SpacingMode {
Proportional(3); Proportional(3);
final int value; final int value;
const SpacingMode(this.value); const SpacingMode(this.value);
} }
@ -450,6 +457,7 @@ enum RotateMode {
ChainScale(2); ChainScale(2);
final int value; final int value;
const RotateMode(this.value); const RotateMode(this.value);
} }
@ -590,7 +598,8 @@ class Bone {
_bindings.spine_bone_update_world_transform(_bone); _bindings.spine_bone_update_world_transform(_bone);
} }
void updateWorldTransformWith(double x, double y, double rotation, double scaleX, double scaleY, double shearX, double shearY) { void updateWorldTransformWith(
double x, double y, double rotation, double scaleX, double scaleY, double shearX, double shearY) {
_bindings.spine_bone_update_world_transform_with(_bone, x, y, rotation, scaleX, scaleY, shearX, shearY); _bindings.spine_bone_update_world_transform_with(_bone, x, y, rotation, scaleX, scaleY, shearX, shearY);
} }
@ -1145,6 +1154,7 @@ enum AttachmentType {
Point(5); Point(5);
final int value; final int value;
const AttachmentType(this.value); const AttachmentType(this.value);
} }
@ -1165,7 +1175,7 @@ abstract class Attachment<T extends Pointer> {
static Attachment _toSubclass(spine_attachment attachment) { static Attachment _toSubclass(spine_attachment attachment) {
final type = AttachmentType.values[_bindings.spine_attachment_get_type(attachment)]; final type = AttachmentType.values[_bindings.spine_attachment_get_type(attachment)];
switch(type) { switch (type) {
case AttachmentType.Region: case AttachmentType.Region:
return RegionAttachment._(attachment.cast()); return RegionAttachment._(attachment.cast());
case AttachmentType.Mesh: case AttachmentType.Mesh:
@ -1191,7 +1201,7 @@ abstract class Attachment<T extends Pointer> {
} }
class RegionAttachment extends Attachment<spine_region_attachment> { class RegionAttachment extends Attachment<spine_region_attachment> {
RegionAttachment._(spine_region_attachment attachment): super._(attachment); RegionAttachment._(spine_region_attachment attachment) : super._(attachment);
List<double> computeWorldVertices(Slot slot) { List<double> computeWorldVertices(Slot slot) {
Pointer<Float> vertices = malloc.allocate(4 * 8).cast(); Pointer<Float> vertices = malloc.allocate(4 * 8).cast();
@ -1297,7 +1307,7 @@ class RegionAttachment extends Attachment<spine_region_attachment> {
} }
class VertexAttachment<T extends Pointer> extends Attachment<T> { class VertexAttachment<T extends Pointer> extends Attachment<T> {
VertexAttachment._(T attachment): super._(attachment); VertexAttachment._(T attachment) : super._(attachment);
List<double> computeWorldVertices(Slot slot) { List<double> computeWorldVertices(Slot slot) {
final worldVerticesLength = _bindings.spine_vertex_attachment_get_world_vertices_length(_attachment.cast()); final worldVerticesLength = _bindings.spine_vertex_attachment_get_world_vertices_length(_attachment.cast());
@ -1327,12 +1337,13 @@ class VertexAttachment<T extends Pointer> extends Attachment<T> {
} }
void setTimelineAttachment(Attachment? attachment) { void setTimelineAttachment(Attachment? attachment) {
_bindings.spine_vertex_attachment_set_timeline_attachment(_attachment.cast(), attachment == null ? nullptr : attachment._attachment.cast()); _bindings.spine_vertex_attachment_set_timeline_attachment(
_attachment.cast(), attachment == null ? nullptr : attachment._attachment.cast());
} }
} }
class MeshAttachment extends VertexAttachment<spine_mesh_attachment> { class MeshAttachment extends VertexAttachment<spine_mesh_attachment> {
MeshAttachment._(spine_mesh_attachment attachment): super._(attachment.cast()); MeshAttachment._(spine_mesh_attachment attachment) : super._(attachment.cast());
void updateRegion() { void updateRegion() {
_bindings.spine_mesh_attachment_update_region(_attachment); _bindings.spine_mesh_attachment_update_region(_attachment);
@ -1424,7 +1435,7 @@ class MeshAttachment extends VertexAttachment<spine_mesh_attachment> {
} }
class ClippingAttachment extends VertexAttachment<spine_clipping_attachment> { class ClippingAttachment extends VertexAttachment<spine_clipping_attachment> {
ClippingAttachment._(spine_clipping_attachment attachment): super._(attachment.cast()); ClippingAttachment._(spine_clipping_attachment attachment) : super._(attachment.cast());
SlotData? getEndSlot() { SlotData? getEndSlot() {
final endSlot = _bindings.spine_clipping_attachment_get_end_slot(_attachment); final endSlot = _bindings.spine_clipping_attachment_get_end_slot(_attachment);
@ -1436,7 +1447,6 @@ class ClippingAttachment extends VertexAttachment<spine_clipping_attachment> {
_bindings.spine_clipping_attachment_set_end_slot(_attachment, endSlot == null ? nullptr : endSlot._data); _bindings.spine_clipping_attachment_set_end_slot(_attachment, endSlot == null ? nullptr : endSlot._data);
} }
Color getColor() { Color getColor() {
final color = _bindings.spine_clipping_attachment_get_color(_attachment); final color = _bindings.spine_clipping_attachment_get_color(_attachment);
return Color(color.r, color.g, color.b, color.a); return Color(color.r, color.g, color.b, color.a);
@ -1448,7 +1458,7 @@ class ClippingAttachment extends VertexAttachment<spine_clipping_attachment> {
} }
class BoundingBoxAttachment extends VertexAttachment<spine_bounding_box_attachment> { class BoundingBoxAttachment extends VertexAttachment<spine_bounding_box_attachment> {
BoundingBoxAttachment._(spine_bounding_box_attachment attachment): super._(attachment); BoundingBoxAttachment._(spine_bounding_box_attachment attachment) : super._(attachment);
Color getColor() { Color getColor() {
final color = _bindings.spine_bounding_box_attachment_get_color(_attachment); final color = _bindings.spine_bounding_box_attachment_get_color(_attachment);
@ -1461,7 +1471,7 @@ class BoundingBoxAttachment extends VertexAttachment<spine_bounding_box_attachme
} }
class PathAttachment extends VertexAttachment<spine_path_attachment> { class PathAttachment extends VertexAttachment<spine_path_attachment> {
PathAttachment._(spine_path_attachment attachment): super._(attachment); PathAttachment._(spine_path_attachment attachment) : super._(attachment);
Float32List getLengths() { Float32List getLengths() {
final num = _bindings.spine_path_attachment_get_num_lengths(_attachment); final num = _bindings.spine_path_attachment_get_num_lengths(_attachment);
@ -1496,7 +1506,7 @@ class PathAttachment extends VertexAttachment<spine_path_attachment> {
} }
class PointAttachment extends Attachment<spine_point_attachment> { class PointAttachment extends Attachment<spine_point_attachment> {
PointAttachment._(spine_point_attachment attachment): super._(attachment); PointAttachment._(spine_point_attachment attachment) : super._(attachment);
Vector2 computeWorldPosition(Bone bone) { Vector2 computeWorldPosition(Bone bone) {
final position = _bindings.spine_point_attachment_compute_world_position(_attachment, bone._bone); final position = _bindings.spine_point_attachment_compute_world_position(_attachment, bone._bone);
@ -1557,7 +1567,8 @@ class Skin {
void setAttachment(int slotIndex, String name, Attachment? attachment) { void setAttachment(int slotIndex, String name, Attachment? attachment) {
final nativeName = name.toNativeUtf8(); final nativeName = name.toNativeUtf8();
_bindings.spine_skin_set_attachment(_skin, slotIndex, nativeName.cast(), attachment == null ? nullptr : attachment._attachment.cast()); _bindings.spine_skin_set_attachment(
_skin, slotIndex, nativeName.cast(), attachment == null ? nullptr : attachment._attachment.cast());
malloc.free(nativeName); malloc.free(nativeName);
} }
@ -1591,7 +1602,8 @@ class Skin {
for (int i = 0; i < numEntries; i++) { for (int i = 0; i < numEntries; i++) {
final entry = entries.ref.entries[i]; final entry = entries.ref.entries[i];
Pointer<Utf8> name = entry.name.cast(); Pointer<Utf8> name = entry.name.cast();
result.add(SkinEntry(entry.slotIndex, name.toDartString(), entry.attachment.address == nullptr.address ? null : Attachment._toSubclass(entry.attachment))); result.add(SkinEntry(entry.slotIndex, name.toDartString(),
entry.attachment.address == nullptr.address ? null : Attachment._toSubclass(entry.attachment)));
} }
return result; return result;
} }
@ -1661,7 +1673,7 @@ class ConstraintData<T extends Pointer> {
} }
class IkConstraintData extends ConstraintData<spine_ik_constraint_data> { class IkConstraintData extends ConstraintData<spine_ik_constraint_data> {
IkConstraintData._(spine_ik_constraint_data data): super._(data); IkConstraintData._(spine_ik_constraint_data data) : super._(data);
List<BoneData> getBones() { List<BoneData> getBones() {
final List<BoneData> result = []; final List<BoneData> result = [];
@ -1815,7 +1827,7 @@ class IkConstraint {
} }
class TransformConstraintData extends ConstraintData<spine_transform_constraint_data> { class TransformConstraintData extends ConstraintData<spine_transform_constraint_data> {
TransformConstraintData._(spine_transform_constraint_data data): super._(data); TransformConstraintData._(spine_transform_constraint_data data) : super._(data);
List<BoneData> getBones() { List<BoneData> getBones() {
final List<BoneData> result = []; final List<BoneData> result = [];
@ -2041,7 +2053,7 @@ class TransformConstraint {
} }
class PathConstraintData extends ConstraintData<spine_path_constraint_data> { class PathConstraintData extends ConstraintData<spine_path_constraint_data> {
PathConstraintData._(spine_path_constraint_data data): super._(data); PathConstraintData._(spine_path_constraint_data data) : super._(data);
List<BoneData> getBones() { List<BoneData> getBones() {
final List<BoneData> result = []; final List<BoneData> result = [];
@ -2280,7 +2292,8 @@ class Skeleton {
Attachment? getAttachmentByName(String slotName, String attachmentName) { Attachment? getAttachmentByName(String slotName, String attachmentName) {
final slotNameNative = slotName.toNativeUtf8(); final slotNameNative = slotName.toNativeUtf8();
final attachmentNameNative = attachmentName.toNativeUtf8(); final attachmentNameNative = attachmentName.toNativeUtf8();
final attachment = _bindings.spine_skeleton_get_attachment_by_name(_skeleton, slotNameNative.cast(), attachmentNameNative.cast()); final attachment =
_bindings.spine_skeleton_get_attachment_by_name(_skeleton, slotNameNative.cast(), attachmentNameNative.cast());
malloc.free(slotNameNative); malloc.free(slotNameNative);
malloc.free(attachmentNameNative); malloc.free(attachmentNameNative);
if (attachment.address == nullptr.address) return null; if (attachment.address == nullptr.address) return null;
@ -2485,6 +2498,7 @@ enum MixBlend {
Add(3); Add(3);
final int value; final int value;
const MixBlend(this.value); const MixBlend(this.value);
} }
@ -2760,14 +2774,7 @@ class TrackEntry {
} }
} }
enum EventType { enum EventType { Start, Interrupt, End, Complete, Dispose, Event }
Start,
Interrupt,
End,
Complete,
Dispose,
Event
}
class EventData { class EventData {
final spine_event_data _data; final spine_event_data _data;
@ -2940,7 +2947,7 @@ class AnimationState {
final Map<spine_track_entry, AnimationStateListener> _trackEntryListeners; final Map<spine_track_entry, AnimationStateListener> _trackEntryListeners;
AnimationStateListener? _stateListener; AnimationStateListener? _stateListener;
AnimationState._(this._state, this._events): _trackEntryListeners = {}; AnimationState._(this._state, this._events) : _trackEntryListeners = {};
void _setTrackEntryListener(spine_track_entry entry, AnimationStateListener? listener) { void _setTrackEntryListener(spine_track_entry entry, AnimationStateListener? listener) {
if (listener == null) { if (listener == null) {
@ -2959,7 +2966,7 @@ class AnimationState {
if (numEvents > 0) { if (numEvents > 0) {
for (int i = 0; i < numEvents; i++) { for (int i = 0; i < numEvents; i++) {
late final EventType type; late final EventType type;
switch(_bindings.spine_animation_state_events_get_event_type(_events, i)) { switch (_bindings.spine_animation_state_events_get_event_type(_events, i)) {
case 0: case 0:
type = EventType.Start; type = EventType.Start;
break; break;
@ -3026,14 +3033,16 @@ class AnimationState {
/// after AnimationState.Dispose. /// after AnimationState.Dispose.
TrackEntry setAnimationByName(int trackIndex, String animationName, bool loop) { TrackEntry setAnimationByName(int trackIndex, String animationName, bool loop) {
final animation = animationName.toNativeUtf8(); final animation = animationName.toNativeUtf8();
final entry = _bindings.spine_animation_state_set_animation_by_name(_state, trackIndex, animation.cast(), loop ? -1 : 0); final entry =
_bindings.spine_animation_state_set_animation_by_name(_state, trackIndex, animation.cast(), loop ? -1 : 0);
malloc.free(animation); malloc.free(animation);
if (entry.address == nullptr.address) throw Exception("Couldn't set animation $animationName"); if (entry.address == nullptr.address) throw Exception("Couldn't set animation $animationName");
return TrackEntry._(entry, this); return TrackEntry._(entry, this);
} }
TrackEntry setAnimation(int trackIndex, Animation animation, bool loop) { TrackEntry setAnimation(int trackIndex, Animation animation, bool loop) {
final entry = _bindings.spine_animation_state_set_animation(_state, trackIndex, animation._animation, loop ? -1 : 0); final entry =
_bindings.spine_animation_state_set_animation(_state, trackIndex, animation._animation, loop ? -1 : 0);
if (entry.address == nullptr.address) throw Exception("Couldn't set animation ${animation.getName()}"); if (entry.address == nullptr.address) throw Exception("Couldn't set animation ${animation.getName()}");
return TrackEntry._(entry, this); return TrackEntry._(entry, this);
} }
@ -3048,14 +3057,16 @@ class AnimationState {
/// after AnimationState.Dispose /// after AnimationState.Dispose
TrackEntry addAnimationByName(int trackIndex, String animationName, bool loop, double delay) { TrackEntry addAnimationByName(int trackIndex, String animationName, bool loop, double delay) {
final animation = animationName.toNativeUtf8(); final animation = animationName.toNativeUtf8();
final entry = _bindings.spine_animation_state_add_animation_by_name(_state, trackIndex, animation.cast(), loop ? -1 : 0, delay); final entry = _bindings.spine_animation_state_add_animation_by_name(
_state, trackIndex, animation.cast(), loop ? -1 : 0, delay);
malloc.free(animation); malloc.free(animation);
if (entry.address == nullptr.address) throw Exception("Couldn't add animation $animationName"); if (entry.address == nullptr.address) throw Exception("Couldn't add animation $animationName");
return TrackEntry._(entry, this); return TrackEntry._(entry, this);
} }
TrackEntry addAnimation(int trackIndex, Animation animation, bool loop, double delay) { TrackEntry addAnimation(int trackIndex, Animation animation, bool loop, double delay) {
final entry = _bindings.spine_animation_state_add_animation(_state, trackIndex, animation._animation, loop ? -1 : 0, delay); final entry =
_bindings.spine_animation_state_add_animation(_state, trackIndex, animation._animation, loop ? -1 : 0, delay);
if (entry.address == nullptr.address) throw Exception("Couldn't add animation ${animation.getName()}"); if (entry.address == nullptr.address) throw Exception("Couldn't add animation ${animation.getName()}");
return TrackEntry._(entry, this); return TrackEntry._(entry, this);
} }
@ -3119,7 +3130,7 @@ class SkeletonDrawable {
final bool _ownsAtlasAndSkeletonData; final bool _ownsAtlasAndSkeletonData;
bool _disposed; bool _disposed;
SkeletonDrawable(this.atlas, this.skeletonData, this._ownsAtlasAndSkeletonData): _disposed = false { SkeletonDrawable(this.atlas, this.skeletonData, this._ownsAtlasAndSkeletonData) : _disposed = false {
_drawable = _bindings.spine_skeleton_drawable_create(skeletonData._data); _drawable = _bindings.spine_skeleton_drawable_create(skeletonData._data);
skeleton = Skeleton._(_drawable.ref.skeleton); skeleton = Skeleton._(_drawable.ref.skeleton);
animationStateData = AnimationStateData._(_drawable.ref.animationStateData); animationStateData = AnimationStateData._(_drawable.ref.animationStateData);
@ -3137,7 +3148,7 @@ class SkeletonDrawable {
if (_disposed) return []; if (_disposed) return [];
Pointer<spine_render_command> nativeCmd = _bindings.spine_skeleton_drawable_render(_drawable); Pointer<spine_render_command> nativeCmd = _bindings.spine_skeleton_drawable_render(_drawable);
List<RenderCommand> commands = []; List<RenderCommand> commands = [];
while(nativeCmd.address != nullptr.address) { while (nativeCmd.address != nullptr.address) {
final atlasPage = atlas.atlasPages[nativeCmd.ref.atlasPage]; final atlasPage = atlas.atlasPages[nativeCmd.ref.atlasPage];
commands.add(RenderCommand._(nativeCmd, atlasPage.width.toDouble(), atlasPage.height.toDouble())); commands.add(RenderCommand._(nativeCmd, atlasPage.width.toDouble(), atlasPage.height.toDouble()));
nativeCmd = nativeCmd.ref.next; nativeCmd = nativeCmd.ref.next;
@ -3167,18 +3178,16 @@ class RenderCommand {
final uvs = nativeCmd.ref.uvs.asTypedList(numVertices * 2); final uvs = nativeCmd.ref.uvs.asTypedList(numVertices * 2);
for (int i = 0; i < numVertices * 2; i += 2) { for (int i = 0; i < numVertices * 2; i += 2) {
uvs[i] *= pageWidth; uvs[i] *= pageWidth;
uvs[i+1] *= pageHeight; uvs[i + 1] *= pageHeight;
} }
// We pass the native data as views directly to Vertices.raw. According to the sources, the data // We pass the native data as views directly to Vertices.raw. According to the sources, the data
// is copied, so it doesn't matter that we free up the underlying memory on the next // is copied, so it doesn't matter that we free up the underlying memory on the next
// render call. See the implementation of Vertices.raw() here: // render call. See the implementation of Vertices.raw() here:
// https://github.com/flutter/engine/blob/5c60785b802ad2c8b8899608d949342d5c624952/lib/ui/painting/vertices.cc#L21 // https://github.com/flutter/engine/blob/5c60785b802ad2c8b8899608d949342d5c624952/lib/ui/painting/vertices.cc#L21
vertices = Vertices.raw(VertexMode.triangles, vertices = Vertices.raw(VertexMode.triangles, nativeCmd.ref.positions.asTypedList(numVertices * 2),
nativeCmd.ref.positions.asTypedList(numVertices * 2),
textureCoordinates: uvs, textureCoordinates: uvs,
colors: nativeCmd.ref.colors.asTypedList(numVertices), colors: nativeCmd.ref.colors.asTypedList(numVertices),
indices: nativeCmd.ref.indices.asTypedList(numIndices) indices: nativeCmd.ref.indices.asTypedList(numIndices));
);
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,8 @@ class SpineWidgetController {
SpineWidgetController([this.onInitialized]); SpineWidgetController([this.onInitialized]);
void _initialize(Atlas atlas, SkeletonData data, SkeletonDrawable drawable) { void _initialize(Atlas atlas, SkeletonData data, SkeletonDrawable drawable) {
if (initialized) throw Exception("SpineWidgetController already initialized. A controller can only be used with one widget."); if (initialized)
throw Exception("SpineWidgetController already initialized. A controller can only be used with one widget.");
_atlas = atlas; _atlas = atlas;
_data = data; _data = data;
_drawable = drawable; _drawable = drawable;
@ -28,18 +29,17 @@ class SpineWidgetController {
} }
Atlas? get atlas => _atlas; Atlas? get atlas => _atlas;
SkeletonData? get skeletonData => _data; SkeletonData? get skeletonData => _data;
AnimationStateData? get animationStateData => _drawable?.animationStateData; AnimationStateData? get animationStateData => _drawable?.animationStateData;
AnimationState? get animationState => _drawable?.animationState; AnimationState? get animationState => _drawable?.animationState;
Skeleton? get skeleton => _drawable?.skeleton; Skeleton? get skeleton => _drawable?.skeleton;
} }
enum AssetType { enum AssetType { Asset, File, Http, Raw }
Asset,
File,
Http,
Raw
}
class SpineWidget extends StatefulWidget { class SpineWidget extends StatefulWidget {
final String? skeletonFile; final String? skeletonFile;
@ -49,10 +49,25 @@ class SpineWidget extends StatefulWidget {
final SpineWidgetController controller; final SpineWidgetController controller;
final AssetType _assetType; final AssetType _assetType;
const SpineWidget.asset(this.skeletonFile, this.atlasFile, this.controller, {super.key}): _assetType = AssetType.Asset, atlas = null, skeletonData = null; const SpineWidget.asset(this.skeletonFile, this.atlasFile, this.controller, {super.key})
const SpineWidget.file(this.skeletonFile, this.atlasFile, this.controller, {super.key}): _assetType = AssetType.File, atlas = null, skeletonData = null; : _assetType = AssetType.Asset,
const SpineWidget.http(this.skeletonFile, this.atlasFile, this.controller, {super.key}): _assetType = AssetType.Http, atlas = null, skeletonData = null; atlas = null,
const SpineWidget.raw(this.skeletonData, this.atlas, this.controller, {super.key}): _assetType = AssetType.Raw, atlasFile = null, skeletonFile = null; skeletonData = null;
const SpineWidget.file(this.skeletonFile, this.atlasFile, this.controller, {super.key})
: _assetType = AssetType.File,
atlas = null,
skeletonData = null;
const SpineWidget.http(this.skeletonFile, this.atlasFile, this.controller, {super.key})
: _assetType = AssetType.Http,
atlas = null,
skeletonData = null;
const SpineWidget.raw(this.skeletonData, this.atlas, this.controller, {super.key})
: _assetType = AssetType.Raw,
atlasFile = null,
skeletonFile = null;
@override @override
State<SpineWidget> createState() => _SpineWidgetState(); State<SpineWidget> createState() => _SpineWidgetState();
@ -84,34 +99,27 @@ class _SpineWidgetState extends State<SpineWidget> {
case AssetType.Asset: case AssetType.Asset:
atlas = await Atlas.fromAsset(rootBundle, atlasFile); atlas = await Atlas.fromAsset(rootBundle, atlasFile);
skeletonData = skeletonFile.endsWith(".json") skeletonData = skeletonFile.endsWith(".json")
? SkeletonData.fromJson( ? SkeletonData.fromJson(atlas, await rootBundle.loadString(skeletonFile))
atlas, await rootBundle.loadString(skeletonFile)) : SkeletonData.fromBinary(atlas, (await rootBundle.load(skeletonFile)).buffer.asUint8List());
: SkeletonData.fromBinary(
atlas, (await rootBundle.load(skeletonFile)).buffer.asUint8List());
break; break;
case AssetType.File: case AssetType.File:
atlas = await Atlas.fromFile(atlasFile); atlas = await Atlas.fromFile(atlasFile);
skeletonData = skeletonFile.endsWith(".json") skeletonData = skeletonFile.endsWith(".json")
? SkeletonData.fromJson( ? SkeletonData.fromJson(atlas, utf8.decode(await File(skeletonFile).readAsBytes()))
atlas, utf8.decode(await File(skeletonFile).readAsBytes())) : SkeletonData.fromBinary(atlas, await File(skeletonFile).readAsBytes());
: SkeletonData.fromBinary(
atlas, await File(skeletonFile).readAsBytes());
break; break;
case AssetType.Http: case AssetType.Http:
atlas = await Atlas.fromUrl(atlasFile); atlas = await Atlas.fromUrl(atlasFile);
skeletonData = skeletonFile.endsWith(".json") skeletonData = skeletonFile.endsWith(".json")
? SkeletonData.fromJson( ? SkeletonData.fromJson(atlas, utf8.decode((await http.get(Uri.parse(skeletonFile))).bodyBytes))
atlas, utf8.decode((await http.get(Uri.parse(skeletonFile))).bodyBytes)) : SkeletonData.fromBinary(atlas, (await http.get(Uri.parse(skeletonFile))).bodyBytes);
: SkeletonData.fromBinary(
atlas, (await http.get(Uri.parse(skeletonFile))).bodyBytes);
break; break;
} }
skeletonDrawable = SkeletonDrawable(atlas, skeletonData, true); skeletonDrawable = SkeletonDrawable(atlas, skeletonData, true);
widget.controller._initialize(atlas, skeletonData, skeletonDrawable!); widget.controller._initialize(atlas, skeletonData, skeletonDrawable!);
skeletonDrawable?.update(0); skeletonDrawable?.update(0);
setState(() { setState(() {});
});
} }
@override @override
@ -144,8 +152,7 @@ class _SpineRenderObjectWidget extends LeafRenderObjectWidget {
} }
@override @override
void updateRenderObject(BuildContext context, void updateRenderObject(BuildContext context, covariant _SpineRenderObject renderObject) {
covariant _SpineRenderObject renderObject) {
renderObject.skeletonDrawable = _skeletonDrawable; renderObject.skeletonDrawable = _skeletonDrawable;
} }
} }
@ -210,8 +217,8 @@ class _SpineRenderObject extends RenderBox {
final commands = _skeletonDrawable.render(); final commands = _skeletonDrawable.render();
for (final cmd in commands) { for (final cmd in commands) {
canvas.drawVertices(cmd.vertices, rendering.BlendMode.modulate, canvas.drawVertices(
_skeletonDrawable.atlas.atlasPagePaints[cmd.atlasPageIndex]); cmd.vertices, rendering.BlendMode.modulate, _skeletonDrawable.atlas.atlasPagePaints[cmd.atlasPageIndex]);
} }
canvas.restore(); canvas.restore();

View File

@ -101,7 +101,7 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) {
uvs = &regionAttachment->getUVs(); uvs = &regionAttachment->getUVs();
indices = &quadIndices; indices = &quadIndices;
indicesCount = 6; indicesCount = 6;
texture = (SDL_Texture *)regionAttachment->getRegion()->rendererObject; texture = (SDL_Texture *) regionAttachment->getRegion()->rendererObject;
} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment *mesh = (MeshAttachment *) attachment; MeshAttachment *mesh = (MeshAttachment *) attachment;
@ -114,7 +114,7 @@ void SkeletonDrawable::draw(SDL_Renderer *renderer) {
} }
worldVertices.setSize(mesh->getWorldVerticesLength(), 0); worldVertices.setSize(mesh->getWorldVerticesLength(), 0);
texture = (SDL_Texture *)mesh->getRegion()->rendererObject; texture = (SDL_Texture *) mesh->getRegion()->rendererObject;
mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices.buffer(), 0, 2); mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices.buffer(), 0, 2);
verticesCount = mesh->getWorldVerticesLength() >> 1; verticesCount = mesh->getWorldVerticesLength() >> 1;
uvs = &mesh->getUVs(); uvs = &mesh->getUVs();

View File

@ -123,7 +123,7 @@ namespace spine {
uvs = &regionAttachment->getUVs(); uvs = &regionAttachment->getUVs();
indices = &quadIndices; indices = &quadIndices;
indicesCount = 6; indicesCount = 6;
texture = (Texture *)regionAttachment->getRegion()->rendererObject; texture = (Texture *) regionAttachment->getRegion()->rendererObject;
} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment *mesh = (MeshAttachment *) attachment; MeshAttachment *mesh = (MeshAttachment *) attachment;
@ -136,7 +136,7 @@ namespace spine {
} }
worldVertices.setSize(mesh->getWorldVerticesLength(), 0); worldVertices.setSize(mesh->getWorldVerticesLength(), 0);
texture = (Texture *)mesh->getRegion()->rendererObject; texture = (Texture *) mesh->getRegion()->rendererObject;
mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices.buffer(), 0, 2); mesh->computeWorldVertices(slot, 0, mesh->getWorldVerticesLength(), worldVertices.buffer(), 0, 2);
uvs = &mesh->getUVs(); uvs = &mesh->getUVs();
indices = &mesh->getTriangles(); indices = &mesh->getTriangles();