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,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) { 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(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 menu.spawn((
.spawn(NodeBundle { PlayButton,
style: Style { thicc_button(),
padding: UiRect { HoverColor(green),
top: Val::Px(30.0), InactiveColor(red),
..default() ))
}, .add(AddChildLabel {
display: Display::Flex, text: String::from("Play"),
width: Val::Percent(100.0), });
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center, menu.spawn(vertical_spacer(32.0));
..default() menu.spawn((
}, QuitButton,
background_color: BackgroundColor(Color::BLACK), thicc_button(),
..default() HoverColor(green),
}) InactiveColor(red),
.with_children(|column| { ))
column.spawn((Title, title_text())); .add(AddChildLabel {
column.spawn(vertical_spacer(20.0)); text: String::from("Quit"),
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()
}),
);
});
}); });
});
} }
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