Skip to content

Commit

Permalink
main menu button interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
stillonearth committed Jan 4, 2024
1 parent c401097 commit 4bc27f5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 138 deletions.
4 changes: 0 additions & 4 deletions src/cutscene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/ldtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::entities::{
player::Player,
};

const ASPECT_RATIO: f32 = 16. / 9.;
const ASPECT_RATIO: f32 = 1. / 1.;

// Events

Expand Down
28 changes: 26 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -37,7 +41,27 @@ fn main() {
let mut app = App::new();

app.add_state::<GameState>()
.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)
Expand Down
164 changes: 33 additions & 131 deletions src/menu.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::loading::FontAssets;
use crate::loading::TextureAssets;
use crate::CutsceneAssets;
use crate::GameState;
Expand Down Expand Up @@ -30,19 +31,22 @@ 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,
}
}
}

#[derive(Component)]
struct Menu;

#[derive(Component)]
struct StartGameButton;

fn setup_menu(
mut commands: Commands,
textures: Res<TextureAssets>,
cutscene_assets: Res<CutsceneAssets>,
font_assets: Res<FontAssets>,
) {
info!("menu");

Expand All @@ -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(),
Expand All @@ -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()
Expand All @@ -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<NextState<GameState>>,
mut interaction_query: Query<
(
&Interaction,
&mut BackgroundColor,
&ButtonColors,
Option<&ChangeState>,
Option<&OpenLink>,
),
(&Interaction, &ButtonColors, Option<&ChangeState>),
(Changed<Interaction>, With<Button>),
>,
mut start_game_button: Query<(&mut Text, &StartGameButton)>,
) {
for (interaction, mut color, button_colors, change_state, open_link) in &mut interaction_query {
for (interaction, button_colors, change_state) in &mut interaction_query {
match *interaction {
Interaction::Pressed => {
if let Some(state) = change_state {
next_state.set(state.0.clone());
} else if let Some(link) = open_link {
if let Err(error) = webbrowser::open(link.0) {
warn!("Failed to open link {error:?}");
}
}
}
Interaction::Hovered => {
*color = button_colors.hovered.into();
for (mut text, _) in start_game_button.iter_mut() {
text.sections[0].style.color = button_colors.hovered.clone().into();
}
}
Interaction::None => {
*color = button_colors.normal.into();
for (mut text, _) in start_game_button.iter_mut() {
text.sections[0].style.color = button_colors.normal.clone().into();
}
}
}
}
Expand Down

0 comments on commit 4bc27f5

Please sign in to comment.