Skip to content

Commit

Permalink
Looks like sliders *do* need a default range
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Sep 19, 2020
1 parent e5c6eb9 commit 2922dab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
11 changes: 11 additions & 0 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,36 +119,47 @@ pub enum DataType {
/// representation in memory as the primitive value described by the associated `KIND` constant.
pub unsafe trait DataTypeKind: Copy {
const KIND: DataType;
const SLIDER_RANGE: RangeInclusive<Self>;
}
unsafe impl DataTypeKind for i8 {
const KIND: DataType = DataType::I8;
const SLIDER_RANGE: RangeInclusive<i8> = i8::MIN..=i8::MAX;
}
unsafe impl DataTypeKind for u8 {
const KIND: DataType = DataType::U8;
const SLIDER_RANGE: RangeInclusive<u8> = u8::MIN..=u8::MAX;
}
unsafe impl DataTypeKind for i16 {
const KIND: DataType = DataType::I16;
const SLIDER_RANGE: RangeInclusive<i16> = i16::MIN..=i16::MAX;
}
unsafe impl DataTypeKind for u16 {
const KIND: DataType = DataType::U16;
const SLIDER_RANGE: RangeInclusive<u16> = u16::MIN..=u16::MAX;
}
unsafe impl DataTypeKind for i32 {
const KIND: DataType = DataType::I32;
const SLIDER_RANGE: RangeInclusive<i32> = (i32::MIN / 2)..=(i32::MAX / 2);
}
unsafe impl DataTypeKind for u32 {
const KIND: DataType = DataType::U32;
const SLIDER_RANGE: RangeInclusive<u32> = (u32::MIN / 2)..=(u32::MAX / 2);
}
unsafe impl DataTypeKind for i64 {
const KIND: DataType = DataType::I64;
const SLIDER_RANGE: RangeInclusive<i64> = (i64::MIN / 2)..=(i64::MAX / 2);
}
unsafe impl DataTypeKind for u64 {
const KIND: DataType = DataType::U64;
const SLIDER_RANGE: RangeInclusive<u64> = (u64::MIN / 2)..=(u64::MAX / 2);
}
unsafe impl DataTypeKind for f32 {
const KIND: DataType = DataType::F32;
const SLIDER_RANGE: RangeInclusive<f32> = (f32::MIN / 2.0)..=(f32::MAX / 2.0);
}
unsafe impl DataTypeKind for f64 {
const KIND: DataType = DataType::F64;
const SLIDER_RANGE: RangeInclusive<f64> = (f64::MIN / 2.0)..=(f64::MAX / 2.0);
}

pub trait InclusiveRangeBounds<T: Copy> {
Expand Down
54 changes: 18 additions & 36 deletions src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ bitflags!(
#[must_use]
pub struct Slider<'a, T: DataTypeKind> {
label: &'a ImStr,
min: Option<T>,
max: Option<T>,
min: T,
max: T,
display_format: Option<&'a ImStr>,
flags: SliderFlags,
}
Expand All @@ -40,17 +40,17 @@ impl<'a, T: DataTypeKind> Slider<'a, T> {
pub fn new(label: &ImStr) -> Slider<T> {
Slider {
label,
min: None,
max: None,
min: *T::SLIDER_RANGE.start(),
max: *T::SLIDER_RANGE.end(),
display_format: None,
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.min = *range.start_bound().unwrap_or(T::SLIDER_RANGE.start());
self.max = *range.end_bound().unwrap_or(T::SLIDER_RANGE.end());
self
}
/// Sets the display format using *a C-style printf string*
Expand All @@ -74,14 +74,8 @@ impl<'a, T: DataTypeKind> Slider<'a, T> {
self.label.as_ptr(),
T::KIND as i32,
value as *mut T as *mut c_void,
self.min
.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.min as *const T as *const c_void,
&self.max as *const T as *const c_void,
self.display_format
.map(ImStr::as_ptr)
.unwrap_or(ptr::null()),
Expand All @@ -99,14 +93,8 @@ impl<'a, T: DataTypeKind> Slider<'a, T> {
T::KIND as i32,
values.as_mut_ptr() as *mut c_void,
values.len() as i32,
self.min
.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.min as *const T as *const c_void,
&self.max as *const T as *const c_void,
self.display_format
.map(ImStr::as_ptr)
.unwrap_or(ptr::null()),
Expand All @@ -122,8 +110,8 @@ impl<'a, T: DataTypeKind> Slider<'a, T> {
pub struct VerticalSlider<'a, T: DataTypeKind + Copy> {
label: &'a ImStr,
size: [f32; 2],
min: Option<T>,
max: Option<T>,
min: T,
max: T,
display_format: Option<&'a ImStr>,
flags: SliderFlags,
}
Expand All @@ -134,17 +122,17 @@ impl<'a, T: DataTypeKind> VerticalSlider<'a, T> {
VerticalSlider {
label,
size,
min: None,
max: None,
min: *T::SLIDER_RANGE.start(),
max: *T::SLIDER_RANGE.end(),
display_format: None,
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.min = *range.start_bound().unwrap_or(T::SLIDER_RANGE.start());
self.max = *range.end_bound().unwrap_or(T::SLIDER_RANGE.end());
self
}
/// Sets the display format using *a C-style printf string*
Expand All @@ -169,14 +157,8 @@ impl<'a, T: DataTypeKind> VerticalSlider<'a, T> {
self.size.into(),
T::KIND as i32,
value as *mut T as *mut c_void,
self.min
.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.min as *const T as *const c_void,
&self.max as *const T as *const c_void,
self.display_format
.map(ImStr::as_ptr)
.unwrap_or(ptr::null()),
Expand Down

0 comments on commit 2922dab

Please sign in to comment.