2020-07-22 16:46:12 +00:00
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
use crate::{
|
2020-07-23 16:54:16 +00:00
|
|
|
storage::{FileStorage, WindowSettings},
|
2020-07-22 16:46:12 +00:00
|
|
|
*,
|
|
|
|
};
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
pub use egui::{
|
|
|
|
app::{App, Backend, RunMode, Storage},
|
|
|
|
Srgba,
|
|
|
|
};
|
2020-07-23 16:54:16 +00:00
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
const EGUI_MEMORY_KEY: &str = "egui";
|
|
|
|
const WINDOW_KEY: &str = "window";
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub struct GliumBackend {
|
2020-07-22 16:46:12 +00:00
|
|
|
frame_times: egui::MovementTracker<f32>,
|
|
|
|
quit: bool,
|
2020-07-22 20:26:49 +00:00
|
|
|
run_mode: RunMode,
|
2020-09-11 06:56:47 +00:00
|
|
|
painter: Painter,
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
impl GliumBackend {
|
2020-09-11 06:56:47 +00:00
|
|
|
pub fn new(run_mode: RunMode, painter: Painter) -> Self {
|
2020-07-22 16:46:12 +00:00
|
|
|
Self {
|
|
|
|
frame_times: egui::MovementTracker::new(1000, 1.0),
|
|
|
|
quit: false,
|
2020-07-22 20:26:49 +00:00
|
|
|
run_mode,
|
2020-09-11 06:56:47 +00:00
|
|
|
painter,
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
2020-07-22 16:46:12 +00:00
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
impl Backend for GliumBackend {
|
|
|
|
fn run_mode(&self) -> RunMode {
|
2020-07-22 20:26:49 +00:00
|
|
|
self.run_mode
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
fn set_run_mode(&mut self, run_mode: RunMode) {
|
2020-07-22 20:26:49 +00:00
|
|
|
self.run_mode = run_mode;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
fn cpu_time(&self) -> f32 {
|
2020-07-22 16:46:12 +00:00
|
|
|
self.frame_times.average().unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
fn fps(&self) -> f32 {
|
2020-07-22 16:46:12 +00:00
|
|
|
1.0 / self.frame_times.mean_time_interval().unwrap_or_default()
|
|
|
|
}
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
fn quit(&mut self) {
|
|
|
|
self.quit = true;
|
|
|
|
}
|
2020-09-11 06:56:47 +00:00
|
|
|
|
|
|
|
fn new_texture_srgba_premultiplied(
|
|
|
|
&mut self,
|
|
|
|
size: (usize, usize),
|
|
|
|
pixels: &[Srgba],
|
|
|
|
) -> egui::TextureId {
|
|
|
|
self.painter.new_user_texture(size, pixels)
|
|
|
|
}
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run an egui app
|
2020-07-22 20:26:49 +00:00
|
|
|
pub fn run(
|
|
|
|
title: &str,
|
|
|
|
run_mode: RunMode,
|
2020-07-23 16:54:16 +00:00
|
|
|
mut storage: FileStorage,
|
2020-07-22 20:26:49 +00:00
|
|
|
mut app: impl App + 'static,
|
|
|
|
) -> ! {
|
2020-07-22 16:46:12 +00:00
|
|
|
let event_loop = glutin::event_loop::EventLoop::new();
|
|
|
|
let mut window = glutin::window::WindowBuilder::new()
|
|
|
|
.with_decorations(true)
|
|
|
|
.with_resizable(true)
|
|
|
|
.with_title(title)
|
|
|
|
.with_transparent(false);
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
let window_settings: Option<WindowSettings> = egui::app::get_value(&storage, WINDOW_KEY);
|
2020-07-22 16:46:12 +00:00
|
|
|
if let Some(window_settings) = &window_settings {
|
|
|
|
window = window_settings.initialize_size(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
let context = glutin::ContextBuilder::new()
|
|
|
|
.with_depth_buffer(0)
|
|
|
|
.with_srgb(true)
|
|
|
|
.with_stencil_buffer(0)
|
|
|
|
.with_vsync(true);
|
|
|
|
let display = glium::Display::new(window, context, &event_loop).unwrap();
|
|
|
|
|
|
|
|
if let Some(window_settings) = &window_settings {
|
|
|
|
window_settings.restore_positions(&display);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut ctx = egui::Context::new();
|
2020-07-23 16:54:16 +00:00
|
|
|
*ctx.memory() = egui::app::get_value(&storage, EGUI_MEMORY_KEY).unwrap_or_default();
|
2020-07-22 16:46:12 +00:00
|
|
|
|
|
|
|
let mut raw_input = make_raw_input(&display);
|
|
|
|
|
|
|
|
// used to keep track of time for animations
|
|
|
|
let start_time = Instant::now();
|
2020-09-11 06:56:47 +00:00
|
|
|
let mut runner = GliumBackend::new(run_mode, Painter::new(&display));
|
2020-07-22 16:46:12 +00:00
|
|
|
let mut clipboard = init_clipboard();
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2020-09-14 18:55:35 +00:00
|
|
|
let mut redraw = || {
|
|
|
|
let egui_start = Instant::now();
|
|
|
|
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
|
|
|
|
raw_input.seconds_since_midnight = Some(local_time_of_day());
|
|
|
|
|
|
|
|
let mut ui = ctx.begin_frame(raw_input.take());
|
|
|
|
app.ui(&mut ui, &mut runner);
|
|
|
|
let (output, paint_jobs) = ctx.end_frame();
|
|
|
|
|
|
|
|
let frame_time = (Instant::now() - egui_start).as_secs_f64() as f32;
|
|
|
|
runner.frame_times.add(raw_input.time, frame_time);
|
|
|
|
|
|
|
|
runner
|
|
|
|
.painter
|
|
|
|
.paint_jobs(&display, paint_jobs, &ctx.texture());
|
|
|
|
|
|
|
|
*control_flow = if runner.quit {
|
|
|
|
glutin::event_loop::ControlFlow::Exit
|
|
|
|
} else if runner.run_mode() == RunMode::Continuous {
|
|
|
|
display.gl_window().window().request_redraw();
|
|
|
|
glutin::event_loop::ControlFlow::Poll
|
|
|
|
} else {
|
|
|
|
if output.needs_repaint {
|
2020-07-22 20:26:49 +00:00
|
|
|
display.gl_window().window().request_redraw();
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
2020-09-14 18:55:35 +00:00
|
|
|
glutin::event_loop::ControlFlow::Wait
|
|
|
|
};
|
|
|
|
|
|
|
|
handle_output(output, &display, clipboard.as_mut());
|
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
|
|
|
// Platform-dependent event handlers to workaround a winit bug
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/987
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/1619
|
|
|
|
glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
|
|
|
|
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
2020-07-22 20:26:49 +00:00
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
glutin::event::Event::WindowEvent { event, .. } => {
|
|
|
|
input_to_egui(event, clipboard.as_mut(), &mut raw_input, control_flow);
|
2020-07-22 20:26:49 +00:00
|
|
|
display.gl_window().window().request_redraw(); // TODO: maybe only on some events?
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
glutin::event::Event::LoopDestroyed => {
|
2020-07-23 16:54:16 +00:00
|
|
|
egui::app::set_value(
|
|
|
|
&mut storage,
|
|
|
|
WINDOW_KEY,
|
|
|
|
&WindowSettings::from_display(&display),
|
|
|
|
);
|
|
|
|
egui::app::set_value(&mut storage, EGUI_MEMORY_KEY, &*ctx.memory());
|
|
|
|
app.on_exit(&mut storage);
|
|
|
|
storage.save();
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|