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
use geom::{Circle, Distance, Polygon, Pt2D};

use crate::{
    Color, Drawable, EventCtx, GeomBatch, GfxCtx, ScreenDims, ScreenPt, ScreenRectangle, Widget,
    WidgetImpl, WidgetOutput,
};

pub struct Slider {
    current_percent: f64,
    mouse_on_slider: bool,
    pub(crate) dragging: bool,

    style: Style,

    draw: Drawable,

    top_left: ScreenPt,
    dims: ScreenDims,
}

enum Style {
    Horizontal { main_bg_len: f64, dragger_len: f64 },
    Vertical { main_bg_len: f64, dragger_len: f64 },
    Area { width: f64 },
}

const BG_CROSS_AXIS_LEN: f64 = 20.0;

impl Slider {
    pub fn horizontal(
        ctx: &EventCtx,
        width: f64,
        dragger_len: f64,
        current_percent: f64,
    ) -> Widget {
        Slider::new(
            ctx,
            Style::Horizontal {
                main_bg_len: width,
                dragger_len,
            },
            current_percent,
        )
    }

    pub fn vertical(ctx: &EventCtx, height: f64, dragger_len: f64, current_percent: f64) -> Widget {
        Slider::new(
            ctx,
            Style::Vertical {
                main_bg_len: height,
                dragger_len,
            },
            current_percent,
        )
    }

    pub fn area(ctx: &EventCtx, width: f64, current_percent: f64) -> Widget {
        Slider::new(ctx, Style::Area { width }, current_percent)
    }

    fn new(ctx: &EventCtx, style: Style, current_percent: f64) -> Widget {
        let mut s = Slider {
            current_percent,
            mouse_on_slider: false,
            dragging: false,

            style,

            draw: Drawable::empty(ctx),

            top_left: ScreenPt::new(0.0, 0.0),
            dims: ScreenDims::new(0.0, 0.0),
        };
        s.recalc(ctx);
        Widget::new(Box::new(s))
    }

    fn recalc(&mut self, ctx: &EventCtx) {
        let mut batch = GeomBatch::new();

        match self.style {
            Style::Horizontal { .. } | Style::Vertical { .. } => {
                // Full dims
                self.dims = match self.style {
                    Style::Horizontal { main_bg_len, .. } => {
                        ScreenDims::new(main_bg_len, BG_CROSS_AXIS_LEN)
                    }
                    Style::Vertical { main_bg_len, .. } => {
                        ScreenDims::new(BG_CROSS_AXIS_LEN, main_bg_len)
                    }
                    _ => unreachable!(),
                };

                // The background
                batch.push(
                    Color::WHITE,
                    Polygon::rectangle(self.dims.width, self.dims.height),
                );

                // The draggy thing
                batch.push(
                    if self.mouse_on_slider {
                        Color::grey(0.7).alpha(0.7)
                    } else {
                        Color::grey(0.7)
                    },
                    self.slider_geom(),
                );
            }
            Style::Area { width } => {
                // Full dims
                self.dims = ScreenDims::new(width, BG_CROSS_AXIS_LEN);

                // The background
                batch.push(
                    Color::hex("#F2F2F2"),
                    Polygon::pill(self.dims.width, self.dims.height),
                );
                // So far
                batch.push(
                    Color::hex("#F4DF4D"),
                    Polygon::pill(self.current_percent * self.dims.width, self.dims.height),
                );

                // The circle dragger
                batch.push(
                    if self.mouse_on_slider {
                        Color::WHITE.alpha(0.7)
                    } else {
                        Color::WHITE
                    },
                    self.slider_geom(),
                );
            }
        }

        self.draw = ctx.upload(batch);
    }

