a play button exists now

main
Jordan Orelli 1 year ago
parent beffb1a12d
commit 75d4c6919a

@ -1,15 +1,25 @@
use bevy::ecs::system::EntityCommand;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::text::DEFAULT_FONT_HANDLE; use bevy::text::DEFAULT_FONT_HANDLE;
use tracing::info; use tracing::info;
pub struct StartMenu {} pub struct StartMenu {}
#[derive(Component)]
struct InactiveColor(Color);
#[derive(Component)]
struct HoverColor(Color);
#[derive(Component)] #[derive(Component)]
struct Title; struct Title;
#[derive(Component)] #[derive(Component)]
struct QuitButton; struct QuitButton;
#[derive(Component)]
struct PlayButton;
fn title_text() -> TextBundle { fn title_text() -> TextBundle {
let style = TextStyle { let style = TextStyle {
font: DEFAULT_FONT_HANDLE.typed(), font: DEFAULT_FONT_HANDLE.typed(),
@ -19,7 +29,7 @@ fn title_text() -> TextBundle {
TextBundle::from_section("Rock, Paper, Scissors", style) TextBundle::from_section("Rock, Paper, Scissors", style)
} }
fn quit_button() -> ButtonBundle { fn thicc_button() -> ButtonBundle {
ButtonBundle { ButtonBundle {
style: Style { style: Style {
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
@ -34,30 +44,50 @@ fn quit_button() -> ButtonBundle {
} }
} }
fn click_quit( fn hover_colors(
mut actions: Query< mut actions: Query<
(&Interaction, &mut BackgroundColor), (
(Changed<Interaction>, With<QuitButton>), &Interaction,
&mut BackgroundColor,
&InactiveColor,
&HoverColor,
),
(Changed<Interaction>, With<InactiveColor>, With<HoverColor>),
>, >,
mut quit: ResMut<Events<bevy::app::AppExit>>,
) { ) {
for (interaction, mut bg) in &mut actions { for (act, mut bg, off, on) in &mut actions {
match interaction { match act {
Interaction::Pressed => { Interaction::Pressed => {}
info!("Quit button clicked");
bg.0 = Color::BLUE;
quit.send_default();
}
Interaction::Hovered => { Interaction::Hovered => {
bg.0 = Color::rgb(0.4, 0.3, 0.3); bg.0 = on.0;
} }
Interaction::None => { Interaction::None => {
bg.0 = Color::rgb(0.4, 0.2, 0.2); bg.0 = off.0;
} }
} }
} }
} }
fn click_play(mut actions: Query<&Interaction, (Changed<Interaction>, With<PlayButton>)>) {
for interaction in &mut actions {
if matches!(interaction, Interaction::Pressed) {
todo!()
}
}
}
fn click_quit(
mut actions: Query<&Interaction, (Changed<Interaction>, With<QuitButton>)>,
mut quit: ResMut<Events<bevy::app::AppExit>>,
) {
for interaction in &mut actions {
if matches!(interaction, Interaction::Pressed) {
info!("Quit button clicked");
quit.send_default();
}
}
}
fn vertical_spacer(height: f32) -> NodeBundle { fn vertical_spacer(height: f32) -> NodeBundle {
NodeBundle { NodeBundle {
style: Style { style: Style {
@ -68,16 +98,9 @@ fn vertical_spacer(height: f32) -> NodeBundle {
} }
} }
fn setup(mut commands: Commands) { fn column() -> NodeBundle {
commands.spawn(Camera2dBundle::default()); NodeBundle {
commands
.spawn(NodeBundle {
style: Style { style: Style {
padding: UiRect {
top: Val::Px(30.0),
..default()
},
display: Display::Flex, display: Display::Flex,
width: Val::Percent(100.0), width: Val::Percent(100.0),
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
@ -86,20 +109,22 @@ fn setup(mut commands: Commands) {
}, },
background_color: BackgroundColor(Color::BLACK), background_color: BackgroundColor(Color::BLACK),
..default() ..default()
}) }
.with_children(|column| { }
column.spawn((Title, title_text()));
column.spawn(vertical_spacer(20.0)); struct AddChildLabel {
column text: String,
.spawn((QuitButton, quit_button())) }
.with_children(|button| {
impl EntityCommand for AddChildLabel {
fn apply(self, id: Entity, world: &mut World) {
let style = TextStyle { let style = TextStyle {
font: DEFAULT_FONT_HANDLE.typed(), font: DEFAULT_FONT_HANDLE.typed(),
font_size: 24.0, font_size: 24.0,
color: Color::WHITE, color: Color::WHITE,
}; };
button.spawn( world.spawn_empty().set_parent(id).insert(
TextBundle::from_section("Quit", style) TextBundle::from_section(self.text, style)
.with_text_alignment(TextAlignment::Center) .with_text_alignment(TextAlignment::Center)
.with_style(Style { .with_style(Style {
display: Display::Flex, display: Display::Flex,
@ -112,6 +137,38 @@ fn setup(mut commands: Commands) {
..Default::default() ..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));
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"),
}); });
}); });
} }
@ -119,6 +176,6 @@ fn setup(mut commands: Commands) {
impl Plugin for StartMenu { impl Plugin for StartMenu {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems(Update, click_quit); app.add_systems(Update, (hover_colors, click_quit, click_play));
} }
} }

Loading…
Cancel
Save