2021-02-23 02:59:57 +03:00
|
|
|
use std::cell::Cell;
|
2020-02-10 21:01:49 +03:00
|
|
|
use std::panic;
|
2020-10-06 06:26:11 +03:00
|
|
|
|
|
|
|
use image::{GenericImageView, Pixel};
|
|
|
|
use instant::Instant;
|
2020-05-09 19:48:23 +03:00
|
|
|
use winit::window::Icon;
|
2018-09-10 03:10:34 +03:00
|
|
|
|
2020-12-08 20:56:59 +03:00
|
|
|
use abstutil::{elapsed_seconds, Timer};
|
2020-10-06 06:26:11 +03:00
|
|
|
use geom::Duration;
|
|
|
|
|
2020-10-23 03:34:59 +03:00
|
|
|
use crate::app_state::App;
|
2020-10-06 06:26:11 +03:00
|
|
|
use crate::assets::Assets;
|
|
|
|
use crate::tools::screenshot::screenshot_everything;
|
2020-10-23 03:34:59 +03:00
|
|
|
use crate::{
|
2021-05-17 23:12:45 +03:00
|
|
|
Canvas, CanvasSettings, Event, EventCtx, GfxCtx, Prerender, SharedAppState, Style, Text,
|
|
|
|
UpdateType, UserInput,
|
2020-10-23 03:34:59 +03:00
|
|
|
};
|
2020-10-06 06:26:11 +03:00
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
const UPDATE_FREQUENCY: std::time::Duration = std::time::Duration::from_millis(1000 / 30);
|
2020-12-08 20:56:59 +03:00
|
|
|
// Manually enable and then check STDOUT
|
|
|
|
const DEBUG_PERFORMANCE: bool = false;
|
2019-02-06 23:30:17 +03:00
|
|
|
|
2020-10-23 03:34:59 +03:00
|
|
|
// TODO Rename this GUI or something
|
|
|
|
pub(crate) struct State<A: SharedAppState> {
|
|
|
|
pub(crate) app: App<A>,
|
2019-02-01 20:02:51 +03:00
|
|
|
pub(crate) canvas: Canvas,
|
2020-04-05 21:57:27 +03:00
|
|
|
style: Style,
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2020-10-23 03:34:59 +03:00
|
|
|
impl<A: SharedAppState> State<A> {
|
2019-03-11 22:44:27 +03:00
|
|
|
// The bool indicates if the input was actually used.
|
2020-07-02 03:47:30 +03:00
|
|
|
fn event(&mut self, mut ev: Event, prerender: &Prerender) -> (Vec<UpdateType>, bool) {
|
2020-02-18 18:30:32 +03:00
|
|
|
if let Event::MouseWheelScroll(dx, dy) = ev {
|
2021-05-17 23:12:45 +03:00
|
|
|
if self.canvas.settings.invert_scroll {
|
2020-03-08 03:13:30 +03:00
|
|
|
ev = Event::MouseWheelScroll(-dx, -dy);
|
2020-02-18 18:30:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 20:23:28 +03:00
|
|
|
// Always reset the cursor, unless we're handling an update event. If we're hovering on a
|
|
|
|
// button, we'll discover that by plumbing through the event.
|
|
|
|
if let Event::Update(_) = ev {
|
|
|
|
} else {
|
|
|
|
prerender
|
|
|
|
.inner
|
|
|
|
.set_cursor_icon(if self.canvas.drag_canvas_from.is_some() {
|
|
|
|
// We haven't run canvas_movement() yet, so we don't know if the button has been
|
|
|
|
// released. Bit of a hack to check this here, but better behavior.
|
|
|
|
if ev == Event::LeftMouseButtonUp {
|
|
|
|
winit::window::CursorIcon::Default
|
|
|
|
} else {
|
|
|
|
winit::window::CursorIcon::Grabbing
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
winit::window::CursorIcon::Default
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-02 03:47:30 +03:00
|
|
|
// It's impossible / very unlikely we'll grab the cursor in map space before the very first
|
2019-01-14 23:33:24 +03:00
|
|
|
// 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
|
|
|
|
2020-08-27 20:37:04 +03:00
|
|
|
// Update some widgetry state that's stashed in Canvas for sad reasons.
|
2019-12-12 03:08:58 +03:00
|
|
|
{
|
2020-07-29 00:39:38 +03:00
|
|
|
if let Event::WindowResized(new_size) = input.event {
|
2021-03-11 20:48:39 +03:00
|
|
|
// On platforms like Linux, new_size jumps around when the window is first created.
|
|
|
|
// As a result, if an app has loading screens at startup and doesn't process all of
|
|
|
|
// these events, new_size may be stuck at an incorrect value during the loading.
|
|
|
|
//
|
|
|
|
// Instead, just use inner_size; it appears correct on all platforms tested.
|
2020-07-29 00:39:38 +03:00
|
|
|
let inner_size = prerender.window_size();
|
2020-09-11 00:20:35 +03:00
|
|
|
trace!(
|
2020-08-12 23:05:16 +03:00
|
|
|
"winit event says the window was resized from {}, {} to {:?}. But inner size \
|
2020-07-29 00:39:38 +03:00
|
|
|
is {:?}, so using that",
|
2020-09-11 00:29:06 +03:00
|
|
|
self.canvas.window_width,
|
|
|
|
self.canvas.window_height,
|
|
|
|
new_size,
|
|
|
|
inner_size
|
2020-06-23 01:51:42 +03:00
|
|
|
);
|
2021-03-11 20:48:39 +03:00
|
|
|
prerender.window_resized(inner_size);
|
2020-07-29 00:39:38 +03:00
|
|
|
self.canvas.window_width = inner_size.width;
|
|
|
|
self.canvas.window_height = inner_size.height;
|
2019-12-12 03:08:58 +03:00
|
|
|
}
|
|
|
|
|
2020-11-23 00:24:08 +03:00
|
|
|
if let Event::KeyPress(key) = input.event {
|
|
|
|
self.canvas.keys_held.insert(key);
|
|
|
|
} else if let Event::KeyRelease(key) = input.event {
|
|
|
|
self.canvas.keys_held.remove(&key);
|
2020-03-09 23:23:03 +03:00
|
|
|
}
|
2019-12-12 03:08:58 +03:00
|
|
|
|
|
|
|
if let Some(pt) = input.get_moved_mouse() {
|
2020-08-13 03:53:17 +03:00
|
|
|
self.canvas.cursor = pt;
|
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,
|
2021-05-14 18:32:56 +03:00
|
|
|
input,
|
2020-02-10 21:01:49 +03:00
|
|
|
canvas: &mut self.canvas,
|
|
|
|
prerender,
|
2020-04-05 21:57:27 +03:00
|
|
|
style: &mut self.style,
|
2020-07-02 03:57:43 +03:00
|
|
|
updates_requested: vec![],
|
2020-02-10 21:01:49 +03:00
|
|
|
};
|
2020-12-08 20:56:59 +03:00
|
|
|
let started = Instant::now();
|
2020-10-23 03:34:59 +03:00
|
|
|
self.app.event(&mut ctx);
|
2020-12-08 20:56:59 +03:00
|
|
|
if DEBUG_PERFORMANCE {
|
|
|
|
println!("- event() took {}s", elapsed_seconds(started));
|
|
|
|
}
|
2020-02-10 21:01:49 +03:00
|
|
|
// 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,
|
|
|
|
};
|
2020-07-02 03:47:30 +03:00
|
|
|
(ctx.updates_requested, input_used)
|
2020-02-10 21:01:49 +03:00
|
|
|
})) {
|
2019-01-25 20:48:33 +03:00
|
|
|
Ok(pair) => pair,
|
|
|
|
Err(err) => {
|
2020-10-23 03:34:59 +03:00
|
|
|
self.app.shared_app_state.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
|
|
|
}
|
|
|
|
|
2021-01-06 21:52:22 +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> {
|
2020-04-05 21:57:27 +03:00
|
|
|
let mut g = GfxCtx::new(prerender, &self.canvas, &self.style, 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
|
|
|
|
2020-12-08 20:56:59 +03:00
|
|
|
let started = Instant::now();
|
2019-04-21 23:07:00 +03:00
|
|
|
if let Err(err) = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2020-10-23 03:34:59 +03:00
|
|
|
self.app.draw(&mut g);
|
2019-04-21 23:07:00 +03:00
|
|
|
})) {
|
2020-10-23 03:34:59 +03:00
|
|
|
self.app.shared_app_state.dump_before_abort(&self.canvas);
|
2019-04-21 23:07:00 +03:00
|
|
|
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-12-08 20:56:59 +03:00
|
|
|
if DEBUG_PERFORMANCE {
|
2020-08-12 23:05:16 +03:00
|
|
|
println!(
|
2020-12-08 20:56:59 +03:00
|
|
|
"----- {} uploads, {} draw calls, {} forks. draw() took {} -----",
|
2020-02-09 00:52:29 +03:00
|
|
|
g.get_num_uploads(),
|
|
|
|
g.num_draw_calls,
|
2020-12-08 20:56:59 +03:00
|
|
|
g.num_forks,
|
|
|
|
elapsed_seconds(started)
|
2020-08-12 23:05:16 +03:00
|
|
|
);
|
2020-02-09 00:52:29 +03:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:58:51 +03:00
|
|
|
prerender.inner.draw_finished(g.inner);
|
2019-02-01 20:10:39 +03:00
|
|
|
naming_hint
|
2019-01-14 21:24:11 +03:00
|
|
|
}
|
2021-06-10 00:31:40 +03:00
|
|
|
|
|
|
|
pub(crate) fn free_memory(&mut self) {
|
|
|
|
self.app.shared_app_state.free_memory();
|
|
|
|
}
|
2019-01-14 21:24:11 +03:00
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2021-05-17 23:12:45 +03:00
|
|
|
/// Customize how widgetry works. Most of these settings can't be changed after starting.
|
2019-10-07 19:45:53 +03:00
|
|
|
pub struct Settings {
|
2021-04-02 05:31:02 +03:00
|
|
|
pub(crate) window_title: String,
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub(crate) root_dom_element_id: String,
|
|
|
|
pub(crate) assets_base_url: Option<String>,
|
|
|
|
pub(crate) assets_are_gzipped: bool,
|
2020-01-12 20:35:38 +03:00
|
|
|
dump_raw_events: bool,
|
2020-05-18 03:02:33 +03:00
|
|
|
scale_factor: Option<f64>,
|
2021-02-23 02:59:57 +03:00
|
|
|
require_minimum_width: Option<f64>,
|
2020-05-09 19:48:23 +03:00
|
|
|
window_icon: Option<String>,
|
2020-09-20 23:02:27 +03:00
|
|
|
loading_tips: Option<Text>,
|
2021-01-04 20:53:24 +03:00
|
|
|
read_svg: Box<dyn Fn(&str) -> Vec<u8>>,
|
2021-05-17 23:12:45 +03:00
|
|
|
canvas_settings: CanvasSettings,
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2021-01-04 20:53:24 +03:00
|
|
|
/// Specify the title of the window to open.
|
2020-07-07 21:09:35 +03:00
|
|
|
pub fn new(window_title: &str) -> Settings {
|
2019-10-07 19:45:53 +03:00
|
|
|
Settings {
|
|
|
|
window_title: window_title.to_string(),
|
2021-04-02 05:31:02 +03:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
root_dom_element_id: "widgetry-canvas".to_string(),
|
|
|
|
assets_base_url: None,
|
|
|
|
assets_are_gzipped: false,
|
2020-01-12 20:35:38 +03:00
|
|
|
dump_raw_events: false,
|
2020-05-18 03:02:33 +03:00
|
|
|
scale_factor: None,
|
2021-02-23 02:59:57 +03:00
|
|
|
require_minimum_width: None,
|
2020-05-09 19:48:23 +03:00
|
|
|
window_icon: None,
|
2020-09-20 23:02:27 +03:00
|
|
|
loading_tips: None,
|
2021-01-04 20:53:24 +03:00
|
|
|
read_svg: Box::new(|path| {
|
|
|
|
use std::io::Read;
|
|
|
|
|
2021-05-14 18:32:56 +03:00
|
|
|
let mut file =
|
|
|
|
std::fs::File::open(path).unwrap_or_else(|_| panic!("Couldn't read {}", path));
|
2021-01-04 20:53:24 +03:00
|
|
|
let mut buffer = Vec::new();
|
|
|
|
file.read_to_end(&mut buffer)
|
2021-05-14 18:32:56 +03:00
|
|
|
.unwrap_or_else(|_| panic!("Couldn't read all of {}", path));
|
2021-01-04 20:53:24 +03:00
|
|
|
buffer
|
|
|
|
}),
|
2021-05-17 23:12:45 +03:00
|
|
|
canvas_settings: CanvasSettings::new(),
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 20:53:24 +03:00
|
|
|
/// Log every raw winit event to the DEBUG level.
|
|
|
|
pub fn dump_raw_events(mut self) -> Self {
|
2020-01-12 20:35:38 +03:00
|
|
|
assert!(!self.dump_raw_events);
|
|
|
|
self.dump_raw_events = true;
|
2021-01-04 20:53:24 +03:00
|
|
|
self
|
2020-01-12 20:35:38 +03:00
|
|
|
}
|
|
|
|
|
2021-01-04 20:53:24 +03:00
|
|
|
/// Override the initial HiDPI scale factor from whatever winit initially detects.
|
|
|
|
pub fn scale_factor(mut self, scale_factor: f64) -> Self {
|
2020-05-18 03:02:33 +03:00
|
|
|
self.scale_factor = Some(scale_factor);
|
2021-01-04 20:53:24 +03:00
|
|
|
self
|
2020-03-02 23:02:25 +03:00
|
|
|
}
|
2020-05-09 19:48:23 +03:00
|
|
|
|
2021-04-02 05:31:02 +03:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub fn root_dom_element_id(mut self, element_id: String) -> Self {
|
|
|
|
self.root_dom_element_id = element_id;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-02-23 02:59:57 +03:00
|
|
|
/// If the screen width using the monitor's detected scale factor is below this value (in units
|
|
|
|
/// of logical pixels, not physical), then force the scale factor to be 1. If `scale_factor()`
|
|
|
|
/// has been called, always use that override. This is helpful for users with HiDPI displays at
|
|
|
|
/// low resolutions, for applications designed for screens with some minimum width. Scaling
|
|
|
|
/// down UI elements isn't ideal (since it doesn't respect the user's device settings), but
|
|
|
|
/// having panels overlap is worse.
|
|
|
|
pub fn require_minimum_width(mut self, width: f64) -> Self {
|
|
|
|
self.require_minimum_width = Some(width);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-04 20:53:24 +03:00
|
|
|
/// Sets the window icon. This should be a 32x32 image.
|
|
|
|
pub fn window_icon(mut self, path: String) -> Self {
|
2020-07-07 21:09:35 +03:00
|
|
|
self.window_icon = Some(path);
|
2021-01-04 20:53:24 +03:00
|
|
|
self
|
2020-05-09 19:48:23 +03:00
|
|
|
}
|
2020-09-20 23:02:27 +03:00
|
|
|
|
2021-01-04 20:53:24 +03:00
|
|
|
/// Sets the text that'll appear during long `ctx.loading_screen` calls. You can use this as a
|
|
|
|
/// way to entertain your users while they're waiting.
|
|
|
|
pub fn loading_tips(mut self, txt: Text) -> Self {
|
2020-09-20 23:02:27 +03:00
|
|
|
self.loading_tips = Some(txt);
|
2021-01-04 20:53:24 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-01-22 02:36:21 +03:00
|
|
|
/// When calling `Widget::draw_svg`, `ButtonBuilder::image_path`, and similar, use this function
|
|
|
|
/// to transform the filename given to the raw bytes of that SVG file. By default, this just
|
|
|
|
/// reads the file normally. You may want to override this to more conveniently locate the
|
|
|
|
/// file (transforming a short filename to a full path) or to handle reading files in WASM
|
|
|
|
/// (where regular filesystem IO doesn't work).
|
2021-01-04 20:53:24 +03:00
|
|
|
pub fn read_svg(mut self, function: Box<dyn Fn(&str) -> Vec<u8>>) -> Self {
|
|
|
|
self.read_svg = function;
|
|
|
|
self
|
2020-09-20 23:02:27 +03:00
|
|
|
}
|
2021-04-02 05:31:02 +03:00
|
|
|
|
|
|
|
pub fn assets_base_url(mut self, value: String) -> Self {
|
|
|
|
self.assets_base_url = Some(value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn assets_are_gzipped(mut self, value: bool) -> Self {
|
|
|
|
self.assets_are_gzipped = value;
|
|
|
|
self
|
|
|
|
}
|
2021-05-17 23:12:45 +03:00
|
|
|
|
|
|
|
pub fn canvas_settings(mut self, settings: CanvasSettings) -> Self {
|
|
|
|
self.canvas_settings = settings;
|
|
|
|
self
|
|
|
|
}
|
2019-10-07 19:45:53 +03:00
|
|
|
}
|
|
|
|
|
2020-10-23 03:34:59 +03:00
|
|
|
pub fn run<
|
|
|
|
A: 'static + SharedAppState,
|
|
|
|
F: FnOnce(&mut EventCtx) -> (A, Vec<Box<dyn crate::app_state::State<A>>>),
|
|
|
|
>(
|
|
|
|
settings: Settings,
|
|
|
|
make_app: F,
|
|
|
|
) -> ! {
|
2020-12-06 07:51:14 +03:00
|
|
|
let mut timer = Timer::new("setup widgetry");
|
2021-04-02 05:31:02 +03:00
|
|
|
let (prerender_innards, event_loop) = crate::backend::setup(&settings, &mut timer);
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2020-05-09 19:48:23 +03:00
|
|
|
if let Some(ref path) = settings.window_icon {
|
2020-08-08 05:49:43 +03:00
|
|
|
if !cfg!(target_arch = "wasm32") {
|
|
|
|
let image = image::open(path).unwrap();
|
|
|
|
let (width, height) = image.dimensions();
|
|
|
|
let mut rgba = Vec::with_capacity((width * height) as usize * 4);
|
|
|
|
for (_, _, pixel) in image.pixels() {
|
|
|
|
rgba.extend_from_slice(&pixel.to_rgba().0);
|
|
|
|
}
|
|
|
|
let icon = Icon::from_rgba(rgba, width, height).unwrap();
|
|
|
|
prerender_innards.set_window_icon(icon);
|
2020-05-09 19:48:23 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 00:39:38 +03:00
|
|
|
|
2021-03-19 23:00:11 +03:00
|
|
|
let mut style = Style::light_bg();
|
2021-02-24 06:30:50 +03:00
|
|
|
style.loading_tips = settings.loading_tips.unwrap_or_else(Text::new);
|
|
|
|
|
2020-07-29 00:39:38 +03:00
|
|
|
let monitor_scale_factor = prerender_innards.monitor_scale_factor();
|
2021-02-23 02:59:57 +03:00
|
|
|
let mut prerender = Prerender {
|
2021-04-02 05:31:02 +03:00
|
|
|
assets: Assets::new(
|
|
|
|
style.clone(),
|
|
|
|
settings.assets_base_url,
|
|
|
|
settings.assets_are_gzipped,
|
|
|
|
settings.read_svg,
|
|
|
|
),
|
2019-02-06 01:43:46 +03:00
|
|
|
num_uploads: Cell::new(0),
|
2020-02-11 03:18:31 +03:00
|
|
|
inner: prerender_innards,
|
2021-02-23 02:59:57 +03:00
|
|
|
scale_factor: settings.scale_factor.unwrap_or(monitor_scale_factor),
|
2019-02-06 01:43:46 +03:00
|
|
|
};
|
2021-02-23 02:59:57 +03:00
|
|
|
if let Some(min_width) = settings.require_minimum_width {
|
|
|
|
let initial_size = prerender.window_size();
|
|
|
|
if initial_size.width < min_width && settings.scale_factor.is_none() {
|
|
|
|
warn!(
|
|
|
|
"Monitor scale factor is {}, screen window is {}, but the application requires \
|
|
|
|
{}. Overriding the scale factor to 1.",
|
|
|
|
monitor_scale_factor, initial_size.width, min_width
|
|
|
|
);
|
|
|
|
prerender.scale_factor = 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 00:39:38 +03:00
|
|
|
let initial_size = prerender.window_size();
|
2021-05-17 23:12:45 +03:00
|
|
|
let mut canvas = Canvas::new(initial_size, settings.canvas_settings);
|
2020-08-12 03:21:30 +03:00
|
|
|
prerender.window_resized(initial_size);
|
2020-07-29 00:39:38 +03:00
|
|
|
|
2020-12-06 07:51:14 +03:00
|
|
|
timer.start("setup app");
|
2020-10-23 03:34:59 +03:00
|
|
|
let (shared_app_state, states) = make_app(&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,
|
2020-04-05 21:57:27 +03:00
|
|
|
style: &mut style,
|
2020-07-02 03:57:43 +03:00
|
|
|
updates_requested: vec![],
|
2019-05-05 03:11:59 +03:00
|
|
|
});
|
2020-12-06 07:51:14 +03:00
|
|
|
timer.stop("setup app");
|
2020-10-23 03:34:59 +03:00
|
|
|
let app = App {
|
|
|
|
states,
|
2021-05-14 18:32:56 +03:00
|
|
|
shared_app_state,
|
2020-10-23 03:34:59 +03:00
|
|
|
};
|
2020-12-06 07:51:14 +03:00
|
|
|
timer.done();
|
2019-01-23 21:30:06 +03:00
|
|
|
|
2021-05-14 18:32:56 +03:00
|
|
|
let mut state = State { app, canvas, style };
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2020-02-10 21:01:49 +03:00
|
|
|
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 {
|
2020-10-06 01:09:12 +03:00
|
|
|
debug!("Event: {:?}", event);
|
2020-02-10 21:01:49 +03:00
|
|
|
}
|
|
|
|
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.
|
2020-10-23 03:34:59 +03:00
|
|
|
state.app.shared_app_state.before_quit(&state.canvas);
|
2020-02-10 21:01:49 +03:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
winit::event::Event::WindowEvent { event, .. } => {
|
2020-07-29 00:39:38 +03:00
|
|
|
let scale_factor = prerender.get_scale_factor();
|
|
|
|
if let Some(ev) = Event::from_winit_event(event, scale_factor) {
|
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-07-02 03:57:43 +03:00
|
|
|
let (mut updates, input_used) = state.event(ev, &prerender);
|
2020-07-02 03:47:30 +03:00
|
|
|
|
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-07-02 03:57:43 +03:00
|
|
|
if updates.is_empty() {
|
|
|
|
updates.push(UpdateType::InputOnly);
|
|
|
|
}
|
2020-07-02 03:47:30 +03:00
|
|
|
for update in updates {
|
|
|
|
match update {
|
|
|
|
UpdateType::InputOnly => {
|
|
|
|
running = false;
|
|
|
|
*control_flow = winit::event_loop::ControlFlow::Wait;
|
2020-07-02 03:57:43 +03:00
|
|
|
}
|
2020-07-02 03:47:30 +03:00
|
|
|
UpdateType::Game => {
|
|
|
|
// 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-07-02 03:57:43 +03:00
|
|
|
}
|
|
|
|
UpdateType::Pan => {}
|
2021-01-07 03:33:53 +03:00
|
|
|
UpdateType::ScreenCaptureEverything {
|
|
|
|
dir,
|
|
|
|
zoom,
|
|
|
|
dims,
|
|
|
|
leaflet_naming,
|
|
|
|
} => {
|
|
|
|
if let Err(err) = screenshot_everything(
|
|
|
|
&mut state,
|
|
|
|
&dir,
|
|
|
|
&prerender,
|
|
|
|
zoom,
|
|
|
|
dims,
|
|
|
|
leaflet_naming,
|
|
|
|
) {
|
2021-01-06 01:58:31 +03:00
|
|
|
error!("Couldn't screenshot everything: {}", err);
|
|
|
|
}
|
2020-07-02 03:47:30 +03:00
|
|
|
}
|
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
|
|
|
}
|