2018-12-18 00:21:59 +03:00
|
|
|
use crate::input::{ContextMenu, ModalMenuState};
|
2018-12-17 23:33:44 +03:00
|
|
|
use crate::{Canvas, Event, GfxCtx, ModalMenu, TopMenu, UserInput};
|
2019-01-14 22:29:06 +03:00
|
|
|
use abstutil::Timer;
|
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-12-19 21:51:35 +03:00
|
|
|
use piston::window::WindowSettings;
|
2019-01-14 21:24:11 +03:00
|
|
|
use std::{env, fs, panic, process};
|
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
|
2018-12-25 06:31:04 +03:00
|
|
|
fn top_menu(&self) -> Option<TopMenu> {
|
2018-12-17 05:03:46 +03:00
|
|
|
None
|
|
|
|
}
|
2018-12-17 23:33:44 +03:00
|
|
|
fn modal_menus() -> Vec<ModalMenu> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
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;
|
2019-01-14 22:16:55 +03:00
|
|
|
// TODO Migrate all callers
|
2018-12-15 23:22:00 +03:00
|
|
|
fn draw(&self, g: &mut GfxCtx, data: &T);
|
2019-01-14 22:16:55 +03:00
|
|
|
fn new_draw(&self, g: &mut GfxCtx, data: &T, _screencap: bool) {
|
2019-01-14 05:04:03 +03:00
|
|
|
self.draw(g, data);
|
|
|
|
}
|
2018-12-13 01:01:36 +03:00
|
|
|
// Will be called if event or draw panics.
|
|
|
|
fn dump_before_abort(&self) {}
|
2018-12-17 22:32:52 +03:00
|
|
|
// Only before a normal exit, like window close
|
|
|
|
fn before_quit(&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,
|
2019-01-14 05:04:03 +03:00
|
|
|
ScreenCaptureEverything { zoom: f64, max_x: f64, max_y: f64 },
|
2018-09-10 03:10:34 +03:00
|
|
|
}
|
|
|
|
|
2018-12-19 02:30:06 +03:00
|
|
|
pub fn run<T, G: GUI<T>>(mut gui: G, window_title: &str) {
|
2019-01-14 01:47:11 +03:00
|
|
|
// DPI is broken on my system; force the old behavior.
|
|
|
|
env::set_var("WINIT_HIDPI_FACTOR", "1.0");
|
|
|
|
|
2018-09-10 03:10:34 +03:00
|
|
|
let opengl = OpenGL::V3_2;
|
2018-12-21 20:49:46 +03:00
|
|
|
let settings = WindowSettings::new(
|
|
|
|
window_title,
|
|
|
|
[
|
|
|
|
gui.get_mut_canvas().window_width as u32,
|
|
|
|
gui.get_mut_canvas().window_height as u32,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.opengl(opengl)
|
|
|
|
.exit_on_esc(false)
|
|
|
|
// TODO it'd be cool to dynamically tweak antialiasing settings as we zoom in
|
|
|
|
.samples(2)
|
|
|
|
.srgb(false);
|
2018-09-10 03:10:34 +03:00
|
|
|
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);
|
|
|
|
|
2018-12-17 23:33:44 +03:00
|
|
|
// TODO Probably time to bundle this state up. :)
|
2018-09-10 03:10:34 +03:00
|
|
|
let mut last_event_mode = EventLoopMode::InputOnly;
|
2018-12-17 02:53:06 +03:00
|
|
|
let mut context_menu = ContextMenu::Inactive;
|
2018-12-25 06:31:04 +03:00
|
|
|
let mut top_menu = gui.top_menu();
|
2018-12-18 00:21:59 +03:00
|
|
|
let mut modal_state = ModalMenuState::new(G::modal_menus());
|
2018-12-15 23:22:00 +03:00
|
|
|
let mut last_data: Option<T> = None;
|
2019-01-14 05:04:03 +03:00
|
|
|
let mut screen_cap: Option<ScreenCaptureState> = None;
|
2018-12-17 23:33:44 +03:00
|
|
|
|
2018-09-10 03:10:34 +03:00
|
|
|
while let Some(ev) = events.next(&mut window) {
|
2018-12-17 22:32:52 +03:00
|
|
|
use piston::input::{CloseEvent, 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-19 21:51:35 +03:00
|
|
|
gui.get_mut_canvas().start_drawing(&mut g);
|
2018-12-16 03:00:34 +03:00
|
|
|
|
2019-01-14 05:04:03 +03:00
|
|
|
if let Err(err) = panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2019-01-14 22:16:55 +03:00
|
|
|
gui.new_draw(&mut g, data, screen_cap.is_some());
|
2019-01-14 05:04:03 +03:00
|
|
|
})) {
|
2018-12-15 23:22:00 +03:00
|
|
|
gui.dump_before_abort();
|
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
|
|
|
|
2019-01-14 05:04:03 +03:00
|
|
|
if screen_cap.is_none() {
|
|
|
|
// Always draw the menus last.
|
|
|
|
if let Some(ref menu) = top_menu {
|
|
|
|
menu.draw(&mut g, gui.get_mut_canvas());
|
|
|
|
}
|
|
|
|
for (_, ref menu) in &modal_state.active {
|
|
|
|
menu.draw(&mut g, gui.get_mut_canvas());
|
|
|
|
}
|
|
|
|
if let ContextMenu::Displaying(ref menu) = context_menu {
|
|
|
|
menu.draw(&mut g, gui.get_mut_canvas());
|
|
|
|
}
|
2018-12-15 23:22:00 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-17 22:32:52 +03:00
|
|
|
} else if ev.close_args().is_some() {
|
|
|
|
gui.before_quit();
|
|
|
|
process::exit(0);
|
2018-12-15 23:22:00 +03:00
|
|
|
} else {
|
2018-12-16 05:27:43 +03:00
|
|
|
// Skip some events.
|
|
|
|
use piston::input::{
|
2018-12-23 03:58:36 +03:00
|
|
|
AfterRenderEvent, FocusEvent, IdleEvent, MouseRelativeEvent, TextEvent,
|
2018-12-16 05:27:43 +03:00
|
|
|
};
|
2019-01-14 05:04:03 +03:00
|
|
|
if ev.after_render_args().is_some() {
|
|
|
|
// Do this after we draw and flush to the screen.
|
2019-01-14 21:24:11 +03:00
|
|
|
// TODO The very first time we grab is wrong. But waiting for one round of draw
|
|
|
|
// also didn't seem to work...
|
2019-01-14 05:04:03 +03:00
|
|
|
if let Some(ref mut cap) = screen_cap {
|
2019-01-14 22:29:06 +03:00
|
|
|
cap.timer.next();
|
2019-01-14 21:14:14 +03:00
|
|
|
if !process::Command::new("scrot")
|
2019-01-14 22:29:06 +03:00
|
|
|
.args(&[
|
|
|
|
"--quality",
|
|
|
|
"100",
|
|
|
|
"--focused",
|
|
|
|
"--silent",
|
|
|
|
&format!("screen{:02}x{:02}.png", cap.tile_x, cap.tile_y),
|
|
|
|
])
|
2019-01-14 20:41:37 +03:00
|
|
|
.status()
|
|
|
|
.unwrap()
|
|
|
|
.success()
|
|
|
|
{
|
2019-01-14 21:14:14 +03:00
|
|
|
println!("scrot failed; aborting");
|
2019-01-14 20:41:37 +03:00
|
|
|
screen_cap = None;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-14 05:04:03 +03:00
|
|
|
let canvas = gui.get_mut_canvas();
|
|
|
|
cap.tile_x += 1;
|
2019-01-14 20:56:12 +03:00
|
|
|
canvas.cam_x += canvas.window_width;
|
2019-01-14 05:04:03 +03:00
|
|
|
if (canvas.cam_x + canvas.window_width) / canvas.cam_zoom >= cap.max_x {
|
2019-01-14 20:56:12 +03:00
|
|
|
cap.tile_x = 1;
|
2019-01-14 05:04:03 +03:00
|
|
|
canvas.cam_x = 0.0;
|
|
|
|
cap.tile_y += 1;
|
2019-01-14 20:56:12 +03:00
|
|
|
canvas.cam_y += canvas.window_height;
|
2019-01-14 05:04:03 +03:00
|
|
|
if (canvas.cam_y + canvas.window_height) / canvas.cam_zoom >= cap.max_y {
|
|
|
|
let canvas = gui.get_mut_canvas();
|
|
|
|
canvas.cam_zoom = cap.orig_zoom;
|
|
|
|
canvas.cam_x = cap.orig_x;
|
|
|
|
canvas.cam_y = cap.orig_y;
|
2019-01-14 22:29:06 +03:00
|
|
|
screen_cap.take().unwrap().combine();
|
2019-01-14 05:04:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if screen_cap.is_some() {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-17 22:36:14 +03:00
|
|
|
if ev.after_render_args().is_some()
|
|
|
|
|| ev.focus_args().is_some()
|
|
|
|
|| ev.idle_args().is_some()
|
2018-12-16 05:27:43 +03:00
|
|
|
|| ev.mouse_relative_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,
|
2018-12-17 21:09:59 +03:00
|
|
|
top_menu,
|
2018-12-18 00:21:59 +03:00
|
|
|
modal_state,
|
2018-12-16 05:27:43 +03:00
|
|
|
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-17 21:09:59 +03:00
|
|
|
top_menu = input.top_menu;
|
2018-12-18 00:21:59 +03:00
|
|
|
modal_state = input.modal_state;
|
2018-12-17 20:20:44 +03:00
|
|
|
if let Some(action) = input.chosen_action {
|
|
|
|
panic!(
|
2018-12-18 00:21:59 +03:00
|
|
|
"\"{}\" chosen from the top or modal menu, but nothing consumed it",
|
2018-12-17 20:20:44 +03:00
|
|
|
action
|
|
|
|
);
|
|
|
|
}
|
2018-12-25 20:44:12 +03:00
|
|
|
let mut still_active = Vec::new();
|
|
|
|
for (mode, menu) in modal_state.active.into_iter() {
|
|
|
|
if input.set_mode_called.contains(&mode) {
|
|
|
|
still_active.push((mode, menu));
|
|
|
|
}
|
2018-12-18 03:08:32 +03:00
|
|
|
}
|
2018-12-25 20:44:12 +03:00
|
|
|
modal_state.active = still_active;
|
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;
|
2019-01-14 05:04:03 +03:00
|
|
|
|
|
|
|
if let EventLoopMode::ScreenCaptureEverything { zoom, max_x, max_y } =
|
|
|
|
new_event_mode
|
|
|
|
{
|
|
|
|
let canvas = gui.get_mut_canvas();
|
2019-01-14 22:29:06 +03:00
|
|
|
let num_tiles_x = (max_x * zoom / canvas.window_width).floor() as usize;
|
|
|
|
let num_tiles_y = (max_y * zoom / canvas.window_height).floor() as usize;
|
|
|
|
let mut timer = Timer::new("capturing screen");
|
|
|
|
timer.start_iter("capturing images", num_tiles_x * num_tiles_y);
|
2019-01-14 05:04:03 +03:00
|
|
|
screen_cap = Some(ScreenCaptureState {
|
2019-01-14 20:56:12 +03:00
|
|
|
tile_x: 1,
|
|
|
|
tile_y: 1,
|
2019-01-14 22:29:06 +03:00
|
|
|
timer,
|
|
|
|
num_tiles_x,
|
|
|
|
num_tiles_y,
|
2019-01-14 05:04:03 +03:00
|
|
|
max_x,
|
|
|
|
max_y,
|
|
|
|
orig_zoom: canvas.cam_zoom,
|
|
|
|
orig_x: canvas.cam_x,
|
|
|
|
orig_y: canvas.cam_y,
|
|
|
|
});
|
|
|
|
canvas.cam_x = 0.0;
|
|
|
|
canvas.cam_y = 0.0;
|
|
|
|
canvas.cam_zoom = zoom;
|
|
|
|
events.set_lazy(false);
|
|
|
|
}
|
2018-12-15 23:22:00 +03:00
|
|
|
}
|
2018-09-10 03:10:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 05:04:03 +03:00
|
|
|
|
|
|
|
struct ScreenCaptureState {
|
|
|
|
tile_x: usize,
|
|
|
|
tile_y: usize,
|
2019-01-14 22:29:06 +03:00
|
|
|
timer: Timer,
|
2019-01-14 05:04:03 +03:00
|
|
|
|
2019-01-14 20:56:12 +03:00
|
|
|
num_tiles_x: usize,
|
|
|
|
num_tiles_y: usize,
|
2019-01-14 05:04:03 +03:00
|
|
|
max_x: f64,
|
|
|
|
max_y: f64,
|
|
|
|
orig_zoom: f64,
|
|
|
|
orig_x: f64,
|
|
|
|
orig_y: f64,
|
|
|
|
}
|
2019-01-14 21:24:11 +03:00
|
|
|
|
|
|
|
impl ScreenCaptureState {
|
2019-01-14 22:29:06 +03:00
|
|
|
fn combine(mut self) {
|
|
|
|
self.timer.start("combining tiles");
|
2019-01-14 21:24:11 +03:00
|
|
|
let mut args = Vec::new();
|
|
|
|
for y in 1..=self.num_tiles_y {
|
|
|
|
for x in 1..=self.num_tiles_x {
|
|
|
|
args.push(format!("screen{:02}x{:02}.png", x, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args.push("-mode".to_string());
|
|
|
|
args.push("Concatenate".to_string());
|
|
|
|
args.push("-tile".to_string());
|
|
|
|
args.push(format!("{}x{}", self.num_tiles_x, self.num_tiles_y));
|
|
|
|
args.push("screencap.png".to_string());
|
2019-01-14 22:29:06 +03:00
|
|
|
if !process::Command::new("montage")
|
2019-01-14 21:24:11 +03:00
|
|
|
.args(&args)
|
|
|
|
.status()
|
|
|
|
.unwrap()
|
2019-01-14 22:29:06 +03:00
|
|
|
.success()
|
|
|
|
{
|
|
|
|
// Amp up limits in /etc/ImageMagick-6/policy.xml
|
|
|
|
println!("Combining tiles failed!");
|
|
|
|
println!("> montage {}", args.join(" "));
|
|
|
|
return;
|
|
|
|
}
|
2019-01-14 21:24:11 +03:00
|
|
|
|
|
|
|
for x in 1..=self.num_tiles_x {
|
|
|
|
for y in 1..=self.num_tiles_y {
|
|
|
|
fs::remove_file(format!("screen{:02}x{:02}.png", x, y)).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 22:29:06 +03:00
|
|
|
self.timer.stop("combining tiles");
|
|
|
|
self.timer.done();
|
|
|
|
println!("Produced screencap.png");
|
2019-01-14 21:24:11 +03:00
|
|
|
}
|
|
|
|
}
|