2020-07-22 16:46:12 +00:00
|
|
|
use std::time::Instant;
|
|
|
|
|
2020-10-19 18:25:05 +00:00
|
|
|
use crate::{storage::WindowSettings, *};
|
2020-07-22 16:46:12 +00:00
|
|
|
|
2020-12-29 13:15:46 +00:00
|
|
|
pub use egui::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-12-29 13:15:46 +00:00
|
|
|
impl epi::TextureAllocator for Painter {
|
2020-11-18 20:38:29 +00:00
|
|
|
fn alloc(&mut self) -> egui::TextureId {
|
|
|
|
self.alloc_user_texture()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_srgba_premultiplied(
|
2020-09-11 06:56:47 +00:00
|
|
|
&mut self,
|
2020-11-18 20:38:29 +00:00
|
|
|
id: egui::TextureId,
|
2020-09-11 06:56:47 +00:00
|
|
|
size: (usize, usize),
|
2020-11-18 20:38:29 +00:00
|
|
|
srgba_pixels: &[Srgba],
|
|
|
|
) {
|
|
|
|
self.set_user_texture(id, size, srgba_pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn free(&mut self, id: egui::TextureId) {
|
|
|
|
self.free_user_texture(id)
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 19:35:16 +00:00
|
|
|
struct RequestRepaintEvent;
|
|
|
|
|
2020-12-27 09:49:26 +00:00
|
|
|
struct GliumRepaintSignal(
|
|
|
|
std::sync::Mutex<glutin::event_loop::EventLoopProxy<RequestRepaintEvent>>,
|
|
|
|
);
|
2020-11-20 19:35:16 +00:00
|
|
|
|
2020-12-29 13:15:46 +00:00
|
|
|
impl epi::RepaintSignal for GliumRepaintSignal {
|
2020-11-20 19:35:16 +00:00
|
|
|
fn request_repaint(&self) {
|
2020-12-27 09:49:26 +00:00
|
|
|
self.0.lock().unwrap().send_event(RequestRepaintEvent).ok();
|
2020-11-20 19:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:49:44 +00:00
|
|
|
fn create_display(
|
2020-10-19 18:25:05 +00:00
|
|
|
title: &str,
|
2020-10-24 16:49:44 +00:00
|
|
|
window_settings: Option<WindowSettings>,
|
2020-12-22 14:20:38 +00:00
|
|
|
is_resizable: bool,
|
2020-11-20 19:35:16 +00:00
|
|
|
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
|
2020-10-24 16:49:44 +00:00
|
|
|
) -> glium::Display {
|
|
|
|
let mut window_builder = glutin::window::WindowBuilder::new()
|
2020-07-22 16:46:12 +00:00
|
|
|
.with_decorations(true)
|
2020-12-22 14:20:38 +00:00
|
|
|
.with_resizable(is_resizable)
|
2020-07-22 16:46:12 +00:00
|
|
|
.with_title(title)
|
|
|
|
.with_transparent(false);
|
|
|
|
|
|
|
|
if let Some(window_settings) = &window_settings {
|
2020-10-24 16:49:44 +00:00
|
|
|
window_builder = window_settings.initialize_size(window_builder);
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:49:44 +00:00
|
|
|
let context_builder = glutin::ContextBuilder::new()
|
2020-07-22 16:46:12 +00:00
|
|
|
.with_depth_buffer(0)
|
|
|
|
.with_srgb(true)
|
|
|
|
.with_stencil_buffer(0)
|
|
|
|
.with_vsync(true);
|
|
|
|
|
2020-12-21 21:10:31 +00:00
|
|
|
let display = glium::Display::new(window_builder, context_builder, &event_loop).unwrap();
|
|
|
|
|
|
|
|
if let Some(window_settings) = &window_settings {
|
|
|
|
window_settings.restore_positions(&display);
|
|
|
|
}
|
|
|
|
|
|
|
|
display
|
2020-10-24 16:49:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 13:15:46 +00:00
|
|
|
fn create_storage(app_name: &str) -> Option<Box<dyn epi::Storage>> {
|
2020-12-19 20:06:59 +00:00
|
|
|
if let Some(proj_dirs) = directories_next::ProjectDirs::from("", "", app_name) {
|
|
|
|
let data_dir = proj_dirs.data_dir().to_path_buf();
|
|
|
|
if let Err(err) = std::fs::create_dir_all(&data_dir) {
|
|
|
|
eprintln!(
|
|
|
|
"Saving disabled: Failed to create app path at {:?}: {}",
|
|
|
|
data_dir, err
|
|
|
|
);
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let mut config_dir = data_dir;
|
|
|
|
config_dir.push("app.json");
|
|
|
|
let storage = crate::storage::FileStorage::from_path(config_dir);
|
|
|
|
Some(Box::new(storage))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintln!("Saving disabled: Failed to find path to data_dir.");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 16:49:44 +00:00
|
|
|
/// Run an egui app
|
2020-12-29 13:15:46 +00:00
|
|
|
pub fn run(mut app: Box<dyn epi::App>) -> ! {
|
2020-12-19 20:06:59 +00:00
|
|
|
let mut storage = create_storage(app.name());
|
|
|
|
|
|
|
|
if let Some(storage) = &mut storage {
|
|
|
|
app.load(storage.as_ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
let window_settings: Option<WindowSettings> = storage
|
|
|
|
.as_mut()
|
2020-12-29 13:15:46 +00:00
|
|
|
.and_then(|storage| epi::get_value(storage.as_ref(), WINDOW_KEY));
|
2020-11-20 19:35:16 +00:00
|
|
|
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
2020-12-22 14:20:38 +00:00
|
|
|
let display = create_display(app.name(), window_settings, app.is_resizable(), &event_loop);
|
2020-07-22 16:46:12 +00:00
|
|
|
|
2020-12-27 09:49:26 +00:00
|
|
|
let repaint_signal = std::sync::Arc::new(GliumRepaintSignal(std::sync::Mutex::new(
|
|
|
|
event_loop.create_proxy(),
|
|
|
|
)));
|
2020-11-20 19:35:16 +00:00
|
|
|
|
2020-12-19 13:48:04 +00:00
|
|
|
let mut ctx = egui::CtxRef::default();
|
2020-12-19 20:06:59 +00:00
|
|
|
*ctx.memory() = storage
|
|
|
|
.as_mut()
|
2020-12-29 13:15:46 +00:00
|
|
|
.and_then(|storage| epi::get_value(storage.as_ref(), EGUI_MEMORY_KEY))
|
2020-12-19 20:06:59 +00:00
|
|
|
.unwrap_or_default();
|
2020-10-31 17:03:13 +00:00
|
|
|
app.setup(&ctx);
|
2020-07-22 16:46:12 +00:00
|
|
|
|
2020-11-13 10:04:45 +00:00
|
|
|
let mut input_state = GliumInputState::from_pixels_per_point(native_pixels_per_point(&display));
|
2020-07-22 16:46:12 +00:00
|
|
|
|
|
|
|
let start_time = Instant::now();
|
2020-10-17 10:33:30 +00:00
|
|
|
let mut previous_frame_time = None;
|
|
|
|
let mut painter = Painter::new(&display);
|
2020-07-22 16:46:12 +00:00
|
|
|
let mut clipboard = init_clipboard();
|
|
|
|
|
2020-12-19 20:06:59 +00:00
|
|
|
let mut last_auto_save = Instant::now();
|
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2020-09-14 18:55:35 +00:00
|
|
|
let mut redraw = || {
|
2020-12-19 19:50:00 +00:00
|
|
|
let frame_start = Instant::now();
|
2020-12-16 20:22:45 +00:00
|
|
|
input_state.raw.time = Some(start_time.elapsed().as_nanos() as f64 * 1e-9);
|
2020-12-16 20:48:02 +00:00
|
|
|
input_state.raw.screen_rect = Some(Rect::from_min_size(
|
|
|
|
Default::default(),
|
|
|
|
screen_size_in_pixels(&display) / input_state.raw.pixels_per_point.unwrap(),
|
|
|
|
));
|
2020-09-14 18:55:35 +00:00
|
|
|
|
2020-11-13 10:04:45 +00:00
|
|
|
ctx.begin_frame(input_state.raw.take());
|
2020-12-29 13:15:46 +00:00
|
|
|
let mut integration_context = epi::IntegrationContext {
|
|
|
|
info: epi::IntegrationInfo {
|
2020-10-24 17:23:16 +00:00
|
|
|
web_info: None,
|
|
|
|
cpu_usage: previous_frame_time,
|
|
|
|
seconds_since_midnight: Some(seconds_since_midnight()),
|
|
|
|
native_pixels_per_point: Some(native_pixels_per_point(&display)),
|
|
|
|
},
|
|
|
|
tex_allocator: Some(&mut painter),
|
|
|
|
output: Default::default(),
|
2020-11-20 19:35:16 +00:00
|
|
|
repaint_signal: repaint_signal.clone(),
|
2020-10-24 17:23:16 +00:00
|
|
|
};
|
|
|
|
app.ui(&ctx, &mut integration_context);
|
|
|
|
let app_output = integration_context.output;
|
2020-11-07 10:44:32 +00:00
|
|
|
let (egui_output, paint_commands) = ctx.end_frame();
|
2020-12-28 23:51:27 +00:00
|
|
|
let paint_jobs = ctx.tessellate(paint_commands);
|
2020-09-14 18:55:35 +00:00
|
|
|
|
2020-12-19 19:50:00 +00:00
|
|
|
let frame_time = (Instant::now() - frame_start).as_secs_f64() as f32;
|
2020-10-17 10:33:30 +00:00
|
|
|
previous_frame_time = Some(frame_time);
|
2020-12-18 21:34:48 +00:00
|
|
|
painter.paint_jobs(
|
|
|
|
&display,
|
|
|
|
ctx.pixels_per_point(),
|
|
|
|
app.clear_color(),
|
|
|
|
paint_jobs,
|
|
|
|
&ctx.texture(),
|
|
|
|
);
|
2020-10-17 10:33:30 +00:00
|
|
|
|
|
|
|
{
|
2020-12-29 13:15:46 +00:00
|
|
|
let epi::AppOutput {
|
2020-10-17 21:54:46 +00:00
|
|
|
quit,
|
2020-10-24 16:45:31 +00:00
|
|
|
window_size,
|
2020-10-17 21:54:46 +00:00
|
|
|
pixels_per_point,
|
|
|
|
} = app_output;
|
2020-10-24 16:45:31 +00:00
|
|
|
|
2020-10-17 21:54:46 +00:00
|
|
|
if let Some(pixels_per_point) = pixels_per_point {
|
|
|
|
// User changed GUI scale
|
2020-11-13 10:04:45 +00:00
|
|
|
input_state.raw.pixels_per_point = Some(pixels_per_point);
|
2020-10-17 21:54:46 +00:00
|
|
|
}
|
2020-10-17 10:33:30 +00:00
|
|
|
|
2020-10-24 16:45:31 +00:00
|
|
|
if let Some(window_size) = window_size {
|
|
|
|
display
|
|
|
|
.gl_window()
|
|
|
|
.window()
|
|
|
|
.set_inner_size(glutin::dpi::LogicalSize {
|
|
|
|
width: window_size.x,
|
|
|
|
height: window_size.y,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-17 10:33:30 +00:00
|
|
|
*control_flow = if quit {
|
|
|
|
glutin::event_loop::ControlFlow::Exit
|
|
|
|
} else if egui_output.needs_repaint {
|
|
|
|
display.gl_window().window().request_redraw();
|
|
|
|
glutin::event_loop::ControlFlow::Poll
|
|
|
|
} else {
|
|
|
|
glutin::event_loop::ControlFlow::Wait
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_output(egui_output, &display, clipboard.as_mut());
|
2020-12-19 20:06:59 +00:00
|
|
|
|
|
|
|
if let Some(storage) = &mut storage {
|
|
|
|
let now = Instant::now();
|
|
|
|
if now - last_auto_save > app.auto_save_interval() {
|
2020-12-29 13:15:46 +00:00
|
|
|
epi::set_value(
|
2020-12-19 20:06:59 +00:00
|
|
|
storage.as_mut(),
|
|
|
|
WINDOW_KEY,
|
|
|
|
&WindowSettings::from_display(&display),
|
|
|
|
);
|
2020-12-29 13:15:46 +00:00
|
|
|
epi::set_value(storage.as_mut(), EGUI_MEMORY_KEY, &*ctx.memory());
|
2020-12-19 20:06:59 +00:00
|
|
|
app.save(storage.as_mut());
|
|
|
|
storage.flush();
|
|
|
|
last_auto_save = now;
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 18:55:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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, .. } => {
|
2020-11-13 10:04:45 +00:00
|
|
|
input_to_egui(event, clipboard.as_mut(), &mut input_state, control_flow);
|
2020-10-01 20:25:44 +00:00
|
|
|
display.gl_window().window().request_redraw(); // TODO: ask Egui if the events warrants a repaint instead
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
|
|
|
glutin::event::Event::LoopDestroyed => {
|
2020-12-19 19:50:00 +00:00
|
|
|
app.on_exit();
|
2020-12-19 20:06:59 +00:00
|
|
|
if let Some(storage) = &mut storage {
|
2020-12-29 13:15:46 +00:00
|
|
|
epi::set_value(
|
2020-12-19 20:06:59 +00:00
|
|
|
storage.as_mut(),
|
|
|
|
WINDOW_KEY,
|
|
|
|
&WindowSettings::from_display(&display),
|
|
|
|
);
|
2020-12-29 13:15:46 +00:00
|
|
|
epi::set_value(storage.as_mut(), EGUI_MEMORY_KEY, &*ctx.memory());
|
2020-12-19 20:06:59 +00:00
|
|
|
app.save(storage.as_mut());
|
|
|
|
storage.flush();
|
|
|
|
}
|
2020-07-22 16:46:12 +00:00
|
|
|
}
|
2020-11-20 19:35:16 +00:00
|
|
|
|
|
|
|
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
|
|
|
|
display.gl_window().window().request_redraw();
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:46:12 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|