1
0
Fork 0

Use plain instead of unnormalized in the APIs

It takes more effort than what should be needed to tell normalized and
unnormalized apart at a glance.
This commit is contained in:
Robbert van der Helm 2022-01-29 14:54:48 +01:00
parent 97a88e0db2
commit ebb74a737c
3 changed files with 20 additions and 19 deletions

View file

@ -33,10 +33,10 @@ pub enum Range<T> {
trait NormalizebleRange<T> { trait NormalizebleRange<T> {
/// Normalize an unnormalized value. Will be clamped to the bounds of the range if the /// Normalize an unnormalized value. Will be clamped to the bounds of the range if the
/// normalized value exceeds `[0, 1]`. /// normalized value exceeds `[0, 1]`.
fn normalize(&self, unnormalized: T) -> f32; fn normalize(&self, plain: T) -> f32;
/// Unnormalize a normalized value. Will be clamped to `[0, 1]` if the unnormalized value would /// Unnormalize a normalized value. Will be clamped to `[0, 1]` if the plain, unnormalized value
/// exceed that range. /// would exceed that range.
fn unnormalize(&self, normalized: f32) -> T; fn unnormalize(&self, normalized: f32) -> T;
} }
@ -54,9 +54,9 @@ pub struct PlainParam<T> {
pub name: &'static str, pub name: &'static str,
/// The parameter value's unit, added after `value_to_string` if that is set. /// The parameter value's unit, added after `value_to_string` if that is set.
pub unit: &'static str, pub unit: &'static str,
/// Optional custom conversion function from an **unnormalized** value to a string. /// Optional custom conversion function from a plain **unnormalized** value to a string.
pub value_to_string: Option<Box<dyn Send + Sync + Fn(T) -> String>>, pub value_to_string: Option<Box<dyn Send + Sync + Fn(T) -> String>>,
/// Optional custom conversion function from a string to an **unnormalized** value. If the /// Optional custom conversion function from a string to a plain **unnormalized** value. If the
/// string cannot be parsed, then this should return a `None`. If this happens while the /// string cannot be parsed, then this should return a `None`. If this happens while the
/// parameter is being updated then the update will be canceled. /// parameter is being updated then the update will be canceled.
pub string_to_value: Option<Box<dyn Send + Sync + Fn(&str) -> Option<T>>>, pub string_to_value: Option<Box<dyn Send + Sync + Fn(&str) -> Option<T>>>,
@ -99,8 +99,8 @@ macro_rules! impl_plainparam {
}; };
match value { match value {
Some(unnormalized) => { Some(plain) => {
self.value = unnormalized; self.value = plain;
true true
} }
None => false, None => false,
@ -155,9 +155,9 @@ impl<T: Display + Copy> Display for PlainParam<T> {
} }
impl NormalizebleRange<f32> for Range<f32> { impl NormalizebleRange<f32> for Range<f32> {
fn normalize(&self, unnormalized: f32) -> f32 { fn normalize(&self, plain: f32) -> f32 {
match &self { match &self {
Range::Linear { min, max } => (unnormalized - min) / (max - min), Range::Linear { min, max } => (plain - min) / (max - min),
} }
.clamp(0.0, 1.0) .clamp(0.0, 1.0)
} }
@ -171,9 +171,9 @@ impl NormalizebleRange<f32> for Range<f32> {
} }
impl NormalizebleRange<i32> for Range<i32> { impl NormalizebleRange<i32> for Range<i32> {
fn normalize(&self, unnormalized: i32) -> f32 { fn normalize(&self, plain: i32) -> f32 {
match &self { match &self {
Range::Linear { min, max } => (unnormalized - min) as f32 / (max - min) as f32, Range::Linear { min, max } => (plain - min) as f32 / (max - min) as f32,
} }
.clamp(0.0, 1.0) .clamp(0.0, 1.0)
} }
@ -284,27 +284,28 @@ impl ParamPtr {
} }
} }
/// Get the normalized value for an unnormalized value, as a float. Used as part of the /// Get the normalized value for a plain, unnormalized value, as a float. Used as part of the
/// wrappers. /// wrappers.
/// ///
/// # Safety /// # Safety
/// ///
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is /// Calling this function is only safe as long as the object this `ParamPtr` was created for is
/// still alive. /// still alive.
pub unsafe fn preview_normalized(&self, unnormalized: f32) -> f32 { pub unsafe fn preview_normalized(&self, plain: f32) -> f32 {
match &self { match &self {
ParamPtr::FloatParam(p) => (**p).range.normalize(unnormalized), ParamPtr::FloatParam(p) => (**p).range.normalize(plain),
ParamPtr::IntParam(p) => (**p).range.normalize(unnormalized as i32), ParamPtr::IntParam(p) => (**p).range.normalize(plain as i32),
} }
} }
/// Get the unnormalized value for a normalized value, as a float. Used as part of the wrappers. /// Get the plain, unnormalized value for a normalized value, as a float. Used as part of the
/// wrappers.
/// ///
/// # Safety /// # Safety
/// ///
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is /// Calling this function is only safe as long as the object this `ParamPtr` was created for is
/// still alive. /// still alive.
pub unsafe fn preview_unnormalized(&self, normalized: f32) -> f32 { pub unsafe fn preview_plain(&self, normalized: f32) -> f32 {
match &self { match &self {
ParamPtr::FloatParam(p) => (**p).range.unnormalize(normalized), ParamPtr::FloatParam(p) => (**p).range.unnormalize(normalized),
ParamPtr::IntParam(p) => (**p).range.unnormalize(normalized) as f32, ParamPtr::IntParam(p) => (**p).range.unnormalize(normalized) as f32,

View file

@ -19,7 +19,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
/// An unnormalized value for a parameter. /// A plain, unnormalized value for a parameter.
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub(crate) enum ParamValue { pub(crate) enum ParamValue {

View file

@ -472,7 +472,7 @@ impl<P: Plugin> IEditController for Wrapper<'_, P> {
if id == *BYPASS_PARAM_HASH { if id == *BYPASS_PARAM_HASH {
value_normalized value_normalized
} else if let Some(param_ptr) = self.param_by_hash.get(&id) { } else if let Some(param_ptr) = self.param_by_hash.get(&id) {
param_ptr.preview_unnormalized(value_normalized as f32) as f64 param_ptr.preview_plain(value_normalized as f32) as f64
} else { } else {
0.5 0.5
} }