Add interactive Slider functionality
This commit is contained in:
parent
238f4f878d
commit
f37ec4251d
|
@ -24,6 +24,7 @@ pub struct Slider {
|
|||
fg_rect: Rect,
|
||||
position: f32,
|
||||
dirty: bool,
|
||||
updating: bool,
|
||||
}
|
||||
|
||||
impl Slider {
|
||||
|
@ -34,12 +35,23 @@ impl Slider {
|
|||
fg_rect: rect,
|
||||
position: 0.5,
|
||||
dirty: true,
|
||||
updating: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_position(&mut self, position: f32) {
|
||||
self.position = position;
|
||||
self.dirty = true;
|
||||
if !self.updating {
|
||||
self.position = position;
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_update(&mut self) -> Option<f32> {
|
||||
if self.updating {
|
||||
Some(self.position)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_rect(&mut self, rect: Rect) {
|
||||
|
@ -57,6 +69,10 @@ impl Slider {
|
|||
self.fg_rect = fg_space;
|
||||
self.dirty = false;
|
||||
}
|
||||
|
||||
pub fn get_position(&self) -> f32 {
|
||||
self.position
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Slider {
|
||||
|
@ -65,4 +81,29 @@ impl Widget for Slider {
|
|||
ctx.draw_rounded_rect(self.bg_rect, self.style.bg_rounding, self.style.bg_color);
|
||||
ctx.draw_rect(self.fg_rect, self.style.fg_color);
|
||||
}
|
||||
|
||||
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {
|
||||
if let CursorEventKind::Select = kind {
|
||||
if self.bg_rect.contains_point(at) {
|
||||
self.updating = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !self.updating {
|
||||
return;
|
||||
}
|
||||
|
||||
match kind {
|
||||
CursorEventKind::Hover => {}
|
||||
CursorEventKind::Select | CursorEventKind::Drag => {
|
||||
let offset = at.x - self.fg_rect.tl.x;
|
||||
let range = self.bg_rect.inset(self.style.bg_padding).width();
|
||||
self.position = (offset / range).clamp(0.0, 1.0);
|
||||
self.dirty = true;
|
||||
}
|
||||
CursorEventKind::Deselect => {
|
||||
self.updating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue