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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

use {CharacterCache, Scalar};
use color::Color;
use position::{Dimensions, Point, Positionable, Sizeable};
use super::canvas;
use widget::{self, Widget};
use ui::Ui;


/// A type of Canvas for flexibly designing and guiding widget layout as splits of a window.
pub struct Split<'a> {
    id: widget::Id,
    style: canvas::Style,
    maybe_splits: Option<(Direction, &'a [Split<'a>])>,
    maybe_length: Option<Scalar>,
    // TODO: Maybe use the `CommonBuilder` (to be used for the wrapped `Canvas`) here instead?
    is_h_scrollable: bool,
    is_v_scrollable: bool,
}


/// The direction in which the the **Split** should layout it's child **Split**s.
#[derive(Copy, Clone, Debug)]
enum Direction {
    Left,
    Right,
    Down,
    Up,
}


impl<'a> Split<'a> {

    /// Construct a default Canvas Split.
    pub fn new(id: widget::Id) -> Split<'a> {
        Split {
            id: id,
            style: canvas::Style::new(),
            maybe_splits: None,
            maybe_length: None,
            is_h_scrollable: false,
            is_v_scrollable: false,
        }
    }

    /// Set the dimension of the Split.
    pub fn length(mut self, length: Scalar) -> Split<'a> {
        self.maybe_length = Some(length);
        self
    }

    /// Set the child Canvas Splits of the current Canvas flowing in a given direction.
    fn flow(mut self, dir: Direction, splits: &'a [Split<'a>]) -> Split<'a> {
        self.maybe_splits = Some((dir, splits));
        self
    }

    /// Set the child Canvasses flowing downwards.
    pub fn flow_down(self, splits: &'a [Split<'a>]) -> Split<'a> {
        self.flow(Direction::Down, splits)
    }

    /// Set the child Canvasses flowing upwards.
    pub fn flow_up(self, splits: &'a [Split<'a>]) -> Split<'a> {
        self.flow(Direction::Up, splits)
    }

    /// Set the child Canvasses flowing to the right.
    pub fn flow_right(self, splits: &'a [Split<'a>]) -> Split<'a> {
        self.flow(Direction::Right, splits)
    }

    /// Set the child Canvasses flowing to the left.
    pub fn flow_left(self, splits: &'a [Split<'a>]) -> Split<'a> {
        self.flow(Direction::Left, splits)
    }

    /// Set the padding from the left edge.
    pub fn pad_left(mut self, pad: Scalar) -> Split<'a> {
        self.style.padding.maybe_left = Some(pad);
        self
    }

    /// Set the padding from the right edge.
    pub fn pad_right(mut self, pad: Scalar) -> Split<'a> {
        self.style.padding.maybe_right = Some(pad);
        self
    }

    /// Set the padding from the top edge.
    pub fn pad_top(mut self, pad: Scalar) -> Split<'a> {
        self.style.padding.maybe_top = Some(pad);
        self
    }

    /// Set the padding from the bottom edge.
    pub fn pad_bottom(mut self, pad: Scalar) -> Split<'a> {
        self.style.padding.maybe_bottom = Some(pad);
        self
    }

    /// Set the padding for all edges.
    pub fn pad(self, pad: Scalar) -> Split<'a> {
        self.pad_left(pad).pad_right(pad).pad_top(pad).pad_bottom(pad)
    }

    /// Set the margin from the left edge.
    pub fn margin_left(mut self, mgn: Scalar) -> Split<'a> {
        self.style.margin.maybe_left = Some(mgn);
        self
    }

    /// Set the margin from the right edge.
    pub fn margin_right(mut self, mgn: Scalar) -> Split<'a> {
        self.style.margin.maybe_right = Some(mgn);
        self
    }

    /// Set the margin from the top edge.
    pub fn margin_top(mut self, mgn: Scalar) -> Split<'a> {
        self.style.margin.maybe_top = Some(mgn);
        self
    }

    /// Set the margin from the bottom edge.
    pub fn margin_bottom(mut self, mgn: Scalar) -> Split<'a> {
        self.style.margin.maybe_bottom = Some(mgn);
        self
    }

    /// Set the margin for all edges.
    pub fn margin(self, mgn: Scalar) -> Split<'a> {
        self.margin_left(mgn).margin_right(mgn).margin_top(mgn).margin_bottom(mgn)
    }

    /// Set whether or not the Canvas' `KidArea` is scrollable (the default is false).
    /// If a widget is scrollable and it has children widgets that fall outside of its `KidArea`,
    /// the `KidArea` will become scrollable.
    pub fn scrolling(mut self, scrollable: bool) -> Self {
        self.is_v_scrollable = scrollable;
        self.is_h_scrollable = scrollable;
        self
    }

    /// Same as `Split::scrolling`, however only activates vertical scrolling.
    pub fn vertical_scrolling(mut self, scrollable: bool) -> Self {
        self.is_v_scrollable = scrollable;
        self
    }

    /// Same as `Split::scrolling`, however only activates horizontal scrolling.
    pub fn horizontal_scrolling(mut self, scrollable: bool) -> Self {
        self.is_h_scrollable = scrollable;
        self
    }

    /// Store the Canvas and its children within the `Ui`.
    pub fn set<C>(self, ui: &mut Ui<C>) where C: CharacterCache {
        let dim = [ui.win_w as f64, ui.win_h as f64];
        self.into_ui(dim, [0.0, 0.0], None, ui);
    }

    /// Construct a Canvas from a Split.
    fn into_ui<C>(&self,
                  dim: Dimensions,
                  xy: Point,
                  maybe_parent: Option<widget::Id>,
                  ui: &mut Ui<C>)
        where C: CharacterCache
    {
        use vecmath::{vec2_add, vec2_sub};

        let Split {
            id,
            ref style,
            ref maybe_splits,
            is_v_scrollable,
            is_h_scrollable,
            ..
        } = *self;

        let frame = style.frame(&ui.theme);
        let pad = style.padding(&ui.theme);
        let mgn = style.margin(&ui.theme);

        let mgn_offset = [(mgn.left - mgn.right), (mgn.bottom - mgn.top)];
        let dim = vec2_sub(dim, [mgn.left + mgn.right, mgn.top + mgn.bottom]);
        let frame_dim = vec2_sub(dim, [frame * 2.0; 2]);
        let pad_offset = [(pad.bottom - pad.top), (pad.left - pad.right)];
        let pad_dim = vec2_sub(frame_dim, [pad.left + pad.right, pad.top + pad.bottom]);

        // Offset xy so that it is in the center of the given margin.
        let xy = vec2_add(xy, mgn_offset);

        // Instantiate the Canvas widget for this split.
        {
            let mut canvas = canvas::Canvas::new();
            canvas.style = style.clone();
            match maybe_parent {
                Some(parent_id) => canvas.parent(parent_id),
                None            => canvas.no_parent(),
            }.xy(xy)
                .wh(dim)
                .vertical_scrolling(is_v_scrollable)
                .horizontal_scrolling(is_h_scrollable)
                .set(id, ui);
        }

        // Offset xy so that it is in the center of the padded area.
        let xy = vec2_add(xy, pad_offset);

        if let Some((direction, splits)) = *maybe_splits {
            use self::Direction::{Up, Down, Left, Right};

            let (stuck_length, num_not_stuck) =
                splits.iter().fold((0.0, splits.len()), |(total, remaining), split| {
                    match split.maybe_length {
                        Some(length) => (total + length, remaining - 1),
                        None => (total, remaining),
                    }
                });

            // Dimensions for Splits that haven't been given a specific length.
            let split_dim = match num_not_stuck {
                0 => [0.0, 0.0],
                _ => match direction {
                    Up   | Down  => {
                        let remaining_height = pad_dim[1] - stuck_length;
                        let height = match remaining_height > 0.0 {
                            true  => remaining_height / num_not_stuck as f64,
                            false => 0.0,
                        };
                        [pad_dim[0], height]
                    },
                    Left | Right => {
                        let remaining_width = pad_dim[0] - stuck_length;
                        let width = match remaining_width > 0.0 {
                            true  => remaining_width / num_not_stuck as f64,
                            false => 0.0
                        };
                        [width, pad_dim[1]]
                    },
                },
            };

            // The length of the previous split.
            let mut prev_length = 0.0;

            // Initialise the `current_xy` at the beginning of the pad_dim.
            let mut current_xy = match direction {
                Down  => [xy[0], xy[1] + pad_dim[1] / 2.0],
                Up    => [xy[0], xy[1] - pad_dim[1] / 2.0],
                Left  => [xy[0] + pad_dim[0] / 2.0, xy[1]],
                Right => [xy[0] - pad_dim[0] / 2.0, xy[1]],
            };

            // Update every split within the Ui.
            for split in splits.iter() {
                let split_dim = match split.maybe_length {
                    Some(len) => match direction {
                        Up   | Down  => [split_dim[0], len],
                        Left | Right => [len, split_dim[1]],
                    },
                    None => split_dim,
                };

                // Shift xy into position for the current split.
                match direction {
                    Down => {
                        current_xy[1] -= split_dim[1] / 2.0 + prev_length / 2.0;
                        prev_length = split_dim[1];
                    },
                    Up   => {
                        current_xy[1] += split_dim[1] / 2.0 + prev_length / 2.0;
                        prev_length = split_dim[1];
                    },
                    Left => {
                        current_xy[0] -= split_dim[0] / 2.0 + prev_length / 2.0;
                        prev_length = split_dim[0];
                    },
                    Right => {
                        current_xy[0] += split_dim[0] / 2.0 + prev_length / 2.0;
                        prev_length = split_dim[0];
                    },
                }

                split.into_ui(split_dim, current_xy, Some(id), ui);
            }
        }

    }

}


impl<'a> ::color::Colorable for Split<'a> {
    fn color(mut self, color: Color) -> Self {
        self.style.framed_rectangle.maybe_color = Some(color);
        self
    }
}

impl<'a> ::frame::Frameable for Split<'a> {
    fn frame(mut self, width: f64) -> Self {
        self.style.framed_rectangle.maybe_frame = Some(width);
        self
    }
    fn frame_color(mut self, color: Color) -> Self {
        self.style.framed_rectangle.maybe_frame_color = Some(color);
        self
    }
}