Repository navigation
Expand file tree
/
Copy pathghost_nodes.rs
More file actions
126 lines (117 loc) · 3.51 KB
/
Copy pathghost_nodes.rs
File metadata and controls
126 lines (117 loc) · 3.51 KB
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
//! This example demonstrates the use of Ghost Nodes.
//!
//! UI layout replaces ghost nodes with their children.
use bevy::{
prelude::*,
text::FontSourceTemplate,
ui_widgets::{Activate, Button},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, scene.spawn())
.run();
}
#[derive(Component, Clone, Default)]
struct Counter(i32);
fn scene() -> impl SceneList {
bsn_list! {
Camera2d
--
@ghost_root()
--
@normal_root()
}
}
/// Ghost UI root
fn ghost_root() -> impl Scene {
bsn! {
GhostNode
Children [
Node
Children [ @label("This text node is rendered under a ghost root") ]
]
}
}
/// Normal UI root
fn normal_root() -> impl Scene {
bsn! {
Node {
width: percent(100),
height: percent(100),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
}
Children [
Node Counter(0)
Children [
// Ghost children using a separate counter state.
// These buttons are being treated as children of the layout parent
// in the context of UI, but they share the ghost node's counter.
GhostNode Counter(0)
Children [ @button() -- @button() ]
--
// A normal child using the layout parent counter
@button()
]
]
}
}
fn button() -> impl Scene {
bsn! {
Button
// Bump the counter belonging to this button's parent, then refresh every
// label under that parent. Buttons sharing a parent share a counter, so
// pressing either ghost child updates both of their labels.
on(|activate: On<Activate>,
child_of_query: Query<&ChildOf>,
children_query: Query<&Children>,
mut counter_query: Query<&mut Counter>,
mut text_query: Query<&mut Text>| {
let Ok(parent) = child_of_query.get(activate.entity).map(ChildOf::parent) else {
return;
};
let Ok(mut counter) = counter_query.get_mut(parent) else {
return;
};
counter.0 += 1;
let value = counter.0.to_string();
let Ok(siblings) = children_query.get(parent) else {
return;
};
for &sibling in siblings {
let Ok(labels) = children_query.get(sibling) else {
continue;
};
for &label in labels {
if let Ok(mut text) = text_query.get_mut(label) {
**text = value.clone();
}
}
}
})
Node {
width: px(150),
height: px(65),
border: px(5),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
border_radius: BorderRadius::MAX,
}
BorderColor::from(Color::BLACK)
BackgroundColor(Color::srgb(0.15, 0.15, 0.15))
Children [ @label("0") ]
}
}
fn label(text: &str) -> impl Scene {
bsn! {
Text(text)
TextFont {
font: FontSourceTemplate::Handle("fonts/FiraSans-Bold.ttf"),
font_size: px(33.0),
}
TextColor(Color::srgb(0.9, 0.9, 0.9))
}
}