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
use std::cmp::min;
use vec::Vec2;
use XY;

/// Location of the view on screen
pub type Position = XY<Offset>;

impl Position {
    /// Returns a position centered on both axis.
    pub fn center() -> Self {
        Position::new(Offset::Center, Offset::Center)
    }

    /// Returns a position absolute on both axis.
    pub fn absolute<T: Into<Vec2>>(offset: T) -> Self {
        let offset = offset.into();
        Position::new(Offset::Absolute(offset.x), Offset::Absolute(offset.y))
    }

    /// Returns a position relative to the parent on both axis.
    pub fn parent<T: Into<XY<isize>>>(offset: T) -> Self {
        let offset = offset.into();
        Position::new(Offset::Parent(offset.x), Offset::Parent(offset.y))
    }

    /// Computes the offset required to draw a view.
    ///
    /// When drawing a view with `size` in a container with `available`,
    /// and a parent with the absolute coordinates `parent`, drawing the
    /// child with its top-left corner at the returned coordinates will
    /// position him appropriately.
    pub fn compute_offset<S, A, P>(
        &self, size: S, available: A, parent: P,
    ) -> Vec2
    where
        S: Into<Vec2>,
        A: Into<Vec2>,
        P: Into<Vec2>,
    {
        let available = available.into();
        let size = size.into();
        let parent = parent.into();

        Vec2::new(
            self.x.compute_offset(size.x, available.x, parent.x),
            self.y.compute_offset(size.y, available.y, parent.y),
        )
    }
}

/// Single-dimensional offset policy.
#[derive(PartialEq, Debug, Clone)]
pub enum Offset {
    /// In the center of the screen
    Center,
    /// Place top-left corner at the given absolute coordinates
    Absolute(usize),

    /// Offset from the previous layer's top-left corner.
    ///
    /// If this is the first layer, behaves like `Absolute`.
    Parent(isize),
}

impl Offset {
    /// Computes a single-dimension offset requred to draw a view.
    pub fn compute_offset(
        &self, size: usize, available: usize, parent: usize,
    ) -> usize {
        if size > available {
            0
        } else {
            match *self {
                Offset::Center => (available - size) / 2,
                Offset::Absolute(offset) => min(offset, available - size),
                Offset::Parent(offset) => {
                    min((parent as isize + offset) as usize, available - size)
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {

    use super::Position;
    use vec::Vec2;

    #[test]
    fn test_center() {
        let c = Position::center();
        assert_eq!(Vec2::new(2, 1), c.compute_offset((1, 1), (5, 3), (0, 0)));
        assert_eq!(Vec2::new(2, 0), c.compute_offset((1, 3), (5, 3), (0, 0)));
        assert_eq!(Vec2::new(1, 1), c.compute_offset((3, 1), (5, 3), (0, 0)));
        assert_eq!(Vec2::new(0, 1), c.compute_offset((5, 1), (5, 3), (0, 0)));
        assert_eq!(Vec2::new(0, 0), c.compute_offset((5, 3), (5, 3), (0, 0)));
        assert_eq!(Vec2::new(0, 0), c.compute_offset((5, 3), (3, 1), (0, 0)));
    }
}