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
use crate::{
    draw_target::DrawTarget,
    geometry::{Dimensions, OriginDimensions},
    image::ImageDrawable,
    primitives::Rectangle,
    transform::Transform,
};

/// Sub image.
///
/// A sub image is rectangular subsection of an [`ImageDrawable`]. It can, for example, be used to
/// draw individual sprites from a larger sprite atlas.
///
/// To create a sub image call the [`sub_image`] method on the parent [`ImageDrawable`]. See the
/// [module-level documentation] for an example.
///
/// [`ImageDrawable`]: trait.ImageDrawable.html
/// [`sub_image`]: trait.ImageDrawableExt.html#tymethod.sub_image
/// [module-level documentation]: index.html#sub-images
#[derive(Debug)]
pub struct SubImage<'a, T> {
    parent: &'a T,
    area: Rectangle,
}

impl<'a, T> SubImage<'a, T>
where
    T: ImageDrawable,
{
    pub(super) fn new(parent: &'a T, area: &Rectangle) -> Self {
        let area = parent.bounding_box().intersection(area);

        Self { parent, area }
    }

    pub(crate) fn new_unchecked(parent: &'a T, area: Rectangle) -> Self {
        Self { parent, area }
    }
}

impl<T> OriginDimensions for SubImage<'_, T> {
    fn size(&self) -> crate::prelude::Size {
        self.area.size
    }
}

impl<'a, T> ImageDrawable for SubImage<'a, T>
where
    T: ImageDrawable,
{
    type Color = T::Color;

    fn draw<DT>(&self, target: &mut DT) -> Result<(), DT::Error>
    where
        DT: DrawTarget<Color = Self::Color>,
    {
        self.parent.draw_sub_image(target, &self.area)
    }

    fn draw_sub_image<DT>(&self, target: &mut DT, area: &Rectangle) -> Result<(), DT::Error>
    where
        DT: DrawTarget<Color = Self::Color>,
    {
        let area = area.translate(self.area.top_left);

        self.parent.draw_sub_image(target, &area)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        geometry::{Point, Size},
        image::ImageDrawableExt,
        mock_display::MockDisplay,
        pixelcolor::BinaryColor,
    };

    struct MockImageDrawable {
        expected_area: Rectangle,
    }

    impl ImageDrawable for MockImageDrawable {
        type Color = BinaryColor;

        fn draw<DT>(&self, _target: &mut DT) -> Result<(), DT::Error>
        where
            DT: DrawTarget<Color = BinaryColor>,
        {
            panic!("draw shouldn't have been called on MockImageDrawable")
        }

        fn draw_sub_image<DT>(&self, _target: &mut DT, area: &Rectangle) -> Result<(), DT::Error>
        where
            DT: DrawTarget<Color = BinaryColor>,
        {
            assert_eq!(area, &self.expected_area);

            Ok(())
        }
    }

    impl OriginDimensions for MockImageDrawable {
        fn size(&self) -> Size {
            Size::new(8, 10)
        }
    }

    #[test]
    fn sub_image() {
        let area = Rectangle::new(Point::new(2, 3), Size::new(3, 4));

        let image = MockImageDrawable {
            expected_area: area,
        };

        let mut display = MockDisplay::new();
        image.sub_image(&area).draw(&mut display).unwrap();
    }

    #[test]
    fn area_larger_than_parent() {
        let area = Rectangle::new(Point::new(-5, -5), Size::new(20, 20));

        let image = MockImageDrawable {
            expected_area: Rectangle::new(Point::zero(), Size::new(8, 10)),
        };

        let mut display = MockDisplay::new();
        image.sub_image(&area).draw(&mut display).unwrap();
    }

    #[test]
    fn sub_image_of_sub_image() {
        let area1 = Rectangle::new(Point::new(2, 3), Size::new(3, 4));
        let area2 = Rectangle::new(Point::new(1, 1), Size::new(2, 2));

        let image = MockImageDrawable {
            expected_area: Rectangle::new(area1.top_left + area2.top_left, area2.size),
        };

        let mut display = MockDisplay::new();
        image
            .sub_image(&area1)
            .sub_image(&area2)
            .draw(&mut display)
            .unwrap();
    }

    #[test]
    fn sub_image_of_sub_image_area_larger_than_parent() {
        let area1 = Rectangle::new(Point::new(2, 3), Size::new(3, 4));
        let area2 = Rectangle::new(Point::new(-10, -10), Size::new(20, 20));

        let image = MockImageDrawable {
            expected_area: area1,
        };

        let mut display = MockDisplay::new();
        image
            .sub_image(&area1)
            .sub_image(&area2)
            .draw(&mut display)
            .unwrap();
    }
}