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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use std::{path::Path, vec};
use image::{GenericImage, GenericImageView, RgbaImage};
use crate::{
error::{Error, Result},
sprite_cell::SpriteCell,
unordered_sprite_sheet::UnorderedSpriteSheet,
utils::IVec2,
Distribution, Sprite,
};
const EXPECT_MSG_OUTOFBOUNDS: &str =
"coords have already been checked to be inbounds and should be inbounds by this point; \
if this panic occurs then the SpriteSheet.size is not in sync with the size of the inner \
SpriteSheet.cells vector";
const EXPECT_MSG_SHEET_FULL: &str =
"Distribution::get_min_size should always return a size that fits";
/// An ordered sprite sheet. Contains a 2 dimensions array of [SpriteCell]s.
pub struct SpriteSheet {
cells: Vec<Vec<SpriteCell>>, // Vector of lines, each line is a vector of cells
size: IVec2,
cell_size: IVec2,
}
impl SpriteSheet {
/// Makes a new, empty [SpriteSheet]. It will be filled with [SpriteCell::Empty] cells.
/// `size` is the size of the [SpriteSheet], in cells.
/// `cell_size` is the size of the [Sprite]s that will go into the [SpriteSheet].
///
/// # Examples
///
/// ```
/// let sheet = SpriteSheet::new((5, 5), (128, 128));
///
/// assert!(sheet.cells().all(|cell| cell.is_empty()));
/// ```
pub fn new(size: IVec2, cell_size: IVec2) -> SpriteSheet {
SpriteSheet {
cells: {
let mut temp_vec = Vec::new(); // vector of lines
temp_vec.reserve_exact(size.1);
for _ in 0..size.1 {
temp_vec.push(vec![SpriteCell::Empty; size.0]);
}
temp_vec
},
size,
cell_size,
}
}
/// Gets the size, in cells, of the [SpriteSheet].
#[inline(always)]
pub fn size(&self) -> IVec2 {
self.size
}
/// Returns an immutable reference to the cell at cell coordonates `coords`.
///
/// # Errors
///
/// - Will return [Error::OutOfRange] if the specified `coords` are out of bounds.
pub fn get_cell(&self, coords: IVec2) -> Result<&SpriteCell> {
self.cells
.get(coords.1)
.ok_or(Error::OutOfBounds {
max: self.size,
provided: coords,
})?
.get(coords.0)
.ok_or(Error::OutOfBounds {
max: self.size,
provided: coords,
})
}
/// Returns a mutable reference to the cell at cell coordonates `coords`.
///
/// # Errors
///
/// - Will return [Error::OutOfRange] if the specified `coords` are out of bounds.
pub fn get_cell_mut(&mut self, coords: IVec2) -> Result<&mut SpriteCell> {
self.cells
.get_mut(coords.1)
.ok_or(Error::OutOfBounds {
max: self.size,
provided: coords,
})?
.get_mut(coords.0)
.ok_or(Error::OutOfBounds {
max: self.size,
provided: coords,
})
}
/// Sets the value of the cell at coordonates `coords` to the specified `cell`, returning the previous value
/// of the cell at these coordonates.
///
/// # Errors
///
/// - Will return [Error::OutOfRange] if the specified `coords` are out of bounds.
pub fn set_cell(&mut self, coords: IVec2, cell: SpriteCell) -> Result<SpriteCell> {
if coords.0 > self.size.0 || coords.1 > self.size.1 {
return Err(Error::OutOfBounds {
max: self.size,
provided: coords,
});
}
if let Some(size) = cell.size() {
if size != self.cell_size {
return Err(Error::MismatchedSpriteSize {
required: self.cell_size,
provided: size,
});
}
}
Ok(std::mem::replace(
self.cells
.get_mut(coords.1)
.expect(EXPECT_MSG_OUTOFBOUNDS)
.get_mut(coords.0)
.expect(EXPECT_MSG_OUTOFBOUNDS),
cell,
))
}
/// Returns an immutable iterator of all cells contained in the [SpriteSheet].
/// Cells are iterated from top left, to max width, and then to max height.
pub fn cells(&self) -> IterCells {
IterCells::new(self)
}
/// Returns a mutable iterator of all cells contained in the [SpriteSheet].
/// Cells are iterated from top left, to max width, and then to max height.
pub fn cells_mut(&mut self) -> IterCellsMut {
IterCellsMut::new(self)
}
/// Consumes this [SpriteSheet] and makes an [UnorderedSpriteSheet] containing all the *non-empty* cells
/// from this [SpriteSheet], from the top left, to max width, and then to max height.
pub fn into_unordered(self) -> Result<UnorderedSpriteSheet> {
UnorderedSpriteSheet::new(self.into_iter().filter_map(|item| item.sprite()).collect())
}
/// Puts a sprite in the first [SpriteCell::Empty] cell of the [SpriteSheet].
///
/// # Errors
///
/// - Will return [Error::SheetFull] if there is no [SpriteCell::Empty] left.
pub fn push_sprite(&mut self, sprite: Sprite) -> Result<()> {
*(self
.cells_mut()
.find(|item| item.is_empty())
.ok_or(Error::SheetFull { amount_fitted: 0 })?) = SpriteCell::Sprite(sprite);
Ok(())
}
/// Consumes an [UnorderedSpriteSheet], pushing all of its sprites into [SpriteCell::Empty] spaces of the
/// [SpriteSheet].
///
/// # Errors
///
/// - Will return [Error::SheetFull] if not all sprites were able to fit in the [SpriteSheet]. The ones that
/// did fit though, will still be pushed into the [SpriteSheet].
pub fn push_sprites(&mut self, sprites: UnorderedSpriteSheet) -> Result<()> {
for (fitted, sprite) in sprites.into_iter().enumerate() {
self.push_sprite(sprite).map_err(|_| Error::SheetFull {
amount_fitted: fitted as u32,
})?;
}
Ok(())
}
/// Makes a new [SpriteSheet] from an [UnorderedSpriteSheet], following the specified [Distribution].
/// [Sprite]s are going to be placed from the top left, to max width, and then to max height.
pub fn from_unordered(sprites: UnorderedSpriteSheet, distribution: Distribution) -> Self {
let mut sheet = Self::new(distribution.get_min_size(sprites.len()), sprites.size());
sheet.push_sprites(sprites).expect(EXPECT_MSG_SHEET_FULL);
sheet
}
/// Concatenates the [UnorderedSpriteSheet]s given in `sprites`, according to `distribution`.
///
/// # Errors
///
/// - Will return [Error::MismatchedSpriteSize] if all the [UnorderedSpriteSheet] don't
/// all have the same [Sprite] size.
pub fn concat<I>(sprites: I, distribution: Distribution) -> Result<Self>
where
I: Iterator<Item = UnorderedSpriteSheet>,
{
let list: Vec<UnorderedSpriteSheet> = sprites.collect();
if list.is_empty() {
return Err(Error::EmptyIterator);
}
let size = list[0].size();
let mut len = 0;
for unordered in list.iter() {
if unordered.size() != size {
return Err(Error::MismatchedSpriteSize {
required: size,
provided: unordered.size(),
});
}
len += unordered.len();
}
let mut sheet = Self::new(distribution.get_min_size(len), size);
for unordered in list {
sheet.push_sprites(unordered).expect(EXPECT_MSG_SHEET_FULL);
}
Ok(sheet)
}
/// Makes a [SpriteSheet] from a full [Sprite] that contains all the cells.
/// Divides the sheet according to the given number of divisions.
pub fn from_image_div(sprite: Sprite, divisions: IVec2) -> Self {
let cell_size = (sprite.size().0 / divisions.0, sprite.size().1 / divisions.1);
Self::from_image(sprite, divisions, cell_size)
}
/// Makes a [SpriteSheet] from a full [Sprite] that contains all the cells.
/// Divides the sheet according to the cell size.
pub fn from_image_cell_size(sprite: Sprite, cell_size: IVec2) -> Self {
let divisions = (sprite.size().0 / cell_size.0, sprite.size().1 / cell_size.1);
Self::from_image(sprite, divisions, cell_size)
}
fn from_image(sprite: Sprite, divisions: IVec2, cell_size: IVec2) -> Self {
let image = sprite.into_image();
let mut sheet = Self::new(divisions, cell_size);
for x in 0..divisions.0 {
for y in 0..divisions.1 {
let sub_sprite: Sprite = image
.view(
(x * cell_size.0) as u32,
(y * cell_size.1) as u32,
cell_size.0 as u32,
cell_size.1 as u32,
)
.to_image()
.into();
sheet
.set_cell(
(x, y),
if sub_sprite.is_empty() {
SpriteCell::Empty
} else {
SpriteCell::Sprite(sub_sprite)
},
)
.expect(EXPECT_MSG_OUTOFBOUNDS);
}
}
sheet
}
/// Consumes this [SpriteSheet], returning an [image::RgbaImage].
pub fn into_image(mut self) -> RgbaImage {
let mut final_image = RgbaImage::new(
(self.cell_size.0 * self.size.0) as u32,
(self.cell_size.1 * self.size.1) as u32,
);
for x in 0..self.size.0 {
for y in 0..self.size.1 {
match std::mem::replace(
self.cells[y].get_mut(x).expect(EXPECT_MSG_OUTOFBOUNDS),
SpriteCell::Empty,
) {
SpriteCell::Sprite(sprite) => final_image
.copy_from(
&sprite.into_image(),
(x * self.cell_size.0) as u32,
(y * self.cell_size.1) as u32,
)
.expect("image should have already been checked to be of the right size at insertion time"),
SpriteCell::Empty => (),
}
}
}
final_image
}
/// Loads a [SpriteSheet] from an image on the disk that contains all the cells.
/// Divides the sheet according to the given number of divisions.
///
/// # Errors
///
/// - Will return [Error::ImageError] if the underlying call to [image::open] returns an error.
pub fn load_div<P>(path: P, divisions: IVec2) -> Result<Self>
where
P: AsRef<Path>,
{
Ok(Self::from_image_div(Sprite::load(path)?, divisions))
}
/// Loads a [SpriteSheet] from an image on the disk that contains all the cells.
/// Divides the sheet according to the cell size.
///
/// # Errors
///
/// - Will return [Error::ImageError] if the underlying call to [image::open] returns an error.
pub fn load_cell_size<P>(path: P, cell_size: IVec2) -> Result<Self>
where
P: AsRef<Path>,
{
Ok(Self::from_image_cell_size(Sprite::load(path)?, cell_size))
}
/// Consumes and saves this [SpriteSheet] as an image to the disk.
/// Uses [image::RgbaImage::save], so the format will be guessed by the file extension.
///
/// # Errors
///
/// - Will return [Error::ImageError] if the underlying call to [image::open] returns an error.
pub fn save<P>(self, path: P) -> Result<()>
where
P: AsRef<Path>,
{
self.into_image().save(path).map_err(Error::ImageError)
}
}
impl IntoIterator for SpriteSheet {
type Item = SpriteCell;
type IntoIter = IntoIterCells;
fn into_iter(self) -> Self::IntoIter {
Self::IntoIter::new(self)
}
}
// IntoIter
pub struct IntoIterCells {
sheet_iter: vec::IntoIter<Vec<SpriteCell>>,
line_iter: Option<Vec<SpriteCell>>,
}
impl IntoIterCells {
fn new(sheet: SpriteSheet) -> Self {
Self {
sheet_iter: sheet.cells.into_iter(),
line_iter: None,
}
}
}
impl Iterator for IntoIterCells {
type Item = SpriteCell;
fn next(&mut self) -> Option<Self::Item> {
// take the next line (if none return none)
// take the next cell on the line (if none return none)
if match self.line_iter {
Some(ref it) if it.is_empty() => true, // is it empty
None => true, // or is it None
_ => false,
} {
self.line_iter = self.sheet_iter.next();
}
self.line_iter.as_mut().map(|vec| vec.remove(0))
}
}
// Iter
pub struct IterCells<'a> {
sheet: &'a SpriteSheet,
next_index: usize,
current_line: usize,
}
impl<'a> IterCells<'a> {
fn new(sheet: &'a SpriteSheet) -> Self {
Self {
sheet,
next_index: 0,
current_line: 0,
}
}
}
impl<'a> Iterator for IterCells<'a> {
type Item = &'a SpriteCell;
fn next(&mut self) -> Option<Self::Item> {
if self.next_index >= self.sheet.size.0 {
self.current_line += 1;
self.next_index = 0;
if self.current_line >= self.sheet.size.1 {
return None;
}
}
let next = self
.sheet
.cells
.get(self.current_line)
.expect(EXPECT_MSG_OUTOFBOUNDS)
.get(self.next_index)
.expect(EXPECT_MSG_OUTOFBOUNDS);
self.next_index += 1;
Some(next)
}
}
pub struct IterCellsMut<'a> {
sheet: &'a mut SpriteSheet,
next_index: usize,
current_line: usize,
}
impl<'a> IterCellsMut<'a> {
fn new(sheet: &'a mut SpriteSheet) -> Self {
Self {
sheet,
next_index: 0,
current_line: 0,
}
}
}
impl<'a> Iterator for IterCellsMut<'a> {
type Item = &'a mut SpriteCell;
fn next(&mut self) -> Option<Self::Item> {
if self.next_index >= self.sheet.size.0 {
self.current_line += 1;
self.next_index = 0;
if self.current_line >= self.sheet.size.1 {
return None;
}
}
let next = self
.sheet
.cells
.get_mut(self.current_line)
.expect(EXPECT_MSG_OUTOFBOUNDS)
.get_mut(self.next_index)
.expect(EXPECT_MSG_OUTOFBOUNDS);
self.next_index += 1;
Some(unsafe { std::mem::transmute(next) })
}
}