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
use math::relative_source_rectangle;
use types::SourceRectangle;

/// Should be implemented by contexts that
/// have source rectangle information.
pub trait SourceRectangled {
    /// Adds a source rectangle.
    fn src_rect(self, x: i32, y: i32, w: i32, h: i32) -> Self;

    /// Moves to a relative source rectangle using
    /// the current source rectangle as tile.
    fn src_rel(self, x: i32, y: i32) -> Self;

    /// Flips the source rectangle horizontally.
    fn src_flip_h(self) -> Self;

    /// Flips the source rectangle vertically.
    fn src_flip_v(self) -> Self;

    /// Flips the source rectangle horizontally and vertically.
    fn src_flip_hv(self) -> Self;
}

impl SourceRectangled for SourceRectangle {
    #[inline(always)]
    fn src_rect(self, x: i32, y: i32, w: i32, h: i32) -> Self {
        [x, y, w, h]
    }

    #[inline(always)]
    fn src_rel(self, x: i32, y: i32) -> Self {
        relative_source_rectangle(self, x, y)
    }

    #[inline(always)]
    fn src_flip_h(self) -> Self {
        [
            self[0] + self[2],
            self[1],
            -self[2],
            self[3]
        ]
    }

    #[inline(always)]
    fn src_flip_v(self) -> Self {
        [
            self[0],
            self[1] + self[3],
            self[2],
            -self[3]
        ]
    }

    #[inline(always)]
    fn src_flip_hv(self) -> Self {
        [
            self[0] + self[2],
            self[1] + self[3],
            -self[2],
            -self[3]
        ]
    }
}