From 4e633cd65152b4ddf2aab70fada8eb0d7a931ff7 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 10 Jun 2025 19:12:26 +0300 Subject: [PATCH] feat(demo): deduplicate button code --- crates/bevy_dice_demo/src/ui.rs | 101 ++++++++++++-------------------- 1 file changed, 37 insertions(+), 64 deletions(-) diff --git a/crates/bevy_dice_demo/src/ui.rs b/crates/bevy_dice_demo/src/ui.rs index 64b6503..0fb9212 100644 --- a/crates/bevy_dice_demo/src/ui.rs +++ b/crates/bevy_dice_demo/src/ui.rs @@ -33,6 +33,41 @@ pub struct ThrowPressed; #[derive(Event)] pub struct DespawnPressed; +fn button(function: ButtonFunction, text: impl Into, color: Srgba) -> impl Bundle { + ( + function, + Button, + Node { + width: Val::Px(180.0), + height: Val::Px(60.0), + border: UiRect::all(Val::Px(2.0)), + flex_direction: FlexDirection::Column, + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + margin: UiRect::bottom(Val::Px(4.0)), + ..Default::default() + }, + BackgroundColor(Color::from(color)), + BorderColor(Color::from(color.darker(0.3))), + BorderRadius::all(Val::Px(10.0)), + BoxShadow::new( + Color::BLACK.with_alpha(0.8), + Val::Px(4.0), + Val::Px(4.0), + Val::DEFAULT, + Val::DEFAULT, + ), + children![( + Text::new(text), + TextFont { + font_size: 18.0, + ..Default::default() + }, + TextColor(Color::BLACK), + ),], + ) +} + fn setup(mut commands: Commands) { commands.spawn(( Node { @@ -46,70 +81,8 @@ fn setup(mut commands: Commands) { ..Default::default() }, children![ - ( - ButtonFunction::Throw, - Button, - Node { - width: Val::Px(180.0), - height: Val::Px(60.0), - border: UiRect::all(Val::Px(2.0)), - flex_direction: FlexDirection::Column, - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - margin: UiRect::bottom(Val::Px(4.0)), - ..Default::default() - }, - BackgroundColor(Color::from(THROW_COLOR)), - BorderColor(Color::from(LIME_700)), - BorderRadius::all(Val::Px(10.0)), - BoxShadow::new( - Color::BLACK.with_alpha(0.8), - Val::Px(4.0), - Val::Px(4.0), - Val::DEFAULT, - Val::DEFAULT, - ), - children![( - Text::new("Throw"), - TextFont { - font_size: 18.0, - ..Default::default() - }, - TextColor(Color::BLACK), - ),], - ), - ( - ButtonFunction::Despawn, - Button, - Node { - width: Val::Px(180.0), - height: Val::Px(60.0), - border: UiRect::all(Val::Px(2.0)), - flex_direction: FlexDirection::Column, - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - margin: UiRect::bottom(Val::Px(4.0)), - ..Default::default() - }, - BackgroundColor(Color::from(DESPAWN_COLOR)), - BorderColor(Color::from(RED_700)), - BorderRadius::all(Val::Px(10.0)), - BoxShadow::new( - Color::BLACK.with_alpha(0.8), - Val::Px(4.0), - Val::Px(4.0), - Val::DEFAULT, - Val::DEFAULT, - ), - children![( - Text::new("Despawn Dice"), - TextFont { - font_size: 18.0, - ..Default::default() - }, - TextColor(Color::BLACK), - ),], - ), + button(ButtonFunction::Throw, "Throw", THROW_COLOR), + button(ButtonFunction::Despawn, "Despawn Dice", DESPAWN_COLOR), ], )); } -- 2.44.1