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

@ -1,19 +1,20 @@
import 'package:flutter/material.dart' hide Animation;
import 'package:flame/animation.dart';
import '../anchor.dart';
import './sprite_widget.dart';
import 'dart:math';
class AnimationWidget extends StatefulWidget {
final Animation animation;
final bool center;
final Anchor anchor;
final bool play;
AnimationWidget({
this.animation,
this.play = true,
this.center = false,
this.anchor = Anchor.topLeft,
}) : assert(animation.loaded(), 'Animation must be loaded');
@override
@ -83,6 +84,6 @@ class _AnimationWidget extends State<AnimationWidget>
@override
Widget build(ctx) {
return SpriteWidget(
sprite: widget.animation.getSprite(), center: widget.center);
sprite: widget.animation.getSprite(), anchor: widget.anchor);
}
}

View File

@ -1,35 +1,36 @@
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import '../sprite.dart';
import '../anchor.dart';
import 'dart:math';
class SpriteWidget extends StatelessWidget {
final Sprite sprite;
final bool center;
final Anchor anchor;
SpriteWidget({
@required this.sprite,
this.center = false,
this.anchor = Anchor.topLeft,
}) : assert(sprite.loaded(), 'Sprite must be loaded');
@override
Widget build(_) {
return Container(
child: CustomPaint(painter: _FlameSpritePainer(sprite, center)),
child: CustomPaint(painter: _SpritePainer(sprite, anchor)),
);
}
}
class _FlameSpritePainer extends CustomPainter {
class _SpritePainer extends CustomPainter {
final Sprite _sprite;
final bool _center;
final Anchor _anchor;
_FlameSpritePainer(this._sprite, this._center);
_SpritePainer(this._sprite, this._anchor);
@override
bool shouldRepaint(_FlameSpritePainer old) =>
old._sprite != _sprite || old._center != _center;
bool shouldRepaint(_SpritePainer old) =>
old._sprite != _sprite || old._anchor != _anchor;
@override
void paint(Canvas canvas, Size size) {
@ -38,20 +39,17 @@ class _FlameSpritePainer extends CustomPainter {
final rate = min(widthRate, heightRate);
final rect = Rect.fromLTWH(
0,
0,
_sprite.size.x * rate,
_sprite.size.y * rate,
final w = _sprite.size.x * rate;
final h = _sprite.size.y * rate;
final double dx = _anchor.relativePosition.dx * size.width;
final double dy = _anchor.relativePosition.dy * size.height;
canvas.translate(
dx - w * _anchor.relativePosition.dx,
dy - h * _anchor.relativePosition.dy,
);
if (_center) {
canvas.translate(
size.width / 2 - rect.width / 2,
size.height / 2 - rect.height / 2,
);
}
_sprite.renderRect(canvas, rect);
_sprite.render(canvas, width: w, height: h);
}
}