-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttons.rs
238 lines (219 loc) · 8.14 KB
/
buttons.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! This example demonstrates using the plugin with interactive UI elements, such as buttons.
use bevy::{app::AppExit, prelude::*};
use bevy_round_ui::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, BevyRoundUiDefaultPlugins))
.init_resource::<ButtonStyle>()
.add_systems(Startup, setup)
.add_systems(Update, (handle_button_interactions, handle_button_actions))
.run();
}
const PANEL_BACKGROUND_COLOR: &str = "#5cb3af";
const PANEL_BORDER_COLOR: &str = "#ffffff";
const PANEL_WIDTH: f32 = 300.0;
const BUTTON_HEIGHT: f32 = 40.0;
const BUTTON_OFFSET_SIZE: f32 = 5.0;
/// Resource containing styles for different button states.
#[derive(Resource)]
pub struct ButtonStyle {
pub default_material: Handle<RoundRectUiMaterial>,
pub default_padding: UiRect,
pub hover_material: Handle<RoundRectUiMaterial>,
pub hover_padding: UiRect,
pub press_material: Handle<RoundRectUiMaterial>,
pub press_padding: UiRect,
}
impl FromWorld for ButtonStyle {
fn from_world(world: &mut World) -> Self {
let mut materials = world
.get_resource_mut::<Assets<RoundRectUiMaterial>>()
.expect("Failed to get Assets<RoundRectMaterial>");
let border_radius = RoundUiBorder::all(15.);
Self {
default_material: materials.add(RoundRectUiMaterial {
background_color: Srgba::hex("#F76161").unwrap().into(),
border_color: Srgba::hex("#A53A3D").unwrap().into(),
border_radius: border_radius.into(),
offset: RoundUiOffset::bottom(BUTTON_OFFSET_SIZE).into(),
}),
default_padding: UiRect::bottom(Val::Px(BUTTON_OFFSET_SIZE)),
hover_material: materials.add(RoundRectUiMaterial {
background_color: Srgba::hex("#F61A39").unwrap().into(),
border_color: Srgba::hex("#A0102A").unwrap().into(),
border_radius: border_radius.into(),
offset: RoundUiOffset::bottom(BUTTON_OFFSET_SIZE).into(),
}),
hover_padding: UiRect::bottom(Val::Px(BUTTON_OFFSET_SIZE)),
press_material: materials.add(RoundRectUiMaterial {
background_color: Srgba::hex("#A0102A").unwrap().into(),
border_color: LinearRgba::NONE,
border_radius: border_radius.into(),
offset: RoundUiOffset::top(BUTTON_OFFSET_SIZE).into(),
}),
press_padding: UiRect::top(Val::Px(BUTTON_OFFSET_SIZE)),
}
}
}
/// Component defining button actions for handling click events
#[derive(Component, Debug)]
enum ButtonAction {
Play,
Settings,
Quit,
}
/// Component identifying round buttons.
#[derive(Component)]
pub struct RoundButton;
/// System that initializes the example.
fn setup(
mut commands: Commands,
button_style: Res<ButtonStyle>,
mut superellipse_materials: ResMut<Assets<SuperellipseUiMaterial>>,
) {
// Camera so we can see UI
commands.spawn(Camera2dBundle::default());
// Define a material for the panel.
// This material looks like it has a border, because we applied an equal offset to all sides.
let panel_material = superellipse_materials.add(SuperellipseUiMaterial {
background_color: Srgba::hex(PANEL_BACKGROUND_COLOR).unwrap().into(),
border_color: Srgba::hex(PANEL_BORDER_COLOR).unwrap().into(),
border_radius: RoundUiBorder::all(20.0).into(),
border_thickness: 6.0,
});
// Spawn the screen layout, containing a centered panel with menu items
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
..default()
})
.with_children(|p| {
p.spawn(MaterialNodeBundle {
material: panel_material,
style: Style {
width: Val::Px(PANEL_WIDTH),
padding: UiRect::axes(Val::Px(40.), Val::Px(60.)),
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
..default()
})
.with_children(|p| {
// Spawn the title
p.spawn(NodeBundle {
style: Style {
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
margin: UiRect::bottom(Val::Px(40.)),
..default()
},
..default()
})
.with_children(|p| {
p.spawn(TextBundle::from_section(
"MENU",
TextStyle {
color: Color::WHITE,
font_size: 40.,
..default()
},
));
});
// Spawn the buttons
spawn_button(p, &button_style, "Play", ButtonAction::Play);
spawn_button(p, &button_style, "Settings", ButtonAction::Settings);
spawn_button(p, &button_style, "Quit", ButtonAction::Quit);
});
});
}
/// Utility that spawns a new button.
fn spawn_button(
parent: &mut ChildBuilder,
button_style: &ButtonStyle,
text: impl Into<String>,
extras: impl Bundle,
) -> Entity {
parent
.spawn((
RoundButton,
MaterialNodeBundle {
material: button_style.default_material.clone(),
style: Style {
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
width: Val::Percent(100.),
height: Val::Px(BUTTON_HEIGHT),
margin: UiRect::top(Val::Px(10.)),
..default()
},
..default()
},
extras,
Interaction::default(),
))
.with_children(|p| {
p.spawn(TextBundle::from_section(
text,
TextStyle {
color: Color::WHITE,
font_size: 20.,
..default()
},
));
})
.id()
}
/// System that updates button materials and styles when their interaction state changes
#[allow(clippy::type_complexity)]
fn handle_button_interactions(
mut interaction_query: Query<
(&Interaction, &mut Handle<RoundRectUiMaterial>, &mut Style),
(Changed<Interaction>, With<RoundButton>),
>,
button_style: Res<ButtonStyle>,
) {
for (interaction, mut material, mut style) in &mut interaction_query {
let (material_handle, padding) = match *interaction {
Interaction::Pressed => (
button_style.press_material.clone(),
button_style.press_padding,
),
Interaction::Hovered => (
button_style.hover_material.clone(),
button_style.hover_padding,
),
Interaction::None => (
button_style.default_material.clone(),
button_style.default_padding,
),
};
*material = material_handle;
style.padding = padding;
}
}
/// System that handles button click events
fn handle_button_actions(
interaction_query: Query<(&Interaction, &ButtonAction), Changed<Interaction>>,
mut app_exit_events: EventWriter<AppExit>,
) {
for (interaction, action) in &interaction_query {
if *interaction == Interaction::Pressed {
println!("Button pressed: {action:?}");
match action {
ButtonAction::Play => (),
ButtonAction::Settings => (),
ButtonAction::Quit => {
app_exit_events.send(AppExit::Success);
}
}
}
}
}