2020-07-18 21:08:46 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
2020-07-28 20:43:07 +00:00
|
|
|
/// This example illustrates how to create a button that changes color and text based on its interaction state.
|
2020-07-18 21:08:46 +00:00
|
|
|
fn main() {
|
|
|
|
App::build()
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2020-07-18 21:36:31 +00:00
|
|
|
.init_resource::<ButtonMaterials>()
|
2020-07-18 21:08:46 +00:00
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.add_system(button_system.system())
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2020-07-18 21:36:31 +00:00
|
|
|
struct ButtonMaterials {
|
|
|
|
normal: Handle<ColorMaterial>,
|
|
|
|
hovered: Handle<ColorMaterial>,
|
|
|
|
pressed: Handle<ColorMaterial>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromResources for ButtonMaterials {
|
|
|
|
fn from_resources(resources: &Resources) -> Self {
|
|
|
|
let mut materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
|
|
|
|
ButtonMaterials {
|
2020-10-12 23:54:22 +00:00
|
|
|
normal: materials.add(Color::rgb(0.15, 0.15, 0.15).into()),
|
|
|
|
hovered: materials.add(Color::rgb(0.25, 0.25, 0.25).into()),
|
|
|
|
pressed: materials.add(Color::rgb(0.35, 0.75, 0.35).into()),
|
2020-07-18 21:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 21:08:46 +00:00
|
|
|
fn button_system(
|
2020-07-18 21:36:31 +00:00
|
|
|
button_materials: Res<ButtonMaterials>,
|
2020-07-28 08:20:19 +00:00
|
|
|
mut interaction_query: Query<(
|
2020-07-18 21:36:31 +00:00
|
|
|
&Button,
|
2020-07-28 08:20:19 +00:00
|
|
|
Mutated<Interaction>,
|
2020-07-18 21:36:31 +00:00
|
|
|
&mut Handle<ColorMaterial>,
|
|
|
|
&Children,
|
|
|
|
)>,
|
2020-10-30 06:39:55 +00:00
|
|
|
mut text_query: Query<&mut Text>,
|
2020-07-18 21:08:46 +00:00
|
|
|
) {
|
2020-10-30 06:39:55 +00:00
|
|
|
for (_button, interaction, mut material, children) in interaction_query.iter_mut() {
|
2020-10-31 01:04:33 +00:00
|
|
|
let mut text = text_query.get_mut(children[0]).unwrap();
|
2020-07-28 08:20:19 +00:00
|
|
|
match *interaction {
|
|
|
|
Interaction::Clicked => {
|
2020-07-20 08:33:30 +00:00
|
|
|
text.value = "Press".to_string();
|
2020-10-18 20:48:15 +00:00
|
|
|
*material = button_materials.pressed.clone();
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2020-07-28 08:20:19 +00:00
|
|
|
Interaction::Hovered => {
|
|
|
|
text.value = "Hover".to_string();
|
2020-10-18 20:48:15 +00:00
|
|
|
*material = button_materials.hovered.clone();
|
2020-07-28 08:20:19 +00:00
|
|
|
}
|
|
|
|
Interaction::None => {
|
|
|
|
text.value = "Button".to_string();
|
2020-10-18 20:48:15 +00:00
|
|
|
*material = button_materials.normal.clone();
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
asset_server: Res<AssetServer>,
|
2020-07-18 21:36:31 +00:00
|
|
|
button_materials: Res<ButtonMaterials>,
|
2020-07-18 21:08:46 +00:00
|
|
|
) {
|
|
|
|
commands
|
|
|
|
// ui camera
|
2020-07-25 06:04:45 +00:00
|
|
|
.spawn(UiCameraComponents::default())
|
2020-07-28 02:12:48 +00:00
|
|
|
.spawn(ButtonComponents {
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Style {
|
2020-07-28 04:04:04 +00:00
|
|
|
size: Size::new(Val::Px(150.0), Val::Px(65.0)),
|
|
|
|
// center button
|
|
|
|
margin: Rect::all(Val::Auto),
|
|
|
|
// horizontally center child text
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
// vertically center child text
|
|
|
|
align_items: AlignItems::Center,
|
2020-07-25 06:04:45 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2020-10-18 20:48:15 +00:00
|
|
|
material: button_materials.normal.clone(),
|
2020-07-18 21:08:46 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
2020-07-28 02:12:48 +00:00
|
|
|
parent.spawn(TextComponents {
|
|
|
|
text: Text {
|
|
|
|
value: "Button".to_string(),
|
2020-10-18 20:48:15 +00:00
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
2020-07-28 02:12:48 +00:00
|
|
|
style: TextStyle {
|
|
|
|
font_size: 40.0,
|
2020-10-12 23:54:22 +00:00
|
|
|
color: Color::rgb(0.9, 0.9, 0.9),
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
2020-07-18 21:08:46 +00:00
|
|
|
});
|
|
|
|
}
|