From 4bc27f52745e77b49942e834753de36263cf0fd0 Mon Sep 17 00:00:00 2001 From: Sergei Surovtsev Date: Thu, 4 Jan 2024 23:00:00 +0300 Subject: [PATCH] main menu button interaction --- src/cutscene.rs | 4 -- src/ldtk.rs | 2 +- src/main.rs | 28 ++++++++- src/menu.rs | 164 ++++++++++-------------------------------------- 4 files changed, 60 insertions(+), 138 deletions(-) diff --git a/src/cutscene.rs b/src/cutscene.rs index ac08a3d..a49440b 100644 --- a/src/cutscene.rs +++ b/src/cutscene.rs @@ -182,16 +182,12 @@ fn setup_cutscene( align_items: AlignItems::Center, ..default() }, - // background_color: Color::BLACK.into(), ..default() }, Cutscene, Name::new("cutscene image container"), )) .with_children(|parent| { - // bevy logo (image) - // A `NodeBundle` is used to display the logo the image as an `ImageBundle` can't automatically - // size itself with a child node present. parent.spawn(( NodeBundle { style: Style { diff --git a/src/ldtk.rs b/src/ldtk.rs index c6628c2..5d9c4f2 100644 --- a/src/ldtk.rs +++ b/src/ldtk.rs @@ -12,7 +12,7 @@ use crate::entities::{ player::Player, }; -const ASPECT_RATIO: f32 = 16. / 9.; +const ASPECT_RATIO: f32 = 1. / 1.; // Events diff --git a/src/main.rs b/src/main.rs index 720c0ad..2b0480c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,10 @@ #![allow(clippy::too_many_arguments, clippy::type_complexity)] -use bevy::{input::common_conditions::input_toggle_active, prelude::*}; +use bevy::{ + input::common_conditions::input_toggle_active, + prelude::*, + window::{PresentMode, WindowTheme}, +}; use bevy_ecs_ldtk::prelude::*; use bevy_inspector_egui::quick::WorldInspectorPlugin; use bevy_particle_systems::*; @@ -37,7 +41,27 @@ fn main() { let mut app = App::new(); app.add_state::() - .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) + // .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()).set()) + .add_plugins(( + DefaultPlugins + .set(WindowPlugin { + primary_window: Some(Window { + title: "I am a window!".into(), + resolution: (1024., 1024.).into(), + present_mode: PresentMode::AutoVsync, + // Tells wasm to resize the window according to the available canvas + fit_canvas_to_parent: true, + // Tells wasm not to override default event handling, like F5, Ctrl+R etc. + prevent_default_event_handling: false, + window_theme: Some(WindowTheme::Dark), + ..default() + }), + ..default() + }) + .set(ImagePlugin::default_nearest()), + // LogDiagnosticsPlugin::default(), + // FrameTimeDiagnosticsPlugin, + )) .add_plugins(LoadingPlugin) .add_plugins(MenuPlugin) .add_plugins(CutscenePlugin) diff --git a/src/menu.rs b/src/menu.rs index cecc6d2..b94e2ec 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -1,3 +1,4 @@ +use crate::loading::FontAssets; use crate::loading::TextureAssets; use crate::CutsceneAssets; use crate::GameState; @@ -30,8 +31,8 @@ struct ButtonColors { impl Default for ButtonColors { fn default() -> Self { ButtonColors { - normal: Color::rgb(0.15, 0.15, 0.15), - hovered: Color::rgb(0.25, 0.25, 0.25), + normal: Color::WHITE, + hovered: Color::BLACK, } } } @@ -39,10 +40,13 @@ impl Default for ButtonColors { #[derive(Component)] struct Menu; +#[derive(Component)] +struct StartGameButton; + fn setup_menu( mut commands: Commands, - textures: Res, cutscene_assets: Res, + font_assets: Res, ) { info!("menu"); @@ -59,21 +63,17 @@ fn setup_menu( align_items: AlignItems::Center, ..default() }, - // background_color: Color::BLACK.into(), ..default() }, Menu, Name::new("cutscene image container"), )) .with_children(|parent| { - // bevy logo (image) - // A `NodeBundle` is used to display the logo the image as an `ImageBundle` can't automatically - // size itself with a child node present. parent.spawn(( NodeBundle { style: Style { - width: Val::Px(512.0), - height: Val::Px(512.0), + width: Val::Percent(100.0), + height: Val::Percent(100.0), ..default() }, background_color: Color::WHITE.into(), @@ -90,8 +90,9 @@ fn setup_menu( width: Val::Percent(100.0), height: Val::Percent(100.0), flex_direction: FlexDirection::Column, - align_items: AlignItems::Center, + align_items: AlignItems::FlexEnd, justify_content: JustifyContent::Center, + margin: UiRect::right(Val::Percent(20.0)), ..default() }, ..default() @@ -104,161 +105,62 @@ fn setup_menu( .spawn(( ButtonBundle { style: Style { - width: Val::Px(256.0), + width: Val::Px(128.0), height: Val::Px(50.0), justify_content: JustifyContent::Center, align_items: AlignItems::Center, ..Default::default() }, - background_color: button_colors.normal.into(), + background_color: Color::rgba_u8(0, 0, 0, 0).into(), ..Default::default() }, button_colors, ChangeState(GameState::Cutscene), )) .with_children(|parent| { - parent.spawn(TextBundle::from_section( - "Matando Mierdas", - TextStyle { - font_size: 30.0, - color: Color::rgb(0.9, 0.9, 0.9), - ..default() - }, + parent.spawn(( + TextBundle::from_section( + "START", + TextStyle { + font_size: 88.0, + font: font_assets.pixeloid_mono.clone(), + color: Color::WHITE.into(), + ..default() + }, + ), + StartGameButton, )); }); }); - - commands - .spawn(( - NodeBundle { - style: Style { - flex_direction: FlexDirection::Row, - align_items: AlignItems::Center, - justify_content: JustifyContent::SpaceAround, - bottom: Val::Px(5.), - width: Val::Percent(100.), - position_type: PositionType::Absolute, - ..default() - }, - ..default() - }, - Menu, - )) - .with_children(|children| { - children - .spawn(( - ButtonBundle { - style: Style { - width: Val::Px(170.0), - height: Val::Px(50.0), - justify_content: JustifyContent::SpaceAround, - align_items: AlignItems::Center, - padding: UiRect::all(Val::Px(5.)), - ..Default::default() - }, - background_color: Color::NONE.into(), - ..Default::default() - }, - ButtonColors { - normal: Color::NONE, - ..default() - }, - OpenLink("https://bevyengine.org"), - )) - .with_children(|parent| { - parent.spawn(TextBundle::from_section( - "Made with Bevy", - TextStyle { - font_size: 15.0, - color: Color::rgb(0.9, 0.9, 0.9), - ..default() - }, - )); - parent.spawn(ImageBundle { - image: textures.bevy.clone().into(), - style: Style { - width: Val::Px(32.), - ..default() - }, - ..default() - }); - }); - children - .spawn(( - ButtonBundle { - style: Style { - width: Val::Px(170.0), - height: Val::Px(50.0), - justify_content: JustifyContent::SpaceAround, - align_items: AlignItems::Center, - padding: UiRect::all(Val::Px(5.)), - ..default() - }, - background_color: Color::NONE.into(), - ..Default::default() - }, - ButtonColors { - normal: Color::NONE, - hovered: Color::rgb(0.25, 0.25, 0.25), - }, - OpenLink("https://github.com/stillonearth/legend_of_mierda"), - )) - .with_children(|parent| { - parent.spawn(TextBundle::from_section( - "Open source", - TextStyle { - font_size: 15.0, - color: Color::rgb(0.9, 0.9, 0.9), - ..default() - }, - )); - parent.spawn(ImageBundle { - image: textures.github.clone().into(), - style: Style { - width: Val::Px(32.), - ..default() - }, - ..default() - }); - }); - }); } #[derive(Component)] struct ChangeState(GameState); -#[derive(Component)] -struct OpenLink(&'static str); - fn click_play_button( mut next_state: ResMut>, mut interaction_query: Query< - ( - &Interaction, - &mut BackgroundColor, - &ButtonColors, - Option<&ChangeState>, - Option<&OpenLink>, - ), + (&Interaction, &ButtonColors, Option<&ChangeState>), (Changed, With