forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flex_layout.rs
185 lines (179 loc) · 6.57 KB
/
flex_layout.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
//! Demonstrates how the `AlignItems` and `JustifyContent` properties can be composed to layout text.
use bevy::prelude::*;
const ALIGN_ITEMS_COLOR: Color = Color::srgb(1., 0.066, 0.349);
const JUSTIFY_CONTENT_COLOR: Color = Color::srgb(0.102, 0.522, 1.);
const MARGIN: Val = Val::Px(12.);
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Bevy Flex Layout Example".to_string(),
..Default::default()
}),
..Default::default()
}))
.add_systems(Startup, spawn_layout)
.run();
}
fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
commands.spawn(Camera2dBundle::default());
commands
.spawn(NodeBundle {
style: Style {
// fill the entire window
width: Val::Percent(100.),
height: Val::Percent(100.),
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
padding: UiRect::all(MARGIN),
row_gap: MARGIN,
..Default::default()
},
background_color: BackgroundColor(Color::BLACK),
..Default::default()
})
.with_children(|builder| {
// spawn the key
builder
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Row,
..Default::default()
},
..Default::default()
})
.with_children(|builder| {
spawn_nested_text_bundle(
builder,
font.clone(),
ALIGN_ITEMS_COLOR,
UiRect::right(MARGIN),
"AlignItems",
);
spawn_nested_text_bundle(
builder,
font.clone(),
JUSTIFY_CONTENT_COLOR,
UiRect::default(),
"JustifyContent",
);
});
builder
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.),
height: Val::Percent(100.),
flex_direction: FlexDirection::Column,
row_gap: MARGIN,
..Default::default()
},
..Default::default()
})
.with_children(|builder| {
// spawn one child node for each combination of `AlignItems` and `JustifyContent`
let justifications = [
JustifyContent::FlexStart,
JustifyContent::Center,
JustifyContent::FlexEnd,
JustifyContent::SpaceEvenly,
JustifyContent::SpaceAround,
JustifyContent::SpaceBetween,
];
let alignments = [
AlignItems::Baseline,
AlignItems::FlexStart,
AlignItems::Center,
AlignItems::FlexEnd,
AlignItems::Stretch,
];
for align_items in alignments {
builder
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.),
height: Val::Percent(100.),
flex_direction: FlexDirection::Row,
column_gap: MARGIN,
..Default::default()
},
..Default::default()
})
.with_children(|builder| {
for justify_content in justifications {
spawn_child_node(
builder,
font.clone(),
align_items,
justify_content,
);
}
});
}
});
});
}
fn spawn_child_node(
builder: &mut ChildBuilder,
font: Handle<Font>,
align_items: AlignItems,
justify_content: JustifyContent,
) {
builder
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
align_items,
justify_content,
width: Val::Percent(100.),
height: Val::Percent(100.),
..Default::default()
},
background_color: BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
..Default::default()
})
.with_children(|builder| {
let labels = [
(format!("{align_items:?}"), ALIGN_ITEMS_COLOR, 0.),
(format!("{justify_content:?}"), JUSTIFY_CONTENT_COLOR, 3.),
];
for (text, color, top_margin) in labels {
// We nest the text within a parent node because margins and padding can't be directly applied to text nodes currently.
spawn_nested_text_bundle(
builder,
font.clone(),
color,
UiRect::top(Val::Px(top_margin)),
&text,
);
}
});
}
fn spawn_nested_text_bundle(
builder: &mut ChildBuilder,
font: Handle<Font>,
background_color: Color,
margin: UiRect,
text: &str,
) {
builder
.spawn(NodeBundle {
style: Style {
margin,
padding: UiRect::axes(Val::Px(5.), Val::Px(1.)),
..Default::default()
},
background_color: BackgroundColor(background_color),
..Default::default()
})
.with_children(|builder| {
builder.spawn(TextBundle::from_section(
text,
TextStyle {
font,
color: Color::BLACK,
..default()
},
));
});
}