ffgl_core/handler/
simplified.rs

1//! This module provides a simplified way to implement a plugin
2//! 1. Implement [SimpleFFGLInstance] for your plugin
3//! 2. Call [crate::plugin_main] with a [SimpleFFGLHandler] and your instance type, such as:
4//!     ```rust plugin_main!(SimpleFFGLHandler<MyInstanceType>);```
5
6use super::FFGLHandler;
7
8use crate::parameters::{ParamInfo, SimpleParamInfo};
9
10use crate::{FFGLData, GLInput};
11
12use super::FFGLInstance;
13
14///This is a handler that just delegates to a SimpleFFGLInstance
15pub struct SimpleFFGLHandler<T: SimpleFFGLInstance> {
16    pub(crate) _marker: std::marker::PhantomData<T>,
17}
18
19///Implement this trait for a plugin without any static state
20pub trait SimpleFFGLInstance: FFGLInstance {
21    fn new(inst_data: &FFGLData) -> Self;
22
23    fn num_params() -> usize {
24        0
25    }
26    fn param_info(_index: usize) -> &'static dyn ParamInfo {
27        panic!("No params")
28    }
29
30    fn plugin_info() -> crate::info::PluginInfo;
31
32    fn get_param(&self, _index: usize) -> f32 {
33        panic!("No params")
34    }
35    fn set_param(&mut self, _index: usize, _value: f32) {
36        panic!("No params")
37    }
38
39    ///Called by [crate::conversions::Op::ProcessOpenGL] to draw the plugin
40    fn draw(&mut self, inst_data: &FFGLData, frame_data: GLInput);
41}
42
43impl<T: SimpleFFGLInstance> FFGLInstance for T {
44    fn get_param(&self, index: usize) -> f32 {
45        SimpleFFGLInstance::get_param(self, index)
46    }
47
48    fn set_param(&mut self, index: usize, value: f32) {
49        SimpleFFGLInstance::set_param(self, index, value)
50    }
51
52    fn draw(&mut self, inst_data: &FFGLData, frame_data: GLInput) {
53        SimpleFFGLInstance::draw(self, inst_data, frame_data)
54    }
55}
56
57impl<T: SimpleFFGLInstance> FFGLHandler for SimpleFFGLHandler<T> {
58    type Instance = T;
59    type NewInstanceError = std::convert::Infallible;
60
61    fn init() -> Self {
62        Self {
63            _marker: std::marker::PhantomData,
64        }
65    }
66
67    fn num_params(&self) -> usize {
68        T::num_params()
69    }
70
71    fn param_info(&'static self, index: usize) -> &'static dyn ParamInfo {
72        T::param_info(index)
73    }
74
75    fn plugin_info(&self) -> crate::info::PluginInfo {
76        T::plugin_info()
77    }
78
79    fn new_instance(&self, inst_data: &FFGLData) -> Result<Self::Instance, Self::NewInstanceError> {
80        Ok(T::new(inst_data))
81    }
82}