grafix_toolbox/gui/elements/
slider.rs1use super::*;
2
3#[derive(Debug)]
4pub struct Slider {
5 pub pip_pos: f32,
6}
7impl Default for Slider {
8 fn default() -> Self {
9 Self { pip_pos: 1. }
10 }
11}
12impl Slider {
13 pub fn draw<'s: 'l, 'l>(&'s mut self, r: &mut RenderLock<'l>, t: &'l Theme, Surf { pos, size }: Surf, pip_size: f32) -> f32 {
14 let (id, Self { pip_pos }) = (ref_UUID(self), self);
15 *pip_pos = pip_pos.clamp(0., 1.);
16
17 let (vert, p) = (size.y() >= size.x(), *pip_pos);
18 r.draw_with_logic(
19 Rect { pos, size, color: t.fg },
20 move |e, focused, mouse_pos| {
21 let orient = |v: Vec2| v.y().or_val(vert, || v.x()).clamp(0., 1.);
22 let mut set_pip = |p: Vec2| *pip_pos = p.sub(pos).sub(pip_size * 0.5).div(size.sub(pip_size)).pipe(orient);
23 match *e {
24 OfferFocus => (),
25 MouseButton { m, .. } if m.released() => return DropFocus,
26 MouseButton { m, .. } if m.pressed() => set_pip(mouse_pos),
27 MouseMove { at, .. } if focused => set_pip(at),
28 Scroll { at, .. } => *pip_pos = at.mul((-1, 1)).mul(pip_size).sum(*pip_pos).pipe(orient),
29 _ => return Pass,
30 }
31 Accept
32 },
33 id,
34 );
35
36 r.draw(Rect {
37 pos: pos.sum(size.sub(pip_size).mul((!vert, vert)).mul(p)),
38 size: (size.x(), pip_size).or_val(vert, || (pip_size, size.y())),
39 color: t.highlight,
40 });
41 p
42 }
43}
44
45impl<'s: 'l, 'l> Lock::Slider<'s, 'l, '_> {
46 pub fn draw(self, g: impl Into<Surf>) -> f32 {
47 let (Self { s, r, t }, g) = (self, g.into());
48 s.draw(r, t, g, g.size.min_comp())
49 }
50}