1use core::any::Any;
2
3pub use glam::{Vec2, Vec3};
4
5use crate::{diff::ParamPath, dsp::volume::Volume, node::NodeID};
6
7pub struct NodeEvent {
9 pub node_id: NodeID,
11 pub event: NodeEventType,
13}
14
15#[non_exhaustive]
17pub enum NodeEventType {
18 Param {
19 data: ParamData,
21 path: ParamPath,
23 },
24 Custom(Box<dyn Any + Send + Sync>),
26 CustomBytes([u8; 16]),
28}
29
30#[non_exhaustive]
35pub enum ParamData {
36 Volume(Volume),
37 F32(f32),
38 F64(f64),
39 I32(i32),
40 U32(u32),
41 U64(u64),
42 Bool(bool),
43 Vector2D(Vec2),
44 Vector3D(Vec3),
45 Any(Box<Box<dyn Any + Send + Sync>>),
46}
47
48impl ParamData {
49 pub fn any<T: Any + Send + Sync>(value: T) -> Self {
51 Self::Any(Box::new(Box::new(value)))
52 }
53
54 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
59 match self {
60 Self::Any(any) => any.downcast_ref(),
61 _ => None,
62 }
63 }
64}
65
66macro_rules! param_data_from {
67 ($ty:ty, $variant:ident) => {
68 impl From<$ty> for ParamData {
69 fn from(value: $ty) -> Self {
70 Self::$variant(value)
71 }
72 }
73
74 impl TryInto<$ty> for &ParamData {
75 type Error = crate::diff::PatchError;
76
77 fn try_into(self) -> Result<$ty, crate::diff::PatchError> {
78 match self {
79 ParamData::$variant(value) => Ok(*value),
80 _ => Err(crate::diff::PatchError::InvalidData),
81 }
82 }
83 }
84 };
85}
86
87param_data_from!(Volume, Volume);
88param_data_from!(f32, F32);
89param_data_from!(f64, F64);
90param_data_from!(i32, I32);
91param_data_from!(u32, U32);
92param_data_from!(u64, U64);
93param_data_from!(bool, Bool);
94param_data_from!(Vec2, Vector2D);
95param_data_from!(Vec3, Vector3D);
96
97pub struct NodeEventList<'a> {
99 event_buffer: &'a mut [NodeEvent],
100 indices: &'a [u32],
101}
102
103impl<'a> NodeEventList<'a> {
104 pub fn new(event_buffer: &'a mut [NodeEvent], indices: &'a [u32]) -> Self {
105 Self {
106 event_buffer,
107 indices,
108 }
109 }
110
111 pub fn num_events(&self) -> usize {
112 self.indices.len()
113 }
114
115 pub fn get_event(&mut self, index: usize) -> Option<&mut NodeEventType> {
116 self.indices
117 .get(index)
118 .map(|idx| &mut self.event_buffer[*idx as usize].event)
119 }
120
121 pub fn for_each(&mut self, mut f: impl FnMut(&mut NodeEventType)) {
122 for &idx in self.indices {
123 if let Some(event) = self.event_buffer.get_mut(idx as usize) {
124 (f)(&mut event.event);
125 }
126 }
127 }
128
129 pub fn for_each_patch<T: crate::diff::Patch>(&mut self, mut f: impl FnMut(T::Patch)) {
154 for &idx in self.indices {
155 if let Some(patch) = self
156 .event_buffer
157 .get_mut(idx as usize)
158 .and_then(|e| T::patch_event(&e.event))
159 {
160 (f)(patch);
161 }
162 }
163 }
164}