tirbofish/dropbear
main / crates / eucalyptus-core / src / ui.rs · 2578 bytes
crates/eucalyptus-core/src/ui.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
use crate::component::{
Component, ComponentDescriptor, ComponentInitFuture, DisabilityFlags, InspectableComponent,
SerializedComponent,
};
use crate::physics::PhysicsState;
use dropbear_engine::graphics::SharedGraphicsContext;
use egui::{CollapsingHeader, Ui};
use hecs::{Entity, World};
use std::sync::Arc;
#[derive(Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct HUDComponent {
pub ui_tree: kino_ui::WidgetTree,
}
impl HUDComponent {
pub fn tree(&self) -> &kino_ui::WidgetTree {
&self.ui_tree
}
pub fn tree_mut(&mut self) -> &mut kino_ui::WidgetTree {
&mut self.ui_tree
}
}
#[typetag::serde]
impl SerializedComponent for HUDComponent {}
impl Component for HUDComponent {
type SerializedForm = Self;
type RequiredComponentTypes = (Self,);
fn descriptor() -> ComponentDescriptor {
ComponentDescriptor {
disabled_flags: DisabilityFlags::Disabled,
internal: false,
fqtn: "eucalyptus_core::ui::HUDComponent".to_string(),
type_name: "HUD".to_string(),
category: Some("UI".to_string()),
description: Some(
"Renders a camera-facing textured quad, typically used for a HUD or 2D context"
.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 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 HUDComponent {
fn inspect(
&mut self,
_world: &World,
entity: Entity,
ui: &mut Ui,
_graphics: Arc<SharedGraphicsContext>,
) {
CollapsingHeader::new("HUD")
.default_open(true)
.id_salt(format!("HUD {}", entity.to_bits()))
.show(ui, |ui| {
if ui.button("Edit in UI Editor").clicked() {
ui.ctx().data_mut(|d| {
d.insert_temp::<Option<Entity>>(
egui::Id::new("open_ui_editor"),
Some(entity),
);
});
}
});
}
}