From 0bc8f9b5b13fbcda2a48109de28c79de608e2cd9 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 5 Feb 2022 17:31:45 +0100 Subject: [PATCH] Move preview_{normalized,plain} to Param We're going to need this for setting parameter values with a gneric API. --- src/param.rs | 28 ++++++++++++++++++++++++++++ src/param/internals.rs | 10 +++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/param.rs b/src/param.rs index 7d749a8e..640edb95 100644 --- a/src/param.rs +++ b/src/param.rs @@ -72,6 +72,14 @@ pub trait Param { /// Get the string representation for a normalized value. Used as part of the wrappers. fn string_to_normalized_value(&self, string: &str) -> Option; + /// 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 /// directly. fn as_ptr(&self) -> internals::ParamPtr; @@ -227,6 +235,14 @@ macro_rules! impl_plainparam { 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 { 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 }) } + 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 { internals::ParamPtr::BoolParam(self as *const BoolParam as *mut BoolParam) } diff --git a/src/param/internals.rs b/src/param/internals.rs index e13aa1c9..d682912c 100644 --- a/src/param/internals.rs +++ b/src/param/internals.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; use std::pin::Pin; -use super::{NormalizebleRange, Param}; +use super::Param; /// Re-export for use in the [Params] proc-macro. pub use serde_json::from_str as deserialize_field; @@ -191,8 +191,8 @@ impl ParamPtr { /// still alive. pub unsafe fn preview_normalized(&self, plain: f32) -> f32 { match &self { - ParamPtr::FloatParam(p) => (**p).range.normalize(plain), - ParamPtr::IntParam(p) => (**p).range.normalize(plain as i32), + ParamPtr::FloatParam(p) => (**p).preview_normalized(plain), + ParamPtr::IntParam(p) => (**p).preview_normalized(plain as i32), ParamPtr::BoolParam(_) => plain, } } @@ -206,8 +206,8 @@ impl ParamPtr { /// still alive. pub unsafe fn preview_plain(&self, normalized: f32) -> f32 { match &self { - ParamPtr::FloatParam(p) => (**p).range.unnormalize(normalized), - ParamPtr::IntParam(p) => (**p).range.unnormalize(normalized) as f32, + ParamPtr::FloatParam(p) => (**p).preview_plain(normalized), + ParamPtr::IntParam(p) => (**p).preview_plain(normalized) as f32, ParamPtr::BoolParam(_) => normalized, } }