2018-12-18 00:21:59 +03:00
|
|
|
use crate::input::{ContextMenu, ModalMenuState};
|
2019-01-23 03:13:18 +03:00
|
|
|
use crate::{Canvas, Event, GfxCtx, ModalMenu, TopMenu, UserInput};
|
2019-01-14 22:29:06 +03:00
|
|
|
use abstutil::Timer;
|
2019-01-23 01:34:11 +03:00
|
|
|
use glium::glutin;
|
2019-01-16 04:20:42 +03:00
|
|
|
use std::io::Write;
|
2019-01-23 01:34:11 +03:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use std::{env, fs, panic, process, thread};
|
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-15 21:33:20 +03:00
|
|
|
// Return optional naming hint for screencap. TODO This API is getting gross.
|
|
|
|
fn new_draw(&self, g: &mut GfxCtx, data: &T, _screencap: bool) -> Option<String> {
|
2019-01-14 05:04:03 +03:00
|
|
|
self.draw(g, data);
|
2019-01-15 21:33:20 +03:00
|
|
|
None
|
2019-01-14 05:04:03 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-01-14 23:33:24 +03:00
|
|
|
struct State<T, G: GUI<T>> {
|
|
|
|
gui: G,
|
|
|
|
context_menu: ContextMenu,
|
|
|
|
top_menu: Option<TopMenu>,
|
|
|
|
modal_state: ModalMenuState,
|
|
|
|
last_data: Option<T>,
|
|
|
|
screen_cap: Option<ScreenCaptureState>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, G: GUI<T>> State<T, G> {
|
2019-01-23 03:13:18 +03:00
|
|
|
fn event(mut self, ev: Event) -> (State<T, G>, EventLoopMode) {
|
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,
|
|
|
|
self.gui.get_mut_canvas(),
|
|
|
|
);
|
|
|
|
let mut gui = self.gui;
|
2019-01-23 03:13:18 +03:00
|
|
|
let (event_mode, data) =
|
2019-01-14 23:33:24 +03:00
|
|
|
match panic::catch_unwind(panic::AssertUnwindSafe(|| gui.event(&mut input))) {
|
|
|
|
Ok(pair) => pair,
|
|
|
|
Err(err) => {
|
|
|
|
gui.dump_before_abort();
|
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.gui = gui;
|
|
|
|
self.last_data = Some(data);
|
|
|
|
self.context_menu = input.context_menu.maybe_build(self.gui.get_mut_canvas());
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Don't constantly reset the events struct -- only when laziness changes.
|
2019-01-23 03:13:18 +03:00
|
|
|
if let EventLoopMode::ScreenCaptureEverything { zoom, max_x, max_y } = event_mode {
|
|
|
|
self.screen_cap = Some(ScreenCaptureState::new(
|
|
|
|
self.gui.get_mut_canvas(),
|
|
|
|
zoom,
|
|
|
|
max_x,
|
|
|
|
max_y,
|
|
|
|
));
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:13:18 +03:00
|
|
|
(self, event_mode)
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 03:13:18 +03:00
|
|
|
fn draw(&mut self, display: &glium::Display, program: &glium::Program) {
|
2019-01-23 01:34:11 +03:00
|
|
|
let mut target = display.draw();
|
2019-01-23 03:13:18 +03:00
|
|
|
let mut g = GfxCtx::new(self.gui.get_mut_canvas(), &display, &mut target, program);
|
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-01-23 03:13:18 +03:00
|
|
|
self.gui.get_mut_canvas().start_drawing();
|
2019-01-14 23:33:24 +03:00
|
|
|
|
2019-01-15 21:33:20 +03:00
|
|
|
match panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2019-01-23 03:13:18 +03:00
|
|
|
self.gui.new_draw(&mut g, data, self.screen_cap.is_some())
|
2019-01-14 23:33:24 +03:00
|
|
|
})) {
|
2019-01-15 21:33:20 +03:00
|
|
|
Ok(naming_hint) => {
|
|
|
|
if let Some(ref mut cap) = self.screen_cap {
|
|
|
|
cap.naming_hint = naming_hint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
self.gui.dump_before_abort();
|
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
2018-12-17 20:20:44 +03:00
|
|
|
}
|
2019-01-14 23:33:24 +03:00
|
|
|
|
|
|
|
if self.screen_cap.is_none() {
|
|
|
|
// Always draw the menus last.
|
|
|
|
if let Some(ref menu) = self.top_menu {
|
2019-01-23 03:13:18 +03:00
|
|
|
menu.draw(&mut g, self.gui.get_mut_canvas());
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
for (_, ref menu) in &self.modal_state.active {
|
2019-01-23 03:13:18 +03:00
|
|
|
menu.draw(&mut g, self.gui.get_mut_canvas());
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
if let ContextMenu::Displaying(ref menu) = self.context_menu {
|
2019-01-23 03:13:18 +03:00
|
|
|
menu.draw(&mut g, self.gui.get_mut_canvas());
|
2018-12-25 20:44:12 +03:00
|
|
|
}
|
2018-12-18 03:08:32 +03:00
|
|
|
}
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
2019-01-23 03:13:18 +03:00
|
|
|
|
|
|
|
target.finish().unwrap();
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn after_render(&mut self) {
|
|
|
|
// Do this after we draw and flush to the screen.
|
|
|
|
// TODO The very first time we grab is wrong. But waiting for one round of draw also didn't
|
|
|
|
// seem to work...
|
|
|
|
if let Some(ref mut cap) = self.screen_cap {
|
|
|
|
cap.timer.next();
|
2019-01-15 21:33:20 +03:00
|
|
|
let suffix = cap.naming_hint.take().unwrap_or_else(String::new);
|
2019-01-16 04:20:42 +03:00
|
|
|
let filename = format!("{:02}x{:02}{}.png", cap.tile_x, cap.tile_y, suffix);
|
2019-01-14 23:33:24 +03:00
|
|
|
if !process::Command::new("scrot")
|
2019-01-16 04:20:42 +03:00
|
|
|
.args(&[
|
|
|
|
"--quality",
|
|
|
|
"100",
|
|
|
|
"--focused",
|
|
|
|
"--silent",
|
|
|
|
&format!("screencap/{}", filename),
|
|
|
|
])
|
2019-01-14 23:33:24 +03:00
|
|
|
.status()
|
|
|
|
.unwrap()
|
|
|
|
.success()
|
|
|
|
{
|
|
|
|
println!("scrot failed; aborting");
|
|
|
|
self.screen_cap = None;
|
|
|
|
return;
|
|
|
|
}
|
2019-01-15 21:33:20 +03:00
|
|
|
cap.filenames.push(filename);
|
2019-01-14 23:33:24 +03:00
|
|
|
|
|
|
|
let canvas = self.gui.get_mut_canvas();
|
|
|
|
cap.tile_x += 1;
|
|
|
|
canvas.cam_x += canvas.window_width;
|
|
|
|
if (canvas.cam_x + canvas.window_width) / canvas.cam_zoom >= cap.max_x {
|
|
|
|
cap.tile_x = 1;
|
|
|
|
canvas.cam_x = 0.0;
|
|
|
|
cap.tile_y += 1;
|
|
|
|
canvas.cam_y += canvas.window_height;
|
|
|
|
if (canvas.cam_y + canvas.window_height) / canvas.cam_zoom >= cap.max_y {
|
|
|
|
let canvas = self.gui.get_mut_canvas();
|
|
|
|
canvas.cam_zoom = cap.orig_zoom;
|
|
|
|
canvas.cam_x = cap.orig_x;
|
|
|
|
canvas.cam_y = cap.orig_y;
|
|
|
|
self.screen_cap.take().unwrap().combine();
|
2019-01-14 05:04:03 +03:00
|
|
|
}
|
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-15 21:33:20 +03:00
|
|
|
naming_hint: Option<String>,
|
|
|
|
filenames: Vec<String>,
|
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 23:33:24 +03:00
|
|
|
fn new(canvas: &mut Canvas, zoom: f64, max_x: f64, max_y: f64) -> ScreenCaptureState {
|
|
|
|
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 23:59:44 +03:00
|
|
|
fs::create_dir("screencap").unwrap();
|
2019-01-14 23:33:24 +03:00
|
|
|
let state = ScreenCaptureState {
|
|
|
|
tile_x: 1,
|
|
|
|
tile_y: 1,
|
|
|
|
timer,
|
2019-01-15 21:33:20 +03:00
|
|
|
naming_hint: None,
|
|
|
|
filenames: Vec::new(),
|
2019-01-14 23:33:24 +03:00
|
|
|
num_tiles_x,
|
|
|
|
num_tiles_y,
|
|
|
|
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;
|
|
|
|
state
|
|
|
|
}
|
|
|
|
|
2019-01-16 04:20:42 +03:00
|
|
|
fn combine(self) {
|
2019-01-15 21:33:20 +03:00
|
|
|
let mut args = self.filenames;
|
2019-01-14 21:24:11 +03:00
|
|
|
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));
|
2019-01-16 04:20:42 +03:00
|
|
|
args.push("full.png".to_string());
|
2019-01-14 21:24:11 +03:00
|
|
|
|
2019-01-16 04:20:42 +03:00
|
|
|
let mut file = fs::File::create("screencap/combine.sh").unwrap();
|
2019-01-17 19:46:31 +03:00
|
|
|
writeln!(file, "#!/bin/bash\n").unwrap();
|
|
|
|
writeln!(file, "montage {}", args.join(" ")).unwrap();
|
|
|
|
writeln!(file, "rm -f combine.sh").unwrap();
|
2019-01-14 21:24:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-01-23 03:13:18 +03:00
|
|
|
pub fn run<T, G: GUI<T>>(mut gui: G, window_title: &str) {
|
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");
|
|
|
|
|
|
|
|
let mut events_loop = glutin::EventsLoop::new();
|
|
|
|
let window = glutin::WindowBuilder::new()
|
|
|
|
.with_title(window_title)
|
|
|
|
.with_dimensions(glutin::dpi::LogicalSize::new(
|
|
|
|
gui.get_mut_canvas().window_width,
|
|
|
|
gui.get_mut_canvas().window_height,
|
|
|
|
));
|
|
|
|
let context = glutin::ContextBuilder::new().with_depth_buffer(24);
|
|
|
|
let display = glium::Display::new(window, context, &events_loop).unwrap();
|
|
|
|
let program = glium::Program::from_source(
|
|
|
|
&display,
|
|
|
|
include_str!("vertex.glsl"),
|
|
|
|
include_str!("fragment.glsl"),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut state = State {
|
|
|
|
context_menu: ContextMenu::Inactive,
|
|
|
|
top_menu: gui.top_menu(),
|
|
|
|
modal_state: ModalMenuState::new(G::modal_menus()),
|
|
|
|
last_data: None,
|
|
|
|
screen_cap: None,
|
|
|
|
gui,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut accumulator = Duration::new(0, 0);
|
|
|
|
let mut previous_clock = Instant::now();
|
2019-01-23 03:13:18 +03:00
|
|
|
let mut send_update_events = false;
|
2019-01-23 01:34:11 +03:00
|
|
|
loop {
|
2019-01-23 03:13:18 +03:00
|
|
|
state.draw(&display, &program);
|
2019-01-23 01:34:11 +03:00
|
|
|
state.after_render();
|
|
|
|
|
2019-01-23 03:13:18 +03:00
|
|
|
let mut new_events: Vec<glutin::WindowEvent> = Vec::new();
|
2019-01-23 01:34:11 +03:00
|
|
|
events_loop.poll_events(|event| {
|
|
|
|
if let glutin::Event::WindowEvent { event, .. } = event {
|
2019-01-23 03:13:18 +03:00
|
|
|
new_events.push(event);
|
2019-01-23 01:34:11 +03:00
|
|
|
}
|
|
|
|
});
|
2019-01-23 03:13:18 +03:00
|
|
|
for event in new_events {
|
|
|
|
if event == glutin::WindowEvent::CloseRequested {
|
|
|
|
state.gui.before_quit();
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
if state.screen_cap.is_none() {
|
|
|
|
if let Some(ev) = Event::from_glutin_event(event) {
|
|
|
|
let (new_state, mode) = state.event(ev);
|
|
|
|
state = new_state;
|
|
|
|
send_update_events = mode == EventLoopMode::Animation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
|
|
|
let now = Instant::now();
|
|
|
|
accumulator += now - previous_clock;
|
|
|
|
previous_clock = now;
|
|
|
|
|
|
|
|
let fixed_time_stamp = Duration::new(0, 16_666_667);
|
|
|
|
while accumulator >= fixed_time_stamp {
|
|
|
|
accumulator -= fixed_time_stamp;
|
|
|
|
// TODO send off an update event
|
|
|
|
}
|
|
|
|
|
|
|
|
thread::sleep(fixed_time_stamp - accumulator);
|
|
|
|
}
|
|
|
|
}
|