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
use crate::*;

/// Linearly transforms positions from one [`Rect`] to another.
///
/// `RectTransform` stores the rectangles, and therefore supports clamping and culling.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RectTransform {
    from: Rect,
    to: Rect,
}

impl RectTransform {
    pub fn identity(from_and_to: Rect) -> Self {
        Self::from_to(from_and_to, from_and_to)
    }

    pub fn from_to(from: Rect, to: Rect) -> Self {
        Self { from, to }
    }

    pub fn from(&self) -> &Rect {
        &self.from
    }

    pub fn to(&self) -> &Rect {
        &self.to
    }

    /// The scale factors.
    pub fn scale(&self) -> Vec2 {
        self.to.size() / self.from.size()
    }

    pub fn inverse(&self) -> RectTransform {
        Self::from_to(self.to, self.from)
    }

    /// Transforms the given coordinate in the `from` space to the `to` space.
    pub fn transform_pos(&self, pos: Pos2) -> Pos2 {
        pos2(
            remap(pos.x, self.from.x_range(), self.to.x_range()),
            remap(pos.y, self.from.y_range(), self.to.y_range()),
        )
    }

    /// Transforms the given rectangle in the `in`-space to a rectangle in the `out`-space.
    pub fn transform_rect(&self, rect: Rect) -> Rect {
        Rect {
            min: self.transform_pos(rect.min),
            max: self.transform_pos(rect.max),
        }
    }

    /// Transforms the given coordinate in the `from` space to the `to` space,
    /// clamping if necessary.
    pub fn transform_pos_clamped(&self, pos: Pos2) -> Pos2 {
        pos2(
            remap_clamp(pos.x, self.from.x_range(), self.to.x_range()),
            remap_clamp(pos.y, self.from.y_range(), self.to.y_range()),
        )
    }
}

/// Transforms the position.
impl std::ops::Mul<Pos2> for RectTransform {
    type Output = Pos2;
    fn mul(self, pos: Pos2) -> Pos2 {
        self.transform_pos(pos)
    }
}

/// Transforms the position.
impl std::ops::Mul<Pos2> for &RectTransform {
    type Output = Pos2;
    fn mul(self, pos: Pos2) -> Pos2 {
        self.transform_pos(pos)
    }
}