mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
size is optional when rendering sprites, if not present use original size; default values to null, instead of -1
This commit is contained in:
@ -11,12 +11,12 @@ class Sprite {
|
||||
|
||||
static final Paint paint = new Paint()..color = Colors.white;
|
||||
|
||||
Sprite(String fileName, {double x = 0.0, double y = 0.0, double width = -1.0, double height = -1.0}) {
|
||||
Sprite(String fileName, {double x = 0.0, double y = 0.0, double width = null, double height = null}) {
|
||||
Flame.images.load(fileName).then((img) {
|
||||
if (width == -1.0) {
|
||||
if (width == null) {
|
||||
width = img.width.toDouble();
|
||||
}
|
||||
if (height == -1.0) {
|
||||
if (height == null) {
|
||||
height = img.height.toDouble();
|
||||
}
|
||||
this.image = img;
|
||||
@ -24,11 +24,11 @@ class Sprite {
|
||||
});
|
||||
}
|
||||
|
||||
Sprite.fromImage(this.image, {double x = 0.0, double y = 0.0, double width = -1.0, double height = -1.0}) {
|
||||
if (width == -1.0) {
|
||||
Sprite.fromImage(this.image, {double x = 0.0, double y = 0.0, double width = null, double height = null}) {
|
||||
if (width == null) {
|
||||
width = image.width.toDouble();
|
||||
}
|
||||
if (height == -1.0) {
|
||||
if (height == null) {
|
||||
height = image.height.toDouble();
|
||||
}
|
||||
this.src = new Rect.fromLTWH(x, y, width, height);
|
||||
@ -43,17 +43,37 @@ class Sprite {
|
||||
return image != null && src != null;
|
||||
}
|
||||
|
||||
void renderPosition(Canvas canvas, Position p, Position size) {
|
||||
double get _imageWidth => this.image.width.toDouble();
|
||||
double get _imageHeight => this.image.height.toDouble();
|
||||
|
||||
Position get originalSize {
|
||||
if (!loaded()) {
|
||||
return null;
|
||||
}
|
||||
return new Position(_imageWidth, _imageHeight);
|
||||
}
|
||||
|
||||
void renderPosition(Canvas canvas, Position p, [Position size]) {
|
||||
if (!this.loaded()) {
|
||||
return;
|
||||
}
|
||||
size ??= originalSize;
|
||||
renderRect(canvas, Position.rectFrom(p, size));
|
||||
}
|
||||
|
||||
void render(Canvas canvas, double width, double height) {
|
||||
void render(Canvas canvas, [double width, double height]) {
|
||||
if (!this.loaded()) {
|
||||
return;
|
||||
}
|
||||
width ??= _imageWidth;
|
||||
height ??= _imageHeight;
|
||||
renderRect(canvas, new Rect.fromLTWH(0.0, 0.0, width, height));
|
||||
}
|
||||
|
||||
void renderRect(Canvas canvas, Rect dst) {
|
||||
if (this.loaded()) {
|
||||
canvas.drawImageRect(image, src, dst, paint);
|
||||
if (!this.loaded()) {
|
||||
return;
|
||||
}
|
||||
canvas.drawImageRect(image, src, dst, paint);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user