Update slider API slightly

This commit is contained in:
Joonas Javanainen 2020-09-19 14:54:03 +03:00
parent 03ad6b10e0
commit 806608740a
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
4 changed files with 66 additions and 21 deletions

View File

@ -18,6 +18,8 @@
- Upgrade to cimgui / imgui 1.78 - Upgrade to cimgui / imgui 1.78
- Store per-texture sampler parameters in imgui-glium-renderer to support - Store per-texture sampler parameters in imgui-glium-renderer to support
customizing them customizing them
- Slider widget constructors no longer require the range parameter. Call the
range function on the builder to set the range.
### Fixed ### Fixed

View File

@ -435,7 +435,7 @@ fn show_test_window(ui: &Ui, state: &mut State, opened: &mut bool) {
)); ));
ui.spacing(); ui.spacing();
Slider::new(im_str!("Wrap width"), -20.0 ..= 600.0) Slider::new(im_str!("Wrap width")).range(-20.0 ..= 600.0)
.display_format(im_str!("%.0f")) .display_format(im_str!("%.0f"))
.build(ui, &mut state.wrap_width); .build(ui, &mut state.wrap_width);
@ -852,7 +852,9 @@ fn show_example_menu_file<'a>(ui: &Ui<'a>, state: &mut FileMenuState) {
ui.text(format!("Scrolling Text {}", i)); ui.text(format!("Scrolling Text {}", i));
} }
}); });
Slider::new(im_str!("Value"), 0.0..=1.0).build(ui, &mut state.f); Slider::new(im_str!("Value"))
.range(0.0..=1.0)
.build(ui, &mut state.f);
ui.input_float(im_str!("Input"), &mut state.f) ui.input_float(im_str!("Input"), &mut state.f)
.step(0.1) .step(0.1)
@ -887,7 +889,9 @@ fn show_example_app_auto_resize(ui: &Ui, state: &mut AutoResizeState, opened: &m
Note that you probably don't want to query the window size to Note that you probably don't want to query the window size to
output your content because that would create a feedback loop.", output your content because that would create a feedback loop.",
); );
Slider::new(im_str!("Number of lines"), 1..=20).build(ui, &mut state.lines); Slider::new(im_str!("Number of lines"))
.range(1..=20)
.build(ui, &mut state.lines);
for i in 0..state.lines { for i in 0..state.lines {
ui.text(format!("{:2$}This is line {}", "", i, i as usize * 4)); ui.text(format!("{:2$}This is line {}", "", i, i as usize * 4));
} }

View File

