2019-03-04 07:28:16 +00:00
|
|
|
mod ui;
|
2019-01-02 22:08:13 +00:00
|
|
|
|
2019-03-17 17:52:54 +00:00
|
|
|
use super::char_selection::CharSelectionState;
|
2019-01-02 21:25:01 +00:00
|
|
|
use crate::{
|
2019-03-15 04:55:52 +00:00
|
|
|
window::{Event, Window},
|
|
|
|
GlobalState, PlayState, PlayStateResult,
|
2019-01-02 21:25:01 +00:00
|
|
|
};
|
2019-03-15 04:55:52 +00:00
|
|
|
use common::clock::Clock;
|
|
|
|
use std::time::Duration;
|
2019-03-17 17:52:54 +00:00
|
|
|
use ui::{Event as MainMenuEvent, MainMenuUi};
|
2019-03-15 04:55:52 +00:00
|
|
|
use vek::*;
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
const FPS: u64 = 60;
|
2019-02-16 03:01:42 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
pub struct MainMenuState {
|
|
|
|
main_menu_ui: MainMenuUi,
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
impl MainMenuState {
|
|
|
|
/// Create a new `MainMenuState`
|
2019-02-16 03:01:42 +00:00
|
|
|
pub fn new(window: &mut Window) -> Self {
|
2019-01-30 12:11:34 +00:00
|
|
|
Self {
|
2019-03-15 04:55:52 +00:00
|
|
|
main_menu_ui: MainMenuUi::new(window),
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 04:55:52 +00:00
|
|
|
// Background colour
|
|
|
|
const BG_COLOR: Rgba<f32> = Rgba {
|
|
|
|
r: 0.0,
|
|
|
|
g: 0.3,
|
|
|
|
b: 1.0,
|
|
|
|
a: 1.0,
|
|
|
|
};
|
2019-01-11 20:14:37 +00:00
|
|
|
|
2019-03-04 07:28:16 +00:00
|
|
|
impl PlayState for MainMenuState {
|
2019-01-07 21:10:31 +00:00
|
|
|
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
|
2019-03-04 07:28:16 +00:00
|
|
|
// Set up an fps clock
|
|
|
|
let mut clock = Clock::new();
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
loop {
|
|
|
|
// Handle window events
|
2019-01-07 21:10:31 +00:00
|
|
|
for event in global_state.window.fetch_events() {
|
|
|
|
match event {
|
2019-01-11 23:18:34 +00:00
|
|
|
Event::Close => return PlayStateResult::Shutdown,
|
2019-02-16 03:01:42 +00:00
|
|
|
// Pass events to ui
|
|
|
|
Event::UiEvent(input) => {
|
2019-03-04 07:28:16 +00:00
|
|
|
self.main_menu_ui.handle_event(input);
|
2019-02-16 03:01:42 +00:00
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
// Ignore all other events
|
2019-03-15 04:55:52 +00:00
|
|
|
_ => {}
|
2019-01-07 21:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2019-02-12 04:14:55 +00:00
|
|
|
global_state.window.renderer_mut().clear(BG_COLOR);
|
|
|
|
|
2019-01-30 12:11:34 +00:00
|
|
|
// Maintain the UI
|
2019-03-17 17:52:54 +00:00
|
|
|
for event in self.main_menu_ui.maintain(global_state.window.renderer_mut()) {
|
|
|
|
match event {
|
|
|
|
MainMenuEvent::LoginAttempt{ username, server_address } =>
|
|
|
|
// For now just start a new session
|
|
|
|
return PlayStateResult::Push(
|
|
|
|
Box::new(CharSelectionState::new(&mut global_state.window))
|
|
|
|
),
|
|
|
|
MainMenuEvent::Quit => return PlayStateResult::Shutdown,
|
|
|
|
}
|
2019-03-04 07:28:16 +00:00
|
|
|
}
|
2019-01-30 12:11:34 +00:00
|
|
|
|
|
|
|
// Draw the UI to the screen
|
2019-03-04 07:28:16 +00:00
|
|
|
self.main_menu_ui.render(global_state.window.renderer_mut());
|
2019-02-11 06:12:46 +00:00
|
|
|
|
2019-02-12 04:14:55 +00:00
|
|
|
// Finish the frame
|
|
|
|
global_state.window.renderer_mut().flush();
|
2019-03-15 04:55:52 +00:00
|
|
|
global_state
|
|
|
|
.window
|
2019-03-04 07:28:16 +00:00
|
|
|
.swap_buffers()
|
|
|
|
.expect("Failed to swap window buffers");
|
|
|
|
|
|
|
|
// Wait for the next tick
|
|
|
|
clock.tick(Duration::from_millis(1000 / FPS));
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
2019-03-15 04:55:52 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"Title"
|
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|