graphics/
source_rectangled.rs

1use crate::{
2    math::{relative_source_rectangle, Scalar},
3    types::SourceRectangle,
4};
5
6/// Should be implemented by contexts that
7/// have source rectangle information.
8pub trait SourceRectangled {
9    /// Adds a source rectangle.
10    fn src_rect(self, x: Scalar, y: Scalar, w: Scalar, h: Scalar) -> Self;
11
12    /// Moves to a relative source rectangle using
13    /// the current source rectangle as tile.
14    fn src_rel(self, x: Scalar, y: Scalar) -> Self;
15
16    /// Flips the source rectangle horizontally.
17    fn src_flip_h(self) -> Self;
18
19    /// Flips the source rectangle vertically.
20    fn src_flip_v(self) -> Self;
21
22    /// Flips the source rectangle horizontally and vertically.
23    fn src_flip_hv(self) -> Self;
24}
25
26impl SourceRectangled for SourceRectangle {
27    #[inline(always)]
28    fn src_rect(self, x: Scalar, y: Scalar, w: Scalar, h: Scalar) -> Self {
29        [x, y, w, h]
30    }
31
32    #[inline(always)]
33    fn src_rel(self, x: Scalar, y: Scalar) -> Self {
34        relative_source_rectangle(self, x, y)
35    }
36
37    #[inline(always)]
38    fn src_flip_h(self) -> Self {
39        [self[0] + self[2], self[1], -self[2], self[3]]
40    }
41
42    #[inline(always)]
43    fn src_flip_v(self) -> Self {
44        [self[0], self[1] + self[3], self[2], -self[3]]
45    }
46
47    #[inline(always)]
48    fn src_flip_hv(self) -> Self {
49        [self[0] + self[2], self[1] + self[3], -self[2], -self[3]]
50    }
51}