@ -31,6 +31,7 @@ impl<'a, T: DataTypeKind> Drag<'a, T> {
flags: SliderFlags::empty(), flags: SliderFlags::empty(),
} }
} }
/// Sets the range (inclusive)
#[inline] #[inline]
pub fn range<R: InclusiveRangeBounds<T>>(mut self, range: R) -> Self { pub fn range<R: InclusiveRangeBounds<T>>(mut self, range: R) -> Self {
self.min = range.start_bound().copied(); self.min = range.start_bound().copied();

View File

@ -1,9 +1,8 @@
use bitflags::bitflags; use bitflags::bitflags;
use std::ops::RangeInclusive;
use std::os::raw::c_void; use std::os::raw::c_void;
use std::ptr; use std::ptr;
use crate::internal::DataTypeKind; use crate::internal::{DataTypeKind, InclusiveRangeBounds};
use crate::string::ImStr; use crate::string::ImStr;
use crate::sys; use crate::sys;
use crate::Ui; use crate::Ui;
@ -30,23 +29,30 @@ bitflags!(
#[must_use] #[must_use]
pub struct Slider<'a, T: DataTypeKind> { pub struct Slider<'a, T: DataTypeKind> {
label: &'a ImStr, label: &'a ImStr,
min: T, min: Option<T>,
max: T, max: Option<T>,
display_format: Option<&'a ImStr>, display_format: Option<&'a ImStr>,
flags: SliderFlags, flags: SliderFlags,
} }
impl<'a, T: DataTypeKind> Slider<'a, T> { impl<'a, T: DataTypeKind> Slider<'a, T> {
/// Constructs a new slider builder with the given range. /// Constructs a new slider builder with the given range.
pub fn new(label: &ImStr, range: RangeInclusive<T>) -> Slider<T> { pub fn new(label: &ImStr) -> Slider<T> {
Slider { Slider {
label, label,
min: *range.start(), min: None,
max: *range.end(), max: None,
display_format: None, display_format: None,
flags: SliderFlags::empty(), flags: SliderFlags::empty(),
} }
} }
/// Sets the range (inclusive)
#[inline]
pub fn range<R: InclusiveRangeBounds<T>>(mut self, range: R) -> Self {
self.min = range.start_bound().copied();
self.max = range.end_bound().copied();
self
}
/// Sets the display format using *a C-style printf string* /// Sets the display format using *a C-style printf string*
#[inline] #[inline]
pub fn display_format(mut self, display_format: &'a ImStr) -> Self { pub fn display_format(mut self, display_format: &'a ImStr) -> Self {
@ -68,8 +74,14 @@ impl<'a, T: DataTypeKind> Slider<'a, T> {
self.label.as_ptr(), self.label.as_ptr(),
T::KIND as i32, T::KIND as i32,
value as *mut T as *mut c_void, value as *mut T as *mut c_void,
&self.min as *const T as *const c_void, self.min
&self.max as *const T as *const c_void, .as_ref()
.map(|min| min as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.max
.as_ref()
.map(|max| max as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.display_format self.display_format
.map(ImStr::as_ptr) .map(ImStr::as_ptr)
.unwrap_or(ptr::null()), .unwrap_or(ptr::null()),
@ -87,8 +99,14 @@ impl<'a, T: DataTypeKind> Slider<'a, T> {
T::KIND as i32, T::KIND as i32,
values.as_mut_ptr() as *mut c_void, values.as_mut_ptr() as *mut c_void,
values.len() as i32, values.len() as i32,
&self.min as *const T as *const c_void, self.min
&self.max as *const T as *const c_void, .as_ref()
.map(|min| min as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.max
.as_ref()
.map(|max| max as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.display_format self.display_format
.map(ImStr::as_ptr) .map(ImStr::as_ptr)
.unwrap_or(ptr::null()), .unwrap_or(ptr::null()),
@ -104,24 +122,31 @@ impl<'a, T: DataTypeKind> Slider<'a, T> {
pub struct VerticalSlider<'a, T: DataTypeKind + Copy> { pub struct VerticalSlider<'a, T: DataTypeKind + Copy> {
label: &'a ImStr, label: &'a ImStr,
size: [f32; 2], size: [f32; 2],
min: T, min: Option<T>,
max: T, max: Option<T>,
display_format: Option<&'a ImStr>, display_format: Option<&'a ImStr>,
flags: SliderFlags, flags: SliderFlags,
} }
impl<'a, T: DataTypeKind> VerticalSlider<'a, T> { impl<'a, T: DataTypeKind> VerticalSlider<'a, T> {
/// Constructs a new vertical slider builder with the given size and range. /// Constructs a new vertical slider builder with the given size and range.
pub fn new(label: &ImStr, size: [f32; 2], range: RangeInclusive<T>) -> VerticalSlider<T> { pub fn new(label: &ImStr, size: [f32; 2]) -> VerticalSlider<T> {
VerticalSlider { VerticalSlider {
label, label,
size, size,
min: *range.start(), min: None,
max: *range.end(), max: None,
display_format: None, display_format: None,
flags: SliderFlags::empty(), flags: SliderFlags::empty(),
} }
} }
/// Sets the range (inclusive)
#[inline]
pub fn range<R: InclusiveRangeBounds<T>>(mut self, range: R) -> Self {
self.min = range.start_bound().copied();
self.max = range.end_bound().copied();
self
}
/// Sets the display format using *a C-style printf string* /// Sets the display format using *a C-style printf string*
#[inline] #[inline]
pub fn display_format(mut self, display_format: &'a ImStr) -> Self { pub fn display_format(mut self, display_format: &'a ImStr) -> Self {
@ -144,8 +169,14 @@ impl<'a, T: DataTypeKind> VerticalSlider<'a, T> {
self.size.into(), self.size.into(),
T::KIND as i32, T::KIND as i32,
value as *mut T as *mut c_void, value as *mut T as *mut c_void,
&self.min as *const T as *const c_void, self.min
&self.max as *const T as *const c_void, .as_ref()
.map(|min| min as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.max
.as_ref()
.map(|max| max as *const T)
.unwrap_or(ptr::null()) as *const c_void,
self.display_format self.display_format
.map(ImStr::as_ptr) .map(ImStr::as_ptr)
.unwrap_or(ptr::null()), .unwrap_or(ptr::null()),
@ -178,6 +209,13 @@ impl<'a> AngleSlider<'a> {
flags: SliderFlags::empty(), flags: SliderFlags::empty(),
} }
} }
/// Sets the range (in degrees, inclusive)
#[inline]
pub fn range_degrees<R: InclusiveRangeBounds<f32>>(mut self, range: R) -> Self {
self.min_degrees = range.start_bound().copied().unwrap_or(-360.0);
self.max_degrees = range.end_bound().copied().unwrap_or(360.0);
self
}
/// Sets the minimum value (in degrees) /// Sets the minimum value (in degrees)
#[inline] #[inline]
pub fn min_degrees(mut self, min_degrees: f32) -> Self { pub fn min_degrees(mut self, min_degrees: f32) -> Self {