2019-11-29 09:41:08 +03:00
|
|
|
use crate::assets::Assets;
|
2019-12-12 03:08:58 +03:00
|
|
|
use crate::{text, widgets, Canvas, Event, EventCtx, GfxCtx, Key, Prerender, UserInput};
|
2019-01-23 01:34:11 +03:00
|
|
|
use glium::glutin;
|
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-07-26 17:31:56 +03:00
|
|
|
use std::{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);
|
|
|
|
|
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-11-29 09:41:08 +03:00
|
|
|
assets: Assets,
|
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.
|
2019-05-05 02:45:23 +03:00
|
|
|
fn event(
|
|
|
|
mut self,
|
|
|
|
ev: Event,
|
|
|
|
prerender: &Prerender,
|
|
|
|
program: &glium::Program,
|
|
|
|
) -> (State<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.
|
2019-12-12 03:17:15 +03:00
|
|
|
let mut input = UserInput::new(ev, &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-12-12 03:08:58 +03:00
|
|
|
|
|
|
|
// Update some ezgui state that's stashed in Canvas for sad reasons.
|
|
|
|
{
|
|
|
|
canvas.button_tooltip = None;
|
|
|
|
|
|
|
|
if let Event::WindowResized(width, height) = input.event {
|
|
|
|
canvas.window_width = width;
|
|
|
|
canvas.window_height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.event == Event::KeyPress(Key::LeftControl) {
|
|
|
|
canvas.lctrl_held = true;
|
|
|
|
}
|
|
|
|
if input.event == Event::KeyRelease(Key::LeftControl) {
|
|
|
|
canvas.lctrl_held = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(pt) = input.get_moved_mouse() {
|
|
|
|
canvas.cursor_x = pt.x;
|
|
|
|
canvas.cursor_y = pt.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.event == Event::WindowGainedCursor {
|
|
|
|
canvas.window_has_cursor = true;
|
|
|
|
}
|
|
|
|
if input.window_lost_cursor() {
|
|
|
|
canvas.window_has_cursor = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 09:41:08 +03:00
|
|
|
let assets = self.assets;
|
2019-04-21 23:07:00 +03:00
|
|
|
let event_mode = match panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2019-04-24 07:23:47 +03:00
|
|
|
gui.event(&mut EventCtx {
|
2019-02-01 03:43:09 +03:00
|
|
|
input: &mut input,
|
|
|
|
canvas: &mut canvas,
|
2019-11-29 09:41:08 +03:00
|
|
|
assets: &assets,
|
2019-02-06 01:43:46 +03:00
|
|
|
prerender,
|
2019-05-05 02:45:23 +03:00
|
|
|
program,
|
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-11-29 09:41:08 +03:00
|
|
|
self.assets = assets;
|
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-01-14 23:33:24 +03:00
|
|
|
|
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-05-03 22:49:27 +03:00
|
|
|
let mut g = GfxCtx::new(
|
|
|
|
&self.canvas,
|
|
|
|
&prerender,
|
|
|
|
&mut target,
|
|
|
|
program,
|
2019-11-29 09:41:08 +03:00
|
|
|
&self.assets,
|
2019-05-03 22:49:27 +03:00
|
|
|
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
|
|
|
|
2019-07-26 16:52:10 +03:00
|
|
|
// Flush text just once, so that GlyphBrush's internal caching works. We have to assume
|
|
|
|
// nothing will ever cover up text.
|
2019-08-08 22:56:29 +03:00
|
|
|
{
|
|
|
|
let top_left = self
|
|
|
|
.canvas
|
|
|
|
.screen_to_map(crate::screen_geom::ScreenPt::new(0.0, 0.0));
|
|
|
|
let bottom_right = self.canvas.screen_to_map(crate::screen_geom::ScreenPt::new(
|
|
|
|
self.canvas.window_width,
|
|
|
|
self.canvas.window_height,
|
|
|
|
));
|
|
|
|
let transform = ortho(
|
|
|
|
(top_left.x() as f32, bottom_right.x() as f32),
|
|
|
|
(top_left.y() as f32, bottom_right.y() as f32),
|
2019-11-11 05:56:00 +03:00
|
|
|
text::SCALE_DOWN,
|
2019-08-08 22:56:29 +03:00
|
|
|
);
|
2019-11-29 09:41:08 +03:00
|
|
|
self.assets
|
2019-08-08 22:56:29 +03:00
|
|
|
.mapspace_glyphs
|
|
|
|
.borrow_mut()
|
|
|
|
.draw_queued_with_transform(transform, display, &mut target);
|
|
|
|
}
|
2019-08-15 02:19:54 +03:00
|
|
|
// The depth buffer doesn't seem to work between mapspace_glyphs and screenspace_glyphs. :\
|
|
|
|
// So draw screenspace_glyphs last.
|
2019-11-11 05:56:00 +03:00
|
|
|
{
|
|
|
|
let transform = ortho(
|
|
|
|
(0.0, self.canvas.window_width as f32),
|
|
|
|
(0.0, self.canvas.window_height as f32),
|
|
|
|
1.0,
|
|
|
|
);
|
2019-11-29 09:41:08 +03:00
|
|
|
self.assets
|
2019-11-11 05:56:00 +03:00
|
|
|
.screenspace_glyphs
|
|
|
|
.borrow_mut()
|
|
|
|
.draw_queued_with_transform(transform, display, &mut target);
|
|
|
|
}
|
2019-07-26 16:52:10 +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-10-07 19:45:53 +03:00
|
|
|
pub struct Settings {
|
|
|
|
window_title: String,
|
|
|
|
initial_dims: (f64, f64),
|
|
|
|
profiling_enabled: bool,
|
2019-10-07 20:39:09 +03:00
|
|
|
default_font_size: usize,
|
2019-12-17 06:09:02 +03:00
|
|
|
override_hidpi_factor: Option<f64>,
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
pub fn new(window_title: &str, initial_dims: (f64, f64)) -> Settings {
|
|
|
|
Settings {
|
|
|
|
window_title: window_title.to_string(),
|
|
|
|
initial_dims,
|
|
|
|
profiling_enabled: false,
|
2019-10-07 20:39:09 +03:00
|
|
|
default_font_size: 30,
|
2019-12-17 06:09:02 +03:00
|
|
|
override_hidpi_factor: None,
|
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
|
|
|
|
|
|
|
pub fn default_font_size(&mut self, size: usize) {
|
|
|
|
self.default_font_size = size;
|
|
|
|
}
|
2019-12-17 06:09:02 +03:00
|
|
|
|
|
|
|
pub fn override_hidpi_factor(&mut self, override_hidpi_factor: f64) {
|
|
|
|
self.override_hidpi_factor = Some(override_hidpi_factor);
|
|
|
|
}
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run<G: GUI, F: FnOnce(&mut EventCtx) -> G>(settings: Settings, make_gui: F) {
|
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()
|
2019-10-07 19:45:53 +03:00
|
|
|
.with_title(settings.window_title)
|
2019-11-11 05:56:00 +03:00
|
|
|
.with_dimensions(glutin::dpi::LogicalSize::new(
|
|
|
|
settings.initial_dims.0,
|
|
|
|
settings.initial_dims.1,
|
2019-07-26 17:31:56 +03:00
|
|
|
));
|
2019-08-15 02:19:54 +03:00
|
|
|
// multisampling: 2 looks bad, 4 looks fine
|
|
|
|
//
|
|
|
|
// The Z values are very simple:
|
|
|
|
// 1.0: The buffer is reset every frame
|
|
|
|
// 0.5: Map-space geometry and text
|
|
|
|
// 0.1: Screen-space text
|
|
|
|
// 0.0: Screen-space geometry
|
|
|
|
// Had weird issues with Z buffering not working as intended, so this is slightly more
|
|
|
|
// complicated than necessary to work.
|
|
|
|
let context = glutin::ContextBuilder::new()
|
|
|
|
.with_multisampling(4)
|
|
|
|
.with_depth_buffer(2);
|
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 {
|
|
|
|
panic!(
|
2019-09-11 00:13:23 +03:00
|
|
|
"GLSL 140 not supported. Try {:?} or {:?}",
|
2019-03-19 00:57:46 +03:00
|
|
|
display.get_opengl_version(),
|
|
|
|
display.get_supported_glsl_version()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-05-13 02:40:52 +03:00
|
|
|
// To quickly iterate on shaders without recompiling...
|
|
|
|
/*let mut vert = String::new();
|
|
|
|
let mut frag = String::new();
|
|
|
|
let (vertex_shader, fragment_shader) = {
|
|
|
|
use std::io::Read;
|
|
|
|
|
2019-05-13 04:05:14 +03:00
|
|
|
let mut f1 = std::fs::File:: open("../ezgui/src/assets/vertex_140.glsl").unwrap();
|
2019-05-13 02:40:52 +03:00
|
|
|
f1.read_to_string(&mut vert).unwrap();
|
|
|
|
|
2019-05-13 04:05:14 +03:00
|
|
|
let mut f2 = std::fs::File:: open("../ezgui/src/assets/fragment_140.glsl").unwrap();
|
2019-05-13 02:40:52 +03:00
|
|
|
f2.read_to_string(&mut frag).unwrap();
|
|
|
|
|
|
|
|
(&vert, &frag)
|
|
|
|
};*/
|
|
|
|
|
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-12-17 06:09:02 +03:00
|
|
|
let mut hidpi_factor = events_loop.get_primary_monitor().get_hidpi_factor();
|
2019-12-12 03:59:13 +03:00
|
|
|
println!("HiDPI factor is purportedly {}", hidpi_factor);
|
2019-12-17 06:09:02 +03:00
|
|
|
if let Some(x) = settings.override_hidpi_factor {
|
|
|
|
println!("... but overriding it to {} by flag", x);
|
|
|
|
hidpi_factor = x;
|
|
|
|
}
|
2019-12-12 03:59:13 +03:00
|
|
|
let mut canvas = Canvas::new(
|
|
|
|
settings.initial_dims.0,
|
|
|
|
settings.initial_dims.1,
|
|
|
|
hidpi_factor,
|
|
|
|
);
|
2019-11-29 09:41:08 +03:00
|
|
|
let assets = Assets::new(&display, settings.default_font_size);
|
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
|
|
|
|
2019-05-05 03:11:59 +03:00
|
|
|
let gui = make_gui(&mut EventCtx {
|
2019-12-12 03:17:15 +03:00
|
|
|
input: &mut UserInput::new(Event::NoOp, &canvas),
|
2019-05-05 03:11:59 +03:00
|
|
|
canvas: &mut canvas,
|
2019-11-29 09:41:08 +03:00
|
|
|
assets: &assets,
|
2019-05-05 03:11:59 +03:00
|
|
|
prerender: &prerender,
|
|
|
|
program: &program,
|
|
|
|
});
|
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
|
|
|
canvas,
|
2019-11-29 09:41:08 +03:00
|
|
|
assets,
|
2019-01-23 01:34:11 +03:00
|
|
|
gui,
|
|
|
|
};
|
|
|
|
|
2019-10-07 19:45:53 +03:00
|
|
|
loop_forever(
|
|
|
|
state,
|
|
|
|
events_loop,
|
|
|
|
program,
|
|
|
|
prerender,
|
|
|
|
settings.profiling_enabled,
|
|
|
|
);
|
2019-02-01 20:25:59 +03:00
|
|
|
}
|
|
|
|
|
2019-04-21 23:07:00 +03:00
|
|
|
fn loop_forever<G: GUI>(
|
|
|
|
mut state: State<G>,
|
2019-02-01 20:25:59 +03:00
|
|
|
mut events_loop: glutin::EventsLoop,
|
|
|
|
program: glium::Program,
|
2019-02-11 23:05:08 +03:00
|
|
|
prerender: Prerender,
|
2019-10-07 19:45:53 +03:00
|
|
|
profiling_enabled: bool,
|
2019-02-01 20:25:59 +03:00
|
|
|
) {
|
2019-10-07 19:45:53 +03:00
|
|
|
if 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
|
|
|
}
|
|
|
|
|
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-10-07 19:45:53 +03:00
|
|
|
if 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().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);
|
|
|
|
}
|
2019-12-12 03:59:13 +03:00
|
|
|
if let Some(ev) = Event::from_glutin_event(event, state.canvas.hidpi_factor) {
|
2019-02-06 23:30:17 +03:00
|
|
|
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-05-05 02:45:23 +03:00
|
|
|
let (new_state, mode, input_used) = state.event(event, &prerender, &program);
|
2019-03-11 22:44:27 +03:00
|
|
|
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-05-06 22:52:11 +03:00
|
|
|
// Don't draw if an event was ignored and we're not in Animation mode. Every keypress also
|
|
|
|
// fires a release event, most of which are ignored.
|
|
|
|
if any_input_used || !wait_for_events {
|
|
|
|
if any_input_used {
|
|
|
|
// But if the event caused a state-change, the drawing state might be different
|
|
|
|
// too. Need to recalculate what menu entries and such are valid. So send through
|
|
|
|
// a no-op event.
|
|
|
|
let (new_state, _, _) = state.event(Event::NoOp, &prerender, &program);
|
|
|
|
state = new_state;
|
|
|
|
}
|
2019-05-06 04:19:19 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2019-08-08 22:56:29 +03:00
|
|
|
|
2019-11-11 05:56:00 +03:00
|
|
|
fn ortho((left, right): (f32, f32), (bottom, top): (f32, f32), scale: f64) -> [[f32; 4]; 4] {
|
|
|
|
let s_x = 2.0 / (right - left) / (scale as f32);
|
|
|
|
let s_y = 2.0 / (top - bottom) / (scale as f32);
|
2019-08-08 22:56:29 +03:00
|
|
|
let t_x = -(right + left) / (right - left);
|
|
|
|
let t_y = -(top + bottom) / (top - bottom);
|
|
|
|
[
|
|
|
|
[s_x, 0.0, 0.0, 0.0],
|
|
|
|
[0.0, s_y, 0.0, 0.0],
|
|
|
|
[0.0, 0.0, 1.0, 0.0],
|
|
|
|
[t_x, t_y, 0.0, 1.0],
|
|
|
|
]
|
|
|
|
}
|