ffgl_core/parameters/
handler.rs

1use std::path::Iter;
2
3use super::{info::ParamInfo, info::ParamValue, info::SimpleParamInfo};
4
5///Handle the info for a collection of parameters. Allows for nested parameters.
6pub trait ParamInfoHandler {
7    fn num_params(&self) -> usize;
8    fn param_info(&self, index: usize) -> &dyn ParamInfo;
9}
10///Handle collection of parameter values. Allows for nested parameters.
11///
12/// Maybe theres some way to just use a Index and IndexMut implementation for this instead?
13pub trait ParamValueHandler {
14    fn get_param(&self, index: usize) -> f32;
15    fn set_param(&mut self, index: usize, value: f32);
16}
17
18impl<T> ParamValueHandler for [T]
19where
20    T: ParamValueHandler + ParamInfoHandler,
21{
22    fn get_param(&self, index: usize) -> f32 {
23        let mut index = index;
24        for p in self.iter() {
25            if index < p.num_params() {
26                return p.get_param(index);
27            }
28            index -= p.num_params();
29        }
30        panic!("Index out of bounds");
31    }
32
33    fn set_param(&mut self, index: usize, value: f32) {
34        let mut index = index;
35        for p in self.iter_mut() {
36            if index < p.num_params() {
37                p.set_param(index, value);
38                return;
39            }
40            index -= p.num_params();
41        }
42        panic!("Index out of bounds");
43    }
44}
45
46impl<T> ParamInfoHandler for [T]
47where
48    T: ParamInfoHandler,
49{
50    fn num_params(&self) -> usize {
51        self.iter().map(|p| p.num_params()).sum()
52    }
53
54    fn param_info(&self, index: usize) -> &dyn ParamInfo {
55        let mut index = index;
56        for p in self.iter() {
57            if index < p.num_params() {
58                return p.param_info(index);
59            }
60            index -= p.num_params();
61        }
62        panic!("Index out of bounds");
63    }
64}
65
66impl<T: ParamInfo> ParamInfoHandler for T {
67    fn num_params(&self) -> usize {
68        1
69    }
70
71    fn param_info(&self, index: usize) -> &dyn ParamInfo {
72        self
73    }
74}
75
76impl<T: ParamValue> ParamValueHandler for T {
77    fn get_param(&self, index: usize) -> f32 {
78        self.get()
79    }
80
81    fn set_param(&mut self, index: usize, value: f32) {
82        self.set(value)
83    }
84}