1
0
Fork 0

Add name, unit, and step count functions to Param

Now we can simplify ParamPtr by generating all of these accessors.
This commit is contained in:
Robbert van der Helm 2022-03-03 13:55:54 +01:00
parent 4eb35ed585
commit b5a471747f
5 changed files with 72 additions and 0 deletions

View file

@ -22,6 +22,15 @@ pub trait Param: Display {
/// The plain parameter type. /// The plain parameter type.
type Plain; type Plain;
/// Get the human readable name for this parameter.
fn name(&self) -> &'static str;
/// Get the unit label for this parameter, if any.
fn unit(&self) -> &'static str;
/// Get the number of steps for this paramter, if it is stepped. Used for the host's generic UI.
fn step_count(&self) -> Option<usize>;
/// Get the unnormalized value for this parameter. /// Get the unnormalized value for this parameter.
fn plain_value(&self) -> Self::Plain; fn plain_value(&self) -> Self::Plain;

View file

@ -54,6 +54,18 @@ impl Display for BoolParam {
impl Param for BoolParam { impl Param for BoolParam {
type Plain = bool; type Plain = bool;
fn name(&self) -> &'static str {
self.name
}
fn unit(&self) -> &'static str {
""
}
fn step_count(&self) -> Option<usize> {
Some(1)
}
fn plain_value(&self) -> Self::Plain { fn plain_value(&self) -> Self::Plain {
self.value self.value
} }

View file

@ -99,6 +99,18 @@ impl Display for EnumParamInner {
impl<T: Enum> Param for EnumParam<T> { impl<T: Enum> Param for EnumParam<T> {
type Plain = T; type Plain = T;
fn name(&self) -> &'static str {
self.inner.name()
}
fn unit(&self) -> &'static str {
self.inner.unit()
}
fn step_count(&self) -> Option<usize> {
self.inner.step_count()
}
fn plain_value(&self) -> Self::Plain { fn plain_value(&self) -> Self::Plain {
T::from_index(self.inner.plain_value() as usize) T::from_index(self.inner.plain_value() as usize)
} }
@ -152,6 +164,18 @@ impl<T: Enum> Param for EnumParam<T> {
impl Param for EnumParamInner { impl Param for EnumParamInner {
type Plain = i32; type Plain = i32;
fn name(&self) -> &'static str {
self.inner.name
}
fn unit(&self) -> &'static str {
""
}
fn step_count(&self) -> Option<usize> {
Some(self.len())
}
fn plain_value(&self) -> Self::Plain { fn plain_value(&self) -> Self::Plain {
self.inner.plain_value() self.inner.plain_value()
} }

View file

@ -101,6 +101,18 @@ macro_rules! impl_plainparam {
impl Param for $ty { impl Param for $ty {
type Plain = $plain; type Plain = $plain;
fn name(&self) -> &'static str {
self.name
}
fn unit(&self) -> &'static str {
self.unit
}
fn step_count(&self) -> Option<usize> {
self.range.step_count()
}
fn plain_value(&self) -> Self::Plain { fn plain_value(&self) -> Self::Plain {
self.value self.value
} }

View file

@ -45,6 +45,9 @@ pub(crate) trait NormalizebleRange<T> {
/// Snap a vlue to a step size, clamping to the minimum and maximum value of the range. /// Snap a vlue to a step size, clamping to the minimum and maximum value of the range.
fn snap_to_step(&self, value: T, step_size: T) -> T; fn snap_to_step(&self, value: T, step_size: T) -> T;
/// The number of steps in this range, if it is stepped. Used for the host's generic UI.
fn step_count(&self) -> Option<usize>;
} }
impl Default for Range<f32> { impl Default for Range<f32> {
@ -132,6 +135,10 @@ impl NormalizebleRange<f32> for Range<f32> {
((value / step_size).round() * step_size).clamp(*min, *max) ((value / step_size).round() * step_size).clamp(*min, *max)
} }
fn step_count(&self) -> Option<usize> {
None
}
} }
impl NormalizebleRange<i32> for Range<i32> { impl NormalizebleRange<i32> for Range<i32> {
@ -197,6 +204,14 @@ impl NormalizebleRange<i32> for Range<i32> {
// builder interface // builder interface
value value
} }
fn step_count(&self) -> Option<usize> {
match self {
Range::Linear { min, max } => Some((max - min) as usize),
Range::Skewed { min, max, .. } => Some((max - min) as usize),
Range::SymmetricalSkewed { min, max, .. } => Some((max - min) as usize),
}
}
} }
#[cfg(test)] #[cfg(test)]