forked from flame-engine/flame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.dart
More file actions
132 lines (117 loc) · 3.11 KB
/
sprite.dart
File metadata and controls
132 lines (117 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'dart:ui';
import 'dart:async';
import 'flame.dart';
import 'position.dart';
import 'palette.dart';
class Sprite {
Paint paint = BasicPalette.white.paint;
Image image;
Rect src;
Sprite(
String fileName, {
double x = 0.0,
double y = 0.0,
double width,
double height,
}) {
Flame.images.load(fileName).then((img) {
width ??= img.width.toDouble();
height ??= img.height.toDouble();
image = img;
src = Rect.fromLTWH(x, y, width, height);
});
}
Sprite.fromImage(
this.image, {
double x = 0.0,
double y = 0.0,
double width,
double height,
}) {
width ??= image.width.toDouble();
height ??= image.height.toDouble();
src = Rect.fromLTWH(x, y, width, height);
}
static Future<Sprite> loadSprite(
String fileName, {
double x = 0.0,
double y = 0.0,
double width,
double height,
}) async {
final Image image = await Flame.images.load(fileName);
return Sprite.fromImage(
image,
x: x,
y: y,
width: width,
height: height,
);
}
bool loaded() {
return image != null && src != null;
}
double get _imageWidth => image.width.toDouble();
double get _imageHeight => image.height.toDouble();
Position get originalSize {
if (!loaded()) {
return null;
}
return Position(_imageWidth, _imageHeight);
}
Position get size {
return Position(src.width, src.height);
}
/// Renders this Sprite on the position [p], scaled by the [scale] factor provided.
///
/// It renders with src size multiplied by [scale] in both directions.
/// Anchor is on top left as default.
/// If not loaded, does nothing.
void renderScaled(Canvas canvas, Position p,
{double scale = 1.0, Paint overridePaint}) {
if (!loaded()) {
return;
}
renderPosition(canvas, p,
size: size.times(scale), overridePaint: overridePaint);
}
void renderPosition(Canvas canvas, Position p,
{Position size, Paint overridePaint}) {
if (!loaded()) {
return;
}
size ??= this.size;
renderRect(canvas, Position.rectFrom(p, size),
overridePaint: overridePaint);
}
void render(Canvas canvas,
{double width, double height, Paint overridePaint}) {
if (!loaded()) {
return;
}
width ??= size.x;
height ??= size.y;
renderRect(canvas, Rect.fromLTWH(0.0, 0.0, width, height),
overridePaint: overridePaint);
}
/// Renders this sprite centered in the position [p], i.e., on [p] - [size] / 2.
///
/// If [size] is not provided, the original size of the src image is used.
/// If the asset is not yet loaded, it does nothing.
void renderCentered(Canvas canvas, Position p,
{Position size, Paint overridePaint}) {
if (!loaded()) {
return;
}
size ??= this.size;
renderRect(canvas,
Rect.fromLTWH(p.x - size.x / 2, p.y - size.y / 2, size.x, size.y),
overridePaint: overridePaint);
}
void renderRect(Canvas canvas, Rect dst, {Paint overridePaint}) {
if (!loaded()) {
return;
}
canvas.drawImageRect(image, src, dst, overridePaint ?? paint);
}
}