2020-07-18 21:08:46 +00:00
|
|
|
use bevy::prelude::*;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::build()
|
|
|
|
.add_default_plugins()
|
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 {
|
|
|
|
normal: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
|
|
|
|
hovered: materials.add(Color::rgb(0.05, 0.05, 0.05).into()),
|
|
|
|
pressed: materials.add(Color::rgb(0.1, 0.5, 0.1).into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 21:08:46 +00:00
|
|
|
fn button_system(
|
2020-07-18 21:36:31 +00:00
|
|
|
button_materials: Res<ButtonMaterials>,
|
|
|
|
mut click_query: Query<(
|
|
|
|
&Button,
|
|
|
|
Changed<Click>,
|
2020-07-19 00:03:37 +00:00
|
|
|
Option<&Hover>,
|
2020-07-18 21:36:31 +00:00
|
|
|
&mut Handle<ColorMaterial>,
|
|
|
|
&Children,
|
|
|
|
)>,
|
|
|
|
mut hover_query: Query<(
|
|
|
|
&Button,
|
|
|
|
Changed<Hover>,
|
2020-07-19 00:03:37 +00:00
|
|
|
Option<&Click>,
|
2020-07-18 21:36:31 +00:00
|
|
|
&mut Handle<ColorMaterial>,
|
|
|
|
&Children,
|
|
|
|
)>,
|
|
|
|
label_query: Query<&mut Label>,
|
2020-07-18 21:08:46 +00:00
|
|
|
) {
|
2020-07-19 00:03:37 +00:00
|
|
|
for (_button, hover, click, mut material, children) in &mut hover_query.iter() {
|
2020-07-18 21:36:31 +00:00
|
|
|
let mut label = label_query.get_mut::<Label>(children[0]).unwrap();
|
2020-07-19 00:03:37 +00:00
|
|
|
match *hover {
|
|
|
|
Hover::Hovered => {
|
|
|
|
if let Some(Click::Released) = click {
|
|
|
|
label.text = "Hover".to_string();
|
|
|
|
*material = button_materials.hovered;
|
|
|
|
}
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2020-07-19 00:03:37 +00:00
|
|
|
Hover::NotHovered => {
|
|
|
|
if let Some(Click::Pressed) = click {
|
|
|
|
label.text = "Press".to_string();
|
|
|
|
*material = button_materials.pressed;
|
|
|
|
} else {
|
|
|
|
label.text = "Button".to_string();
|
|
|
|
*material = button_materials.normal;
|
|
|
|
}
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 00:03:37 +00:00
|
|
|
for (_button, click, hover, mut material, children) in &mut click_query.iter() {
|
2020-07-18 21:36:31 +00:00
|
|
|
let mut label = label_query.get_mut::<Label>(children[0]).unwrap();
|
2020-07-19 00:03:37 +00:00
|
|
|
match *click {
|
|
|
|
Click::Pressed => {
|
|
|
|
label.text = "Press".to_string();
|
|
|
|
*material = button_materials.pressed;
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2020-07-19 00:03:37 +00:00
|
|
|
Click::Released => {
|
|
|
|
if let Some(Hover::Hovered) = hover {
|
|
|
|
label.text = "Hover".to_string();
|
|
|
|
*material = button_materials.hovered;
|
|
|
|
} else {
|
|
|
|
label.text = "Button".to_string();
|
|
|
|
*material = button_materials.normal;
|
|
|
|
}
|
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
|
|
|
|
.spawn(OrthographicCameraComponents::default())
|
|
|
|
.spawn(ButtonComponents {
|
2020-07-18 21:36:31 +00:00
|
|
|
node: Node::new(Anchors::CENTER, Margins::new(-75.0, 75.0, -35.0, 35.0)),
|
|
|
|
material: button_materials.normal,
|
2020-07-18 21:08:46 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
|
|
|
parent.spawn(LabelComponents {
|
2020-07-19 00:03:37 +00:00
|
|
|
node: Node::new(Anchors::FULL, Margins::new(0.0, 0.0, 12.0, 0.0)),
|
2020-07-18 21:08:46 +00:00
|
|
|
label: Label {
|
|
|
|
text: "Button".to_string(),
|
|
|
|
font: asset_server.load("assets/fonts/FiraSans-Bold.ttf").unwrap(),
|
|
|
|
style: TextStyle {
|
|
|
|
font_size: 40.0,
|
2020-07-18 21:36:31 +00:00
|
|
|
color: Color::rgb(0.8, 0.8, 0.8),
|
2020-07-19 00:03:37 +00:00
|
|
|
align: TextAlign::Center,
|
2020-07-18 21:08:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|