DEVELOPMENT ENVIRONMENT

~liljamo/bevy_dice

4e633cd65152b4ddf2aab70fada8eb0d7a931ff7 — Jonni Liljamo 2 days ago a6e5cb1
feat(demo): deduplicate button code
1 files changed, 37 insertions(+), 64 deletions(-)

M crates/bevy_dice_demo/src/ui.rs
M crates/bevy_dice_demo/src/ui.rs => crates/bevy_dice_demo/src/ui.rs +37 -64
@@ 33,6 33,41 @@ pub struct ThrowPressed;
#[derive(Event)]
pub struct DespawnPressed;

fn button(function: ButtonFunction, text: impl Into<String>, 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),
        ],
    ));
}