tirbofish/dropbear
main / crates / eucalyptus-core / src / properties.rs · 14371 bytes
crates/eucalyptus-core/src/properties.rs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use crate::component::{
Component, ComponentDescriptor, ComponentInitFuture, DisabilityFlags, InspectableComponent,
SerializedComponent,
};
use crate::states::Property;
use crate::warn;
use dropbear_engine::graphics::SharedGraphicsContext;
use egui::{CollapsingHeader, ComboBox, DragValue, Grid, RichText, TextEdit, Ui};
use hecs::{Entity, World};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::{Display, Formatter};
use std::sync::Arc;
/// Properties for an entity, typically queries with `entity.getProperty<Float>` and `entity.setProperty(21)`
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct CustomProperties {
pub custom_properties: Vec<Property>,
pub next_id: u64,
}
#[typetag::serde]
impl SerializedComponent for CustomProperties {}
impl Component for CustomProperties {
type SerializedForm = Self;
type RequiredComponentTypes = (Self,);
fn descriptor() -> ComponentDescriptor {
ComponentDescriptor {
disabled_flags: DisabilityFlags::Disabled,
internal: false,
fqtn: "eucalyptus_core::properties::CustomProperties".to_string(),
type_name: "CustomProperties".to_string(),
category: Some("Properties".to_string()),
description: Some("Custom properties for an entity".to_string()),
}
}
fn init(
ser: &Self::SerializedForm,
_graphics: Arc<SharedGraphicsContext>,
) -> ComponentInitFuture<'_, Self> {
Box::pin(async move { Ok((ser.clone(),)) })
}
fn update_component(
&mut self,
_world: &World,
_physics: &mut crate::physics::PhysicsState,
_entity: Entity,
_dt: f32,
_graphics: Arc<SharedGraphicsContext>,
) {
}
fn save(&self, _world: &World, _entity: Entity) -> Box<dyn SerializedComponent> {
Box::new(self.clone())
}
}
impl InspectableComponent for CustomProperties {
fn inspect(
&mut self,
_world: &World,
entity: Entity,
ui: &mut Ui,
_graphics: Arc<SharedGraphicsContext>,
) {
CollapsingHeader::new("Custom Properties")
.default_open(true)
.id_salt(format!("Custom Properties {}", entity.to_bits()))
.show(ui, |ui| {
ui.vertical(|ui| {
Grid::new("properties").striped(true).show(ui, |ui| {
ui.label(RichText::new("Key"));
ui.label(RichText::new("Type"));
ui.label(RichText::new("Value"));
ui.label(RichText::new("Action"));
ui.end_row();
let mut to_delete: Option<u64> = None;
let mut to_rename: Option<(u64, String)> = None;
for (_i, property) in self.custom_properties.iter_mut().enumerate() {
let mut edited_key = property.key.clone();
ui.add_sized([100.0, 20.0], TextEdit::singleline(&mut edited_key));
if edited_key != property.key {
to_rename = Some((property.id, edited_key));
}
let current_type = ValueType::from(&mut property.value);
let mut selected_type = current_type;
ComboBox::from_id_salt(format!("type_{}", property.id))
.selected_text(format!("{:?}", selected_type))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut selected_type,
ValueType::String,
"String",
);
ui.selectable_value(
&mut selected_type,
ValueType::Float,
"Float",
);
ui.selectable_value(&mut selected_type, ValueType::Int, "Int");
ui.selectable_value(
&mut selected_type,
ValueType::Bool,
"Bool",
);
ui.selectable_value(
&mut selected_type,
ValueType::Vec3,
"Vec3",
);
});
if selected_type != current_type {
property.value = match selected_type {
ValueType::String => Value::String(String::new()),
ValueType::Float => Value::Double(0.0),
ValueType::Int => Value::Int(0),
ValueType::Bool => Value::Bool(false),
ValueType::Vec3 => Value::Vec3([0.0, 0.0, 0.0]),
};
}
let speed = {
let input = ui.input(|i| i.modifiers);
if input.shift {
0.01
} else if cfg!(target_os = "macos") && input.mac_cmd
|| !cfg!(target_os = "macos") && input.ctrl
{
1.0
} else {
0.1
}
};
match &mut property.value {
Value::String(s) => {
ui.add_sized([100.0, 20.0], egui::TextEdit::singleline(s));
}
Value::Int(n) => {
ui.add(DragValue::new(n).speed(1.0));
}
Value::Double(f) => {
ui.add(DragValue::new(f).speed(speed));
}
Value::Bool(b) => {
if ui.button(if *b { "✅" } else { "❌" }).clicked() {
*b = !*b;
}
}
Value::Vec3(v) => {
ui.horizontal(|ui| {
ui.add(DragValue::new(&mut v[0]).speed(speed));
ui.add(DragValue::new(&mut v[1]).speed(speed));
ui.add(DragValue::new(&mut v[2]).speed(speed));
});
}
}
if ui.button("🗑️").clicked() {
log::debug!("Trashing {}", property.key);
to_delete = Some(property.id);
}
ui.end_row();
}
if let Some(id) = to_delete {
self.custom_properties.retain(|p| p.id != id);
}
if let Some((id, new_key)) = to_rename {
if let Some(property) =
self.custom_properties.iter_mut().find(|p| p.id == id)
{
property.key = new_key;
} else {
warn!("Failed to rename property: id not found");
}
}
if ui.button("Add").clicked() {
log::debug!("Inserting new default value");
let mut new_key = String::from("new_property");
let mut counter = 1;
while self.custom_properties.iter().any(|p| p.key == new_key) {
new_key = format!("new_property_{}", counter);
counter += 1;
}
self.custom_properties.push(Property {
id: self.next_id,
key: new_key,
value: Value::default(),
});
self.next_id += 1;
}
});
});
});
}
}
impl CustomProperties {
/// Creates a new [CustomProperties]
pub fn new() -> Self {
Self {
custom_properties: Vec::new(),
next_id: 0,
}
}
/// Sets the property based on the [Value] (type) and its key.
///
/// If the value does NOT exist, it will be created.
/// If the value does exist, it will replace the contents of that item.
pub fn set_property(&mut self, key: String, value: Value) {
if let Some(prop) = self.custom_properties.iter_mut().find(|p| p.key == key) {
prop.value = value;
} else {
self.custom_properties.push(Property {
id: self.next_id,
key,
value,
});
self.next_id += 1;
}
}
/// Fetches the property by its key.
pub fn get_property(&self, key: &str) -> Option<&Value> {
self.custom_properties
.iter()
.find(|p| p.key == key)
.map(|p| &p.value)
}
/// Fetches the float property
pub fn get_float(&self, key: &str) -> Option<f64> {
match self.get_property(key)? {
Value::Double(f) => Some(*f),
_ => None,
}
}
/// Fetches the integer property
pub fn get_int(&self, key: &str) -> Option<i64> {
match self.get_property(key)? {
Value::Int(i) => Some(*i),
_ => None,
}
}
/// Creates a new property based on a key and a value.
///
/// It will push that value again to the property vector.
pub fn add_property(&mut self, key: String, value: Value) {
self.custom_properties.push(Property {
id: self.next_id,
key,
value,
});
self.next_id += 1;
}
/// Shows a template of the different values when inspected as a component in the editor.
pub fn show_value_editor(ui: &mut Ui, value: &mut Value) -> bool {
match value {
Value::String(s) => ui.text_edit_singleline(s).changed(),
Value::Int(i) => ui
.add(egui::Slider::new(i, -1000..=1000).text(""))
.changed(),
Value::Double(f) => ui
.add(egui::Slider::new(f, -100.0..=100.0).text(""))
.changed(),
Value::Bool(b) => ui.checkbox(b, "").changed(),
Value::Vec3(vec) => {
let mut changed = false;
ui.horizontal(|ui| {
changed |= ui
.add(
egui::Slider::new(&mut vec[0], -10.0..=10.0)
.text("X")
.fixed_decimals(2),
)
.changed();
changed |= ui
.add(
egui::Slider::new(&mut vec[1], -10.0..=10.0)
.text("Y")
.fixed_decimals(2),
)
.changed();
changed |= ui
.add(
egui::Slider::new(&mut vec[2], -10.0..=10.0)
.text("Z")
.fixed_decimals(2),
)
.changed();
});
changed
}
}
}
}
impl Default for CustomProperties {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Value {
String(String),
Int(i64),
Double(f64),
Bool(bool),
Vec3([f64; 3]),
}
impl Default for Value {
fn default() -> Self {
Self::String(String::new())
}
}
impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let string: String = match self {
Value::String(_) => "String".into(),
Value::Int(_) => "Int".into(),
Value::Double(_) => "Double".into(),
Value::Bool(_) => "Bool".into(),
Value::Vec3(_) => "Vec3".into(),
};
write!(f, "{}", string)
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ValueType {
String,
Float,
Int,
Bool,
Vec3,
}
impl From<Value> for ValueType {
fn from(value: Value) -> Self {
match value {
Value::String(_) => ValueType::String,
Value::Int(_) => ValueType::Int,
Value::Double(_) => ValueType::Float,
Value::Bool(_) => ValueType::Bool,
Value::Vec3(_) => ValueType::Vec3,
}
}
}
impl From<&mut Value> for ValueType {
fn from(value: &mut Value) -> Self {
match value {
Value::String(_) => ValueType::String,
Value::Int(_) => ValueType::Int,
Value::Double(_) => ValueType::Float,
Value::Bool(_) => ValueType::Bool,
Value::Vec3(_) => ValueType::Vec3,
}
}
}
pub mod shared {
use crate::properties::CustomProperties;
use hecs::World;
pub fn custom_properties_exists_for_entity(world: &World, entity: hecs::Entity) -> bool {
world.get::<&CustomProperties>(entity).is_ok()
}
}