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
use amethyst_assets::{Asset, Handle};
use amethyst_core::specs::prelude::VecStorage;
pub type SpriteSheetHandle = Handle<SpriteSheet>;
#[derive(Clone, Debug)]
pub struct SpriteSheet {
pub index: usize,
pub sprites: Vec<Sprite>,
}
impl Asset for SpriteSheet {
const NAME: &'static str = "renderer::Sprite";
type Data = Self;
type HandleStorage = VecStorage<Handle<Self>>;
}
#[derive(Clone, Debug, PartialEq)]
pub struct Sprite {
pub left: f32,
pub right: f32,
pub top: f32,
pub bottom: f32,
}
impl From<((f32, f32), (f32, f32))> for Sprite {
fn from(((left, right), (top, bottom)): ((f32, f32), (f32, f32))) -> Self {
Sprite {
left,
right,
top,
bottom,
}
}
}
impl From<[f32; 4]> for Sprite {
fn from(uv: [f32; 4]) -> Self {
Sprite {
left: uv[0],
right: uv[1],
top: uv[2],
bottom: uv[3],
}
}
}
#[cfg(test)]
mod test {
use super::Sprite;
#[test]
fn sprite_from_tuple_maps_coordinates_correctly() {
assert_eq!(
Sprite {
left: 0.,
right: 0.5,
top: 0.75,
bottom: 1.0,
},
((0.0, 0.5), (0.75, 1.0)).into()
);
}
#[test]
fn sprite_from_slice_maps_coordinates_correctly() {
assert_eq!(
Sprite {
left: 0.,
right: 0.5,
top: 0.75,
bottom: 1.0,
},
[0.0, 0.5, 0.75, 1.0].into()
);
}
}