-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
transparency_ui.rs
80 lines (74 loc) · 2.78 KB
/
transparency_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
//! Demonstrates how to use transparency with UI.
//! Shows two colored buttons with transparent text.
use bevy::prelude::*;
fn main() {
App::new()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2d);
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
commands
.spawn(Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::SpaceAround,
..default()
})
.with_children(|parent| {
parent
.spawn((
Button,
Node {
width: Val::Px(150.0),
height: Val::Px(65.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
BackgroundColor(Color::srgb(0.1, 0.5, 0.1)),
))
.with_children(|parent| {
parent.spawn((
Text::new("Button 1"),
TextFont {
font: font_handle.clone(),
font_size: 33.0,
..default()
},
// Alpha channel of the color controls transparency.
TextColor(Color::srgba(1.0, 1.0, 1.0, 0.2)),
));
});
// Button with a different color,
// to demonstrate the text looks different due to its transparency.
parent
.spawn((
Button,
Node {
width: Val::Px(150.0),
height: Val::Px(65.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
BackgroundColor(Color::srgb(0.5, 0.1, 0.5)),
))
.with_children(|parent| {
parent.spawn((
Text::new("Button 2"),
TextFont {
font: font_handle.clone(),
font_size: 33.0,
..default()
},
// Alpha channel of the color controls transparency.
TextColor(Color::srgba(1.0, 1.0, 1.0, 0.2)),
));
});
});
}