mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 20:36:31 +08:00
box fit for sprite components
This commit is contained in:
@ -1,20 +1,25 @@
|
|||||||
import 'dart:ui';
|
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flutter/painting.dart';
|
||||||
|
|
||||||
import 'flame.dart';
|
import 'flame.dart';
|
||||||
|
|
||||||
abstract class Component {
|
abstract class Component {
|
||||||
|
|
||||||
void update(double t);
|
void update(double t);
|
||||||
|
|
||||||
void render(Canvas c);
|
void render(Canvas c);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class PositionComponent extends Component {
|
abstract class PositionComponent extends Component {
|
||||||
double x, y, angle;
|
double x = 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
double distance(PositionComponent c) {
|
double distance(PositionComponent c) {
|
||||||
@ -28,22 +33,33 @@ abstract class SpriteComponent extends PositionComponent {
|
|||||||
double width, height;
|
double width, height;
|
||||||
Image image;
|
Image image;
|
||||||
|
|
||||||
final Paint paint = new Paint()..color = new Color(0xffffffff);
|
final Paint paint = new Paint()
|
||||||
|
..color = new Color(0xffffffff);
|
||||||
|
|
||||||
SpriteComponent.square(double size, String imagePath) : this.rectangle(size, size, imagePath);
|
SpriteComponent.square(double size, String imagePath)
|
||||||
|
: this.rectangle(size, size, imagePath);
|
||||||
|
|
||||||
SpriteComponent.rectangle(this.width, this.height, String imagePath) {
|
SpriteComponent.rectangle(this.width, this.height, String imagePath) {
|
||||||
Flame.images.load(imagePath).then((image) { this.image = image; });
|
Flame.images.load(imagePath).then((image) {
|
||||||
|
this.image = image;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
render(Canvas canvas) {
|
render(Canvas canvas) {
|
||||||
canvas.translate(x, y);
|
canvas.translate(x, y);
|
||||||
canvas.rotate(PI/2 + angle);
|
canvas.rotate(angle); // TODO: rotate around center
|
||||||
canvas.translate(-width/2, -height/2);
|
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
Rect src = new Rect.fromLTWH(0.0, 0.0, image.width.toDouble(), image.height.toDouble());
|
final Rect outputRect = new Rect.fromLTWH(0.0, 0.0, width, height);
|
||||||
Rect dst = new Rect.fromLTWH(0.0, 0.0, width, height);
|
|
||||||
canvas.drawImageRect(image, src, dst, paint);
|
final Size imageSize = new Size(
|
||||||
|
image.width.toDouble(), image.height.toDouble());
|
||||||
|
final FittedSizes sizes = applyBoxFit(
|
||||||
|
BoxFit.fill, imageSize, outputRect.size);
|
||||||
|
final Rect inputSubrect = Alignment.center.inscribe(
|
||||||
|
sizes.source, Offset.zero & imageSize);
|
||||||
|
final Rect outputSubrect = Alignment.center.inscribe(
|
||||||
|
sizes.destination, outputRect);
|
||||||
|
canvas.drawImageRect(image, inputSubrect, outputSubrect, paint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user