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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use super::image::Image;
use crate::builder::{DrawBuilder, DrawProcess};
use crate::draw::Draw;
use crate::transform::DrawTransform;
use notan_graphics::color::Color;
use notan_graphics::pipeline::BlendMode;
use notan_graphics::Texture;
use notan_math::Mat3;
enum TextureSource<'a> {
Grid {
texture: &'a Texture,
cols: usize,
rows: usize,
},
List(&'a [&'a Texture]),
}
pub struct ImageAnimation<'a> {
source: TextureSource<'a>,
color: Color,
alpha: f32,
pos: (f32, f32),
size: Option<(f32, f32)>,
time: f32,
frames: Option<&'a [usize]>,
matrix: Option<Mat3>,
blend_mode: Option<BlendMode>,
}
impl<'a> ImageAnimation<'a> {
pub fn from_grid(texture: &'a Texture, cols: usize, rows: usize) -> Self {
Self {
source: TextureSource::Grid {
texture,
cols,
rows,
},
color: Color::WHITE,
alpha: 1.0,
pos: (0.0, 0.0),
size: None,
matrix: None,
blend_mode: None,
frames: None,
time: 0.0,
}
}
pub fn from_list(list: &'a [&'a Texture]) -> Self {
Self {
source: TextureSource::List(list),
color: Color::WHITE,
alpha: 1.0,
pos: (0.0, 0.0),
size: None,
matrix: None,
blend_mode: None,
frames: None,
time: 0.0,
}
}
pub fn position(&mut self, x: f32, y: f32) -> &mut Self {
self.pos = (x, y);
self
}
pub fn size(&mut self, width: f32, height: f32) -> &mut Self {
self.size = Some((width, height));
self
}
pub fn frames(&mut self, frames: &'a [usize]) -> &mut Self {
self.frames = Some(frames);
self
}
pub fn time(&mut self, time: f32) -> &mut Self {
if time < 0.0 {
self.time = 1.0 + (time % 1.0)
} else {
self.time = time % 1.0;
};
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = color;
self
}
pub fn alpha(&mut self, alpha: f32) -> &mut Self {
self.alpha = alpha;
self
}
pub fn blend_mode(&mut self, mode: BlendMode) -> &mut Self {
self.blend_mode = Some(mode);
self
}
}
impl DrawTransform for ImageAnimation<'_> {
fn matrix(&mut self) -> &mut Option<Mat3> {
&mut self.matrix
}
}
impl DrawProcess for ImageAnimation<'_> {
fn draw_process(self, draw: &mut Draw) {
let Self {
color,
alpha,
pos: (x, y),
size,
time,
source,
matrix,
blend_mode,
frames,
} = self;
match source {
TextureSource::Grid {
texture,
cols,
rows,
} => {
let (tex_w, tex_h) = texture.size();
let tw = tex_w as usize / cols;
let th = tex_h as usize / rows;
let cs = cols as f32;
let rs = rows as f32;
let i = match frames {
None => (cs * rs * time).floor() as usize,
Some(f) => {
debug_assert!(f.iter().max().map(|v| *v < (cols * rows)).unwrap_or(false));
let i = (f.len() as f32 * time).floor() as usize;
f[i]
}
};
let xx = i % cols;
let yy = i / cols;
let tx = xx * tw;
let ty = yy * th;
let size = size.unwrap_or((tw as _, th as _));
img(draw, texture, matrix, blend_mode)
.crop((tx as _, ty as _), (tw as _, th as _))
.size(size.0, size.1)
.position(x, y)
.color(color)
.alpha(alpha);
}
TextureSource::List(list) => {
let i = match frames {
None => (list.len() as f32 * time).floor() as usize,
Some(f) => {
debug_assert!(f.iter().max().map(|v| *v < list.len()).unwrap_or(false));
let i = (f.len() as f32 * time).floor() as usize;
f[i]
}
};
let texture = list[i];
let size = size.unwrap_or_else(|| texture.size());
img(draw, texture, matrix, blend_mode)
.size(size.0, size.1)
.position(x, y)
.color(color)
.alpha(alpha);
}
}
}
}
#[inline(always)]
fn img<'a>(
draw: &'a mut Draw,
tex: &'a Texture,
mat: Option<Mat3>,
blend_mode: Option<BlendMode>,
) -> DrawBuilder<'a, Image<'a>> {
let mut img = Image::new(tex);
if let Some(bm) = blend_mode {
img.blend_mode(bm);
}
if let Some(m) = mat {
img.transform(m);
}
DrawBuilder::new(draw, img)
}