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

use color::{Color, Colorable};
use frame::Frameable;
use graphics::character::CharacterCache;
use label::{FontSize, Labelable};
use mouse::Mouse;
use num::{Float, NumCast, ToPrimitive};
use position::{self, Depth, Dimensions, HorizontalAlign, Position, VerticalAlign};
use ui::{UiId, Ui};
use utils::{clamp, percentage, value_from_perc};
use widget::Kind;

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

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

widget_fns!(Slider, State, Kind::Slider(State::Normal));

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

/// 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> {
    value: T,
    min: T,
    max: T,
    pos: Position,
    maybe_h_align: Option<HorizontalAlign>,
    maybe_v_align: Option<VerticalAlign>,
    dim: Dimensions,
    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, 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 {
            value: value,
            min: min,
            max: max,
            pos: Position::default(),
            maybe_h_align: None,
            maybe_v_align: None,
            dim: [192.0, 48.0],
            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 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
    }

    /// After building the Button, 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(T),
            T: Float + NumCast + ToPrimitive,
    {
        use elmesque::form::{collage, rect, text};
        use utils::{is_over_rect, map_range};

        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 is_over = is_over_rect([0.0, 0.0], mouse.xy, dim);
        let new_state = get_new_state(is_over, state, mouse);

        let frame_w = self.maybe_frame.unwrap_or(ui.theme.frame_width);
        let frame_w2 = frame_w * 2.0;
        let (inner_w, inner_h) = (dim[0] - frame_w2, dim[1] - frame_w2);
        let (half_inner_w, half_inner_h) = (inner_w / 2.0, inner_h / 2.0);

        let is_horizontal = dim[0] > dim[1];
        let (new_value, pad_rel_xy, pad_dim) = if is_horizontal {
            // Horizontal.
            let w = match (is_over, state, new_state) {
                (true, State::Highlighted, State::Clicked) | (_, State::Clicked, State::Clicked) => {
                    let w = map_range(mouse.xy[0], -half_inner_w, half_inner_w, 0.0, inner_w);
                    clamp(w, 0.0, inner_w)
                },
                _ => {
                    let value_percentage = percentage(self.value, self.min, self.max);
                    clamp(value_percentage as f64 * inner_w, 0.0, inner_w)
                },
            };
            let new_value = value_from_perc((w / inner_w) as f32, self.min, self.max);
            let p = [-(inner_w - w) / 2.0, 0.0];
            (new_value, p, [w, inner_h])
        } else {
            // Vertical.
            let h = match (is_over, state, new_state) {
                (true, State::Highlighted, State::Clicked) | (_, State::Clicked, State::Clicked) => {
                    let h = map_range(mouse.xy[1], -half_inner_h, half_inner_h, 0.0, inner_h);
                    clamp(h, 0.0, inner_h)
                },
                _ => {
                    let value_percentage = percentage(self.value, self.min, self.max);
                    clamp(value_percentage as f64 * inner_h, 0.0, inner_h)
                },
            };
            let new_value = value_from_perc((h / inner_h) as f32, self.min, self.max);
            let rel_xy = [0.0, -(inner_h - h) / 2.0];
            (new_value, rel_xy, [inner_w, h])
        };

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

        let draw_new_element_condition = true;

        // Only update the element if the state or value has changed.
        let maybe_new_element = if draw_new_element_condition {

            // Draw.
            let frame_color = new_state.color(self.maybe_frame_color.unwrap_or(ui.theme.frame_color));
            let color = new_state.color(self.maybe_color.unwrap_or(ui.theme.shape_color));

            // Rectangle frame / backdrop Form.
            let frame_form = rect(dim[0], dim[1])
                .filled(frame_color);
            // Slider rectangle Form.
            let pad_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 = self.maybe_label.map(|label_text| {
                use elmesque::text::Text;
                use label;
                const TEXT_PADDING: f64 = 10.0;
                let text_color = self.maybe_label_color.unwrap_or(ui.theme.label_color);
                let size = self.maybe_label_font_size.unwrap_or(ui.theme.font_size_medium);
                let label_w = label::width(ui, size, &label_text);
                let is_horizontal = self.dim[0] > self.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.to_string()).color(text_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).into_iter())
                .map(|form| form.shift(xy[0], xy[1]))
                .chain(maybe_label_form.into_iter());

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

            Some(element)

        } else { None };

        // Store the slider's state in the `Ui`.
        ui.update_widget(ui_id, Kind::Slider(new_state), xy, self.depth, maybe_new_element);

    }

}

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

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

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