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
use math::{relative_source_rectangle, Scalar};
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: Scalar, y: Scalar, w: Scalar, h: Scalar) -> Self;

    /// Moves to a relative source rectangle using
    /// the current source rectangle as tile.
    fn src_rel(self, x: Scalar, y: Scalar) -> 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: Scalar, y: Scalar, w: Scalar, h: Scalar) -> Self {
        [x, y, w, h]
    }

    #[inline(always)]
    fn src_rel(self, x: Scalar, y: Scalar) -> 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]]
    }
}