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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![warn(missing_docs)]
use fyrox_core_derive::impl_inspect;
use std::{
any::{Any, TypeId},
fmt::{self, Debug},
};
pub mod prelude {
pub use super::{Inspect, PropertyInfo};
}
pub trait PropertyValue: Any + Debug {
fn as_any(&self) -> &dyn Any;
}
impl<T: Debug + 'static> PropertyValue for T {
fn as_any(&self) -> &dyn Any {
self
}
}
#[derive(Debug)]
pub enum CastError {
TypeMismatch {
property_name: String,
expected_type_id: TypeId,
actual_type_id: TypeId,
},
}
pub struct PropertyInfo<'a> {
pub owner_type_id: TypeId,
pub name: &'a str,
pub display_name: &'static str,
pub value: &'a dyn PropertyValue,
pub read_only: bool,
pub min_value: Option<f64>,
pub max_value: Option<f64>,
pub step: Option<f64>,
pub precision: Option<usize>,
pub description: String,
pub is_modified: bool,
}
impl<'a> PartialEq<Self> for PropertyInfo<'a> {
fn eq(&self, other: &Self) -> bool {
let value_ptr_a = &*self.value as *const _ as *const ();
let value_ptr_b = &*other.value as *const _ as *const ();
self.owner_type_id == other.owner_type_id
&& self.name == other.name
&& self.display_name == other.display_name
&& std::ptr::eq(value_ptr_a, value_ptr_b)
&& self.read_only == other.read_only
&& self.min_value == other.min_value
&& self.max_value == other.max_value
&& self.step == other.step
&& self.precision == other.precision
&& self.description == other.description
}
}
impl<'a> fmt::Debug for PropertyInfo<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PropertyInfo")
.field("owner_type_id", &self.owner_type_id)
.field("name", &self.name)
.field("display_name", &self.display_name)
.field("value", &format_args!("{:?}", self.value as *const _))
.field("read_only", &self.read_only)
.field("min_value", &self.min_value)
.field("max_value", &self.max_value)
.field("step", &self.step)
.field("precision", &self.precision)
.field("description", &self.description)
.finish()
}
}
impl<'a> PropertyInfo<'a> {
pub fn cast_value<T: 'static>(&self) -> Result<&T, CastError> {
match self.value.as_any().downcast_ref::<T>() {
Some(value) => Ok(value),
None => Err(CastError::TypeMismatch {
property_name: self.name.to_string(),
expected_type_id: TypeId::of::<T>(),
actual_type_id: self.value.type_id(),
}),
}
}
}
pub trait Inspect {
fn properties(&self) -> Vec<PropertyInfo<'_>>;
}
impl_inspect! {
pub enum Option<T: Inspect + Debug + 'static> {
Some(T),
None
}
}
impl_inspect! {
pub struct Box<T: Inspect + Debug + 'static>;
}
macro_rules! impl_self_inspect {
($ty:ty, $min:expr, $max:expr, $step:expr, $precision:expr) => {
impl Inspect for $ty {
fn properties(&self) -> Vec<PropertyInfo<'_>> {
vec![PropertyInfo {
owner_type_id: TypeId::of::<Self>(),
name: "self",
display_name: "self",
value: self,
read_only: false,
min_value: Some($min),
max_value: Some($max),
step: Some($step),
precision: Some($precision),
description: "".to_string(),
is_modified: false,
}]
}
}
};
}
impl_self_inspect!(f32, f32::MIN as f64, f32::MAX as f64, 1.0, 7);
impl_self_inspect!(f64, f64::MIN, f64::MAX, 1.0, 15);
impl_self_inspect!(i64, i64::MIN as f64, i64::MAX as f64, 1.0, 0);
impl_self_inspect!(u64, u64::MIN as f64, u64::MAX as f64, 1.0, 0);
impl_self_inspect!(i32, i32::MIN as f64, i32::MAX as f64, 1.0, 0);
impl_self_inspect!(u32, u32::MIN as f64, u32::MAX as f64, 1.0, 0);
impl_self_inspect!(i16, i16::MIN as f64, i16::MAX as f64, 1.0, 0);
impl_self_inspect!(u16, u16::MIN as f64, u16::MAX as f64, 1.0, 0);
impl_self_inspect!(i8, i8::MIN as f64, i8::MAX as f64, 1.0, 0);
impl_self_inspect!(u8, u8::MIN as f64, u8::MAX as f64, 1.0, 0);
pub use fyrox_core_derive::Inspect;