[flutter] Formatting.

This commit is contained in:
Mario Zechner 2022-12-06 10:04:51 +01:00
parent 4b47af1e66
commit 1cf95f49d2
10 changed files with 206 additions and 222 deletions

View File

@ -1,6 +1,6 @@
// ignore_for_file: avoid_print // ignore_for_file: avoid_print
import 'package:flutter/material.dart';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart'; import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flutter/material.dart';
class AnimationStateEvents extends StatelessWidget { class AnimationStateEvents extends StatelessWidget {
const AnimationStateEvents({Key? key}) : super(key: key); const AnimationStateEvents({Key? key}) : super(key: key);
@ -22,22 +22,18 @@ class AnimationStateEvents extends StatelessWidget {
}); });
controller.animationState.setListener((type, trackEntry, event) { controller.animationState.setListener((type, trackEntry, event) {
if (type == EventType.event) { if (type == EventType.event) {
print("User event: { name: ${event?.getData().getName()}, intValue: ${event?.getIntValue()}, floatValue: intValue: ${event?.getFloatValue()}, stringValue: ${event?.getStringValue()} }"); print(
"User event: { name: ${event?.getData().getName()}, intValue: ${event?.getIntValue()}, floatValue: intValue: ${event?.getFloatValue()}, stringValue: ${event?.getStringValue()} }");
} }
}); });
print("Current: ${controller.animationState.getCurrent(0)?.getAnimation().getName()}"); print("Current: ${controller.animationState.getCurrent(0)?.getAnimation().getName()}");
}); });
return Scaffold( return Scaffold(
appBar: AppBar(title: const Text('Spineboy')), appBar: AppBar(title: const Text('Spineboy')),
body: Column( body: Column(children: [
children: [
const Text("See output in console!"), const Text("See output in console!"),
Expanded( Expanded(child: SpineWidget.asset("assets/spineboy.atlas", "assets/spineboy-pro.skel", controller))
child: SpineWidget.asset("assets/spineboy.atlas", "assets/spineboy-pro.skel", controller) ]));
)
]
)
);
} }
} }

View File

@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart'; import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flutter/material.dart';
class DebugRendering extends StatelessWidget { class DebugRendering extends StatelessWidget {
const DebugRendering({Key? key}) : super(key: key); const DebugRendering({Key? key}) : super(key: key);
@ -9,18 +9,15 @@ class DebugRendering extends StatelessWidget {
reportLeaks(); reportLeaks();
const debugRenderer = DebugRenderer(); const debugRenderer = DebugRenderer();
final controller = SpineWidgetController( final controller = SpineWidgetController(onInitialized: (controller) {
onInitialized: (controller) { controller.animationState.setAnimationByName(0, "walk", true);
controller.animationState.setAnimationByName(0, "walk", true); }, onAfterPaint: (controller, canvas, commands) {
}, debugRenderer.render(controller.drawable, canvas, commands);
onAfterPaint: (controller, canvas, commands) { });
debugRenderer.render(controller.drawable, canvas, commands);
}
);
return Scaffold( return Scaffold(
appBar: AppBar(title: const Text('Debug Renderer')), appBar: AppBar(title: const Text('Debug Renderer')),
body: SpineWidget.asset("assets/spineboy.atlas", "assets/spineboy-pro.skel", controller), body: SpineWidget.asset("assets/spineboy.atlas", "assets/spineboy-pro.skel", controller),
); );
} }
} }

View File

