1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#[cfg(feature = "livemod-derive")]
pub use livemod_derive::LiveMod;
#[cfg_attr(feature = "disabled", allow(dead_code))]
mod enabled;
#[cfg_attr(not(feature = "disabled"), allow(dead_code))]
mod disabled;
#[cfg(not(feature = "disabled"))]
pub use enabled::*;
#[cfg(feature = "disabled")]
pub use disabled::*;
use nanoserde::{DeBin, SerBin};
#[derive(SerBin, DeBin)]
pub struct TrackedData {
pub name: String,
pub data_type: TrackedDataRepr,
}
#[derive(SerBin, DeBin)]
pub enum TrackedDataRepr {
SignedSlider {
storage_min: i64,
storage_max: i64,
suggested_min: i64,
suggested_max: i64,
},
UnsignedSlider {
storage_min: u64,
storage_max: u64,
suggested_min: u64,
suggested_max: u64,
},
SignedInteger {
min: i64,
max: i64,
},
UnsignedInteger {
min: u64,
max: u64,
},
Struct {
name: String,
fields: Vec<TrackedData>,
},
}
#[derive(SerBin, DeBin)]
pub enum TrackedDataValue {
SignedInt(i64),
UnsignedInt(u64),
}
impl TrackedDataValue {
pub fn as_signed_int(&self) -> Option<&i64> {
if let Self::SignedInt(v) = self {
Some(v)
} else {
None
}
}
pub fn as_unsigned_int(&self) -> Option<&u64> {
if let Self::UnsignedInt(v) = self {
Some(v)
} else {
None
}
}
}
pub trait LiveMod {
fn data_type(&self) -> TrackedDataRepr;
fn get_named_value(&mut self, name: &str) -> &mut dyn LiveMod;
fn set_self(&mut self, value: TrackedDataValue);
}
macro_rules! unsigned_primitive_impl {
($($ty:ident),*) => {
$(
impl LiveMod for $ty {
fn data_type(&self) -> TrackedDataRepr {
TrackedDataRepr::UnsignedInteger {
min: $ty::MIN as u64,
max: $ty::MAX as u64,
}
}
fn get_named_value(&mut self, _: &str) -> &mut dyn LiveMod {
unimplemented!()
}
fn set_self(&mut self, value: TrackedDataValue) {
*self = *value.as_unsigned_int().unwrap() as $ty
}
}
)*
}
}
macro_rules! signed_primitive_impl {
($($ty:ident),*) => {
$(
impl LiveMod for $ty {
fn data_type(&self) -> TrackedDataRepr {
TrackedDataRepr::SignedInteger {
min: $ty::MIN as i64,
max: $ty::MAX as i64,
}
}
fn get_named_value(&mut self, _: &str) -> &mut dyn LiveMod {
unimplemented!()
}
fn set_self(&mut self, value: TrackedDataValue) {
*self = *value.as_signed_int().unwrap() as $ty
}
}
)*
}
}
unsigned_primitive_impl!(u8, u16, u32, u64, usize);
signed_primitive_impl!(i8, i16, i32, i64, isize);