From 95370667d7319b730952d1d091697370683a0d48 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 19 Mar 2022 16:09:31 +0100 Subject: [PATCH] Rearrange the Param methods --- src/param.rs | 12 ++++++------ src/param/boolean.rs | 16 ++++++++-------- src/param/enums.rs | 32 ++++++++++++++++---------------- src/param/float.rs | 16 ++++++++-------- src/param/integer.rs | 16 ++++++++-------- src/param/internals.rs | 6 +++--- 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/param.rs b/src/param.rs index e1dea1ec..d0a41743 100644 --- a/src/param.rs +++ b/src/param.rs @@ -30,6 +30,12 @@ pub trait Param: Display { /// Get the unit label for this parameter, if any. fn unit(&self) -> &'static str; + /// Get the unnormalized value for this parameter. + fn plain_value(&self) -> Self::Plain; + + /// Get the normalized `[0, 1]` value for this parameter. + fn normalized_value(&self) -> f32; + /// Get the number of steps for this paramter, if it is discrete. Used for the host's generic /// UI. fn step_count(&self) -> Option; @@ -46,18 +52,12 @@ pub trait Param: Display { /// hundredth of the normalized range instead. fn next_step(&self, from: Self::Plain) -> Self::Plain; - /// Get the unnormalized value for this parameter. - fn plain_value(&self) -> Self::Plain; - /// Set this parameter based on a plain, unnormalized value. This does **not** snap to step /// sizes for continuous parameters (i.e. [`FloatParam`]). /// /// This does **not** update the smoother. fn set_plain_value(&mut self, plain: Self::Plain); - /// Get the normalized `[0, 1]` value for this parameter. - fn normalized_value(&self) -> f32; - /// Set this parameter based on a normalized value. This **does** snap to step sizes for /// continuous parameters (i.e. [`FloatParam`]). /// diff --git a/src/param/boolean.rs b/src/param/boolean.rs index 0a354c45..3b1c81ca 100644 --- a/src/param/boolean.rs +++ b/src/param/boolean.rs @@ -62,6 +62,14 @@ impl Param for BoolParam { "" } + fn plain_value(&self) -> Self::Plain { + self.value + } + + fn normalized_value(&self) -> f32 { + self.preview_normalized(self.value) + } + fn step_count(&self) -> Option { Some(1) } @@ -74,10 +82,6 @@ impl Param for BoolParam { true } - fn plain_value(&self) -> Self::Plain { - self.value - } - fn set_plain_value(&mut self, plain: Self::Plain) { self.value = plain; if let Some(f) = &self.value_changed { @@ -85,10 +89,6 @@ impl Param for BoolParam { } } - fn normalized_value(&self) -> f32 { - self.preview_normalized(self.value) - } - fn set_normalized_value(&mut self, normalized: f32) { self.set_plain_value(self.preview_plain(normalized)); } diff --git a/src/param/enums.rs b/src/param/enums.rs index 4e184f20..d485becd 100644 --- a/src/param/enums.rs +++ b/src/param/enums.rs @@ -109,6 +109,14 @@ impl Param for EnumParam { self.inner.unit() } + fn plain_value(&self) -> Self::Plain { + T::from_index(self.inner.plain_value() as usize) + } + + fn normalized_value(&self) -> f32 { + self.inner.normalized_value() + } + fn step_count(&self) -> Option { self.inner.step_count() } @@ -121,18 +129,10 @@ impl Param for EnumParam { T::from_index(self.inner.next_step(T::to_index(from) as i32) as usize) } - fn plain_value(&self) -> Self::Plain { - T::from_index(self.inner.plain_value() as usize) - } - fn set_plain_value(&mut self, plain: Self::Plain) { self.inner.set_plain_value(T::to_index(plain) as i32) } - fn normalized_value(&self) -> f32 { - self.inner.normalized_value() - } - fn set_normalized_value(&mut self, normalized: f32) { self.inner.set_normalized_value(normalized) } @@ -178,6 +178,14 @@ impl Param for EnumParamInner { "" } + fn plain_value(&self) -> Self::Plain { + self.inner.plain_value() + } + + fn normalized_value(&self) -> f32 { + self.inner.normalized_value() + } + fn step_count(&self) -> Option { Some(self.len() - 1) } @@ -190,18 +198,10 @@ impl Param for EnumParamInner { self.inner.next_step(from) } - fn plain_value(&self) -> Self::Plain { - self.inner.plain_value() - } - fn set_plain_value(&mut self, plain: Self::Plain) { self.inner.set_plain_value(plain) } - fn normalized_value(&self) -> f32 { - self.inner.normalized_value() - } - fn set_normalized_value(&mut self, normalized: f32) { self.inner.set_normalized_value(normalized) } diff --git a/src/param/float.rs b/src/param/float.rs index 3e27feeb..66bd6466 100644 --- a/src/param/float.rs +++ b/src/param/float.rs @@ -103,6 +103,14 @@ impl Param for FloatParam { self.unit } + fn plain_value(&self) -> Self::Plain { + self.value + } + + fn normalized_value(&self) -> f32 { + self.preview_normalized(self.value) + } + fn step_count(&self) -> Option { None } @@ -130,10 +138,6 @@ impl Param for FloatParam { .clamp(self.range.min(), self.range.max()) } - fn plain_value(&self) -> Self::Plain { - self.value - } - fn set_plain_value(&mut self, plain: Self::Plain) { self.value = plain; if let Some(f) = &self.value_changed { @@ -141,10 +145,6 @@ impl Param for FloatParam { } } - fn normalized_value(&self) -> f32 { - self.preview_normalized(self.value) - } - fn set_normalized_value(&mut self, normalized: f32) { self.set_plain_value(self.preview_plain(normalized)); } diff --git a/src/param/integer.rs b/src/param/integer.rs index d7e913c3..256e98be 100644 --- a/src/param/integer.rs +++ b/src/param/integer.rs @@ -94,6 +94,14 @@ impl Param for IntParam { self.unit } + fn plain_value(&self) -> Self::Plain { + self.value + } + + fn normalized_value(&self) -> f32 { + self.preview_normalized(self.value) + } + fn step_count(&self) -> Option { Some(self.range.step_count()) } @@ -106,10 +114,6 @@ impl Param for IntParam { (from + 1).clamp(self.range.min(), self.range.max()) } - fn plain_value(&self) -> Self::Plain { - self.value - } - fn set_plain_value(&mut self, plain: Self::Plain) { self.value = plain; if let Some(f) = &self.value_changed { @@ -117,10 +121,6 @@ impl Param for IntParam { } } - fn normalized_value(&self) -> f32 { - self.preview_normalized(self.value) - } - fn set_normalized_value(&mut self, normalized: f32) { self.set_plain_value(self.preview_plain(normalized)); } diff --git a/src/param/internals.rs b/src/param/internals.rs index 0af42523..c084fb9f 100644 --- a/src/param/internals.rs +++ b/src/param/internals.rs @@ -146,12 +146,12 @@ macro_rules! param_ptr_forward( impl ParamPtr { param_ptr_forward!(pub unsafe fn name(&self) -> &'static str); - param_ptr_forward!(pub unsafe fn step_count(&self) -> Option); param_ptr_forward!(pub unsafe fn unit(&self) -> &'static str); + param_ptr_forward!(pub unsafe fn normalized_value(&self) -> f32); + param_ptr_forward!(pub unsafe fn step_count(&self) -> Option); + param_ptr_forward!(pub unsafe fn set_normalized_value(&self, normalized: f32)); param_ptr_forward!(pub unsafe fn update_smoother(&self, sample_rate: f32, reset: bool)); param_ptr_forward!(pub unsafe fn initialize_block_smoother(&mut self, max_block_size: usize)); - param_ptr_forward!(pub unsafe fn normalized_value(&self) -> f32); - param_ptr_forward!(pub unsafe fn set_normalized_value(&self, normalized: f32)); param_ptr_forward!(pub unsafe fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String); param_ptr_forward!(pub unsafe fn string_to_normalized_value(&self, string: &str) -> Option);