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::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<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 {
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<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 {
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));
}
}

Loading…
Cancel
Save