From a531c906a64e05f10911c5b51561db505c1b8a37 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sat, 18 Jul 2020 14:36:31 -0700 Subject: [PATCH] ui: improve button example --- examples/ui/button.rs | 60 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/examples/ui/button.rs b/examples/ui/button.rs index b165159522..8ca71a0065 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -3,33 +3,69 @@ use bevy::prelude::*; fn main() { App::build() .add_default_plugins() + .init_resource::() .add_startup_system(setup.system()) .add_system(button_system.system()) .run(); } +struct ButtonMaterials { + normal: Handle, + hovered: Handle, + pressed: Handle, +} + +impl FromResources for ButtonMaterials { + fn from_resources(resources: &Resources) -> Self { + let mut materials = resources.get_mut::>().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()), + } + } +} + fn button_system( - mut click_query: Query<(&Button, Changed)>, - mut hover_query: Query<(&Button, Changed)>, + button_materials: Res, + mut click_query: Query<( + &Button, + Changed, + &mut Handle, + &Children, + )>, + mut hover_query: Query<( + &Button, + Changed, + &mut Handle, + &Children, + )>, + label_query: Query<&mut Label>, ) { - for (_button, click) in &mut click_query.iter() { + for (_button, click, mut material, children) in &mut click_query.iter() { + let mut label = label_query.get_mut::