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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

use color::{Color, Colorable};
use frame::Frameable;
use graphics::character::CharacterCache;
use label::{FontSize, Labelable};
use mouse::Mouse;
use position::{Depth, Dimensions, HorizontalAlign, Point, Position, Positionable, VerticalAlign};
use ui::{UiId, Ui};
use widget::Kind;

/// Tuple / React params.
pub type Idx = usize;
pub type Len = usize;

/// Represents the state of the menu.
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum State {
    Closed(DrawState),
    Open(DrawState),
}

/// Represents the state of the DropDownList widget.
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum DrawState {
    Normal,
    Highlighted(Idx, Len),
    Clicked(Idx, Len),
}

impl DrawState {
    fn color(&self, color: Color) -> Color {
        match *self {
            DrawState::Normal => color,
            DrawState::Highlighted(_, _) => color.highlighted(),
            DrawState::Clicked(_, _) => color.clicked(),
        }
    }
}

widget_fns!(DropDownList, State, Kind::DropDownList(State::Closed(DrawState::Normal)));

/// Is the cursor currently over the widget? If so which item?
fn is_over(mouse_pos: Point,
           frame_w: f64,
           dim: Dimensions,
           state: State,
           len: Len) -> Option<Idx> {
    use utils::is_over_rect;
    match state {
        State::Closed(_) => match is_over_rect([0.0, 0.0], mouse_pos, dim) {
            false => None,
            true => Some(0),
        },
        State::Open(_) => {
            let item_h = dim[1] - frame_w;
            let total_h = item_h * len as f64;
            let open_centre_y = -(total_h - item_h) / 2.0;
            match is_over_rect([0.0, open_centre_y], mouse_pos, [dim[0], total_h]) {
                false => None,
                true => Some(((mouse_pos[1] - item_h / 2.0).abs() / item_h) as usize),
            }
        },
    }
}

/// Determine and return the new State by comparing the mouse state
/// and position to the previous State.
fn get_new_state(is_over_idx: Option<Idx>,
                 len: Len,
                 state: State,
                 mouse: Mouse) -> State {
    use self::DrawState::{Normal, Clicked, Highlighted};
    use mouse::ButtonState::{Down, Up};
    match state {
        State::Closed(draw_state) => {
            match is_over_idx {
                Some(_) => {
                    match (draw_state, mouse.left) {
                        (Normal,            Down) => State::Closed(Normal),
                        (Normal,            Up)   |
                        (Highlighted(_, _), Up)   => State::Closed(Highlighted(0, len)),
                        (Highlighted(_, _), Down) => State::Closed(Clicked(0, len)),
                        (Clicked(_, _),     Down) => State::Closed(Clicked(0, len)),
                        (Clicked(_, _),     Up)   => State::Open(Normal),
                    }
                },
                None => State::Closed(Normal),
            }
        },
        State::Open(draw_state) => {
            match is_over_idx {
                Some(idx) => {
                    match (draw_state, mouse.left) {
                        (Normal,            Down) => State::Open(Normal),
                        (Normal,            Up)   |
                        (Highlighted(_, _), Up)   => State::Open(Highlighted(idx, len)),
                        (Highlighted(_, _), Down) => State::Open(Clicked(idx, len)),
                        (Clicked(p_idx, _), Down) => State::Open(Clicked(p_idx, len)),
                        (Clicked(_, _),     Up)   => State::Closed(Normal),
                    }
                },
                None => {
                    match (draw_state, mouse.left) {
                        (Highlighted(p_idx, _), Up) => State::Open(Highlighted(p_idx, len)),
                        _ => State::Closed(Normal),
                    }
                },
            }
        }
    }
}

/// Displays a given `Vec<String>` as a selectable drop down menu. It's reaction is triggered upon
/// selection of a list item.
pub struct DropDownList<'a, F> {
    strings: &'a mut Vec<String>,
    selected: &'a mut Option<Idx>,
    pos: Position,
    dim: Dimensions,
    maybe_h_align: Option<HorizontalAlign>,
    maybe_v_align: Option<VerticalAlign>,
    depth: Depth,
    maybe_react: Option<F>,
    maybe_color: Option<Color>,
    maybe_frame: Option<f64>,
    maybe_frame_color: Option<Color>,
    maybe_label: Option<&'a str>,
    maybe_label_color: Option<Color>,
    maybe_label_font_size: Option<u32>,
}