@ -1,6 +1,6 @@
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/painting.dart' as painting; import 'package:flutter/painting.dart' as painting;
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:raw_image_provider/raw_image_provider.dart'; import 'package:raw_image_provider/raw_image_provider.dart';
class DressUp extends StatefulWidget { class DressUp extends StatefulWidget {
@ -61,40 +61,38 @@ class DressUpState extends State<DressUp> {
appBar: AppBar(title: const Text('Dress Up')), appBar: AppBar(title: const Text('Dress Up')),
body: _skinImages.isEmpty body: _skinImages.isEmpty
? const SizedBox() ? const SizedBox()
: Row( : Row(children: [
children: [ SizedBox(
SizedBox(width: thumbnailSize, child: width: thumbnailSize,
ListView( child: ListView(
children: _skinImages.keys.map((skinName) { children: _skinImages.keys.map((skinName) {
var rawImageData = _skinImages[skinName]!; var rawImageData = _skinImages[skinName]!;
var image = Image(image: RawImageProvider(rawImageData)); var image = Image(image: RawImageProvider(rawImageData));
var box = SizedBox(width: 200, height: 200, child: image); var box = SizedBox(width: 200, height: 200, child: image);
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
_toggleSkin(skinName); _toggleSkin(skinName);
setState(() {}); setState(() {});
}, },
child: _selectedSkins[skinName] == true child: _selectedSkins[skinName] == true
? box ? box
// Does not work on web. // Does not work on web.
//: ColorFiltered(colorFilter: const ColorFilter.mode(Colors.grey, painting.BlendMode.saturation,), child: box) //: ColorFiltered(colorFilter: const ColorFilter.mode(Colors.grey, painting.BlendMode.saturation,), child: box)
: Container( : Container(
foregroundDecoration: const BoxDecoration( foregroundDecoration: const BoxDecoration(
color: Colors.grey, color: Colors.grey,
backgroundBlendMode: painting.BlendMode.saturation, backgroundBlendMode: painting.BlendMode.saturation,
), ),
child: box child: box));
) }).toList()),
); ),
}).toList() Expanded(
), child: SpineWidget.drawable(
), _drawable,
Expanded( controller,
child: SpineWidget.drawable(_drawable, controller, boundsProvider: SkinAndAnimationBounds(skins: ["full-skins/girl"]),) boundsProvider: SkinAndAnimationBounds(skins: ["full-skins/girl"]),
) ))
] ]));
)
);
} }
@override @override
@ -103,4 +101,4 @@ class DressUpState extends State<DressUp> {
_drawable.dispose(); _drawable.dispose();
_customSkin?.dispose(); _customSkin?.dispose();
} }
} }

View File

