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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431

use color::{Color, Colorable};
use elmesque::Element;
use frame::Frameable;
use graphics::character::CharacterCache;
use graphics::math::Scalar;
use label::{FontSize, Labelable};
use mouse::Mouse;
use num::{Float, NumCast, ToPrimitive};
use theme::Theme;
use ui::GlyphCache;
use utils::{clamp, percentage, value_from_perc};
use widget::{self, Widget};


/// Linear value selection. If the slider's width is greater than it's height, it will
/// automatically become a horizontal slider, otherwise it will be a vertical slider. Its reaction
/// is triggered if the value is updated or if the mouse button is released while the cursor is
/// above the rectangle.
pub struct Slider<'a, T, F> {
    common: widget::CommonBuilder,
    value: T,
    min: T,
    max: T,
    skew: f32,
    maybe_react: Option<F>,
    maybe_label: Option<&'a str>,
    style: Style,
    enabled: bool,
}

/// Styling for the Slider, necessary for constructing its renderable Element.
#[allow(missing_docs, missing_copy_implementations)]
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Style {
    pub maybe_color: Option<Color>,
    pub maybe_frame: Option<Scalar>,
    pub maybe_frame_color: Option<Color>,
    pub maybe_label_color: Option<Color>,
    pub maybe_label_font_size: Option<u32>,
}

/// Represents the state of the Slider widget.
#[derive(Clone, Debug, PartialEq)]
pub struct State<T> {
    value: T,
    min: T,
    max: T,
    skew: f32,
    maybe_label: Option<String>,
    interaction: Interaction,
}

/// The ways in which the Slider can be interacted with.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Interaction {
    Normal,
    Highlighted,
    Clicked,
}


impl<T> State<T> {
    /// Return the color associated with the state.
    fn color(&self, color: Color) -> Color {
        match self.interaction {
            Interaction::Normal => color,
            Interaction::Highlighted => color.highlighted(),
            Interaction::Clicked => color.clicked(),
        }
    }
}

/// Check the current state of the slider.
fn get_new_interaction(is_over: bool, prev: Interaction, mouse: Mouse) -> Interaction {
    use mouse::ButtonPosition::{Down, Up};
    use self::Interaction::{Normal, Highlighted, Clicked};
    match (is_over, prev, mouse.left.position) {
        (true,  Normal,  Down) => Normal,
        (true,  _,       Down) => Clicked,
        (true,  _,       Up)   => Highlighted,
        (false, Clicked, Down) => Clicked,
        _ => Normal,
    }
}

impl<'a, T, F> Slider<'a, T, F> {

    /// Construct a new Slider widget.
    pub fn new(value: T, min: T, max: T) -> Slider<'a, T, F> {
        Slider {
            common: widget::CommonBuilder::new(),
            value: value,
            min: min,
            max: max,
            skew: 1.0,
            maybe_react: None,
            maybe_label: None,
            style: Style::new(),
            enabled: true,
        }
    }

    /// Set the amount in which the slider's display should be skewed.
    /// Higher skew amounts (above 1.0) will weight lower values.
    /// Lower skew amounts (below 1.0) will weight heigher values.
    /// All skew amounts should be greater than 0.0.
    pub fn skew(mut self, skew: f32) -> Slider<'a, T, F> {
        self.skew = skew;
        self
    }

    /// Set the reaction for the Slider. It will be triggered if the value is updated or if the
    /// mouse button is released while the cursor is above the rectangle.
    pub fn react(mut self, reaction: F) -> Slider<'a, T, F> {
        self.maybe_react = Some(reaction);
        self
    }

    /// If true, will allow user inputs.  If false, will disallow user inputs.
    pub fn enabled(mut self, flag: bool) -> Self {
        self.enabled = flag;
        self
    }

}

