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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use super::control::*;
use super::property::*;

///
/// Converts Positions to actual coordinates
///
pub struct PositionLayout {
    /// Most recent position
    last_pos: f32,

    /// The starting position in this layout
    start: f32,

    /// The length of the axis that this layout is along
    length: f32,
}

impl PositionLayout {
    ///
    /// Creates a new position layout
    ///
    pub fn new(length: f32) -> PositionLayout {
        PositionLayout { last_pos: 0.0, start: 0.0, length: length }
    }

    ///
    /// Converts a point to an absolute position within this layout
    ///
    pub fn to_abs(&mut self, pos: &Position) -> f32 {
        use Position::*;

        let next_pos = match pos {
            &At(pos)            => pos,
            &Offset(pos)        => self.last_pos + pos,
            &Stretch(_ratio)    => self.last_pos,
            &Start              => self.start,
            &End                => self.start + self.length,
            &After              => self.last_pos,

            &Floating(Property::Float(pos), offset) => (pos as f32) + offset,
            &Floating(_, _)                         => 0.0
        };

        self.last_pos = next_pos;

        next_pos
    }
}

///
/// Perfoms layout with a bounding box
///
pub struct BoundsLayout {
    x_layout: PositionLayout,
    y_layout: PositionLayout
}

impl BoundsLayout {
    ///
    /// Creates a new bounds layout that acts within a particular bounding box
    ///
    pub fn new(width: f32, height: f32) -> BoundsLayout {
        BoundsLayout {
            x_layout: PositionLayout::new(width),
            y_layout: PositionLayout::new(height)
        }
    }

    ///
    /// Lays out a single point within this bounding box
    ///
    pub fn to_abs(&mut self, x: &Position, y: &Position) -> (f32, f32) {
        (self.x_layout.to_abs(x), self.y_layout.to_abs(y))
    }

    ///
    /// Lays out some rectangular bounds within this bounding box
    ///
    pub fn to_abs_bounds(&mut self, bounds: &Bounds) -> ((f32, f32), (f32, f32)) {
        (self.to_abs(&bounds.x1, &bounds.y1), self.to_abs(&bounds.x2, &bounds.y2))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use Position::*;

    #[test]
    pub fn position_layout_begins_at_start() {
        let mut layout = PositionLayout::new(100.0);

        assert!(layout.to_abs(&Start) == 0.0);
    }

    #[test]
    pub fn position_layout_after_pos_is_initially_zero() {
        let mut layout = PositionLayout::new(100.0);

        assert!(layout.to_abs(&After) == 0.0);
    }

    #[test]
    pub fn position_layout_absolute_is_absolute() {
        let mut layout = PositionLayout::new(100.0);

        assert!(layout.to_abs(&At(50.0)) == 50.0);
    }

    #[test]
    pub fn position_layout_end_is_end() {
        let mut layout = PositionLayout::new(100.0);

        assert!(layout.to_abs(&End) == 100.0);
    }

    #[test]
    pub fn position_layout_sequence() {
        let mut layout = PositionLayout::new(100.0);

        assert!(layout.to_abs(&Start) == 0.0);
        assert!(layout.to_abs(&Offset(20.0)) == 20.0);

        assert!(layout.to_abs(&After) == 20.0);
        assert!(layout.to_abs(&Offset(20.0)) == 40.0);

        assert!(layout.to_abs(&After) == 40.0);
        assert!(layout.to_abs(&End) == 100.0);
    }

    #[test]
    pub fn bounds_layout_begins_at_start() {
        let mut layout = BoundsLayout::new(200.0, 100.0);

        assert!(layout.to_abs(&Start, &Start) == (0.0, 0.0));
    }

    #[test]
    pub fn bounds_layout_after_pos_is_initially_zero() {
        let mut layout = BoundsLayout::new(200.0, 100.0);

        assert!(layout.to_abs(&After, &After) == (0.0, 0.0));
    }

    #[test]
    pub fn bounds_layout_absolute_is_absolute() {
        let mut layout = BoundsLayout::new(200.0, 100.0);

        assert!(layout.to_abs(&At(20.0), &At(50.0)) == (20.0, 50.0));
    }

    #[test]
    pub fn bounds_layout_end_is_end() {
        let mut layout = BoundsLayout::new(200.0, 100.0);

        assert!(layout.to_abs(&End, &End) == (200.0, 100.0));
    }

    #[test]
    pub fn bounds_layout_can_layout_controls_vertical() {
        let mut layout = BoundsLayout::new(20.0, 200.0);

        // Like we're laying out a vertical tool pane
        assert!(layout.to_abs_bounds(&Bounds::next_vert(20.0)) == ((0.0, 0.0), (20.0, 20.0)));
        assert!(layout.to_abs_bounds(&Bounds::next_vert(20.0)) == ((0.0, 20.0), (20.0, 40.0)));
        assert!(layout.to_abs_bounds(&Bounds::next_vert(20.0)) == ((0.0, 40.0), (20.0, 60.0)));

        assert!(layout.to_abs_bounds(&Bounds::fill_vert()) == ((0.0, 60.0), (20.0, 200.0)));
    }

    #[test]
    pub fn bounds_layout_can_layout_controls_horizontal() {
        let mut layout = BoundsLayout::new(200.0, 20.0);

        // Like we're laying out a vertical tool pane
        assert!(layout.to_abs_bounds(&Bounds::next_horiz(20.0)) == ((0.0, 0.0), (20.0, 20.0)));
        assert!(layout.to_abs_bounds(&Bounds::next_horiz(20.0)) == ((20.0, 0.0), (40.0, 20.0)));
        assert!(layout.to_abs_bounds(&Bounds::next_horiz(20.0)) == ((40.0, 0.0), (60.0, 20.0)));

        assert!(layout.to_abs_bounds(&Bounds::fill_horiz()) == ((60.0, 0.0), (200.0, 20.0)));
    }
}