2018-12-15 02:10:33 +03:00
|
|
|
use crate::input::ContextMenu;
|
2018-12-17 05:03:46 +03:00
|
|
|
use crate::{Canvas, Event, GfxCtx, TopMenu, UserInput};
|
2018-09-10 03:10:34 +03:00
|
|
|
use glutin_window::GlutinWindow;
|
2018-12-17 01:56:28 +03:00
|
|
|
use opengl_graphics::{GlGraphics, OpenGL};
|
2018-09-10 03:10:34 +03:00
|
|
|
use piston::event_loop::{EventLoop, EventSettings, Events};
|
2018-10-06 21:50:55 +03:00
|
|
|
use piston::window::{Window, WindowSettings};
|
2018-12-13 01:01:36 +03:00
|
|
|
use std::panic;
|
2018-09-10 03:10:34 +03:00
|
|
|
|
2018-12-04 20:58:34 +03:00
|
|
|
pub trait GUI<T> {
|
2018-12-17 05:03:46 +03:00
|
|
|
// Called once
|
|
|
|
fn top_menu() -> Option<TopMenu> {
|
|
|
|
None
|
|
|
|
}
|
2018-12-15 02:10:33 +03:00
|
|
|
fn event(&mut self, input: &mut UserInput) -> (EventLoopMode, T);
|
2018-10-06 21:50:55 +03:00
|
|
|
fn get_mut_canvas(&mut self) -> &mut Canvas;
|
2018-12-15 23:22:00 +03:00
|
|
|
fn draw(&self, g: &mut GfxCtx, data: &T);
|
2018-12-13 01:01:36 +03:00
|
|
|
// Will be called if event or draw panics.
|
|
|
|
fn dump_before_abort(&self) {}
|
2018-09-10 03:10:34 +03:00
|
|
|
}
|
|
|
|
|
2018-11-12 21:44:24 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
2018-09-10 03:10:34 +03:00
|
|
|
pub enum EventLoopMode {
|
|
|
|
Animation,
|
|
|
|
InputOnly,
|
|
|
|
}
|
|
|
|
|
2018-12-04 20:58:34 +03:00
|
|
|
pub fn run<T, G: GUI<T>>(mut gui: G, window_title: &str, initial_width: u32, initial_height: u32) {
|
2018-09-10 03:10:34 +03:00
|
|
|
let opengl = OpenGL::V3_2;
|
|
|
|
let settings = WindowSettings::new(window_title, [initial_width, initial_height])
|
|
|
|
.opengl(opengl)
|
|
|
|
.exit_on_esc(false)
|
|
|
|
// TODO it'd be cool to dynamically tweak antialiasing settings as we zoom in
|
|
|
|
.samples(2)
|
|
|
|
.srgb(false);
|
|
|
|
let mut window: GlutinWindow = settings.build().expect("Could not create window");
|
|
|
|
let mut events = Events::new(EventSettings::new().lazy(true));
|
|
|
|
let mut gl = GlGraphics::new(opengl);
|
|
|
|
|
|
|
|
let mut last_event_mode = EventLoopMode::InputOnly;
|
2018-12-17 02:53:06 +03:00
|
|
|
let mut context_menu = ContextMenu::Inactive;
|
2018-12-17 06:05:18 +03:00
|
|
|
let top_menu = G::top_menu();
|
2018-12-15 23:22:00 +03:00
|
|
|
let mut last_data: Option<T> = None;
|
2018-09-10 03:10:34 +03:00
|
|
|
while let Some(ev) = events.next(&mut window) {
|
2018-12-16 05:27:43 +03:00
|
|
|
use piston::input::RenderEvent;
|
2018-09-10 03:10:34 +03:00
|
|
|
if let Some(args) = ev.render_args() {
|
2018-12-15 23:22:00 +03:00
|
|
|
// If the very first event is render, then just wait.
|
|
|
|
if let Some(ref data) = last_data {
|
|
|
|
gl.draw(args.viewport(), |c, g| {
|
2018-12-17 01:56:28 +03:00
|
|
|
let mut g = GfxCtx::new(g, c);
|
2018-12-15 23:22:00 +03:00
|
|
|
gui.get_mut_canvas()
|
|
|
|
.start_drawing(&mut g, window.draw_size());
|
2018-12-16 03:00:34 +03:00
|
|
|
|
2018-12-15 23:22:00 +03:00
|
|
|
if let Err(err) =
|
|
|
|
panic::catch_unwind(panic::AssertUnwindSafe(|| gui.draw(&mut g, data)))
|
|
|
|
{
|
|
|
|
gui.dump_before_abort();
|
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
|
|
|
|
2018-12-17 06:05:18 +03:00
|
|
|
// Always draw the top menu and context menus last.
|
|
|
|
if let Some(ref menu) = top_menu {
|
|
|
|
menu.draw(&mut g, gui.get_mut_canvas());
|
|
|
|
}
|
2018-12-17 02:53:06 +03:00
|
|
|
if let ContextMenu::Displaying(ref menu) = context_menu {
|
|
|
|
menu.draw(&mut g, gui.get_mut_canvas());
|
2018-12-15 23:22:00 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-16 05:27:43 +03:00
|
|
|
// Skip some events.
|
|
|
|
use piston::input::{
|
|
|
|
AfterRenderEvent, CursorEvent, FocusEvent, MouseRelativeEvent, ResizeEvent,
|
|
|
|
TextEvent,
|
|
|
|
};
|
|
|
|
if ev.resize_args().is_some()
|
|
|
|
|| ev.focus_args().is_some()
|
|
|
|
|| ev.cursor_args().is_some()
|
|
|
|
|| ev.mouse_relative_args().is_some()
|
|
|
|
|| ev.after_render_args().is_some()
|
|
|
|
|| ev.text_args().is_some()
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-15 23:22:00 +03:00
|
|
|
// It's impossible / very unlikey we'll grab the cursor in map space before the very first
|
|
|
|
// start_drawing call.
|
2018-12-16 05:27:43 +03:00
|
|
|
let mut input = UserInput::new(
|
|
|
|
Event::from_piston_event(ev),
|
|
|
|
context_menu,
|
|
|
|
gui.get_mut_canvas(),
|
|
|
|
);
|
2018-12-15 23:22:00 +03:00
|
|
|
let (new_event_mode, data) =
|
|
|
|
match panic::catch_unwind(panic::AssertUnwindSafe(|| gui.event(&mut input))) {
|
|
|
|
Ok(pair) => pair,
|
|
|
|
Err(err) => {
|
|
|
|
gui.dump_before_abort();
|
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
last_data = Some(data);
|
2018-12-17 02:53:06 +03:00
|
|
|
context_menu = input.context_menu.maybe_build(gui.get_mut_canvas());
|
2018-12-15 02:10:33 +03:00
|
|
|
|
2018-12-15 23:22:00 +03:00
|
|
|
// Don't constantly reset the events struct -- only when laziness changes.
|
|
|
|
if new_event_mode != last_event_mode {
|
|
|
|
events.set_lazy(new_event_mode == EventLoopMode::InputOnly);
|
|
|
|
last_event_mode = new_event_mode;
|
|
|
|
}
|
2018-09-10 03:10:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|