1
0
Fork 0

Rearrange the Param methods

This commit is contained in:
Robbert van der Helm 2022-03-19 16:09:31 +01:00
parent d9330628c0
commit 95370667d7
6 changed files with 49 additions and 49 deletions

View file

@ -30,6 +30,12 @@ pub trait Param: Display {
/// Get the unit label for this parameter, if any. /// Get the unit label for this parameter, if any.
fn unit(&self) -> &'static str; 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 /// Get the number of steps for this paramter, if it is discrete. Used for the host's generic
/// UI. /// UI.
fn step_count(&self) -> Option<usize>; fn step_count(&self) -> Option<usize>;
@ -46,18 +52,12 @@ pub trait Param: Display {
/// hundredth of the normalized range instead. /// hundredth of the normalized range instead.
fn next_step(&self, from: Self::Plain) -> Self::Plain; 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 /// Set this parameter based on a plain, unnormalized value. This does **not** snap to step
/// sizes for continuous parameters (i.e. [`FloatParam`]). /// sizes for continuous parameters (i.e. [`FloatParam`]).
/// ///
/// This does **not** update the smoother. /// This does **not** update the smoother.
fn set_plain_value(&mut self, plain: Self::Plain); 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 /// Set this parameter based on a normalized value. This **does** snap to step sizes for
/// continuous parameters (i.e. [`FloatParam`]). /// continuous parameters (i.e. [`FloatParam`]).
/// ///

View file

@ -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<usize> { fn step_count(&self) -> Option<usize> {
Some(1) Some(1)
} }
@ -74,10 +82,6 @@ impl Param for BoolParam {
true true
} }
fn plain_value(&self) -> Self::Plain {
self.value
}
fn set_plain_value(&mut self, plain: Self::Plain) { fn set_plain_value(&mut self, plain: Self::Plain) {
self.value = plain; self.value = plain;
if let Some(f) = &self.value_changed { 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) { fn set_normalized_value(&mut self, normalized: f32) {
self.set_plain_value(self.preview_plain(normalized)); self.set_plain_value(self.preview_plain(normalized));
} }

View file

@ -109,6 +109,14 @@ impl<T: Enum + PartialEq> Param for EnumParam<T> {
self.inner.unit() 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<usize> { fn step_count(&self) -> Option<usize> {
self.inner.step_count() self.inner.step_count()
} }
@ -121,18 +129,10 @@ impl<T: Enum + PartialEq> Param for EnumParam<T> {
T::from_index(self.inner.next_step(T::to_index(from) as i32) as usize) 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) { fn set_plain_value(&mut self, plain: Self::Plain) {
self.inner.set_plain_value(T::to_index(plain) as i32) 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) { fn set_normalized_value(&mut self, normalized: f32) {
self.inner.set_normalized_value(normalized) 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<usize> { fn step_count(&self) -> Option<usize> {
Some(self.len() - 1) Some(self.len() - 1)
} }
@ -190,18 +198,10 @@ impl Param for EnumParamInner {
self.inner.next_step(from) self.inner.next_step(from)
} }
fn plain_value(&self) -> Self::Plain {
self.inner.plain_value()
}
fn set_plain_value(&mut self, plain: Self::Plain) { fn set_plain_value(&mut self, plain: Self::Plain) {
self.inner.set_plain_value(plain) self.inner.set_plain_value(plain)
} }
fn normalized_value(&self) -> f32 {
self.inner.normalized_value()
}
fn set_normalized_value(&mut self, normalized: f32) { fn set_normalized_value(&mut self, normalized: f32) {
self.inner.set_normalized_value(normalized) self.inner.set_normalized_value(normalized)
} }

View file

@ -103,6 +103,14 @@ impl Param for FloatParam {
self.unit 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<usize> { fn step_count(&self) -> Option<usize> {
None None
} }
@ -130,10 +138,6 @@ impl Param for FloatParam {
.clamp(self.range.min(), self.range.max()) .clamp(self.range.min(), self.range.max())
} }
fn plain_value(&self) -> Self::Plain {
self.value
}
fn set_plain_value(&mut self, plain: Self::Plain) { fn set_plain_value(&mut self, plain: Self::Plain) {
self.value = plain; self.value = plain;
if let Some(f) = &self.value_changed { 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) { fn set_normalized_value(&mut self, normalized: f32) {
self.set_plain_value(self.preview_plain(normalized)); self.set_plain_value(self.preview_plain(normalized));
} }

View file

@ -94,6 +94,14 @@ impl Param for IntParam {
self.unit 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<usize> { fn step_count(&self) -> Option<usize> {
Some(self.range.step_count()) Some(self.range.step_count())
} }
@ -106,10 +114,6 @@ impl Param for IntParam {
(from + 1).clamp(self.range.min(), self.range.max()) (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) { fn set_plain_value(&mut self, plain: Self::Plain) {
self.value = plain; self.value = plain;
if let Some(f) = &self.value_changed { 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) { fn set_normalized_value(&mut self, normalized: f32) {
self.set_plain_value(self.preview_plain(normalized)); self.set_plain_value(self.preview_plain(normalized));
} }

View file

@ -146,12 +146,12 @@ macro_rules! param_ptr_forward(
impl ParamPtr { impl ParamPtr {
param_ptr_forward!(pub unsafe fn name(&self) -> &'static str); param_ptr_forward!(pub unsafe fn name(&self) -> &'static str);
param_ptr_forward!(pub unsafe fn step_count(&self) -> Option<usize>);
param_ptr_forward!(pub unsafe fn unit(&self) -> &'static str); 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<usize>);
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 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 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 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<f32>); param_ptr_forward!(pub unsafe fn string_to_normalized_value(&self, string: &str) -> Option<f32>);