ffgl_core/parameters/
info.rs

1#![allow(non_camel_case_types)]
2use std::ffi::CString;
3
4use std::ffi::CStr;
5
6use crate::{conversions::FFGLVal, ffi::ffgl2::*};
7use num_derive::FromPrimitive;
8
9#[repr(u32)]
10#[derive(Default, FromPrimitive, Debug, Clone, Copy)]
11pub enum ParameterTypes {
12    Boolean = FF_TYPE_BOOLEAN,
13    Event = FF_TYPE_EVENT,
14    Red = FF_TYPE_RED,
15    Green = FF_TYPE_GREEN,
16    Blue = FF_TYPE_BLUE,
17    X = FF_TYPE_XPOS,
18    Y = FF_TYPE_YPOS,
19    #[default]
20    Standard = FF_TYPE_STANDARD,
21    Option = FF_TYPE_OPTION,
22    Buffer = FF_TYPE_BUFFER,
23    Integer = FF_TYPE_INTEGER,
24    Hue = FF_TYPE_HUE,
25    Saturation = FF_TYPE_SATURATION,
26    Brightness = FF_TYPE_BRIGHTNESS,
27    Alpha = FF_TYPE_ALPHA,
28}
29
30impl From<ParameterTypes> for FFGLVal {
31    fn from(value: ParameterTypes) -> Self {
32        FFGLVal { num: value as u32 }
33    }
34}
35
36impl ParameterTypes {
37    pub fn default_value(&self) -> f32 {
38        0.0
39    }
40}
41
42#[repr(u32)]
43#[derive(Debug, Clone, Copy)]
44pub enum InputStatus {
45    NotInUse = FF_INPUT_NOTINUSE,
46    InUse = FF_INPUT_INUSE,
47}
48
49#[repr(u32)]
50#[derive(Debug, Clone, Copy)]
51pub enum ParameterUsages {
52    Standard = FF_USAGE_STANDARD,
53    FFT = FF_USAGE_FFT,
54}
55
56#[repr(u64)]
57#[derive(Debug, Clone, Copy)]
58pub enum ParameterEventFlags {
59    Visibility = FF_EVENT_FLAG_VISIBILITY,
60    DisplayName = FF_EVENT_FLAG_DISPLAY_NAME,
61    Value = FF_EVENT_FLAG_VALUE,
62    Elements = FF_EVENT_FLAG_ELEMENTS,
63}
64
65//Param as a trait
66pub trait ParamInfo {
67    fn name(&self) -> &CStr;
68    fn display_name(&self) -> &CStr {
69        self.name()
70    }
71
72    fn usage(&self) -> ParameterUsages {
73        ParameterUsages::Standard
74    }
75
76    fn min(&self) -> f32 {
77        0.0
78    }
79
80    fn max(&self) -> f32 {
81        1.0
82    }
83
84    fn param_type(&self) -> ParameterTypes {
85        ParameterTypes::Standard
86    }
87
88    fn default_val(&self) -> f32 {
89        self.param_type().default_value()
90    }
91
92    fn group(&self) -> &str {
93        ""
94    }
95}
96
97pub trait ParamValue {
98    fn get(&self) -> f32;
99    fn set(&mut self, value: f32);
100}
101
102impl ParamValue for f32 {
103    fn get(&self) -> f32 {
104        *self
105    }
106
107    fn set(&mut self, value: f32) {
108        *self = value;
109    }
110}
111
112#[derive(Default, Debug, Clone)]
113pub struct SimpleParamInfo {
114    pub name: CString,
115    pub param_type: ParameterTypes,
116    pub default: Option<f32>,
117    pub min: Option<f32>,
118    pub max: Option<f32>,
119    pub group: Option<String>,
120}
121
122impl SimpleParamInfo {
123    pub fn from_name(name: &str) -> Self {
124        let name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) };
125
126        SimpleParamInfo {
127            name: name.into(),
128            param_type: ParameterTypes::Standard,
129            ..Default::default()
130        }
131    }
132}
133
134impl ParamInfo for SimpleParamInfo {
135    fn name(&self) -> &CStr {
136        &self.name
137    }
138
139    fn param_type(&self) -> ParameterTypes {
140        self.param_type
141    }
142
143    fn min(&self) -> f32 {
144        self.min.unwrap_or(0.0)
145    }
146
147    fn max(&self) -> f32 {
148        self.max.unwrap_or(1.0)
149    }
150
151    fn default_val(&self) -> f32 {
152        self.default.unwrap_or(self.param_type.default_value())
153    }
154
155    fn group(&self) -> &str {
156        self.group.as_deref().unwrap_or("")
157    }
158}