1
0
Fork 0

Move preview_{normalized,plain} to Param

We're going to need this for setting parameter values with a gneric API.
This commit is contained in:
Robbert van der Helm 2022-02-05 17:31:45 +01:00
parent 9d2ddf016a
commit 0bc8f9b5b1
2 changed files with 33 additions and 5 deletions

View file

@ -72,6 +72,14 @@ pub trait Param {
/// Get the string representation for a normalized value. Used as part of the wrappers. /// Get the string representation for a normalized value. Used as part of the wrappers.
fn string_to_normalized_value(&self, string: &str) -> Option<f32>; fn string_to_normalized_value(&self, string: &str) -> Option<f32>;
/// Get the normalized value for a plain, unnormalized value, as a float. Used as part of the
/// wrappers.
fn preview_normalized(&self, plain: Self::Plain) -> f32;
/// Get the plain, unnormalized value for a normalized value, as a float. Used as part of the
/// wrappers.
fn preview_plain(&self, normalized: f32) -> Self::Plain;
/// Internal implementation detail for implementing [internals::Params]. This should not be used /// Internal implementation detail for implementing [internals::Params]. This should not be used
/// directly. /// directly.
fn as_ptr(&self) -> internals::ParamPtr; fn as_ptr(&self) -> internals::ParamPtr;
@ -227,6 +235,14 @@ macro_rules! impl_plainparam {
Some(self.range.normalize(value)) Some(self.range.normalize(value))
} }
fn preview_normalized(&self, plain: Self::Plain) -> f32 {
self.range.normalize(plain)
}
fn preview_plain(&self, normalized: f32) -> Self::Plain {
self.range.unnormalize(normalized)
}
fn as_ptr(&self) -> internals::ParamPtr { fn as_ptr(&self) -> internals::ParamPtr {
internals::ParamPtr::$ty(self as *const $ty as *mut $ty) internals::ParamPtr::$ty(self as *const $ty as *mut $ty)
} }
@ -300,6 +316,18 @@ impl Param for BoolParam {
Some(if value { 1.0 } else { 0.0 }) Some(if value { 1.0 } else { 0.0 })
} }
fn preview_normalized(&self, plain: Self::Plain) -> f32 {
if plain {
1.0
} else {
0.0
}
}
fn preview_plain(&self, normalized: f32) -> Self::Plain {
normalized > 0.5
}
fn as_ptr(&self) -> internals::ParamPtr { fn as_ptr(&self) -> internals::ParamPtr {
internals::ParamPtr::BoolParam(self as *const BoolParam as *mut BoolParam) internals::ParamPtr::BoolParam(self as *const BoolParam as *mut BoolParam)
} }

View file

@ -19,7 +19,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::pin::Pin; use std::pin::Pin;
use super::{NormalizebleRange, Param}; use super::Param;
/// Re-export for use in the [Params] proc-macro. /// Re-export for use in the [Params] proc-macro.
pub use serde_json::from_str as deserialize_field; pub use serde_json::from_str as deserialize_field;
@ -191,8 +191,8 @@ impl ParamPtr {
/// still alive. /// still alive.
pub unsafe fn preview_normalized(&self, plain: f32) -> f32 { pub unsafe fn preview_normalized(&self, plain: f32) -> f32 {
match &self { match &self {
ParamPtr::FloatParam(p) => (**p).range.normalize(plain), ParamPtr::FloatParam(p) => (**p).preview_normalized(plain),
ParamPtr::IntParam(p) => (**p).range.normalize(plain as i32), ParamPtr::IntParam(p) => (**p).preview_normalized(plain as i32),
ParamPtr::BoolParam(_) => plain, ParamPtr::BoolParam(_) => plain,
} }
} }
@ -206,8 +206,8 @@ impl ParamPtr {
/// still alive. /// still alive.
pub unsafe fn preview_plain(&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).preview_plain(normalized),
ParamPtr::IntParam(p) => (**p).range.unnormalize(normalized) as f32, ParamPtr::IntParam(p) => (**p).preview_plain(normalized) as f32,
ParamPtr::BoolParam(_) => normalized, ParamPtr::BoolParam(_) => normalized,
} }
} }