Add Slider::clamp_to_range(bool)
This commit is contained in:
parent
2219e135fa
commit
6d57a24f35
3 changed files with 38 additions and 1 deletions
|
@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|||
|
||||
* Add support for secondary and middle mouse buttons.
|
||||
* `egui::popup::popup_below_widget`: show a popup area below another widget.
|
||||
* Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range.
|
||||
|
||||
### Changed 🔧
|
||||
|
||||
|
|
|
@ -36,12 +36,19 @@ struct SliderSpec {
|
|||
}
|
||||
|
||||
/// Control a number by a horizontal slider.
|
||||
///
|
||||
/// The slider range defines the values you get when pulling the slider to the far edges.
|
||||
/// By default, the slider can still show values outside this range,
|
||||
/// and still allows users to enter values outside the range by clicking the slider value and editing it.
|
||||
/// If you want to clamp incoming and outgoing values, use [`Slider::clamp_to_range`].
|
||||
///
|
||||
/// The range can include any numbers, and go from low-to-high or from high-to-low.
|
||||
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
|
||||
pub struct Slider<'a> {
|
||||
get_set_value: GetSetValue<'a>,
|
||||
range: RangeInclusive<f64>,
|
||||
spec: SliderSpec,
|
||||
clamp_to_range: bool,
|
||||
smart_aim: bool,
|
||||
// TODO: label: Option<Label>
|
||||
text: Option<String>,
|
||||
|
@ -62,6 +69,7 @@ impl<'a> Slider<'a> {
|
|||
logarithmic: false,
|
||||
smallest_positive: 1e-6,
|
||||
},
|
||||
clamp_to_range: false,
|
||||
smart_aim: true,
|
||||
text: None,
|
||||
text_color: None,
|
||||
|
@ -156,6 +164,13 @@ impl<'a> Slider<'a> {
|
|||
self
|
||||
}
|
||||
|
||||
/// If set to `true`, all incoming and outgoing values will be clamped to the slider range.
|
||||
/// Default: `false`.
|
||||
pub fn clamp_to_range(mut self, clamp_to_range: bool) -> Self {
|
||||
self.clamp_to_range = clamp_to_range;
|
||||
self
|
||||
}
|
||||
|
||||
/// Turn smart aim on/off. Default is ON.
|
||||
/// There is almost no point in turning this off.
|
||||
pub fn smart_aim(mut self, smart_aim: bool) -> Self {
|
||||
|
@ -205,10 +220,18 @@ impl<'a> Slider<'a> {
|
|||
}
|
||||
|
||||
fn get_value(&mut self) -> f64 {
|
||||
get(&mut self.get_set_value)
|
||||
let value = get(&mut self.get_set_value);
|
||||
if self.clamp_to_range {
|
||||
clamp(value, self.range.clone())
|
||||
} else {
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
fn set_value(&mut self, mut value: f64) {
|
||||
if self.clamp_to_range {
|
||||
value = clamp(value, self.range.clone());
|
||||
}
|
||||
if let Some(max_decimals) = self.max_decimals {
|
||||
value = math::round_to_decimals(value, max_decimals);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ pub struct Sliders {
|
|||
pub min: f64,
|
||||
pub max: f64,
|
||||
pub logarithmic: bool,
|
||||
pub clamp_to_range: bool,
|
||||
pub smart_aim: bool,
|
||||
pub integer: bool,
|
||||
pub value: f64,
|
||||
|
@ -20,6 +21,7 @@ impl Default for Sliders {
|
|||
min: 0.0,
|
||||
max: 10000.0,
|
||||
logarithmic: true,
|
||||
clamp_to_range: false,
|
||||
smart_aim: true,
|
||||
integer: false,
|
||||
value: 10.0,
|
||||
|
@ -33,6 +35,7 @@ impl Sliders {
|
|||
min,
|
||||
max,
|
||||
logarithmic,
|
||||
clamp_to_range,
|
||||
smart_aim,
|
||||
integer,
|
||||
value,
|
||||
|
@ -56,6 +59,7 @@ impl Sliders {
|
|||
ui.add(
|
||||
Slider::i32(&mut value_i32, (*min as i32)..=(*max as i32))
|
||||
.logarithmic(*logarithmic)
|
||||
.clamp_to_range(*clamp_to_range)
|
||||
.smart_aim(*smart_aim)
|
||||
.text("i32 demo slider"),
|
||||
);
|
||||
|
@ -64,6 +68,7 @@ impl Sliders {
|
|||
ui.add(
|
||||
Slider::f64(value, (*min)..=(*max))
|
||||
.logarithmic(*logarithmic)
|
||||
.clamp_to_range(*clamp_to_range)
|
||||
.smart_aim(*smart_aim)
|
||||
.text("f64 demo slider"),
|
||||
);
|
||||
|
@ -101,13 +106,21 @@ impl Sliders {
|
|||
ui.radio_value(integer, false, "f64");
|
||||
});
|
||||
ui.label("(f32, usize etc are also possible)");
|
||||
ui.advance_cursor(8.0);
|
||||
|
||||
ui.checkbox(logarithmic, "Logarithmic");
|
||||
ui.label("Logarithmic sliders are great for when you want to span a huge range, i.e. from zero to a million.");
|
||||
ui.label("Logarithmic sliders can include infinity and zero.");
|
||||
ui.advance_cursor(8.0);
|
||||
|
||||
ui.checkbox(clamp_to_range, "Clamp to range");
|
||||
ui.label("If true, the slider will clamp incoming and outgoing values to the given range.");
|
||||
ui.label("If false, the slider can shows values outside its range, and you can manually enter values outside the range.");
|
||||
ui.advance_cursor(8.0);
|
||||
|
||||
ui.checkbox(smart_aim, "Smart Aim");
|
||||
ui.label("Smart Aim will guide you towards round values when you drag the slider so you you are more likely to hit 250 than 247.23");
|
||||
ui.advance_cursor(8.0);
|
||||
|
||||
egui::reset_button(ui, self);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue