2019-01-02 22:08:13 +00:00
|
|
|
// Library
|
|
|
|
use vek::*;
|
2019-02-16 03:01:42 +00:00
|
|
|
|
2019-01-02 22:08:13 +00:00
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
// Crate
|
|
|
|
use crate::{
|
|
|
|
PlayState,
|
2019-01-07 21:10:31 +00:00
|
|
|
PlayStateResult,
|
2019-01-02 21:25:01 +00:00
|
|
|
GlobalState,
|
2019-02-16 03:01:42 +00:00
|
|
|
window::{
|
|
|
|
Event,
|
|
|
|
Window,
|
2019-01-30 12:11:34 +00:00
|
|
|
},
|
2019-02-16 03:01:42 +00:00
|
|
|
session::SessionState,
|
2019-01-02 21:25:01 +00:00
|
|
|
};
|
|
|
|
|
2019-02-16 03:23:15 +00:00
|
|
|
// Local
|
|
|
|
use super::title_ui::TitleUi;
|
2019-02-16 03:01:42 +00:00
|
|
|
|
2019-01-30 12:11:34 +00:00
|
|
|
pub struct TitleState {
|
2019-02-16 03:01:42 +00:00
|
|
|
title_ui: TitleUi,
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
|
|
|
|
impl TitleState {
|
2019-01-11 23:18:34 +00:00
|
|
|
/// Create a new `TitleState`
|
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-02-16 03:01:42 +00:00
|
|
|
title_ui: TitleUi::new(window)
|
2019-01-30 12:11:34 +00:00
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
2019-02-16 03:01:42 +00:00
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:18:34 +00:00
|
|
|
// The background colour
|
2019-01-12 14:01:01 +00:00
|
|
|
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-01-02 21:25:01 +00:00
|
|
|
impl PlayState for TitleState {
|
2019-01-07 21:10:31 +00:00
|
|
|
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult {
|
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,
|
|
|
|
// When space is pressed, start a session
|
|
|
|
Event::Char(' ') => return PlayStateResult::Push(
|
2019-02-16 03:01:42 +00:00
|
|
|
Box::new(SessionState::new(&mut global_state.window).unwrap()), // TODO: Handle this error
|
2019-01-11 23:18:34 +00:00
|
|
|
),
|
2019-02-16 03:01:42 +00:00
|
|
|
// Pass events to ui
|
|
|
|
Event::UiEvent(input) => {
|
|
|
|
self.title_ui.handle_event(input);
|
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
// Ignore all other events
|
|
|
|
_ => {},
|
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-02-16 03:01:42 +00:00
|
|
|
self.title_ui.maintain(global_state.window.renderer_mut());
|
2019-01-30 12:11:34 +00:00
|
|
|
|
|
|
|
// Draw the UI to the screen
|
2019-02-16 03:01:42 +00:00
|
|
|
self.title_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();
|
|
|
|
global_state.window
|
|
|
|
.swap_buffers()
|
|
|
|
.expect("Failed to swap window buffers");
|
2019-01-30 12:11:34 +00:00
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 23:18:34 +00:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str { "Title" }
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|