mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 20:36:31 +08:00
move prepare canvas to position component
This commit is contained in:
@ -12,9 +12,7 @@ abstract class Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class PositionComponent extends Component {
|
abstract class PositionComponent extends Component {
|
||||||
double x = 0.0,
|
double x = 0.0, y = 0.0, angle = 0.0;
|
||||||
y = 0.0,
|
|
||||||
angle = 0.0;
|
|
||||||
|
|
||||||
double angleBetween(PositionComponent c) {
|
double angleBetween(PositionComponent c) {
|
||||||
return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI);
|
return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI);
|
||||||
@ -23,14 +21,18 @@ abstract class PositionComponent extends Component {
|
|||||||
double distance(PositionComponent c) {
|
double distance(PositionComponent c) {
|
||||||
return sqrt(pow(this.y - c.y, 2) + pow(this.x - c.x, 2));
|
return sqrt(pow(this.y - c.y, 2) + pow(this.x - c.x, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void prepareCanvas(Canvas canvas) {
|
||||||
|
canvas.translate(x, y);
|
||||||
|
canvas.rotate(angle); // TODO: rotate around center
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class SpriteComponent extends PositionComponent {
|
abstract class SpriteComponent extends PositionComponent {
|
||||||
double width, height;
|
double width, height;
|
||||||
Image image;
|
Image image;
|
||||||
|
|
||||||
final Paint paint = new Paint()
|
final Paint paint = new Paint()..color = new Color(0xffffffff);
|
||||||
..color = new Color(0xffffffff);
|
|
||||||
|
|
||||||
SpriteComponent.square(double size, String imagePath)
|
SpriteComponent.square(double size, String imagePath)
|
||||||
: this.rectangle(size, size, imagePath);
|
: this.rectangle(size, size, imagePath);
|
||||||
@ -43,26 +45,21 @@ abstract class SpriteComponent extends PositionComponent {
|
|||||||
|
|
||||||
render(Canvas canvas) {
|
render(Canvas canvas) {
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
_prepareCanvas(canvas);
|
prepareCanvas(canvas);
|
||||||
_drawImage(canvas);
|
_drawImage(canvas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _prepareCanvas(Canvas canvas) {
|
|
||||||
canvas.translate(x, y);
|
|
||||||
canvas.rotate(angle); // TODO: rotate around center
|
|
||||||
}
|
|
||||||
|
|
||||||
void _drawImage(Canvas canvas) {
|
void _drawImage(Canvas canvas) {
|
||||||
final Rect outputRect = new Rect.fromLTWH(0.0, 0.0, width, height);
|
final Rect outputRect = new Rect.fromLTWH(0.0, 0.0, width, height);
|
||||||
final Size imageSize =
|
final Size imageSize =
|
||||||
new Size(image.width.toDouble(), image.height.toDouble());
|
new Size(image.width.toDouble(), image.height.toDouble());
|
||||||
final FittedSizes sizes =
|
final FittedSizes sizes =
|
||||||
applyBoxFit(BoxFit.cover, imageSize, outputRect.size);
|
applyBoxFit(BoxFit.cover, imageSize, outputRect.size);
|
||||||
final Rect inputSubrect =
|
final Rect inputSubrect =
|
||||||
Alignment.center.inscribe(sizes.source, Offset.zero & imageSize);
|
Alignment.center.inscribe(sizes.source, Offset.zero & imageSize);
|
||||||
final Rect outputSubrect =
|
final Rect outputSubrect =
|
||||||
Alignment.center.inscribe(sizes.destination, outputRect);
|
Alignment.center.inscribe(sizes.destination, outputRect);
|
||||||
canvas.drawImageRect(image, inputSubrect, outputSubrect, paint);
|
canvas.drawImageRect(image, inputSubrect, outputSubrect, paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user