main menu and game save work
This commit is contained in:
+131
-12
@@ -1,8 +1,8 @@
|
||||
use crate::save::get_saves;
|
||||
use crate::save::{ActiveGameSave, create_game_save, delete_game_save, get_saves};
|
||||
use crate::{AppState, WINDOW_TARGET_HEIGHT, WINDOW_TARGET_WIDTH};
|
||||
use bevy::color::palettes::tailwind::SLATE_900;
|
||||
use bevy::input_focus::tab_navigation::TabIndex;
|
||||
use bevy::text::{EditableText, TextCursorStyle};
|
||||
use bevy::text::{EditableText, EditableTextFilter, TextCursorStyle};
|
||||
use bevy::ui_widgets::Button;
|
||||
use bevy::{prelude::*, ui_widgets::Activate};
|
||||
use derive_more::{Deref, DerefMut};
|
||||
@@ -22,6 +22,10 @@ impl Plugin for MainMenuPlugin {
|
||||
|
||||
const N_LAYERS: usize = 5;
|
||||
|
||||
/// The text field for the name of the game.
|
||||
#[derive(Component, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Debug)]
|
||||
struct GameNameTextField;
|
||||
|
||||
/// A UI object that is selected.
|
||||
#[derive(Component, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Debug)]
|
||||
struct Selected;
|
||||
@@ -74,16 +78,18 @@ struct MenuRoot;
|
||||
struct Layer(usize);
|
||||
|
||||
/// A component that indicates the layer that this button points to.
|
||||
#[derive(Component, Copy, Clone, PartialEq, Eq, Debug, Default)]
|
||||
#[derive(Component, Clone, PartialEq, Eq, Debug, Default)]
|
||||
struct NextLayer(MenuLayer);
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
|
||||
pub enum MenuLayer {
|
||||
#[default]
|
||||
Root,
|
||||
Play,
|
||||
LoadSave,
|
||||
NewGame,
|
||||
// TODO: replace with some better game identifier, probably the game metadata
|
||||
GameInfo(String),
|
||||
Browse,
|
||||
DirectConnect,
|
||||
Settings,
|
||||
@@ -121,9 +127,12 @@ impl MenuLayer {
|
||||
let saves = get_saves().unwrap_or_default();
|
||||
let entries: Vec<_> = saves
|
||||
.into_iter()
|
||||
.map(|m| menu_button(m.save_name, None))
|
||||
.map(|m| {
|
||||
menu_button(&m.save_name, Some(MenuLayer::GameInfo(m.save_name.clone())))
|
||||
})
|
||||
.collect();
|
||||
|
||||
// TODO: get rid of row gap when many saves are present.
|
||||
Box::new(bsn! {
|
||||
menu_layer(layer)
|
||||
ChildOf(menu_root)
|
||||
@@ -143,6 +152,13 @@ impl MenuLayer {
|
||||
{new_game_menu()}
|
||||
]
|
||||
}),
|
||||
MenuLayer::GameInfo(game) => Box::new(bsn! {
|
||||
full_menu_layer(layer)
|
||||
ChildOf(menu_root)
|
||||
Children [
|
||||
{game_info_menu(game)}
|
||||
]
|
||||
}),
|
||||
MenuLayer::Browse => Box::new(bsn! {
|
||||
full_menu_layer(layer)
|
||||
ChildOf(menu_root)
|
||||
@@ -289,16 +305,18 @@ fn new_game_menu() -> impl SceneList {
|
||||
),
|
||||
(
|
||||
#NewGameTextField
|
||||
GameNameTextField
|
||||
Node {
|
||||
border: Val::all(px(6)),
|
||||
padding: Val::all(px(12)),
|
||||
border_radius: BorderRadius::all(px(12)),
|
||||
}
|
||||
// BorderColor::from(SLATE_800)
|
||||
BorderColor::from(SLATE_900)
|
||||
EditableText {
|
||||
cursor_width: 0.05,
|
||||
max_characters: Option::Some(14),
|
||||
}
|
||||
EditableTextFilter::new(|c| c == ' ' || c.is_ascii_alphanumeric())
|
||||
TextLayout::no_wrap()
|
||||
TextFont {
|
||||
font_size: FontSize::Px(66.),
|
||||
@@ -315,11 +333,110 @@ fn new_game_menu() -> impl SceneList {
|
||||
}
|
||||
Children [
|
||||
menu_button("Create", None)
|
||||
InteractionColors::GOOD
|
||||
Node {
|
||||
margin: UiRect::left(Val::Auto),
|
||||
}
|
||||
InteractionColors::GOOD
|
||||
on(|event: On<Activate>, q_text:| { info!("create!") })
|
||||
on(|_event: On<Activate>, text_field: Single<&EditableText, With<GameNameTextField>>| {
|
||||
let value = text_field.value();
|
||||
info!("Create triggered for: {}", value);
|
||||
create_game_save(value.to_string()).unwrap();
|
||||
// TODO: refresh listing
|
||||
})
|
||||
]
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
fn game_info_menu(name: impl Into<String>) -> impl SceneList {
|
||||
let name = name.into();
|
||||
bsn_list![
|
||||
(
|
||||
#GameInfoTitle
|
||||
Text("Load Save")
|
||||
Node {
|
||||
align_self: AlignSelf::Center,
|
||||
}
|
||||
TextFont {
|
||||
// font: FontSourceTemplate::Handle("fonts/FiraSans-Bold.ttf"),
|
||||
font_size: px(100.0),
|
||||
}
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9))
|
||||
TextShadow
|
||||
),
|
||||
(
|
||||
Text("Save Name")
|
||||
Node {
|
||||
margin: UiRect::axes(px(12), px(0)),
|
||||
}
|
||||
TextFont {
|
||||
// font: FontSourceTemplate::Handle("fonts/FiraSans-Bold.ttf"),
|
||||
font_size: px(66.0),
|
||||
}
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9))
|
||||
TextShadow
|
||||
),
|
||||
(
|
||||
#SaveNameTextField
|
||||
GameNameTextField
|
||||
Node {
|
||||
border: Val::all(px(6)),
|
||||
padding: Val::all(px(12)),
|
||||
border_radius: BorderRadius::all(px(12)),
|
||||
}
|
||||
BorderColor::from(SLATE_900)
|
||||
EditableText::new(name)
|
||||
EditableText {
|
||||
cursor_width: 0.05,
|
||||
max_characters: Option::Some(14),
|
||||
}
|
||||
EditableTextFilter::new(|c| c == ' ' || c.is_ascii_alphanumeric())
|
||||
TextLayout::no_wrap()
|
||||
TextFont {
|
||||
font_size: FontSize::Px(66.),
|
||||
}
|
||||
TextCursorStyle {
|
||||
color: Color::BLACK
|
||||
}
|
||||
TabIndex(0)
|
||||
BackgroundColor(Color::srgb(0.48, 0.84, 0.87))
|
||||
),
|
||||
(
|
||||
Node {
|
||||
margin: UiRect::top(px(48)),
|
||||
}
|
||||
Children [
|
||||
(
|
||||
menu_button("Delete", None)
|
||||
InteractionColors::DANGER
|
||||
Node {
|
||||
margin: UiRect::right(Val::Auto),
|
||||
}
|
||||
on(|_event: On<Activate>, text_field: Single<&EditableText, With<GameNameTextField>>| {
|
||||
let value = text_field.value();
|
||||
info!("Delete triggered for: {}", value);
|
||||
delete_game_save(value.to_string()).unwrap();
|
||||
// TODO: refresh listing
|
||||
})
|
||||
),
|
||||
(
|
||||
// TODO: when the user changed the name of the game, change this to a "rename" button
|
||||
menu_button("Play", None)
|
||||
InteractionColors::GOOD
|
||||
Node {
|
||||
margin: UiRect::left(Val::Auto),
|
||||
}
|
||||
on(|_event: On<Activate>, text_field: Single<&EditableText, With<GameNameTextField>>, mut commands: Commands, mut app_state: ResMut<NextState<AppState>>| {
|
||||
// TODO: we should definately get this value from an added
|
||||
// resource. Call it something like `SelectedGameSave`
|
||||
let value = text_field.value().to_string();
|
||||
info!("Play triggered for: {}", value);
|
||||
commands.insert_resource(ActiveGameSave::new(value));
|
||||
app_state.set(AppState::Lobby);
|
||||
|
||||
// TODO: refresh listing
|
||||
})
|
||||
)
|
||||
]
|
||||
),
|
||||
]
|
||||
@@ -356,8 +473,10 @@ fn full_menu_layer(layer: usize) -> impl Scene {
|
||||
}
|
||||
}
|
||||
|
||||
fn menu_button(label: impl Into<String>, next_layer: Option<MenuLayer>) -> impl Scene {
|
||||
bsn! {
|
||||
fn menu_button(label: impl Into<String>, next_layer: Option<MenuLayer>) -> Box<dyn Scene> {
|
||||
let label = label.into();
|
||||
|
||||
Box::new(bsn! {
|
||||
Button
|
||||
Node {
|
||||
width: px(600),
|
||||
@@ -381,7 +500,7 @@ fn menu_button(label: impl Into<String>, next_layer: Option<MenuLayer>) -> impl
|
||||
TextColor(Color::srgb(0.9, 0.9, 0.9))
|
||||
TextShadow
|
||||
)]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn button_activate_observer(
|
||||
@@ -405,7 +524,7 @@ fn button_activate_observer(
|
||||
menu.0.drain((layer.0 + 1)..);
|
||||
}
|
||||
// push next layer
|
||||
menu.push(next_layer.0);
|
||||
menu.push(next_layer.0.clone());
|
||||
|
||||
// update the buttons so the correct one is selected
|
||||
for (entity, selected) in q_selected.iter_many(layer_entries) {
|
||||
|
||||
Reference in New Issue
Block a user