From 75d4c6919a53bb38e000dfaed857843a19a3ba69 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 24 Sep 2023 18:58:49 -0500 Subject: [PATCH] a play button exists now --- src/start_menu.rs | 171 ++++++++++++++++++++++++++++++---------------- 1 file changed, 114 insertions(+), 57 deletions(-) diff --git a/src/start_menu.rs b/src/start_menu.rs index e3b9a41..4784d06 100644 --- a/src/start_menu.rs +++ b/src/start_menu.rs @@ -1,15 +1,25 @@ +use bevy::ecs::system::EntityCommand; use bevy::prelude::*; use bevy::text::DEFAULT_FONT_HANDLE; use tracing::info; pub struct StartMenu {} +#[derive(Component)] +struct InactiveColor(Color); + +#[derive(Component)] +struct HoverColor(Color); + #[derive(Component)] struct Title; #[derive(Component)] struct QuitButton; +#[derive(Component)] +struct PlayButton; + fn title_text() -> TextBundle { let style = TextStyle { font: DEFAULT_FONT_HANDLE.typed(), @@ -19,7 +29,7 @@ fn title_text() -> TextBundle { TextBundle::from_section("Rock, Paper, Scissors", style) } -fn quit_button() -> ButtonBundle { +fn thicc_button() -> ButtonBundle { ButtonBundle { style: Style { flex_direction: FlexDirection::Column, @@ -34,30 +44,50 @@ fn quit_button() -> ButtonBundle { } } -fn click_quit( +fn hover_colors( mut actions: Query< - (&Interaction, &mut BackgroundColor), - (Changed, With), + ( + &Interaction, + &mut BackgroundColor, + &InactiveColor, + &HoverColor, + ), + (Changed, With, With), >, - mut quit: ResMut>, ) { - for (interaction, mut bg) in &mut actions { - match interaction { - Interaction::Pressed => { - info!("Quit button clicked"); - bg.0 = Color::BLUE; - quit.send_default(); - } + for (act, mut bg, off, on) in &mut actions { + match act { + Interaction::Pressed => {} Interaction::Hovered => { - bg.0 = Color::rgb(0.4, 0.3, 0.3); + bg.0 = on.0; } Interaction::None => { - bg.0 = Color::rgb(0.4, 0.2, 0.2); + bg.0 = off.0; } } } } +fn click_play(mut actions: Query<&Interaction, (Changed, With)>) { + for interaction in &mut actions { + if matches!(interaction, Interaction::Pressed) { + todo!() + } + } +} + +fn click_quit( + mut actions: Query<&Interaction, (Changed, With)>, + mut quit: ResMut>, +) { + for interaction in &mut actions { + if matches!(interaction, Interaction::Pressed) { + info!("Quit button clicked"); + quit.send_default(); + } + } +} + fn vertical_spacer(height: f32) -> NodeBundle { NodeBundle { style: Style { @@ -68,57 +98,84 @@ fn vertical_spacer(height: f32) -> NodeBundle { } } +fn column() -> NodeBundle { + NodeBundle { + style: Style { + display: Display::Flex, + width: Val::Percent(100.0), + flex_direction: FlexDirection::Column, + align_items: AlignItems::Center, + ..default() + }, + background_color: BackgroundColor(Color::BLACK), + ..default() + } +} + +struct AddChildLabel { + text: String, +} + +impl EntityCommand for AddChildLabel { + fn apply(self, id: Entity, world: &mut World) { + let style = TextStyle { + font: DEFAULT_FONT_HANDLE.typed(), + font_size: 24.0, + color: Color::WHITE, + }; + world.spawn_empty().set_parent(id).insert( + TextBundle::from_section(self.text, style) + .with_text_alignment(TextAlignment::Center) + .with_style(Style { + display: Display::Flex, + margin: UiRect { + left: Val::Auto, + right: Val::Auto, + top: Val::Auto, + bottom: Val::Auto, + }, + ..Default::default() + }), + ); + } +} + fn setup(mut commands: Commands) { + let red = Color::rgb(0.8, 0.2, 0.2); + let green = Color::rgb(0.2, 0.8, 0.2); + commands.spawn(Camera2dBundle::default()); + commands.spawn(column()).with_children(|menu| { + menu.spawn(vertical_spacer(80.0)); + menu.spawn((Title, title_text())); + menu.spawn(vertical_spacer(32.0)); - commands - .spawn(NodeBundle { - style: Style { - padding: UiRect { - top: Val::Px(30.0), - ..default() - }, - display: Display::Flex, - width: Val::Percent(100.0), - flex_direction: FlexDirection::Column, - align_items: AlignItems::Center, - ..default() - }, - background_color: BackgroundColor(Color::BLACK), - ..default() - }) - .with_children(|column| { - column.spawn((Title, title_text())); - column.spawn(vertical_spacer(20.0)); - column - .spawn((QuitButton, quit_button())) - .with_children(|button| { - let style = TextStyle { - font: DEFAULT_FONT_HANDLE.typed(), - font_size: 24.0, - color: Color::WHITE, - }; - button.spawn( - TextBundle::from_section("Quit", style) - .with_text_alignment(TextAlignment::Center) - .with_style(Style { - display: Display::Flex, - margin: UiRect { - left: Val::Auto, - right: Val::Auto, - top: Val::Auto, - bottom: Val::Auto, - }, - ..Default::default() - }), - ); - }); + menu.spawn(( + PlayButton, + thicc_button(), + HoverColor(green), + InactiveColor(red), + )) + .add(AddChildLabel { + text: String::from("Play"), + }); + + menu.spawn(vertical_spacer(32.0)); + menu.spawn(( + QuitButton, + thicc_button(), + HoverColor(green), + InactiveColor(red), + )) + .add(AddChildLabel { + text: String::from("Quit"), }); + }); } impl Plugin for StartMenu { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); - app.add_systems(Update, click_quit); + app.add_systems(Update, (hover_colors, click_quit, click_play)); } }