@ -1,9 +1,9 @@
import 'dart:math'; import 'dart:math';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
class SpineComponent extends PositionComponent { class SpineComponent extends PositionComponent {
final BoundsProvider _boundsProvider; final BoundsProvider _boundsProvider;
@ -11,34 +11,36 @@ class SpineComponent extends PositionComponent {
late final Bounds _bounds; late final Bounds _bounds;
final bool _ownsDrawable; final bool _ownsDrawable;
SpineComponent(this._drawable, { SpineComponent(
bool ownsDrawable = true, this._drawable, {
BoundsProvider boundsProvider = const SetupPoseBounds(), bool ownsDrawable = true,
super.position, BoundsProvider boundsProvider = const SetupPoseBounds(),
super.scale, super.position,
double super.angle = 0.0, super.scale,
Anchor super.anchor = Anchor.topLeft, double super.angle = 0.0,
super.children, Anchor super.anchor = Anchor.topLeft,
super.priority, super.children,
}) : super.priority,
_ownsDrawable = ownsDrawable, }) : _ownsDrawable = ownsDrawable,
_boundsProvider = boundsProvider { _boundsProvider = boundsProvider {
_drawable.update(0); _drawable.update(0);
_bounds = _boundsProvider.computeBounds(_drawable); _bounds = _boundsProvider.computeBounds(_drawable);
size = Vector2(_bounds.width, _bounds.height); size = Vector2(_bounds.width, _bounds.height);
} }
static Future<SpineComponent> fromAssets(String atlasFile, String skeletonFile, { static Future<SpineComponent> fromAssets(
AssetBundle? bundle, BoundsProvider boundsProvider = const SetupPoseBounds(), String atlasFile,
Vector2? position, String skeletonFile, {
Vector2? scale, AssetBundle? bundle,
double angle = 0.0, BoundsProvider boundsProvider = const SetupPoseBounds(),
Anchor anchor = Anchor.topLeft, Vector2? position,
Iterable<Component>? children, Vector2? scale,
int? priority, double angle = 0.0,
}) async { Anchor anchor = Anchor.topLeft,
return SpineComponent( Iterable<Component>? children,
await SkeletonDrawable.fromAsset(atlasFile, skeletonFile, bundle: bundle), int? priority,
}) async {
return SpineComponent(await SkeletonDrawable.fromAsset(atlasFile, skeletonFile, bundle: bundle),
ownsDrawable: true, ownsDrawable: true,
boundsProvider: boundsProvider, boundsProvider: boundsProvider,
position: position, position: position,
@ -54,12 +56,12 @@ class SpineComponent extends PositionComponent {
_drawable.dispose(); _drawable.dispose();
} }
} }
@override @override
void update(double dt) { void update(double dt) {
_drawable.update(dt); _drawable.update(dt);
} }
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
canvas.save(); canvas.save();
@ -69,7 +71,9 @@ class SpineComponent extends PositionComponent {
} }
get animationState => _drawable.animationState; get animationState => _drawable.animationState;
get animationStateData => _drawable.animationStateData; get animationStateData => _drawable.animationStateData;
get skeleton => _drawable.skeleton; get skeleton => _drawable.skeleton;
} }
@ -81,12 +85,8 @@ class SimpleFlameExample extends FlameGame {
// Load the Spineboy atlas and skeleton data from asset files // Load the Spineboy atlas and skeleton data from asset files
// and create a SpineComponent from them, scaled down and // and create a SpineComponent from them, scaled down and
// centered on the screen // centered on the screen
spineboy = await SpineComponent.fromAssets( spineboy = await SpineComponent.fromAssets("assets/spineboy.atlas", "assets/spineboy-pro.skel",
"assets/spineboy.atlas", "assets/spineboy-pro.skel", scale: Vector2(0.4, 0.4), anchor: Anchor.center, position: Vector2(size.x / 2, size.y / 2));
scale: Vector2(0.4, 0.4),
anchor: Anchor.center,
position: Vector2(size.x / 2, size.y / 2)
);
// Set the "walk" animation on track 0 in looping mode // Set the "walk" animation on track 0 in looping mode
spineboy.animationState.setAnimationByName(0, "walk", true); spineboy.animationState.setAnimationByName(0, "walk", true);
@ -119,11 +119,7 @@ class PreloadAndShareSpineDataExample extends FlameGame {
final drawable = SkeletonDrawable(cachedAtlas, cachedSkeletonData, false); final drawable = SkeletonDrawable(cachedAtlas, cachedSkeletonData, false);
final scale = 0.1 + rng.nextDouble() * 0.2; final scale = 0.1 + rng.nextDouble() * 0.2;
final position = Vector2(rng.nextDouble() * size.x, rng.nextDouble() * size.y); final position = Vector2(rng.nextDouble() * size.x, rng.nextDouble() * size.y);
final spineboy = SpineComponent( final spineboy = SpineComponent(drawable, scale: Vector2(scale, scale), position: position);
drawable,
scale: Vector2(scale, scale),
position: position
);
spineboy.animationState.setAnimationByName(0, "walk", true); spineboy.animationState.setAnimationByName(0, "walk", true);
spineboys.add(spineboy); spineboys.add(spineboy);
await add(spineboy); await add(spineboy);
@ -145,13 +141,11 @@ class PreloadAndShareSpineDataExample extends FlameGame {
class SpineFlameGameWidget extends StatelessWidget { class SpineFlameGameWidget extends StatelessWidget {
final FlameGame game; final FlameGame game;
const SpineFlameGameWidget(this.game, {super.key}); const SpineFlameGameWidget(this.game, {super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(appBar: AppBar(title: const Text('Flame Integration')), body: GameWidget(game: game));
appBar: AppBar(title: const Text('Flame Integration')),
body: GameWidget(game: game)
);
} }
} }

View File

@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart'; import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flutter/material.dart';
class IkFollowing extends StatefulWidget { class IkFollowing extends StatefulWidget {
const IkFollowing({Key? key}) : super(key: key); const IkFollowing({Key? key}) : super(key: key);

View File

@ -1,13 +1,13 @@
import 'package:flutter/material.dart';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart'; import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flutter/material.dart';
import 'package:spine_flutter_example/debug_rendering.dart'; import 'package:spine_flutter_example/debug_rendering.dart';
import 'flame_example.dart';
import 'simple_animation.dart';
import 'animation_state_events.dart'; import 'animation_state_events.dart';
import 'pause_play_animation.dart';
import 'dress_up.dart'; import 'dress_up.dart';
import 'flame_example.dart';
import 'ik_following.dart'; import 'ik_following.dart';
import 'pause_play_animation.dart';
import 'simple_animation.dart';
class ExampleSelector extends StatelessWidget { class ExampleSelector extends StatelessWidget {
const ExampleSelector({super.key}); const ExampleSelector({super.key});
@ -18,117 +18,109 @@ class ExampleSelector extends StatelessWidget {
return Scaffold( return Scaffold(
appBar: AppBar(title: const Text('Spine Examples')), appBar: AppBar(title: const Text('Spine Examples')),
body: Center( body: Center(
child: Column( child: Column(mainAxisSize: MainAxisSize.min, children: [
mainAxisSize: MainAxisSize.min, ElevatedButton(
children: [ child: const Text('Simple Animation'),
ElevatedButton( onPressed: () {
child: const Text('Simple Animation'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => const SimpleAnimation(),
MaterialPageRoute<void>( ),
builder: (context) => const SimpleAnimation(), );
), },
); ),
}, spacer,
), ElevatedButton(
spacer, child: const Text('Pause/Play animation'),
ElevatedButton( onPressed: () {
child: const Text('Pause/Play animation'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => const PlayPauseAnimation(),
MaterialPageRoute<void>( ),
builder: (context) => const PlayPauseAnimation(), );
), },
); ),
}, spacer,
), ElevatedButton(
spacer, child: const Text('Animation State Listener'),
ElevatedButton( onPressed: () {
child: const Text('Animation State Listener'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => const AnimationStateEvents(),
MaterialPageRoute<void>( ),
builder: (context) => const AnimationStateEvents(), );
), },
); ),
}, spacer,
), ElevatedButton(
spacer, child: const Text('Debug Rendering'),
ElevatedButton( onPressed: () {
child: const Text('Debug Rendering'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => const DebugRendering(),
MaterialPageRoute<void>( ),
builder: (context) => const DebugRendering(), );
), },
); ),
}, spacer,
), ElevatedButton(
spacer, child: const Text('Dress Up'),
ElevatedButton( onPressed: () {
child: const Text('Dress Up'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => const DressUp(),
MaterialPageRoute<void>( ),
builder: (context) => const DressUp(), );
), },
); ),
}, spacer,
), ElevatedButton(
spacer, child: const Text('IK Following'),
ElevatedButton( onPressed: () {
child: const Text('IK Following'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => const IkFollowing(),
MaterialPageRoute<void>( ),
builder: (context) => const IkFollowing(), );
), },
); ),
}, spacer,
), ElevatedButton(
spacer, child: const Text('Flame: Simple Example'),
ElevatedButton( onPressed: () {
child: const Text('Flame: Simple Example'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => SpineFlameGameWidget(SimpleFlameExample()),
MaterialPageRoute<void>( ),
builder: (context) => SpineFlameGameWidget(SimpleFlameExample()), );
), },
); ),
}, spacer,
), ElevatedButton(
spacer, child: const Text('Flame: Pre-load and share Spine data'),
ElevatedButton( onPressed: () {
child: const Text('Flame: Pre-load and share Spine data'), Navigator.push(
onPressed: () { context,
Navigator.push( MaterialPageRoute<void>(
context, builder: (context) => SpineFlameGameWidget(PreloadAndShareSpineDataExample()),
MaterialPageRoute<void>( ),
builder: (context) => SpineFlameGameWidget(PreloadAndShareSpineDataExample()), );
), },
); ),
}, spacer
), ])));
spacer
]
)
)
);
} }
} }
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
await initSpineFlutter(enableMemoryDebugging: false); await initSpineFlutter(enableMemoryDebugging: false);
runApp(const MaterialApp( runApp(const MaterialApp(title: "Spine Examples", home: ExampleSelector()));
title: "Spine Examples",
home: ExampleSelector()
));
} }

