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 {
@ -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);
@ -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);
} }
@ -1327,7 +1337,8 @@ 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());
} }
} }
@ -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);
@ -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;
} }
@ -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;
@ -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);
} }
@ -3173,12 +3184,10 @@ class RenderCommand {
// 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();