impl<'a, F> DropDownList<'a, F> {

    /// Construct a new DropDownList.
    pub fn new(strings: &'a mut Vec<String>, selected: &'a mut Option<Idx>) -> DropDownList<'a, F> {
        DropDownList {
            strings: strings,
            selected: selected,
            pos: Position::default(),
            dim: [128.0, 32.0],
            maybe_h_align: None,
            maybe_v_align: None,
            depth: 0.0,
            maybe_react: None,
            maybe_color: None,
            maybe_frame: None,
            maybe_frame_color: None,
            maybe_label: None,
            maybe_label_color: None,
            maybe_label_font_size: None,
        }
    }

    /// Set the DropDownList's reaction. It will be triggered upon selection of a list item.
    pub fn react(mut self, reaction: F) -> DropDownList<'a, F> {
        self.maybe_react = Some(reaction);
        self
    }

    /// After building the DropDownList, use this method to set its current state into the given
    /// `Ui`. It will use this state for rendering the next time `ui.draw(graphics)` is called.
    pub fn set<C>(mut self, ui_id: UiId, ui: &mut Ui<C>)
        where
            C: CharacterCache,
            F: FnMut(&mut Option<Idx>, Idx, String),
    {

        let state = *get_state(ui, ui_id);
        let dim = self.dim;
        let h_align = self.maybe_h_align.unwrap_or(ui.theme.align.horizontal);
        let v_align = self.maybe_v_align.unwrap_or(ui.theme.align.vertical);
        let xy = ui.get_xy(self.pos, dim, h_align, v_align);
        let mouse = ui.get_mouse_state(ui_id).relative_to(xy);
        let frame_w = self.maybe_frame.unwrap_or(ui.theme.frame_width);
        let is_over_idx = is_over(mouse.xy, frame_w, dim, state, self.strings.len());
        let new_state = get_new_state(is_over_idx, self.strings.len(), state, mouse);
        let selected = self.selected.and_then(|idx| if idx < self.strings.len() { Some(idx) }
                                                    else { None });

        // Check whether or not we need to capture or uncapture the mouse.
        // We need to capture the cursor if the DropDownList has just been opened.
        // We need to uncapture the cursor if the DropDownList has just been closed.
        match (state, new_state) {
            (State::Closed(_), State::Open(_)) => ui.mouse_captured_by(ui_id),
            (State::Open(_), State::Closed(_)) => ui.mouse_uncaptured_by(ui_id),
            _ => (),
        }

        // Call the `react` closure if mouse was released on one of the DropDownList items.
        if let Some(ref mut react) = self.maybe_react {
            if let (State::Open(o_d_state), State::Closed(c_d_state)) = (state, new_state) {
                if let (DrawState::Clicked(idx, _), DrawState::Normal) = (o_d_state, c_d_state) {
                    *self.selected = selected;
                    react(self.selected, idx, self.strings[idx].clone())
                }
            }
        }

        let draw_new_element_condition = true;

        // If the state has changed, construct the new Element.
        let maybe_new_element = if draw_new_element_condition {
            use elmesque::form::{collage, rect, text};
            use elmesque::text::Text;

            // Get the DropDownList's styling.
            let color = self.maybe_color.unwrap_or(ui.theme.shape_color);
            let t_size = self.maybe_label_font_size.unwrap_or(ui.theme.font_size_medium);
            let t_color = self.maybe_label_color.unwrap_or(ui.theme.label_color);
            let pad_dim = ::vecmath::vec2_sub(dim, [frame_w * 2.0; 2]);
            let frame_color = self.maybe_frame_color.unwrap_or(ui.theme.frame_color);

            // Construct the DropDownList's Element.
            match new_state {

                State::Closed(draw_state) => {
                    let string = match selected {
                        Some(idx) => &(*self.strings)[idx][..],
                        None => match self.maybe_label {
                            Some(text) => text,
                            None => &(*self.strings)[0][..],
                        },
                    }.to_string();
                    let frame_form = rect(dim[0], dim[1]).filled(frame_color);
                    let inner_form = rect(pad_dim[0], pad_dim[1]).filled(draw_state.color(color));
                    let text_form = text(Text::from_string(string)
                                             .color(t_color)
                                             .height(t_size as f64));

                    // Chain and shift the Forms into position.
                    let form_chain = Some(frame_form).into_iter()
                        .chain(Some(inner_form).into_iter())
                        .chain(Some(text_form).into_iter())
                        .map(|form| form.shift(xy[0].floor(), xy[1].floor()));

                    // Collect the Form's into a renderable Element.
                    Some(collage(dim[0] as i32, dim[1] as i32, form_chain.collect()))
                },

                State::Open(draw_state) => {
                    // Chain and shift the Forms into position.
                    let form_chain = self.strings.iter().enumerate().flat_map(|(i, string)| {
                        let color = match selected {
                            None => match draw_state {
                                DrawState::Normal => color,
                                DrawState::Highlighted(idx, _) => {
                                    if i == idx { color.highlighted() }
                                    else { color }
                                },
                                DrawState::Clicked(idx, _) => {
                                    if i == idx { color.clicked() }
                                    else { color }
                                },
                            },
                            Some(sel_idx) => {
                                if sel_idx == i { color.clicked() }
                                else {
                                    match draw_state {
                                        DrawState::Normal => color,
                                        DrawState::Highlighted(idx, _) => {
                                            if i == idx { color.highlighted() }
                                            else { color }
                                        },
                                        DrawState::Clicked(idx, _) => {
                                            if i == idx { color.clicked() }
                                            else { color }
                                        },
                                    }
                                }
                            },
                        };
                        let shift_amt = -(i as f64 * dim[1] - i as f64 * frame_w).floor();
                        let frame_form = rect(dim[0], dim[1]).filled(frame_color);
                        let inner_form = rect(pad_dim[0], pad_dim[1]).filled(color);
                        let text_form = text(Text::from_string(string.clone())
                                                 .color(t_color)
                                                 .height(t_size as f64));
                        Some(frame_form.shift_y(shift_amt)).into_iter()
                            .chain(Some(inner_form.shift_y(shift_amt)).into_iter())
                            .chain(Some(text_form.shift_y(shift_amt.floor())).into_iter())
                    }).map(|form| form.shift(xy[0].floor(), xy[1].floor()));

                    // Collect the Form's into a renderable Element.
                    Some(collage(dim[0] as i32, dim[1] as i32, form_chain.collect()))
                },

            }

        } else { None };

        // Store the drop down list's new state in the Ui.
        ui.update_widget(ui_id, Kind::DropDownList(new_state), xy, self.depth, maybe_new_element);

    }

}

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

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

