2018-12-18 00:21:59 +03:00
|
|
|
use crate::input::{ContextMenu, ModalMenuState};
|
2019-02-01 03:43:09 +03:00
|
|
|
use crate::{text, Canvas, Event, EventCtx, GfxCtx, ModalMenu, Prerender, 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-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-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
|
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
|
|
|
|
}
|
2018-12-17 23:33:44 +03:00
|
|
|
fn modal_menus() -> Vec<ModalMenu> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
2019-02-01 03:43:09 +03:00
|
|
|
fn event(&mut self, ctx: EventCtx) -> (EventLoopMode, T);
|
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.
|
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
|
|
|
}
|
|
|
|
|
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,
|
2019-02-01 03:43:09 +03:00
|
|
|
canvas: Canvas,
|
2019-01-14 23:33:24 +03:00
|
|
|
context_menu: ContextMenu,
|
|
|
|
top_menu: Option<TopMenu>,
|
|
|
|
modal_state: ModalMenuState,
|
|
|
|
last_data: Option<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, G: GUI<T>> State<T, G> {
|
2019-01-25 20:48:33 +03:00
|
|
|
fn event(mut self, ev: Event, display: &glium::Display) -> (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,
|
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,
|
|
|
|
prerender: &Prerender { display },
|
|
|
|
})
|
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-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-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-02-01 03:43:09 +03:00
|
|
|
let mut g = GfxCtx::new(&self.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-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(|| {
|
|
|
|
self.gui.new_draw(&mut g, data, false)
|
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-01-26 19:44:12 +03:00
|
|
|
// Always draw the menus last.
|
|
|
|
if let Some(ref menu) = self.top_menu {
|
2019-02-01 03:43:09 +03:00
|
|
|
menu.draw(&mut g);
|
2018-12-18 03:08:32 +03:00
|
|
|
}
|
2019-01-26 19:44:12 +03:00
|
|
|
for (_, ref menu) in &self.modal_state.active {
|
2019-02-01 03:43:09 +03:00
|
|
|
menu.draw(&mut g);
|
2019-01-14 23:33:24 +03:00
|
|
|
}
|
2019-01-26 19:44:12 +03:00
|
|
|
if let ContextMenu::Displaying(ref menu) = self.context_menu {
|
2019-02-01 03:43:09 +03:00
|
|
|
menu.draw(&mut g);
|
2018-12-15 23:22:00 +03:00
|
|
|
}
|
2019-01-14 23:33:24 +03:00
|
|
|
|
2019-01-26 19:44:12 +03:00
|
|
|
// Always draw text last
|
2019-02-01 03:43:09 +03:00
|
|
|
self.canvas
|
2019-01-26 19:44:12 +03:00
|
|
|
.glyphs
|
|
|
|
.borrow_mut()
|
|
|
|
.draw_queued(display, &mut target);
|
|
|
|
}
|
2019-01-14 21:24:11 +03:00
|
|
|
|
2019-01-26 19:44:12 +03:00
|
|
|
target.finish().unwrap();
|
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");
|
|
|
|
|
|
|
|
let mut events_loop = glutin::EventsLoop::new();
|
|
|
|
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-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 {
|
|
|
|
vertex_shader: include_str!("vertex.glsl"),
|
|
|
|
tessellation_control_shader: None,
|
|
|
|
tessellation_evaluation_shader: None,
|
|
|
|
geometry_shader: None,
|
|
|
|
fragment_shader: include_str!("fragment.glsl"),
|
|
|
|
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-01-23 21:30:06 +03:00
|
|
|
let dejavu: &[u8] = include_bytes!("DejaVuSans.ttf");
|
|
|
|
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);
|
|
|
|
let gui = make_gui(&mut canvas, &Prerender { display: &display });
|
2019-01-23 21:30:06 +03:00
|
|
|
|
2019-01-23 01:34:11 +03:00
|
|
|
let mut 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,
|
|
|
|
modal_state: ModalMenuState::new(G::modal_menus()),
|
|
|
|
last_data: None,
|
|
|
|
gui,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut accumulator = Duration::new(0, 0);
|
|
|
|
let mut previous_clock = Instant::now();
|
2019-01-24 22:47:46 +03:00
|
|
|
let mut wait_for_events = false;
|
2019-01-23 01:34:11 +03:00
|
|
|
loop {
|
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-24 22:47:46 +03:00
|
|
|
let any_new_events = !new_events.is_empty();
|
2019-01-23 03:13:18 +03:00
|
|
|
for event in new_events {
|
|
|
|
if event == glutin::WindowEvent::CloseRequested {
|
2019-02-01 03:43:09 +03:00
|
|
|
state.gui.before_quit(&state.canvas);
|
2019-01-23 03:13:18 +03:00
|
|
|
process::exit(0);
|
|
|
|
}
|
2019-01-26 19:44:12 +03:00
|
|
|
if let Some(ev) = Event::from_glutin_event(event) {
|
|
|
|
let (new_state, mode) = state.event(ev, &display);
|
|
|
|
state = new_state;
|
|
|
|
wait_for_events = mode == EventLoopMode::InputOnly;
|
|
|
|
if let EventLoopMode::ScreenCaptureEverything { zoom, max_x, max_y } = mode {
|
2019-02-01 03:43:09 +03:00
|
|
|
state = ScreenCaptureState::new(&mut state.canvas, zoom, max_x, max_y)
|
|
|
|
.run(state, &display, &program);
|
2019-01-23 03:13:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-01-24 22:47:46 +03:00
|
|
|
if any_new_events || !wait_for_events {
|
|
|
|
state.draw(&display, &program);
|
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
|
2019-01-24 22:47:46 +03:00
|
|
|
if !wait_for_events {
|
2019-01-25 20:48:33 +03:00
|
|
|
let (new_state, mode) = state.event(Event::Update, &display);
|
2019-01-24 22:47:46 +03:00
|
|
|
state = new_state;
|
|
|
|
wait_for_events = mode == EventLoopMode::InputOnly;
|
2019-01-23 01:34:11 +03:00
|
|
|
}
|
|
|
|
|
2019-01-24 22:47:46 +03:00
|
|
|
// TODO This isn't right at all... sleep only if nothing happened.
|
|
|
|
if !any_new_events && wait_for_events {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
thread::sleep(fixed_time_stamp - accumulator);
|
2019-01-23 09:30:25 +03:00
|
|
|
}
|
2019-01-23 01:34:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-26 19:44:12 +03:00
|
|
|
|
|
|
|
struct ScreenCaptureState {
|
|
|
|
timer: Timer,
|
|
|
|
filenames: Vec<String>,
|
|
|
|
|
|
|
|
num_tiles_x: usize,
|
|
|
|
num_tiles_y: usize,
|
|
|
|
orig_zoom: f64,
|
|
|
|
orig_x: f64,
|
|
|
|
orig_y: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ScreenCaptureState {
|
|
|
|
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);
|
|
|
|
fs::create_dir("screencap").unwrap();
|
|
|
|
let state = ScreenCaptureState {
|
|
|
|
timer,
|
|
|
|
filenames: Vec::new(),
|
|
|
|
num_tiles_x,
|
|
|
|
num_tiles_y,
|
|
|
|
orig_zoom: canvas.cam_zoom,
|
|
|
|
orig_x: canvas.cam_x,
|
|
|
|
orig_y: canvas.cam_y,
|
|
|
|
};
|
|
|
|
canvas.cam_zoom = zoom;
|
|
|
|
state
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run<T, G: GUI<T>>(
|
|
|
|
mut self,
|
2019-02-01 03:43:09 +03:00
|
|
|
mut state: State<T, G>,
|
2019-01-26 19:44:12 +03:00
|
|
|
display: &glium::Display,
|
|
|
|
program: &glium::Program,
|
2019-02-01 03:43:09 +03:00
|
|
|
) -> State<T, G> {
|
2019-01-26 19:44:12 +03:00
|
|
|
let last_data = state.last_data.as_ref().unwrap();
|
|
|
|
|
|
|
|
for tile_y in 0..self.num_tiles_y {
|
|
|
|
for tile_x in 0..self.num_tiles_x {
|
|
|
|
self.timer.next();
|
2019-02-01 03:43:09 +03:00
|
|
|
state.canvas.cam_x = (tile_x as f64) * state.canvas.window_width;
|
|
|
|
state.canvas.cam_y = (tile_y as f64) * state.canvas.window_height;
|
2019-01-26 19:44:12 +03:00
|
|
|
|
|
|
|
let mut target = display.draw();
|
2019-02-01 03:43:09 +03:00
|
|
|
let mut g = GfxCtx::new(&state.canvas, &display, &mut target, program);
|
2019-01-26 19:44:12 +03:00
|
|
|
|
2019-02-01 03:43:09 +03:00
|
|
|
let gui = state.gui;
|
2019-01-26 19:44:12 +03:00
|
|
|
let naming_hint = match panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
2019-02-01 03:43:09 +03:00
|
|
|
gui.new_draw(&mut g, last_data, true)
|
2019-01-26 19:44:12 +03:00
|
|
|
})) {
|
|
|
|
Ok(naming_hint) => naming_hint,
|
|
|
|
Err(err) => {
|
2019-02-01 03:43:09 +03:00
|
|
|
gui.dump_before_abort(&state.canvas);
|
2019-01-26 19:44:12 +03:00
|
|
|
panic::resume_unwind(err);
|
|
|
|
}
|
|
|
|
};
|
2019-02-01 03:43:09 +03:00
|
|
|
state.gui = gui;
|
2019-01-26 19:44:12 +03:00
|
|
|
target.finish().unwrap();
|
|
|
|
|
|
|
|
if !self.screencap(tile_x, tile_y, naming_hint) {
|
2019-02-01 03:43:09 +03:00
|
|
|
return state;
|
2019-01-26 19:44:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 03:43:09 +03:00
|
|
|
state.canvas.cam_zoom = self.orig_zoom;
|
|
|
|
state.canvas.cam_x = self.orig_x;
|
|
|
|
state.canvas.cam_y = self.orig_y;
|
2019-01-26 19:44:12 +03:00
|
|
|
self.finish();
|
2019-02-01 03:43:09 +03:00
|
|
|
|
|
|
|
state
|
2019-01-26 19:44:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn screencap(&mut self, tile_x: usize, tile_y: usize, mut naming_hint: Option<String>) -> bool {
|
|
|
|
let suffix = naming_hint.take().unwrap_or_else(String::new);
|
|
|
|
let filename = format!("{:02}x{:02}{}.png", tile_x + 1, tile_y + 1, suffix);
|
|
|
|
if !process::Command::new("scrot")
|
|
|
|
.args(&[
|
|
|
|
"--quality",
|
|
|
|
"100",
|
|
|
|
"--focused",
|
|
|
|
"--silent",
|
|
|
|
&format!("screencap/{}", filename),
|
|
|
|
])
|
|
|
|
.status()
|
|
|
|
.unwrap()
|
|
|
|
.success()
|
|
|
|
{
|
|
|
|
println!("scrot failed; aborting");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
self.filenames.push(filename);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(self) {
|
|
|
|
let mut args = self.filenames;
|
|
|
|
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("full.png".to_string());
|
|
|
|
|
|
|
|
let mut file = fs::File::create("screencap/combine.sh").unwrap();
|
|
|
|
writeln!(file, "#!/bin/bash\n").unwrap();
|
|
|
|
writeln!(file, "montage {}", args.join(" ")).unwrap();
|
|
|
|
writeln!(file, "rm -f combine.sh").unwrap();
|
|
|
|
}
|
|
|
|
}
|