    // Doesn't touch self.top_left
    fn slider_geom(&self) -> Polygon {
        match self.style {
            Style::Horizontal {
                main_bg_len,
                dragger_len,
            } => Polygon::rectangle(dragger_len, BG_CROSS_AXIS_LEN)
                .translate(self.current_percent * (main_bg_len - dragger_len), 0.0),
            Style::Vertical {
                main_bg_len,
                dragger_len,
            } => Polygon::rectangle(BG_CROSS_AXIS_LEN, dragger_len)
                .translate(0.0, self.current_percent * (main_bg_len - dragger_len)),
            Style::Area { width } => Circle::new(
                Pt2D::new(self.current_percent * width, BG_CROSS_AXIS_LEN / 2.0),
                Distance::meters(BG_CROSS_AXIS_LEN),
            )
            .to_polygon(),
        }
    }

    fn pt_to_percent(&self, pt: ScreenPt) -> f64 {
        match self.style {
            Style::Horizontal {
                main_bg_len,
                dragger_len,
            } => (pt.x - self.top_left.x - (dragger_len / 2.0)) / (main_bg_len - dragger_len),
            Style::Vertical {
                main_bg_len,
                dragger_len,
            } => (pt.y - self.top_left.y - (dragger_len / 2.0)) / (main_bg_len - dragger_len),
            Style::Area { width } => (pt.x - self.top_left.x) / width,
        }
    }

    pub fn get_percent(&self) -> f64 {
        self.current_percent
    }

    pub fn get_value(&self, num_items: usize) -> usize {
        (self.current_percent * (num_items as f64 - 1.0)) as usize
    }

    pub(crate) fn set_percent(&mut self, ctx: &EventCtx, percent: f64) {
        assert!(percent >= 0.0 && percent <= 1.0);
        self.current_percent = percent;
        self.recalc(ctx);
        if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
            self.mouse_on_slider = self
                .slider_geom()
                .translate(self.top_left.x, self.top_left.y)
                .contains_pt(pt.to_pt());
        } else {
            self.mouse_on_slider = false;
        }
    }

    fn inner_event(&mut self, ctx: &mut EventCtx) -> bool {
        if self.dragging {
            if ctx.input.get_moved_mouse().is_some() {
                self.current_percent = self
                    .pt_to_percent(ctx.canvas.get_cursor())
                    .min(1.0)
                    .max(0.0);
                return true;
            }
            if ctx.input.left_mouse_button_released() {
                self.dragging = false;
                return true;
            }
            return false;
        }

        if ctx.redo_mouseover() {
            let old = self.mouse_on_slider;
            if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
                self.mouse_on_slider = self
                    .slider_geom()
                    .translate(self.top_left.x, self.top_left.y)
                    .contains_pt(pt.to_pt());
            } else {
                self.mouse_on_slider = false;
            }
            return self.mouse_on_slider != old;
        }
        if ctx.input.left_mouse_button_pressed() {
            if self.mouse_on_slider {
                self.dragging = true;
                return true;
            }

            // Did we click somewhere else on the bar?
            if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
                if Polygon::rectangle(self.dims.width, self.dims.height)
                    .translate(self.top_left.x, self.top_left.y)
                    .contains_pt(pt.to_pt())
                {
                    self.current_percent = self
                        .pt_to_percent(ctx.canvas.get_cursor())
                        .min(1.0)
                        .max(0.0);
                    self.mouse_on_slider = true;
                    self.dragging = true;
                    return true;
                }
            }
        }
        false
    }
}

impl WidgetImpl for Slider {
    fn get_dims(&self) -> ScreenDims {
        self.dims
    }

    fn set_pos(&mut self, top_left: ScreenPt) {
        self.top_left = top_left;
    }

    fn event(&mut self, ctx: &mut EventCtx, _: &mut WidgetOutput) {
        if self.inner_event(ctx) {
            self.recalc(ctx);
        }
    }

    fn draw(&self, g: &mut GfxCtx) {
        g.redraw_at(self.top_left, &self.draw);
        // TODO Since the sliders in Panels are scrollbars outside of the clipping rectangle,
        // this stays for now. It has no effect for other sliders.
        g.canvas
            .mark_covered_area(ScreenRectangle::top_left(self.top_left, self.dims));
    }
}