impl<'a, F> Labelable<'a> for DropDownList<'a, F>
{
    fn label(mut self, text: &'a str) -> Self {
        self.maybe_label = Some(text);
        self
    }

    fn label_color(mut self, color: Color) -> Self {
        self.maybe_label_color = Some(color);
        self
    }

    fn label_font_size(mut self, size: FontSize) -> Self {
        self.maybe_label_font_size = Some(size);
        self
    }
}

impl<'a, F> Positionable for DropDownList<'a, F> {
    fn position(mut self, pos: Position) -> Self {
        self.pos = pos;
        self
    }
    #[inline]
    fn horizontal_align(self, h_align: HorizontalAlign) -> Self {
        DropDownList { maybe_h_align: Some(h_align), ..self }
    }
    #[inline]
    fn vertical_align(self, v_align: VerticalAlign) -> Self {
        DropDownList { maybe_v_align: Some(v_align), ..self }
    }
}

impl<'a, F> ::position::Sizeable for DropDownList<'a, F> {
    #[inline]
    fn width(self, w: f64) -> Self {
        let h = self.dim[1];
        DropDownList { dim: [w, h], ..self }
    }
    #[inline]
    fn height(self, h: f64) -> Self {
        let w = self.dim[0];
        DropDownList { dim: [w, h], ..self }
    }
}