-
Notifications
You must be signed in to change notification settings - Fork 569
widget: slider: Add stepping functionality #1875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c385a44
fe7b157
fb9003e
e3b0783
8e586fd
1b91b8b
cfdcc87
5a41f52
350ab14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,4 @@ Robert Wittams | |
| Jaap Aarts | ||
| Maximilian Köstler | ||
| Bruno Dupuis | ||
| Christopher Noel Hesse | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,6 +31,7 @@ const KNOB_STROKE_WIDTH: f64 = 2.0; | |||||||||||||||||||||||||||||||||
| pub struct Slider { | ||||||||||||||||||||||||||||||||||
| min: f64, | ||||||||||||||||||||||||||||||||||
| max: f64, | ||||||||||||||||||||||||||||||||||
| step: Option<f64>, | ||||||||||||||||||||||||||||||||||
| knob_pos: Point, | ||||||||||||||||||||||||||||||||||
| knob_hovered: bool, | ||||||||||||||||||||||||||||||||||
| x_offset: f64, | ||||||||||||||||||||||||||||||||||
|
|
@@ -42,6 +43,7 @@ impl Slider { | |||||||||||||||||||||||||||||||||
| Slider { | ||||||||||||||||||||||||||||||||||
| min: 0., | ||||||||||||||||||||||||||||||||||
| max: 1., | ||||||||||||||||||||||||||||||||||
| step: None, | ||||||||||||||||||||||||||||||||||
| knob_pos: Default::default(), | ||||||||||||||||||||||||||||||||||
| knob_hovered: Default::default(), | ||||||||||||||||||||||||||||||||||
| x_offset: Default::default(), | ||||||||||||||||||||||||||||||||||
|
|
@@ -57,6 +59,24 @@ impl Slider { | |||||||||||||||||||||||||||||||||
| self | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /// Builder-style method to set the stepping. | ||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||
| /// The default step size is `0.0` (smooth). | ||||||||||||||||||||||||||||||||||
| pub fn with_step(mut self, step: f64) -> Self { | ||||||||||||||||||||||||||||||||||
| if step < 0.0 { | ||||||||||||||||||||||||||||||||||
| warn!("bad stepping (must be positive): {}", step); | ||||||||||||||||||||||||||||||||||
| return self; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| self.step = if step > 0.0 { | ||||||||||||||||||||||||||||||||||
| Some(step) | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| // A stepping value of 0.0 would yield an infinite amount of steps. | ||||||||||||||||||||||||||||||||||
| // Enforce no stepping instead. | ||||||||||||||||||||||||||||||||||
| None | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+66
to
+76
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to change it, but just for posterity I'd have just written:
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That has a drawback IMO: you'd actually need to check for We decided to allow
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets change that |
||||||||||||||||||||||||||||||||||
| self | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /// check self.min <= self.max, if not swaps the values. | ||||||||||||||||||||||||||||||||||
| fn check_range(&mut self) { | ||||||||||||||||||||||||||||||||||
| if self.max < self.min { | ||||||||||||||||||||||||||||||||||
|
|
@@ -79,7 +99,24 @@ impl Slider { | |||||||||||||||||||||||||||||||||
| let scalar = ((mouse_x + self.x_offset - knob_width / 2.) / (slider_width - knob_width)) | ||||||||||||||||||||||||||||||||||
| .max(0.0) | ||||||||||||||||||||||||||||||||||
| .min(1.0); | ||||||||||||||||||||||||||||||||||
| self.min + scalar * (self.max - self.min) | ||||||||||||||||||||||||||||||||||
| let mut value = self.min + scalar * (self.max - self.min); | ||||||||||||||||||||||||||||||||||
| if let Some(step) = self.step { | ||||||||||||||||||||||||||||||||||
| let max_step_value = ((self.max - self.min) / step).floor() * step + self.min; | ||||||||||||||||||||||||||||||||||
| if value > max_step_value { | ||||||||||||||||||||||||||||||||||
| // edge case: make sure max is reachable | ||||||||||||||||||||||||||||||||||
| let left_dist = value - max_step_value; | ||||||||||||||||||||||||||||||||||
| let right_dist = self.max - value; | ||||||||||||||||||||||||||||||||||
| value = if left_dist < right_dist { | ||||||||||||||||||||||||||||||||||
| max_step_value | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| self.max | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| // snap to discrete intervals | ||||||||||||||||||||||||||||||||||
| value = (((value - self.min) / step).round() * step + self.min).min(self.max); | ||||||||||||||||||||||||||||||||||
raymanfx marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| value | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| fn normalize(&self, data: f64) -> f64 { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.