impl<'a, T, F> Widget for Slider<'a, T, F> where
    F: FnMut(T),
    T: ::std::any::Any + ::std::fmt::Debug + Float + NumCast + ToPrimitive,
{
    type State = State<T>;
    type Style = Style;
    fn common(&self) -> &widget::CommonBuilder { &self.common }
    fn common_mut(&mut self) -> &mut widget::CommonBuilder { &mut self.common }
    fn unique_kind(&self) -> &'static str { "Slider" }
    fn init_state(&self) -> State<T> {
        State {
            value: self.value,
            min: self.min,
            max: self.max,
            skew: self.skew,
            maybe_label: None,
            interaction: Interaction::Normal,
        }
    }
    fn style(&self) -> Style { self.style.clone() }

    fn capture_mouse(prev: &State<T>, new: &State<T>) -> bool {
        match (prev.interaction, new.interaction) {
            (Interaction::Highlighted, Interaction::Clicked) => true,
            _ => false,
        }
    }

    fn uncapture_mouse(prev: &State<T>, new: &State<T>) -> bool {
        match (prev.interaction, new.interaction) {
            (Interaction::Clicked, Interaction::Highlighted) => true,
            (Interaction::Clicked, Interaction::Normal) => true,
            _ => false,
        }
    }

    fn default_width<C: CharacterCache>(&self, theme: &Theme, _: &GlyphCache<C>) -> Scalar {
        const DEFAULT_WIDTH: Scalar = 192.0;
        self.common.maybe_width.or(theme.maybe_slider.as_ref().map(|default| {
            default.common.maybe_width.unwrap_or(DEFAULT_WIDTH)
        })).unwrap_or(DEFAULT_WIDTH)
    }

    fn default_height(&self, theme: &Theme) -> Scalar {
        const DEFAULT_HEIGHT: Scalar = 48.0;
        self.common.maybe_height.or(theme.maybe_slider.as_ref().map(|default| {
            default.common.maybe_height.unwrap_or(DEFAULT_HEIGHT)
        })).unwrap_or(DEFAULT_HEIGHT)
    }

    /// Update the state of the Slider.
    fn update<C>(mut self, args: widget::UpdateArgs<Self, C>) -> Option<State<T>>
        where C: CharacterCache,
    {
        use utils::map_range;

        let widget::UpdateArgs { prev_state, rect, style, ui, .. } = args;
        let widget::State { ref state, .. } = *prev_state;
        let (xy, dim) = rect.xy_dim();
        let maybe_mouse = ui.input().maybe_mouse.map(|mouse| mouse.relative_to(xy));
        let new_interaction = match (self.enabled, maybe_mouse) {
            (false, _) | (true, None) => Interaction::Normal,
            (true, Some(mouse)) => {
                let is_over = ::position::is_over_rect([0.0, 0.0], dim, mouse.xy);
                get_new_interaction(is_over, state.interaction, mouse)
            },
        };

        let new_value = if let Some(mouse) = maybe_mouse {
            let Slider { value, min, max, skew, .. } = self;
            let frame = style.frame(ui.theme());
            let frame_2 = frame * 2.0;
            let (inner_w, inner_h) = (dim[0] - frame_2, dim[1] - frame_2);
            let (half_inner_w, half_inner_h) = (inner_w / 2.0, inner_h / 2.0);
            let is_horizontal = dim[0] > dim[1];

            if is_horizontal {
                // Horizontal.
                let w_perc = match (state.interaction, new_interaction) {
                    (Interaction::Highlighted, Interaction::Clicked) |
                    (Interaction::Clicked, Interaction::Clicked) => {
                        let w = map_range(mouse.xy[0], -half_inner_w, half_inner_w, 0.0, inner_w);
                        let perc = clamp(w, 0.0, inner_w) / inner_w;
                        (perc).powf(skew as f64)
                    },
                    _ => {
                        let value_percentage = percentage(value, min, max);
                        let w = clamp(value_percentage as f64 * inner_w, 0.0, inner_w);
                        (w / inner_w)
                    },
                };
                value_from_perc(w_perc as f32, min, max)
            } else {
                // Vertical.
                let h_perc = match (state.interaction, new_interaction) {
                    (Interaction::Highlighted, Interaction::Clicked) |
                    (Interaction::Clicked, Interaction::Clicked) => {
                        let h = map_range(mouse.xy[1], -half_inner_h, half_inner_h, 0.0, inner_h);
                        let perc = clamp(h, 0.0, inner_h) / inner_h;
                        (perc).powf(skew as f64)
                    },
                    _ => {
                        let value_percentage = percentage(value, min, max);
                        let h = clamp(value_percentage as f64 * inner_h, 0.0, inner_h);
                        (h / inner_h)
                    },
                };
                value_from_perc(h_perc as f32, min, max)
            }
        } else {
            self.value
        };

        // React.
        match self.maybe_react {
            Some(ref mut react) => {
                if self.value != new_value || match (state.interaction, new_interaction) {
                    (Interaction::Highlighted, Interaction::Clicked) |
                    (Interaction::Clicked, Interaction::Highlighted) => true,
                    _ => false,
                } { react(new_value) }
            }, None => (),
        }

        // A function for constructing a new state.
        let new_state = || {
            State {
                interaction: new_interaction,
                value: self.value,
                min: self.min,
                max: self.max,
                skew: self.skew,
                maybe_label: self.maybe_label.as_ref().map(|label| label.to_string()),
            }
        };

        // Check whether or not the state has changed since the previous update.
        let state_has_changed = state.interaction != new_interaction
            || state.value != self.value
            || state.min != self.min || state.max != self.max
            || state.skew != self.skew
            || state.maybe_label.as_ref().map(|string| &string[..]) != self.maybe_label;

        // Construct the new state if there was a change.
        if state_has_changed { Some(new_state()) } else { None }
    }

    /// Construct an Element from the given Slider State.
    fn draw<C>(args: widget::DrawArgs<Self, C>) -> Element
        where C: CharacterCache,
    {
        use elmesque::form::{self, collage, text};

        let widget::DrawArgs { rect, state, style, theme, glyph_cache, .. } = args;
        let (xy, dim) = rect.xy_dim();
        let frame = style.frame(theme);
        let (inner_w, inner_h) = (dim[0] - frame * 2.0, dim[1] - frame * 2.0);
        let frame_color = state.color(style.frame_color(theme));
        let color = state.color(style.color(theme));

        let new_value = NumCast::from(state.value).unwrap();
        let is_horizontal = dim[0] > dim[1];
        let (pad_rel_xy, pad_dim) = if is_horizontal {
            // Horizontal.
            let value_percentage = percentage(new_value, state.min, state.max).powf(1.0/state.skew);
            let w = clamp(value_percentage as f64 * inner_w, 0.0, inner_w);
            let rel_xy = [-(inner_w - w) / 2.0, 0.0];
            (rel_xy, [w, inner_h])
        } else {
            // Vertical.
            let value_percentage = percentage(new_value, state.min, state.max).powf(1.0/state.skew);
            let h = clamp(value_percentage as f64 * inner_h, 0.0, inner_h);
            let rel_xy = [0.0, -(inner_h - h) / 2.0];
            (rel_xy, [inner_w, h])
        };

        // Rectangle frame / backdrop Form.
        let frame_form = form::rect(dim[0], dim[1])
            .filled(frame_color);
        // Slider rectangle Form.
        let pad_form = form::rect(pad_dim[0], pad_dim[1])
            .filled(color)
            .shift(pad_rel_xy[0], pad_rel_xy[1]);

        // Label Form.
        let maybe_label_form = state.maybe_label.as_ref().map(|label_text| {
            use elmesque::text::Text;
            use position;
            const TEXT_PADDING: f64 = 10.0;
            let label_color = style.label_color(theme);
            let size = style.label_font_size(theme);
            let label_w = glyph_cache.width(size, &label_text);
            let is_horizontal = dim[0] > dim[1];
            let l_pos = if is_horizontal {
                let x = position::align_left_of(dim[0], label_w) + TEXT_PADDING;
                [x, 0.0]
            } else {
                let y = position::align_bottom_of(dim[1], size as f64) + TEXT_PADDING;
                [0.0, y]
            };
            text(Text::from_string(label_text.clone()).color(label_color).height(size as f64))
                .shift(l_pos[0].floor(), l_pos[1].floor())
                .shift(xy[0].floor(), xy[1].floor())
        });

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

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

}


impl Style {

    /// Construct the default Style.
    pub fn new() -> Style {
        Style {
            maybe_color: None,
            maybe_frame: None,
            maybe_frame_color: None,
            maybe_label_color: None,
            maybe_label_font_size: None,
        }
    }

    /// Get the Color for an Element.
    pub fn color(&self, theme: &Theme) -> Color {
        self.maybe_color.or(theme.maybe_slider.as_ref().map(|default| {
            default.style.maybe_color.unwrap_or(theme.shape_color)
        })).unwrap_or(theme.shape_color)
    }

    /// Get the frame for an Element.
    pub fn frame(&self, theme: &Theme) -> f64 {
        self.maybe_frame.or(theme.maybe_slider.as_ref().map(|default| {
            default.style.maybe_frame.unwrap_or(theme.frame_width)
        })).unwrap_or(theme.frame_width)
    }

    /// Get the frame Color for an Element.
    pub fn frame_color(&self, theme: &Theme) -> Color {
        self.maybe_frame_color.or(theme.maybe_slider.as_ref().map(|default| {
            default.style.maybe_frame_color.unwrap_or(theme.frame_color)
        })).unwrap_or(theme.frame_color)
    }

    /// Get the label Color for an Element.
    pub fn label_color(&self, theme: &Theme) -> Color {
        self.maybe_label_color.or(theme.maybe_slider.as_ref().map(|default| {
            default.style.maybe_label_color.unwrap_or(theme.label_color)
        })).unwrap_or(theme.label_color)
    }

    /// Get the label font size for an Element.
    pub fn label_font_size(&self, theme: &Theme) -> FontSize {
        self.maybe_label_font_size.or(theme.maybe_slider.as_ref().map(|default| {
            default.style.maybe_label_font_size.unwrap_or(theme.font_size_medium)
        })).unwrap_or(theme.font_size_medium)
    }

}


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

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

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

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

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