From b5a471747f4be7da5da993d285dd9490bccf5108 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Thu, 3 Mar 2022 13:55:54 +0100 Subject: [PATCH] Add name, unit, and step count functions to Param Now we can simplify ParamPtr by generating all of these accessors. --- src/param.rs | 9 +++++++++ src/param/boolean.rs | 12 ++++++++++++ src/param/enums.rs | 24 ++++++++++++++++++++++++ src/param/plain.rs | 12 ++++++++++++ src/param/range.rs | 15 +++++++++++++++ 5 files changed, 72 insertions(+) diff --git a/src/param.rs b/src/param.rs index 9b45c17c..6ca2255f 100644 --- a/src/param.rs +++ b/src/param.rs @@ -22,6 +22,15 @@ pub trait Param: Display { /// The plain parameter type. 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; + /// Get the unnormalized value for this parameter. fn plain_value(&self) -> Self::Plain; diff --git a/src/param/boolean.rs b/src/param/boolean.rs index 5f822515..51399725 100644 --- a/src/param/boolean.rs +++ b/src/param/boolean.rs @@ -54,6 +54,18 @@ impl Display for BoolParam { impl Param for BoolParam { type Plain = bool; + fn name(&self) -> &'static str { + self.name + } + + fn unit(&self) -> &'static str { + "" + } + + fn step_count(&self) -> Option { + Some(1) + } + fn plain_value(&self) -> Self::Plain { self.value } diff --git a/src/param/enums.rs b/src/param/enums.rs index 1466b24a..8b6c5cc2 100644 --- a/src/param/enums.rs +++ b/src/param/enums.rs @@ -99,6 +99,18 @@ impl Display for EnumParamInner { impl Param for EnumParam { type Plain = T; + fn name(&self) -> &'static str { + self.inner.name() + } + + fn unit(&self) -> &'static str { + self.inner.unit() + } + + fn step_count(&self) -> Option { + self.inner.step_count() + } + fn plain_value(&self) -> Self::Plain { T::from_index(self.inner.plain_value() as usize) } @@ -152,6 +164,18 @@ impl Param for EnumParam { impl Param for EnumParamInner { type Plain = i32; + fn name(&self) -> &'static str { + self.inner.name + } + + fn unit(&self) -> &'static str { + "" + } + + fn step_count(&self) -> Option { + Some(self.len()) + } + fn plain_value(&self) -> Self::Plain { self.inner.plain_value() } diff --git a/src/param/plain.rs b/src/param/plain.rs index 10d08bfe..f2ef437b 100644 --- a/src/param/plain.rs +++ b/src/param/plain.rs @@ -101,6 +101,18 @@ macro_rules! impl_plainparam { impl Param for $ty { type Plain = $plain; + fn name(&self) -> &'static str { + self.name + } + + fn unit(&self) -> &'static str { + self.unit + } + + fn step_count(&self) -> Option { + self.range.step_count() + } + fn plain_value(&self) -> Self::Plain { self.value } diff --git a/src/param/range.rs b/src/param/range.rs index b0127ea8..1ffc10e9 100644 --- a/src/param/range.rs +++ b/src/param/range.rs @@ -45,6 +45,9 @@ pub(crate) trait NormalizebleRange { /// 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; + + /// The number of steps in this range, if it is stepped. Used for the host's generic UI. + fn step_count(&self) -> Option; } impl Default for Range { @@ -132,6 +135,10 @@ impl NormalizebleRange for Range { ((value / step_size).round() * step_size).clamp(*min, *max) } + + fn step_count(&self) -> Option { + None + } } impl NormalizebleRange for Range { @@ -197,6 +204,14 @@ impl NormalizebleRange for Range { // builder interface value } + + fn step_count(&self) -> Option { + 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)]