2019-11-29 09:41:08 +03:00
|
|
|
use crate::assets::Assets;
|
2020-02-07 04:29:26 +03:00
|
|
|
use crate::{widgets, Canvas, Event, EventCtx, GfxCtx, Key, Prerender, UserInput};
|
2020-01-28 20:39:44 +03:00
|
|
|
use geom::Duration;
|
2019-02-06 01:43:46 +03:00
|
|
|
use std::cell::Cell;
|
2020-02-10 21:01:49 +03:00
|
|
|
use std::panic;
|
2020-01-28 20:39:44 +03:00
|
|
|
use std::time::Instant;
|
2018-09-10 03:10:34 +03:00
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
const UPDATE_FREQUENCY: std::time::Duration = std::time::Duration::from_millis(1000 / 30);
|
2019-02-06 23:30:17 +03:00
|
|
|
|
2019-04-21 23:07:00 +03:00
|
|
|
pub trait GUI {
|
2019-04-24 07:23:47 +03:00
|
|
|
fn event(&mut self, ctx: &mut EventCtx) -> EventLoopMode;
|
2019-04-24 01:33:52 +03:00
|
|
|
fn draw(&self, g: &mut GfxCtx);
|
2018-12-13 01:01:36 +03:00
|
|
|
// Will be called if event or draw panics.
|
2019-02-01 03:43:09 +03:00
|
|
|
fn dump_before_abort(&self, _canvas: &Canvas) {}
|
2018-12-17 22:32:52 +03:00
|
|
|
// Only before a normal exit, like window close
|
2019-02-01 03:43:09 +03:00
|
|
|
fn before_quit(&self, _canvas: &Canvas) {}
|
2018-09-10 03:10:34 +03:00
|
|
|
}
|
|
|
|
|
2019-02-18 00:42:01 +03:00
|
|
|
#[derive(Clone, PartialEq)]
|
2018-09-10 03:10:34 +03:00
|
|
|
pub enum EventLoopMode {
|
|
|
|
Animation,
|
|
|
|
InputOnly,
|
2019-02-18 00:42:01 +03:00
|
|
|
ScreenCaptureEverything {
|
|
|
|
dir: String,
|
|
|
|
zoom: f64,
|
|
|
|
max_x: f64,
|
|
|
|
max_y: f64,
|
|
|
|
},
|
2019-04-15 19:50:00 +03:00
|
|
|
ScreenCaptureCurrentShot,
|
2018-09-10 03:10:34 +03:00
|
|
|
}
|
|
|
|
|
2019-04-21 23:07:00 +03:00
|
|
|
pub(crate) struct State<G: GUI> {
|
2019-02-01 20:02:51 +03:00
|
|
|
pub(crate) gui: G,
|
|
|
|
pub(crate) canvas: Canvas,
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2019-04-21 23:07:00 +03:00
|
|
|
impl<G: GUI> State<G> {
|
2019-03-11 22:44:27 +03:00
|
|
|
// The bool indicates if the input was actually used.
|
2020-02-11 03:18:31 +03:00
|
|
|
fn event(&mut self, ev: Event, prerender: &Prerender) -> (EventLoopMode, bool) {
|
2019-01-14 23:33:24 +03:00
|
|
|
// It's impossible / very unlikey we'll grab the cursor in map space before the very first
|
|
|
|
// start_drawing call.
|
2020-01-07 20:43:34 +03:00
|
|
|
let input = UserInput::new(ev, &self.canvas);
|
2019-12-12 03:08:58 +03:00
|
|
|
|
|
|
|
// Update some ezgui state that's stashed in Canvas for sad reasons.
|
|
|
|
{
|
2020-02-10 21:01:49 +03:00
|
|
|
self.canvas.button_tooltip = None;
|
2019-12-12 03:08:58 +03:00
|
|
|
|
|
|
|
if let Event::WindowResized(width, height) = input.event {
|
2020-02-11 06:41:15 +03:00
|
|
|
prerender.inner.window_resized(width, height);
|
2020-02-10 21:01:49 +03:00
|
|
|
self.canvas.window_width = width;
|
|
|
|
self.canvas.window_height = height;
|
2019-12-12 03:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if input.event == Event::KeyPress(Key::LeftControl) {
|
2020-02-10 21:01:49 +03:00
|
|
|
self.canvas.lctrl_held = true;
|
2019-12-12 03:08:58 +03:00
|
|
|
}
|
|
|
|
if input.event == Event::KeyRelease(Key::LeftControl) {
|
2020-02-10 21:01:49 +03:00
|
|
|
self.canvas.lctrl_held = false;
|
2019-12-12 03:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(pt) = input.get_moved_mouse() {
|
2020-02-10 21:01:49 +03:00
|
|
|
self.canvas.cursor_x = pt.x;
|
|
|
|
self.canvas.cursor_y = pt.y;
|
2019-12-12 03:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if input.event == Event::WindowGainedCursor {
|
2020-02-10 21:01:49 +03:00
|
|
|
self.canvas.window_has_cursor = true;
|
2019-12-12 03:08:58 +03:00
|
|
|
}
|
|
|
|
if input.window_lost_cursor() {
|
2020-02-10 21:01:49 +03:00
|
|
|
self.canvas.window_has_cursor = false;
|
2019-12-12 03:08:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
match panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
|
|
|
let mut ctx = EventCtx {
|
|
|
|
fake_mouseover: false,
|
|
|
|
input: input,
|
|
|
|
canvas: &mut self.canvas,
|
|
|
|
prerender,
|
|
|
|
};
|
|
|
|
let evloop = self.gui.event(&mut ctx);
|
|
|
|
// TODO We should always do has_been_consumed, but various hacks prevent this from being
|
|
|
|
// true. For now, just avoid the specific annoying redraw case when a KeyRelease event
|
|
|
|
// is unused.
|
|
|
|
let input_used = match ev {
|
|
|
|
Event::KeyRelease(_) => ctx.input.has_been_consumed(),
|
|
|
|
_ => true,
|
|
|
|
};
|
|
|
|
(evloop, input_used)
|
|
|
|
})) {
|
2019-01-25 20:48:33 +03:00
|
|
|
Ok(pair) => pair,
|
|
|
|
Err(err) => {
|
2020-02-10 21:01:49 +03:00
|
|
|
self.gui.dump_before_abort(&self.canvas);
|
2019-01-25 20:48:33 +03:00
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
2020-02-10 21:01:49 +03:00
|
|
|
}
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2019-02-06 01:43:46 +03:00
|
|
|
// Returns naming hint. Logically consumes the number of uploads.
|
2020-02-11 03:18:31 +03:00
|
|
|
pub(crate) fn draw(&mut self, prerender: &Prerender, screenshot: bool) -> Option<String> {
|
|
|
|
let mut g = GfxCtx::new(prerender, &self.canvas, screenshot);
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-04-21 23:07:00 +03:00
|
|
|
self.canvas.start_drawing();
|
2019-01-14 23:33:24 +03:00
|
|
|
|
2019-04-21 23:07:00 +03:00
|
|
|
if let Err(err) = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2019-04-24 01:33:52 +03:00
|
|
|
self.gui.draw(&mut g);
|
2019-04-21 23:07:00 +03:00
|
|
|
})) {
|
|
|
|
self.gui.dump_before_abort(&self.canvas);
|
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
2019-04-24 01:33:52 +03:00
|
|
|
let naming_hint = g.naming_hint.take();
|
2019-01-14 23:33:24 +03:00
|
|
|
|
2020-02-09 00:52:29 +03:00
|
|
|
if false {
|
|
|
|
println!(
|
|
|
|
"----- {} uploads, {} draw calls, {} forks -----",
|
|
|
|
g.get_num_uploads(),
|
|
|
|
g.num_draw_calls,
|
|
|
|
g.num_forks
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-11 03:18:31 +03:00
|
|
|
g.inner.finish();
|
2019-02-01 20:10:39 +03:00
|
|
|
naming_hint
|
2019-01-14 21:24:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-10-07 19:45:53 +03:00
|
|
|
pub struct Settings {
|
|
|
|
window_title: String,
|
2020-02-07 22:51:41 +03:00
|
|
|
font_dir: String,
|
2019-10-07 19:45:53 +03:00
|
|
|
profiling_enabled: bool,
|
2019-10-07 20:39:09 +03:00
|
|
|
default_font_size: usize,
|
2020-01-12 20:35:38 +03:00
|
|
|
dump_raw_events: bool,
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2020-02-07 22:51:41 +03:00
|
|
|
pub fn new(window_title: &str, font_dir: &str) -> Settings {
|
2019-10-07 19:45:53 +03:00
|
|
|
Settings {
|
|
|
|
window_title: window_title.to_string(),
|
2020-02-07 22:51:41 +03:00
|
|
|
font_dir: font_dir.to_string(),
|
2019-10-07 19:45:53 +03:00
|
|
|
profiling_enabled: false,
|
2019-10-07 20:39:09 +03:00
|
|
|
default_font_size: 30,
|
2020-01-12 20:35:38 +03:00
|
|
|
dump_raw_events: false,
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn enable_profiling(&mut self) {
|
|
|
|
assert!(!self.profiling_enabled);
|
|
|
|
self.profiling_enabled = true;
|
|
|
|
}
|
2019-10-07 20:39:09 +03:00
|
|
|
|
2020-01-12 20:35:38 +03:00
|
|
|
pub fn dump_raw_events(&mut self) {
|
|
|
|
assert!(!self.dump_raw_events);
|
|
|
|
self.dump_raw_events = true;
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:39:09 +03:00
|
|
|
pub fn default_font_size(&mut self, size: usize) {
|
|
|
|
self.default_font_size = size;
|
|
|
|
}
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
pub fn run<G: 'static + GUI, F: FnOnce(&mut EventCtx) -> G>(settings: Settings, make_gui: F) -> ! {
|
2020-02-11 03:59:02 +03:00
|
|
|
let (prerender_innards, event_loop) = crate::backend_glow::setup(&settings.window_title);
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2020-02-09 21:56:50 +03:00
|
|
|
let window_size = event_loop.primary_monitor().size();
|
|
|
|
let mut canvas = Canvas::new(window_size.width.into(), window_size.height.into());
|
2020-02-11 07:10:58 +03:00
|
|
|
prerender_innards.window_resized(canvas.window_width, canvas.window_height);
|
2019-02-06 01:43:46 +03:00
|
|
|
let prerender = Prerender {
|
2020-02-07 22:51:41 +03:00
|
|
|
assets: Assets::new(settings.default_font_size, settings.font_dir),
|
2019-02-06 01:43:46 +03:00
|
|
|
num_uploads: Cell::new(0),
|
2020-02-11 03:18:31 +03:00
|
|
|
inner: prerender_innards,
|
2019-02-06 01:43:46 +03:00
|
|
|
};
|
2019-04-03 02:18:12 +03:00
|
|
|
|
2019-05-05 03:11:59 +03:00
|
|
|
let gui = make_gui(&mut EventCtx {
|
2020-01-07 20:43:34 +03:00
|
|
|
fake_mouseover: true,
|
|
|
|
input: UserInput::new(Event::NoOp, &canvas),
|
2019-05-05 03:11:59 +03:00
|
|
|
canvas: &mut canvas,
|
|
|
|
prerender: &prerender,
|
|
|
|
});
|
2019-01-23 21:30:06 +03:00
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
let mut state = State { canvas, gui };
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
if settings.profiling_enabled {
|
2019-09-23 23:37:47 +03:00
|
|
|
#[cfg(feature = "profiler")]
|
2019-03-12 02:23:39 +03:00
|
|
|
{
|
|
|
|
cpuprofiler::PROFILER
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.start("./profile")
|
|
|
|
.unwrap();
|
|
|
|
}
|
2019-02-15 00:01:26 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
let profiling_enabled = settings.profiling_enabled;
|
|
|
|
let dump_raw_events = settings.dump_raw_events;
|
|
|
|
|
|
|
|
let mut running = true;
|
|
|
|
let mut last_update = Instant::now();
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
if dump_raw_events {
|
|
|
|
println!("Event: {:?}", event);
|
|
|
|
}
|
|
|
|
let ev = match event {
|
|
|
|
winit::event::Event::WindowEvent {
|
|
|
|
event: winit::event::WindowEvent::CloseRequested,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
// ControlFlow::Exit cleanly shuts things down, meaning on larger maps, lots of
|
|
|
|
// GPU stuff is dropped. Better to just abort violently and let the OS clean
|
|
|
|
// up.
|
|
|
|
if profiling_enabled {
|
|
|
|
#[cfg(feature = "profiler")]
|
|
|
|
{
|
|
|
|
cpuprofiler::PROFILER.lock().unwrap().stop().unwrap();
|
2019-02-15 00:01:26 +03:00
|
|
|
}
|
2020-01-12 20:35:38 +03:00
|
|
|
}
|
2020-02-10 21:01:49 +03:00
|
|
|
state.gui.before_quit(&state.canvas);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
winit::event::Event::WindowEvent { event, .. } => {
|
2020-02-09 21:56:50 +03:00
|
|
|
if let Some(ev) = Event::from_winit_event(event) {
|
2020-02-10 21:01:49 +03:00
|
|
|
ev
|
|
|
|
} else {
|
|
|
|
// Don't touch control_flow if we got an irrelevant event
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
winit::event::Event::RedrawRequested(_) => {
|
2020-02-11 03:18:31 +03:00
|
|
|
state.draw(&prerender, false);
|
2020-02-10 21:01:49 +03:00
|
|
|
prerender.num_uploads.set(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
winit::event::Event::MainEventsCleared => {
|
|
|
|
// We might've switched to InputOnly after the WaitUntil was requested.
|
|
|
|
if running {
|
|
|
|
Event::Update(Duration::realtime_elapsed(last_update))
|
|
|
|
} else {
|
|
|
|
return;
|
2019-01-23 03:13:18 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-10 21:01:49 +03:00
|
|
|
_ => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// We want a max of UPDATE_FREQUENCY between updates, so measure the update time before
|
|
|
|
// doing the work (which takes time).
|
|
|
|
if let Event::Update(_) = ev {
|
|
|
|
last_update = Instant::now();
|
|
|
|
*control_flow =
|
|
|
|
winit::event_loop::ControlFlow::WaitUntil(Instant::now() + UPDATE_FREQUENCY);
|
2019-01-23 03:13:18 +03:00
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2020-02-11 03:18:31 +03:00
|
|
|
let (mode, input_used) = state.event(ev, &prerender);
|
2020-02-10 21:01:49 +03:00
|
|
|
if input_used {
|
2020-02-11 03:18:31 +03:00
|
|
|
prerender.request_redraw();
|
2020-02-10 21:01:49 +03:00
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
match mode {
|
|
|
|
EventLoopMode::InputOnly => {
|
|
|
|
running = false;
|
|
|
|
*control_flow = winit::event_loop::ControlFlow::Wait;
|
2019-03-11 22:44:27 +03:00
|
|
|
}
|
2020-02-10 21:01:49 +03:00
|
|
|
EventLoopMode::Animation => {
|
|
|
|
// If we just unpaused, then don't act as if lots of time has passed.
|
|
|
|
if !running {
|
|
|
|
last_update = Instant::now();
|
|
|
|
*control_flow = winit::event_loop::ControlFlow::WaitUntil(
|
|
|
|
Instant::now() + UPDATE_FREQUENCY,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
running = true;
|
2020-01-28 20:39:44 +03:00
|
|
|
}
|
2020-02-10 21:01:49 +03:00
|
|
|
EventLoopMode::ScreenCaptureEverything {
|
|
|
|
dir,
|
|
|
|
zoom,
|
|
|
|
max_x,
|
|
|
|
max_y,
|
|
|
|
} => {
|
2020-02-11 03:18:31 +03:00
|
|
|
widgets::screenshot_everything(&mut state, &dir, &prerender, zoom, max_x, max_y);
|
2020-02-10 21:01:49 +03:00
|
|
|
}
|
|
|
|
EventLoopMode::ScreenCaptureCurrentShot => {
|
2020-02-11 03:18:31 +03:00
|
|
|
widgets::screenshot_current(&mut state, &prerender);
|
2020-02-10 21:01:49 +03:00
|
|
|
}
|
2019-01-23 09:30:25 +03:00
|
|
|
}
|
2020-02-10 21:01:49 +03:00
|
|
|
});
|
2019-01-23 01:34:11 +03:00
|
|
|
}
|