Allow custom names for enum variants
This commit is contained in:
parent
0c1f105910
commit
847887db9d
3 changed files with 30 additions and 19 deletions
|
@ -18,7 +18,7 @@ pub use param::range::Range;
|
||||||
pub use param::smoothing::{Smoother, SmoothingStyle};
|
pub use param::smoothing::{Smoother, SmoothingStyle};
|
||||||
pub use param::{BoolParam, FloatParam, IntParam, Param};
|
pub use param::{BoolParam, FloatParam, IntParam, Param};
|
||||||
// TODO: Consider re-exporting these from another module so you can import them all at once
|
// TODO: Consider re-exporting these from another module so you can import them all at once
|
||||||
pub use param::{Display, EnumIter, EnumParam};
|
pub use param::{Display, EnumIter, EnumMessage, EnumParam};
|
||||||
pub use plugin::{
|
pub use plugin::{
|
||||||
BufferConfig, BusConfig, Editor, NoteEvent, ParentWindowHandle, Plugin, ProcessStatus,
|
BufferConfig, BusConfig, Editor, NoteEvent, ParentWindowHandle, Plugin, ProcessStatus,
|
||||||
Vst3Plugin,
|
Vst3Plugin,
|
||||||
|
|
43
src/param.rs
43
src/param.rs
|
@ -7,7 +7,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
// Re-export for the [EnumParam]
|
// Re-export for the [EnumParam]
|
||||||
// TODO: Consider re-exporting this from a non-root module to make it a bit less spammy:w
|
// TODO: Consider re-exporting this from a non-root module to make it a bit less spammy:w
|
||||||
pub use strum::{Display, EnumIter, IntoEnumIterator as EnumIter};
|
pub use strum::{Display, EnumIter, EnumMessage, IntoEnumIterator as EnumIter};
|
||||||
|
|
||||||
pub mod internals;
|
pub mod internals;
|
||||||
pub mod range;
|
pub mod range;
|
||||||
|
@ -149,11 +149,11 @@ pub struct BoolParam {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An [IntParam]-backed categorical parameter that allows convenient conversion to and from a
|
/// An [IntParam]-backed categorical parameter that allows convenient conversion to and from a
|
||||||
/// simple enum. This enum must derive the re-exported [EnumIter], [EnumString] and [Display]
|
/// simple enum. This enum must derive the re-exported [EnumIter] and [EnumMessage] and [Display]
|
||||||
/// traits.
|
/// traits. You can use the `#[strum(message = "Foo Bar")]` to override the name of the variant.
|
||||||
//
|
//
|
||||||
// TODO: Figure out a more sound way to get the same interface
|
// TODO: Figure out a more sound way to get the same interface
|
||||||
pub struct EnumParam<T: EnumIter + Eq + Copy + Display> {
|
pub struct EnumParam<T: EnumIter + EnumMessage + Eq + Copy + Display> {
|
||||||
/// The integer parameter backing this enum parameter.
|
/// The integer parameter backing this enum parameter.
|
||||||
pub inner: IntParam,
|
pub inner: IntParam,
|
||||||
/// An associative list of the variants converted to an i32 and their names. We need this
|
/// An associative list of the variants converted to an i32 and their names. We need this
|
||||||
|
@ -196,15 +196,16 @@ impl Default for BoolParam {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EnumIter + Eq + Copy + Display + Default> Default for EnumParam<T> {
|
impl<T: EnumIter + EnumMessage + Eq + Copy + Display + Default> Default for EnumParam<T> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let variants: Vec<_> = T::iter().map(|v| (v, v.to_string())).collect();
|
let variants: Vec<_> = Self::build_variants();
|
||||||
let default = T::default();
|
let default = T::default();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
inner: IntParam {
|
inner: IntParam {
|
||||||
value: T::iter()
|
value: variants
|
||||||
.position(|v| v == default)
|
.iter()
|
||||||
|
.position(|(v, _)| v == &default)
|
||||||
.expect("Invalid variant in init") as i32,
|
.expect("Invalid variant in init") as i32,
|
||||||
range: Range::Linear {
|
range: Range::Linear {
|
||||||
min: 0,
|
min: 0,
|
||||||
|
@ -393,7 +394,7 @@ impl Param for BoolParam {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EnumIter + Eq + Copy + Display> Param for EnumParam<T> {
|
impl<T: EnumIter + EnumMessage + Eq + Copy + Display> Param for EnumParam<T> {
|
||||||
type Plain = T;
|
type Plain = T;
|
||||||
|
|
||||||
fn update_smoother(&mut self, sample_rate: f32, reset: bool) {
|
fn update_smoother(&mut self, sample_rate: f32, reset: bool) {
|
||||||
|
@ -480,7 +481,7 @@ impl Display for BoolParam {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EnumIter + Eq + Copy + Display> Display for EnumParam<T> {
|
impl<T: EnumIter + EnumMessage + Eq + Copy + Display> Display for EnumParam<T> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{}", self.variants[self.inner.plain_value() as usize].1)
|
write!(f, "{}", self.variants[self.inner.plain_value() as usize].1)
|
||||||
}
|
}
|
||||||
|
@ -602,11 +603,11 @@ impl BoolParam {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EnumIter + Eq + Copy + Display> EnumParam<T> {
|
impl<T: EnumIter + EnumMessage + Eq + Copy + Display> EnumParam<T> {
|
||||||
/// Build a new [Self]. Use the other associated functions to modify the behavior of the
|
/// Build a new [Self]. Use the other associated functions to modify the behavior of the
|
||||||
/// parameter.
|
/// parameter.
|
||||||
pub fn new(name: &'static str, default: T) -> Self {
|
pub fn new(name: &'static str, default: T) -> Self {
|
||||||
let variants: Vec<_> = T::iter().map(|v| (v, v.to_string())).collect();
|
let variants: Vec<_> = Self::build_variants();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
inner: IntParam {
|
inner: IntParam {
|
||||||
|
@ -630,7 +631,7 @@ impl<T: EnumIter + Eq + Copy + Display> EnumParam<T> {
|
||||||
// TODO: Do exactly that
|
// TODO: Do exactly that
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EnumIter + Eq + Copy + Display> EnumParam<T> {
|
impl<T: EnumIter + EnumMessage + Eq + Copy + Display> EnumParam<T> {
|
||||||
// TODO: There doesn't seem to be a single enum crate that gives you a dense [0, n_variatns)
|
// TODO: There doesn't seem to be a single enum crate that gives you a dense [0, n_variatns)
|
||||||
// mapping between integers and enum variants. So far linear search over this variants has
|
// mapping between integers and enum variants. So far linear search over this variants has
|
||||||
// been the best approach. We should probably replace this with our own macro at some
|
// been the best approach. We should probably replace this with our own macro at some
|
||||||
|
@ -645,13 +646,11 @@ impl<T: EnumIter + Eq + Copy + Display> EnumParam<T> {
|
||||||
//
|
//
|
||||||
// TODO: Come up with a sounder way to do this.
|
// TODO: Come up with a sounder way to do this.
|
||||||
#[allow(clippy::len_without_is_empty)]
|
#[allow(clippy::len_without_is_empty)]
|
||||||
#[inline(never)]
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.variants.len()
|
self.variants.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the index associated to an enum variant.
|
/// Get the index associated to an enum variant.
|
||||||
#[inline(never)]
|
|
||||||
fn to_index(&self, variant: T) -> i32 {
|
fn to_index(&self, variant: T) -> i32 {
|
||||||
self.variants
|
self.variants
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -667,10 +666,22 @@ impl<T: EnumIter + Eq + Copy + Display> EnumParam<T> {
|
||||||
///
|
///
|
||||||
/// indices `>= Self::len()` will trigger a panic.
|
/// indices `>= Self::len()` will trigger a panic.
|
||||||
#[allow(clippy::wrong_self_convention)]
|
#[allow(clippy::wrong_self_convention)]
|
||||||
#[inline(never)]
|
|
||||||
fn from_index(&self, index: i32) -> T {
|
fn from_index(&self, index: i32) -> T {
|
||||||
self.variants[index as usize].0
|
self.variants[index as usize].0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_variants() -> Vec<(T, String)> {
|
||||||
|
T::iter()
|
||||||
|
.map(|v| {
|
||||||
|
(
|
||||||
|
v,
|
||||||
|
v.get_message()
|
||||||
|
.map(|custom_name| custom_name.to_string())
|
||||||
|
.unwrap_or_else(|| v.to_string()),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Caldculate how many decimals to round to when displaying a floating point value with a specific
|
/// Caldculate how many decimals to round to when displaying a floating point value with a specific
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use super::{Display, EnumIter, Param};
|
use super::{Display, EnumIter, EnumMessage, 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;
|
||||||
|
@ -50,7 +50,7 @@ pub trait Params {
|
||||||
|
|
||||||
/// Dummy enum for in [ParamPtr]. This type needs an explicit representation size so we can compare
|
/// Dummy enum for in [ParamPtr]. This type needs an explicit representation size so we can compare
|
||||||
/// the discriminants.
|
/// the discriminants.
|
||||||
#[derive(Display, Clone, Copy, PartialEq, Eq, EnumIter)]
|
#[derive(Display, Clone, Copy, PartialEq, Eq, EnumIter, EnumMessage)]
|
||||||
#[repr(i32)]
|
#[repr(i32)]
|
||||||
pub enum AnyEnum {
|
pub enum AnyEnum {
|
||||||
Foo,
|
Foo,
|
||||||
|
|
Loading…
Add table
Reference in a new issue