View File

@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart'; import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flutter/material.dart';
class PlayPauseAnimation extends StatefulWidget { class PlayPauseAnimation extends StatefulWidget {
const PlayPauseAnimation({Key? key}) : super(key: key); const PlayPauseAnimation({Key? key}) : super(key: key);
@ -33,11 +33,16 @@ class PlayPauseAnimationState extends State<PlayPauseAnimation> {
return Scaffold( return Scaffold(
appBar: AppBar(title: const Text('Play/Pause')), appBar: AppBar(title: const Text('Play/Pause')),
body: SpineWidget.asset("assets/dragon.atlas", "assets/dragon-ess.skel", controller, boundsProvider: SkinAndAnimationBounds(animation: "flying"),), body: SpineWidget.asset(
"assets/dragon.atlas",
"assets/dragon-ess.skel",
controller,
boundsProvider: SkinAndAnimationBounds(animation: "flying"),
),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: _togglePlay, onPressed: _togglePlay,
child: Icon(isPlaying ? Icons.pause : Icons.play_arrow), child: Icon(isPlaying ? Icons.pause : Icons.play_arrow),
), ),
); );
} }
} }

View File

@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:esotericsoftware_spine_flutter/spine_flutter.dart'; import 'package:esotericsoftware_spine_flutter/spine_flutter.dart';
import 'package:flutter/material.dart';
class SimpleAnimation extends StatelessWidget { class SimpleAnimation extends StatelessWidget {
const SimpleAnimation({Key? key}) : super(key: key); const SimpleAnimation({Key? key}) : super(key: key);
@ -19,4 +19,4 @@ class SimpleAnimation extends StatelessWidget {
// body: const SpineWidget.http("https://marioslab.io/dump/spineboy/spineboy.atlas", "https://marioslab.io/dump/spineboy/spineboy-pro.json"), // body: const SpineWidget.http("https://marioslab.io/dump/spineboy/spineboy.atlas", "https://marioslab.io/dump/spineboy/spineboy-pro.json"),
); );
} }
} }

