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;

/// An asset handle to sprite sheet metadata.
pub type SpriteSheetHandle = Handle<SpriteSheet>;

/// Meta data for a sprite sheet texture.
///
/// Contains a handle to the texture and the sprite coordinates on the texture.
#[derive(Clone, Debug)]
pub struct SpriteSheet {
    /// Index of the texture for this sprite sheet.
    pub index: usize,
    /// A list of sprites in this sprite sheet.
    pub sprites: Vec<Sprite>,
}

impl Asset for SpriteSheet {
    const NAME: &'static str = "renderer::Sprite";
    type Data = Self;
    type HandleStorage = VecStorage<Handle<Self>>;
}

/// A description of a frame in a sprite sheet.
///
/// These should be in normalized coordinates:
///
/// * X axis: 0.0 is the left side and 1.0 is the right side.
/// * Y axis: 0.0 is the top and 1.0 is the bottom.
#[derive(Clone, Debug, PartialEq)]
pub struct Sprite {
    /// Normalized left x coordinate
    pub left: f32,
    /// Normalized right x coordinate
    pub right: f32,
    /// Normalized top y coordinate
    pub top: f32,
    /// Normalized bottom y coordinate
    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()
        );
    }
}