2018-12-18 00:21:59 +03:00
|
|
|
use crate::input::{ContextMenu, ModalMenuState};
|
2019-02-01 20:02:51 +03:00
|
|
|
use crate::{
|
2019-04-03 02:18:12 +03:00
|
|
|
text, widgets, Canvas, Event, EventCtx, GfxCtx, HorizontalAlignment, ModalMenu, Prerender,
|
|
|
|
Text, TopMenu, UserInput, VerticalAlignment,
|
2019-02-01 20:02:51 +03:00
|
|
|
};
|
2019-01-23 01:34:11 +03:00
|
|
|
use glium::glutin;
|
2019-01-23 21:30:06 +03:00
|
|
|
use glium_glyph::glyph_brush::rusttype::Font;
|
2019-01-24 00:46:05 +03:00
|
|
|
use glium_glyph::glyph_brush::rusttype::Scale;
|
2019-01-23 21:30:06 +03:00
|
|
|
use glium_glyph::GlyphBrush;
|
2019-02-06 01:43:46 +03:00
|
|
|
use std::cell::Cell;
|
2019-01-23 01:34:11 +03:00
|
|
|
use std::time::{Duration, Instant};
|
2019-02-01 20:02:51 +03:00
|
|
|
use std::{env, panic, process, thread};
|
2018-09-10 03:10:34 +03:00
|
|
|
|
2019-02-06 23:30:17 +03:00
|
|
|
// 30fps is 1000 / 30
|
|
|
|
const SLEEP_BETWEEN_FRAMES: Duration = Duration::from_millis(33);
|
|
|
|
|
2018-12-04 20:58:34 +03:00
|
|
|
pub trait GUI<T> {
|
2018-12-17 05:03:46 +03:00
|
|
|
// Called once
|
2019-02-01 03:43:09 +03:00
|
|
|
fn top_menu(&self, _canvas: &Canvas) -> Option<TopMenu> {
|
2018-12-17 05:03:46 +03:00
|
|
|
None
|
|
|
|
}
|
2019-04-21 04:21:07 +03:00
|
|
|
fn modal_menus(&self) -> Vec<ModalMenu> {
|
2018-12-17 23:33:44 +03:00
|
|
|
Vec::new()
|
|
|
|
}
|
2019-02-01 03:43:09 +03:00
|
|
|
fn event(&mut self, ctx: EventCtx) -> (EventLoopMode, T);
|
2019-01-15 21:33:20 +03:00
|
|
|
// Return optional naming hint for screencap. TODO This API is getting gross.
|
2019-04-21 00:45:03 +03:00
|
|
|
fn draw(&self, g: &mut GfxCtx, data: &T, _screencap: bool) -> Option<String>;
|
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) {}
|
2019-02-15 00:01:26 +03:00
|
|
|
|
|
|
|
fn profiling_enabled(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
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-02-01 20:02:51 +03:00
|
|
|
pub(crate) struct State<T, G: GUI<T>> {
|
|
|
|
pub(crate) gui: G,
|
|
|
|
pub(crate) canvas: Canvas,
|
2019-01-14 23:33:24 +03:00
|
|
|
context_menu: ContextMenu,
|
|
|
|
top_menu: Option<TopMenu>,
|
|
|
|
modal_state: ModalMenuState,
|
2019-02-01 20:02:51 +03:00
|
|
|
pub(crate) last_data: Option<T>,
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, G: GUI<T>> State<T, G> {
|
2019-03-11 22:44:27 +03:00
|
|
|
// The bool indicates if the input was actually used.
|
|
|
|
fn event(mut self, ev: Event, prerender: &Prerender) -> (State<T, G>, 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.
|
|
|
|
let mut input = UserInput::new(
|
2019-01-23 03:13:18 +03:00
|
|
|
ev,
|
2019-01-14 23:33:24 +03:00
|
|
|
self.context_menu,
|
|
|
|
self.top_menu,
|
|
|
|
self.modal_state,
|
2019-02-01 03:43:09 +03:00
|
|
|
&mut self.canvas,
|
2019-01-14 23:33:24 +03:00
|
|
|
);
|
|
|
|
let mut gui = self.gui;
|
2019-02-01 03:43:09 +03:00
|
|
|
let mut canvas = self.canvas;
|
2019-01-25 20:48:33 +03:00
|
|
|
let (event_mode, data) = match panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2019-02-01 03:43:09 +03:00
|
|
|
gui.event(EventCtx {
|
|
|
|
input: &mut input,
|
|
|
|
canvas: &mut canvas,
|
2019-02-06 01:43:46 +03:00
|
|
|
prerender,
|
2019-02-01 03:43:09 +03:00
|
|
|
})
|
2019-01-25 20:48:33 +03:00
|
|
|
})) {
|
|
|
|
Ok(pair) => pair,
|
|
|
|
Err(err) => {
|
2019-02-01 03:43:09 +03:00
|
|
|
gui.dump_before_abort(&canvas);
|
2019-01-25 20:48:33 +03:00
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
|
|
|
};
|
2019-01-14 23:33:24 +03:00
|
|
|
self.gui = gui;
|
2019-02-01 03:43:09 +03:00
|
|
|
self.canvas = canvas;
|
2019-01-14 23:33:24 +03:00
|
|
|
self.last_data = Some(data);
|
2019-03-11 22:44:27 +03:00
|
|
|
// TODO We should always do has_been_consumed, but various hacks prevent this from being
|
2019-03-11 23:14:58 +03:00
|
|
|
// true. For now, just avoid the specific annoying redraw case when a KeyRelease or Update
|
|
|
|
// event is unused.
|
2019-03-11 22:44:27 +03:00
|
|
|
let input_used = match ev {
|
2019-03-11 23:14:58 +03:00
|
|
|
Event::KeyRelease(_) | Event::Update => input.has_been_consumed(),
|
2019-03-11 22:44:27 +03:00
|
|
|
_ => true,
|
|
|
|
};
|
2019-02-01 03:43:09 +03:00
|
|
|
self.context_menu = input.context_menu.maybe_build(&self.canvas);
|
2019-01-14 23:33:24 +03:00
|
|
|
self.top_menu = input.top_menu;
|
|
|
|
self.modal_state = input.modal_state;
|
|
|
|
if let Some(action) = input.chosen_action {
|
|
|
|
panic!(
|
|
|
|
"\"{}\" chosen from the top or modal menu, but nothing consumed it",
|
|
|
|
action
|
2018-12-16 05:27:43 +03:00
|
|
|
);
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
let mut still_active = Vec::new();
|
|
|
|
for (mode, menu) in self.modal_state.active.into_iter() {
|
|
|
|
if input.set_mode_called.contains(&mode) {
|
|
|
|
still_active.push((mode, menu));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.modal_state.active = still_active;
|
|
|
|
|
2019-03-11 22:44:27 +03:00
|
|
|
(self, event_mode, input_used)
|
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.
|
2019-02-01 20:10:39 +03:00
|
|
|
pub(crate) fn draw(
|
|
|
|
&mut self,
|
|
|
|
display: &glium::Display,
|
|
|
|
program: &glium::Program,
|
2019-02-11 23:05:08 +03:00
|
|
|
prerender: &Prerender,
|
2019-02-01 20:10:39 +03:00
|
|
|
screenshot: bool,
|
|
|
|
) -> Option<String> {
|
2019-01-23 01:34:11 +03:00
|
|
|
let mut target = display.draw();
|
2019-02-06 01:43:46 +03:00
|
|
|
let mut g = GfxCtx::new(&self.canvas, &prerender, &mut target, program);
|
2019-02-01 20:10:39 +03:00
|
|
|
let mut naming_hint: Option<String> = None;
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-01-14 23:33:24 +03:00
|
|
|
// If the very first event is render, then just wait.
|
|
|
|
if let Some(ref data) = self.last_data {
|
2019-02-01 03:43:09 +03:00
|
|
|
self.canvas.start_drawing();
|
2019-01-14 23:33:24 +03:00
|
|
|
|
2019-01-26 19:44:12 +03:00
|
|
|
if let Err(err) = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2019-04-21 00:45:03 +03:00
|
|
|
naming_hint = self.gui.draw(&mut g, data, screenshot);
|
2019-01-14 23:33:24 +03:00
|
|
|
})) {
|
2019-02-01 03:43:09 +03:00
|
|
|
self.gui.dump_before_abort(&self.canvas);
|
2019-01-26 19:44:12 +03:00
|
|
|
panic::resume_unwind(err);
|
2018-12-17 20:20:44 +03:00
|
|
|
}
|
2019-01-14 23:33:24 +03:00
|
|
|
|
2019-02-01 20:10:39 +03:00
|
|
|
if !screenshot {
|
|
|
|
// Always draw the menus last.
|
|
|
|
if let Some(ref menu) = self.top_menu {
|
|
|
|
menu.draw(&mut g);
|
|
|
|
}
|
|
|
|
for (_, ref menu) in &self.modal_state.active {
|
|
|
|
menu.draw(&mut g);
|
|
|
|
}
|
|
|
|
if let ContextMenu::Displaying(ref menu) = self.context_menu {
|
|
|
|
menu.draw(&mut g);
|
|
|
|
}
|
2019-01-14 23:33:24 +03:00
|
|
|
|
2019-02-01 20:10:39 +03:00
|
|
|
// Always draw text last
|
|
|
|
self.canvas
|
|
|
|
.glyphs
|
|
|
|
.borrow_mut()
|
|
|
|
.draw_queued(display, &mut target);
|
|
|
|
}
|
2019-01-26 19:44:12 +03:00
|
|
|
}
|
2019-01-14 21:24:11 +03:00
|
|
|
|
2019-01-26 19:44:12 +03:00
|
|
|
target.finish().unwrap();
|
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-02-01 03:43:09 +03:00
|
|
|
pub fn run<T, G: GUI<T>, F: FnOnce(&mut Canvas, &Prerender) -> G>(
|
2019-01-25 20:48:33 +03:00
|
|
|
window_title: &str,
|
|
|
|
initial_width: f64,
|
|
|
|
initial_height: f64,
|
|
|
|
make_gui: F,
|
|
|
|
) {
|
2019-01-23 01:34:11 +03:00
|
|
|
// DPI is broken on my system; force the old behavior.
|
|
|
|
env::set_var("WINIT_HIDPI_FACTOR", "1.0");
|
|
|
|
|
2019-02-01 20:25:59 +03:00
|
|
|
let events_loop = glutin::EventsLoop::new();
|
2019-01-23 01:34:11 +03:00
|
|
|
let window = glutin::WindowBuilder::new()
|
|
|
|
.with_title(window_title)
|
2019-01-25 20:48:33 +03:00
|
|
|
.with_dimensions(glutin::dpi::LogicalSize::new(initial_width, initial_height));
|
2019-01-23 09:47:30 +03:00
|
|
|
// 2 looks bad, 4 looks fine
|
|
|
|
let context = glutin::ContextBuilder::new().with_multisampling(4);
|
2019-01-23 01:34:11 +03:00
|
|
|
let display = glium::Display::new(window, context, &events_loop).unwrap();
|
2019-03-19 00:57:46 +03:00
|
|
|
|
|
|
|
let (vertex_shader, fragment_shader) =
|
|
|
|
if display.is_glsl_version_supported(&glium::Version(glium::Api::Gl, 1, 4)) {
|
|
|
|
(
|
|
|
|
include_str!("assets/vertex_140.glsl"),
|
|
|
|
include_str!("assets/fragment_140.glsl"),
|
|
|
|
)
|
|
|
|
} else if display.is_glsl_version_supported(&glium::Version(glium::Api::Gl, 1, 1)) {
|
|
|
|
(
|
|
|
|
include_str!("assets/vertex_110.glsl"),
|
|
|
|
include_str!("assets/fragment_110.glsl"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
panic!(
|
|
|
|
"GLSL 140 and 110 not supported. Try {:?} or {:?}",
|
|
|
|
display.get_opengl_version(),
|
|
|
|
display.get_supported_glsl_version()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-01-24 09:27:32 +03:00
|
|
|
let program = glium::Program::new(
|
2019-01-23 01:34:11 +03:00
|
|
|
&display,
|
2019-01-24 09:27:32 +03:00
|
|
|
glium::program::ProgramCreationInput::SourceCode {
|
2019-03-19 00:57:46 +03:00
|
|
|
vertex_shader,
|
2019-01-24 09:27:32 +03:00
|
|
|
tessellation_control_shader: None,
|
|
|
|
tessellation_evaluation_shader: None,
|
|
|
|
geometry_shader: None,
|
2019-03-19 00:57:46 +03:00
|
|
|
fragment_shader,
|
2019-01-24 09:27:32 +03:00
|
|
|
transform_feedback_varyings: None,
|
|
|
|
// Without this, SRGB gets enabled and post-processes the color from the fragment
|
|
|
|
// shader.
|
|
|
|
outputs_srgb: true,
|
|
|
|
uses_point_size: false,
|
|
|
|
},
|
2019-01-23 01:34:11 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2019-02-01 20:30:26 +03:00
|
|
|
let dejavu: &[u8] = include_bytes!("assets/DejaVuSans.ttf");
|
2019-01-23 21:30:06 +03:00
|
|
|
let fonts = vec![Font::from_bytes(dejavu).unwrap()];
|
2019-01-24 00:46:05 +03:00
|
|
|
let vmetrics = fonts[0].v_metrics(Scale::uniform(text::FONT_SIZE));
|
|
|
|
// TODO This works for this font, but could be more paranoid with abs()
|
2019-01-25 20:48:33 +03:00
|
|
|
let line_height = f64::from(vmetrics.ascent - vmetrics.descent + vmetrics.line_gap);
|
|
|
|
let glyphs = GlyphBrush::new(&display, fonts);
|
|
|
|
|
2019-02-01 03:43:09 +03:00
|
|
|
let mut canvas = Canvas::new(initial_width, initial_height, glyphs, line_height);
|
2019-02-06 01:43:46 +03:00
|
|
|
let prerender = Prerender {
|
|
|
|
display: &display,
|
|
|
|
num_uploads: Cell::new(0),
|
2019-02-11 22:38:43 +03:00
|
|
|
total_bytes_uploaded: Cell::new(0),
|
2019-02-06 01:43:46 +03:00
|
|
|
};
|
2019-04-03 02:18:12 +03:00
|
|
|
|
|
|
|
// Just display a loading screen. Ideally capture STDOUT during make_gui and asynchronously
|
|
|
|
// show the logs, but that's too hard.
|
|
|
|
{
|
|
|
|
let mut target = display.draw();
|
|
|
|
let mut g = GfxCtx::new(&canvas, &prerender, &mut target, &program);
|
|
|
|
g.draw_blocking_text(
|
|
|
|
Text::from_line("Loading... Check terminal for details".to_string()),
|
|
|
|
(HorizontalAlignment::Center, VerticalAlignment::Center),
|
|
|
|
);
|
|
|
|
canvas
|
|
|
|
.glyphs
|
|
|
|
.borrow_mut()
|
|
|
|
.draw_queued(&display, &mut target);
|
|
|
|
target.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
2019-02-06 01:43:46 +03:00
|
|
|
let gui = make_gui(&mut canvas, &prerender);
|
2019-01-23 21:30:06 +03:00
|
|
|
|
2019-02-01 20:25:59 +03:00
|
|
|
let state = State {
|
2019-02-01 03:43:09 +03:00
|
|
|
top_menu: gui.top_menu(&canvas),
|
|
|
|
canvas,
|
2019-01-23 01:34:11 +03:00
|
|
|
context_menu: ContextMenu::Inactive,
|
2019-04-21 04:21:07 +03:00
|
|
|
modal_state: ModalMenuState::new(gui.modal_menus()),
|
2019-01-23 01:34:11 +03:00
|
|
|
last_data: None,
|
|
|
|
gui,
|
|
|
|
};
|
|
|
|
|
2019-02-11 23:05:08 +03:00
|
|
|
loop_forever(state, events_loop, program, prerender);
|
2019-02-01 20:25:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn loop_forever<T, G: GUI<T>>(
|
|
|
|
mut state: State<T, G>,
|
|
|
|
mut events_loop: glutin::EventsLoop,
|
|
|
|
program: glium::Program,
|
2019-02-11 23:05:08 +03:00
|
|
|
prerender: Prerender,
|
2019-02-01 20:25:59 +03:00
|
|
|
) {
|
2019-02-15 00:01:26 +03:00
|
|
|
if state.gui.profiling_enabled() {
|
2019-04-19 19:16:13 +03:00
|
|
|
#[cfg(target_os = "linux")]
|
2019-03-12 02:23:39 +03:00
|
|
|
{
|
|
|
|
cpuprofiler::PROFILER
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.start("./profile")
|
|
|
|
.unwrap();
|
|
|
|
}
|
2019-02-15 00:01:26 +03:00
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:46 +03:00
|
|
|
let mut wait_for_events = false;
|
2019-02-11 23:05:08 +03:00
|
|
|
|
2019-01-23 01:34:11 +03:00
|
|
|
loop {
|
2019-02-06 23:30:17 +03:00
|
|
|
let start_frame = Instant::now();
|
|
|
|
|
|
|
|
let mut new_events: Vec<Event> = Vec::new();
|
2019-01-23 01:34:11 +03:00
|
|
|
events_loop.poll_events(|event| {
|
|
|
|
if let glutin::Event::WindowEvent { event, .. } = event {
|
2019-02-06 23:30:17 +03:00
|
|
|
if event == glutin::WindowEvent::CloseRequested {
|
2019-02-15 00:01:26 +03:00
|
|
|
if state.gui.profiling_enabled() {
|
2019-04-19 19:16:13 +03:00
|
|
|
#[cfg(target_os = "linux")]
|
2019-03-12 02:23:39 +03:00
|
|
|
{
|
|
|
|
cpuprofiler::PROFILER.lock().unwrap().stop().unwrap();
|
|
|
|
}
|
2019-02-15 00:01:26 +03:00
|
|
|
}
|
2019-02-06 23:30:17 +03:00
|
|
|
state.gui.before_quit(&state.canvas);
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
if let Some(ev) = Event::from_glutin_event(event) {
|
|
|
|
new_events.push(ev);
|
2019-01-23 03:13:18 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 23:30:17 +03:00
|
|
|
});
|
|
|
|
if !wait_for_events {
|
|
|
|
new_events.push(Event::Update);
|
2019-01-23 03:13:18 +03:00
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-03-11 22:44:27 +03:00
|
|
|
let mut any_input_used = false;
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-02-06 23:30:17 +03:00
|
|
|
for event in new_events {
|
2019-03-11 22:44:27 +03:00
|
|
|
let (new_state, mode, input_used) = state.event(event, &prerender);
|
|
|
|
if input_used {
|
|
|
|
any_input_used = true;
|
|
|
|
}
|
2019-01-24 22:47:46 +03:00
|
|
|
state = new_state;
|
|
|
|
wait_for_events = mode == EventLoopMode::InputOnly;
|
2019-04-15 19:50:00 +03:00
|
|
|
match mode {
|
|
|
|
EventLoopMode::ScreenCaptureEverything {
|
|
|
|
dir,
|
2019-02-11 23:05:08 +03:00
|
|
|
zoom,
|
|
|
|
max_x,
|
|
|
|
max_y,
|
2019-04-15 19:50:00 +03:00
|
|
|
} => {
|
|
|
|
state = widgets::screenshot_everything(
|
|
|
|
&dir,
|
|
|
|
state,
|
|
|
|
&prerender.display,
|
|
|
|
&program,
|
|
|
|
&prerender,
|
|
|
|
zoom,
|
|
|
|
max_x,
|
|
|
|
max_y,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
EventLoopMode::ScreenCaptureCurrentShot => {
|
|
|
|
widgets::screenshot_current(
|
|
|
|
&mut state,
|
|
|
|
&prerender.display,
|
|
|
|
&program,
|
|
|
|
&prerender,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
2019-01-23 01:34:11 +03:00
|
|
|
}
|
|
|
|
|
2019-03-11 22:44:27 +03:00
|
|
|
// Don't draw if an event was ignored. Every keypress also fires a release event, most of
|
|
|
|
// which are ignored.
|
|
|
|
if any_input_used {
|
2019-02-11 23:05:08 +03:00
|
|
|
state.draw(&prerender.display, &program, &prerender, false);
|
|
|
|
prerender.num_uploads.set(0);
|
2019-02-06 23:30:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Primitive event loop.
|
|
|
|
// TODO Read http://gameprogrammingpatterns.com/game-loop.html carefully.
|
|
|
|
let this_frame = Instant::now().duration_since(start_frame);
|
|
|
|
if SLEEP_BETWEEN_FRAMES > this_frame {
|
|
|
|
thread::sleep(SLEEP_BETWEEN_FRAMES - this_frame);
|
2019-01-23 09:30:25 +03:00
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
}
|
|
|
|
}
|