forked from Anti-Alias/bevy_ui_dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.rs
More file actions
164 lines (153 loc) · 4.8 KB
/
widgets.rs
File metadata and controls
164 lines (153 loc) · 4.8 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
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
use bevy_asset::AssetServer;
use bevy_text::{TextStyle, TextSection};
use bevy_ecs::entity::Entity;
use bevy_ecs::system::Commands;
use bevy_ui::{Size, Val, FlexWrap, Style, JustifyContent, AlignItems};
use bevy_ui::node_bundles::{NodeBundle, TextBundle, ButtonBundle, ImageBundle};
use bevy_hierarchy::BuildChildren;
use super::{Class, AssetClass, UiChildBuilder};
/// Spawns a [`NodeBundle`] as the root with children.
pub fn root(
class: impl Class<NodeBundle>,
assets: &AssetServer,
commands: &mut Commands,
children: impl FnOnce(&mut UiChildBuilder)
) -> Entity {
let mut bundle = NodeBundle::default();
class.apply(&mut bundle);
commands
.spawn(bundle)
.with_children(|builder| {
let mut builder = UiChildBuilder {
builder,
assets
};
children(&mut builder);
})
.id()
}
/// Spawns a [`NodeBundle`] with children.
pub fn node(
class: impl Class<NodeBundle>,
parent: &mut UiChildBuilder,
children: impl FnOnce(&mut UiChildBuilder)
) -> Entity {
let mut bundle = NodeBundle::default();
class.apply(&mut bundle);
parent.spawn(bundle).with_children(children).id()
}
/// Spawns a [`TextBundle`].
pub fn text(
text: &str,
class: impl AssetClass<TextBundle>,
text_class: impl AssetClass<TextStyle>,
parent: &mut UiChildBuilder
) -> Entity {
let mut bundle = TextBundle::default();
class.apply(parent.assets, &mut bundle);
let sections = &mut bundle.text.sections;
let mut style = TextStyle::default();
text_class.apply(parent.assets, &mut style);
sections.push(TextSection {
value: text.to_string(),
style,
});
parent.spawn(bundle).id()
}
/// Spawns a [`ButtonBundle`] with children.
pub fn button(
class: impl AssetClass<ButtonBundle>,
parent: &mut UiChildBuilder,
children: impl FnOnce(&mut UiChildBuilder)
) -> Entity {
let mut bundle = ButtonBundle::default();
class.apply(parent.assets, &mut bundle);
parent
.spawn(bundle)
.with_children(children)
.id()
}
/// Spawns a [`ButtonBundle`] without children.
pub fn simple_button(
class: impl AssetClass<ButtonBundle>,
parent: &mut UiChildBuilder
) -> Entity {
let mut bundle = ButtonBundle::default();
class.apply(parent.assets, &mut bundle);
parent.spawn(bundle).id()
}
/// Spawns a [`ButtonBundle`] with a single [`TextBundle`] as its child.
pub fn text_button(
txt: &str,
class: impl AssetClass<ButtonBundle>,
text_style: impl AssetClass<TextStyle>,
parent: &mut UiChildBuilder
) -> Entity {
fn c_text(_a: &AssetServer,_b: &mut TextBundle) {} // No need to overwrite the default!
button(class, parent, |p| {
text(txt, c_text, text_style, p);
})
}
/// Spawns an [`ImageBundle`].
pub fn image(
class: impl AssetClass<ImageBundle>,
parent: &mut UiChildBuilder
) -> Entity {
let mut bundle = ImageBundle::default();
class.apply(parent.assets, &mut bundle);
parent.spawn(bundle).id()
}
/// Spawns an [`ImageBundle`] with children.
pub fn image_pane(
class: impl AssetClass<ImageBundle>,
parent: &mut UiChildBuilder,
children: impl FnOnce(&mut UiChildBuilder)
) -> Entity {
let mut bundle = ImageBundle::default();
class.apply(parent.assets, &mut bundle);
parent
.spawn(bundle)
.with_children(children).id()
}
/// Spawns a [`NodeBundle`] composed of [`NodeBundle`] cells in the form of a grid.
/// The callback function argument spawns the contents of those cells.
pub fn grid(
rows: usize,
columns: usize,
class: impl Class<NodeBundle>,
parent: &mut UiChildBuilder,
mut children: impl FnMut(&mut UiChildBuilder, usize, usize)
) -> Entity {
// Spawns container
let mut container_bundle = NodeBundle::default();
class.apply(&mut container_bundle);
container_bundle.style.flex_wrap = FlexWrap::Wrap;
let mut container = parent.spawn(container_bundle);
// Spawns cells as children of the container
let mut cell_bundle = NodeBundle {
style: Style {
size: Size::new(
Val::Percent(100.0 / rows as f32),
Val::Percent(100.0 / columns as f32)
),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..Default::default()
},
..Default::default()
};
cell_bundle.style.size = Size::new(
Val::Percent(100.0 / columns as f32),
Val::Percent(100.0 / rows as f32)
);
for row in 0..rows {
for col in 0..columns {
container = container.with_children(|container| {
container
.spawn(cell_bundle.clone())
.with_children(|cell| children(cell, row, col));
});
}
}
container.id()
}