Skip to content

Commit

Permalink
feat: menu buttons and quit
Browse files Browse the repository at this point in the history
  • Loading branch information
roboteng committed Oct 13, 2023
1 parent 00926ee commit d042b02
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions game/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bevy::prelude::*;
use bevy::{app::AppExit, prelude::*};
use bevy_ui_dsl::*;

pub struct BasePlugin;

impl Plugin for BasePlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_state::<GameState>()
.add_systems(Startup, draw_main_menu);
.add_systems(Startup, draw_main_menu)
.add_systems(Update, button_interaction);
}
}

Expand All @@ -24,39 +25,74 @@ fn c_background(node: &mut NodeBundle) {
node.style.width = Val::Percent(100.0);
}

fn c_m(node: &mut NodeBundle) {
fn c_main_menu(node: &mut NodeBundle) {
node.background_color = Color::MIDNIGHT_BLUE.into();
node.style.padding = UiRect::all(Val::Px(20.0));
node.style.flex_direction = FlexDirection::Column;
node.style.align_items = AlignItems::Center;
node.style.justify_content = JustifyContent::Start;
node.style.column_gap = Val::Px(10.0);
node.style.row_gap = Val::Px(10.0);

node.style.max_width = Val::Vw(33.3);
}

fn c_child(node: &mut NodeBundle) {
node.background_color = Color::ORANGE_RED.into();
fn c_button(_: &AssetServer, node: &mut ButtonBundle) {
node.style.padding = UiRect::all(Val::Px(8.0));
}

fn c_button_text(_b: &AssetServer, a: &mut TextStyle) {
a.color = Color::BLACK.into();
}

fn c_title_test(_: &AssetServer, a: &mut TextStyle) {
a.font_size = 32.0;
}

#[derive(Component)]
struct QuitButton;

fn draw_main_menu(mut commands: Commands, assets: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());

root((c_background, c_centers), &assets, &mut commands, |p| {
node(c_m, p, |p| {
text("Hello, DSL", (), (), p);
node(c_child, p, |p| {
text("Goodbye, DSL", (), (), p);
node(c_main_menu, p, |p| {
text("The Tales our\nAncestors Told", (), c_title_test, p);

button(c_button, p, |p| {
text("Single Player", (), c_button_text, p);
});
button((), p, |p| {
text("Hello, button", (), c_button_text, p);
button(c_button, p, |p| {
text("Multi Player", (), c_button_text, p);
});
button(c_button, p, |p| {
text("Settings", (), c_button_text, p);
});
buttoni(c_button, QuitButton, p, |p| {
text("Quit", (), c_button_text, p);
});
});
});
}

fn button_interaction(
query: Query<&Interaction, (Changed<Interaction>, With<QuitButton>)>,
mut window: Query<&mut Window>,
mut exit_event: ResMut<Events<AppExit>>,
) {
for interaction in &query {
for mut window in &mut window {
window.cursor.icon = match interaction {
Interaction::Pressed => {
exit_event.send(AppExit);
CursorIcon::Default
}
Interaction::Hovered => CursorIcon::Hand,
Interaction::None => CursorIcon::Default,
}
}
}
}

#[derive(Debug, Default, Hash, PartialEq, Eq, Clone, States)]
enum GameState {
#[default]
Expand Down

0 comments on commit d042b02

Please sign in to comment.