adding anchor to sprite and animation widget

This commit is contained in:
Erick Zanardo
2020-05-30 15:15:24 -03:00
parent 19b57b0c38
commit 9e4e27e100
4 changed files with 67 additions and 35 deletions

View File

@ -8,6 +8,32 @@ import 'package:flame/widgets/nine_tile_box.dart';
import 'package:flame/widgets/sprite_button.dart';
import 'package:flame/widgets/sprite_widget.dart';
import 'package:flame/widgets/animation_widget.dart';
import 'package:flame/anchor.dart';
Anchor parseAnchor(String name) {
switch (name) {
case 'Anchor.topLeft':
return Anchor.topLeft;
case 'Anchor.topCenter':
return Anchor.topCenter;
case 'Anchor.topRight':
return Anchor.topRight;
case 'Anchor.centerLeft':
return Anchor.centerLeft;
case 'Anchor.center':
return Anchor.center;
case 'Anchor.centerRight':
return Anchor.centerRight;
case 'Anchor.bottomLeft':
return Anchor.bottomLeft;
case 'Anchor.bottomCenter':
return Anchor.bottomCenter;
case 'Anchor.bottomRight':
return Anchor.bottomRight;
}
return null;
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
@ -63,16 +89,27 @@ void main() async {
),
);
final anchorOptions = [
'Anchor.topLeft',
'Anchor.topCenter',
'Anchor.topRight',
'Anchor.centerLeft',
'Anchor.center',
'Anchor.centerRight',
'Anchor.bottomLeft',
'Anchor.bottomCenter',
'Anchor.bottomRight',
];
final shieldSprite = await Sprite.loadSprite('shield.png');
dashbook.storiesOf('SpriteWidget').decorator(CenterDecorator()).add(
'default',
(ctx) => Container(
width: ctx.numberProperty('container width', 400),
height: ctx.numberProperty('container height', 200),
padding: const EdgeInsets.all(20),
child: SpriteWidget(
sprite: shieldSprite,
center: ctx.boolProperty('center', true),
anchor: parseAnchor(
ctx.listProperty('anchor', 'Anchor.center', anchorOptions)),
),
),
);
@ -85,22 +122,18 @@ void main() async {
columns: 4,
rows: 1,
);
final _animation = _animationSpriteSheet.createAnimation(
0,
stepTime: 0.2,
to: 3,
loop: true,
);
final _animation = _animationSpriteSheet.createAnimation(0,
stepTime: 0.2, to: 3, loop: true);
dashbook.storiesOf('AnimationWidget').decorator(CenterDecorator()).add(
'default',
(ctx) => Container(
width: ctx.numberProperty('container width', 400),
height: ctx.numberProperty('container height', 200),
padding: const EdgeInsets.all(20),
child: AnimationWidget(
animation: _animation,
play: ctx.boolProperty('playing', true),
center: ctx.boolProperty('center', true),
anchor: parseAnchor(
ctx.listProperty('anchor', 'Anchor.center', anchorOptions)),
),
),
);