View File

@ -3,9 +3,10 @@
// BSD-style license that can be found in the LICENSE file. // BSD-style license that can be found in the LICENSE file.
import 'dart:convert'; import 'dart:convert';
import 'ffi_proxy.dart';
import 'dart:typed_data'; import 'dart:typed_data';
import 'ffi_proxy.dart';
/// The contents of a native zero-terminated array of UTF-8 code units. /// The contents of a native zero-terminated array of UTF-8 code units.
/// ///
/// The Utf8 type itself has no functionality, it's only intended to be used /// The Utf8 type itself has no functionality, it's only intended to be used
@ -57,8 +58,7 @@ extension Utf8Pointer on Pointer<Utf8> {
void _ensureNotNullptr(String operation) { void _ensureNotNullptr(String operation) {
if (this == nullptr) { if (this == nullptr) {
throw UnsupportedError( throw UnsupportedError("Operation '$operation' not allowed on a 'nullptr'.");
"Operation '$operation' not allowed on a 'nullptr'.");
} }
} }
} }
@ -84,4 +84,4 @@ extension StringUtf8Pointer on String {
nativeString[units.length] = 0; nativeString[units.length] = 0;
return result.cast(); return result.cast();
} }
} }

View File

@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import 'package:inject_js/inject_js.dart' as js; import 'package:inject_js/inject_js.dart' as js;
import 'package:web_ffi_fork/web_ffi.dart'; import 'package:web_ffi_fork/web_ffi.dart';
import 'package:web_ffi_fork/web_ffi_modules.dart'; import 'package:web_ffi_fork/web_ffi_modules.dart';
import 'spine_flutter_bindings_generated.dart'; import 'spine_flutter_bindings_generated.dart';
Module? _module; Module? _module;
@ -61,7 +62,8 @@ Future<SpineFlutterFFI> initSpineFlutterFFI() async {
registerOpaqueType<spine_skin_entries_wrapper>(); registerOpaqueType<spine_skin_entries_wrapper>();
await js.importLibrary('assets/packages/esotericsoftware_spine_flutter/lib/assets/libspine_flutter.js'); await js.importLibrary('assets/packages/esotericsoftware_spine_flutter/lib/assets/libspine_flutter.js');
Uint8List wasmBinaries = (await rootBundle.load('packages/esotericsoftware_spine_flutter/lib/assets/libspine_flutter.wasm')).buffer.asUint8List(); Uint8List wasmBinaries =
(await rootBundle.load('packages/esotericsoftware_spine_flutter/lib/assets/libspine_flutter.wasm')).buffer.asUint8List();
_module = await EmscriptenModule.compile(wasmBinaries, 'libspine_flutter'); _module = await EmscriptenModule.compile(wasmBinaries, 'libspine_flutter');
} }
Module? m = _module; Module? m = _module;
@ -71,4 +73,4 @@ Future<SpineFlutterFFI> initSpineFlutterFFI() async {
} else { } else {
throw Exception("Couldn't load libspine-flutter.js/.wasm"); throw Exception("Couldn't load libspine-flutter.js/.wasm");
} }
} }