/*
* This file is part of laurelin_client
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::prelude::*;
use bevy_egui::egui;
use crate::{plugins::{
GameCreateCallEvent, BrowseState,
GameFormingCallEvent, GameMyGamesCallEvent,
GameJoinCallEvent, menu::MenuState
}, api::game::GameState, Global, AppState};
use super::{MenuData, MenuUIState};
pub fn view(
mut commands: &mut Commands,
ui: &mut egui::Ui,
mut global: &mut Global,
data: &mut MenuData,
create_ev_w: &mut EventWriter<GameCreateCallEvent>,
forming_ev_w: &mut EventWriter<GameFormingCallEvent>,
mygames_ev_w: &mut EventWriter<GameMyGamesCallEvent>,
join_ev_w: &mut EventWriter<GameJoinCallEvent>,
) {
egui::SidePanel::left("browse_side_panel")
.resizable(false)
.show_inside(ui, |ui| {
ui.vertical_centered(|ui| {
ui.add_enabled_ui(!data.waiting, |ui| {
if ui.button("Create").clicked() {
create_ev_w.send(GameCreateCallEvent);
}
if ui.button("Back").clicked() {
data.ui_state = MenuUIState::Main;
}
});
});
});
egui::TopBottomPanel::top("browse_top_panel")
.resizable(false)
.show_inside(ui, |ui| {
ui.horizontal(|ui| {
ui.selectable_value(&mut data.browse_state, BrowseState::Forming, "Forming");
ui.selectable_value(&mut data.browse_state, BrowseState::InProgress, "In Progress");
ui.selectable_value(&mut data.browse_state, BrowseState::Finished, "Finished");
ui.with_layout(
egui::Layout::right_to_left(egui::Align::Center),
|ui| {
if ui.button("Refresh").clicked() {
match data.browse_state {
BrowseState::Forming => {
if !data.waiting {
forming_ev_w.send(GameFormingCallEvent);
}
}
BrowseState::InProgress => {
if !data.waiting {
mygames_ev_w.send(GameMyGamesCallEvent);
}
}
BrowseState::Finished => {
if !data.waiting {
mygames_ev_w.send(GameMyGamesCallEvent);
}
}
}
}
},
);
});
});
ui.vertical_centered(|ui| {
egui::ScrollArea::vertical().show(ui, |ui| match data.browse_state {
BrowseState::Forming => {
for game in &data.forming_games {
egui::Frame::none()
.fill(egui::Color32::BLACK)
.rounding(4.)
.outer_margin(4.)
.inner_margin(4.)
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(format!(
"Host: {}",
game.host.as_ref().unwrap().username
));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if game.host_id == global.user.as_ref().unwrap().id {
ui.add_enabled(false, egui::Button::new("Host"));
} else if ui.button("Join").clicked() {
join_ev_w.send(GameJoinCallEvent {
game_id: game.id.clone(),
});
}
});
});
});
}
}
BrowseState::InProgress => {
let mut games = data.my_games.clone();
games.retain(|g| g.state == GameState::InProgress);
for game in &games {
egui::Frame::none()
.fill(egui::Color32::BLACK)
.rounding(4.)
.outer_margin(4.)
.inner_margin(4.)
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(format!(
"Host: {}",
game.host.as_ref().unwrap().username
));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui.button("Enter").clicked() {
enter_game(&mut commands, &mut global, &game.id);
}
});
});
});
}
}
BrowseState::Finished => {
let mut games = data.my_games.clone();
games.retain(|g| g.state == GameState::Finished);
for game in &games {
egui::Frame::none()
.fill(egui::Color32::BLACK)
.rounding(4.)
.outer_margin(4.)
.inner_margin(4.)
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.label(format!(
"Host: {}",
game.host.as_ref().unwrap().username
));
});
});
}
}
}
);
});
}
fn enter_game(commands: &mut Commands, global: &mut Global, game_id: &str) {
global.cur_game_id = game_id.to_string();
commands.insert_resource(NextState(Some(MenuState::None)));
commands.insert_resource(NextState(Some(AppState::InGame)));
}