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
use bevy::ecs::component::Component;
use bevy::ui::{
BackgroundColor, BorderColor, BorderRadius, FlexDirection, Node, UiRect, Val,
widget::{Text, TextShadow},
};
use bevy::{color::Color, text::TextColor, utils::default};
use bevy_immediate::{
Imm,
attach::{BevyImmediateAttachPlugin, ImmediateAttach},
ui::CapsUi,
};
pub struct HelloWorldPlugin;
impl bevy::app::Plugin for HelloWorldPlugin {
fn build(&self, app: &mut bevy::app::App) {
// Add bevy immediate plugin with UI support which will construct UI
// rooted at entity with `HelloWorldRoot` component
app.add_plugins(BevyImmediateAttachPlugin::<CapsUi, HelloWorldRoot>::new());
}
}
#[derive(Component)]
pub struct HelloWorldRoot;
impl ImmediateAttach<CapsUi> for HelloWorldRoot {
type Params = (); // Access data from World using SystemParam
fn construct(ui: &mut Imm<CapsUi>, _: &mut ()) {
// Construct entity hierarchies
// and attach necessary components
ui.ch()
.on_spawn_insert(|| {
(
Node {
flex_direction: FlexDirection::Column,
border: UiRect::all(Val::Px(10.)),
padding: UiRect::all(Val::Px(10.)),
column_gap: Val::Px(10.),
row_gap: Val::Px(10.),
border_radius: BorderRadius::all(Val::Px(5.)),
..default()
},
BorderColor::all(Color::srgb(1., 0., 0.)),
BackgroundColor(Color::srgb(0.05, 0.05, 0.05)),
)
})
.add(|ui| {
ui.ch().on_spawn_insert(|| {
(
TextColor(Color::srgb(0.9, 0.9, 0.9)),
TextShadow::default(),
Text("Hello world".into()),
)
});
});
}
}
// To spawn UI rooted at HelloWorldRoot, you need to add it to world
//
// 1. Using classical approach
// ```
// fn commands(mut commmand: Commands) {
// commands.spawn((Node::DEFAULT, HelloWorldRoot));
// }
// ```
// Remember to spawn necessary camera for UI too if not done already.
//
//
// 2. Spawn ui ussing immediate mode systems
//
// See "./plain_ui.rs" example