2019-04-21 08:13:05 +00:00
|
|
|
#![deny(warnings)]
|
2020-05-10 17:04:10 +00:00
|
|
|
#![warn(clippy::all)]
|
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
use std::time::Instant;
|
2019-11-18 19:06:41 +00:00
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
use {
|
2020-07-21 22:36:17 +00:00
|
|
|
egui::{examples::ExampleApp, pos2, vec2, Pos2, Vec2},
|
2020-04-29 19:58:14 +00:00
|
|
|
glium::glutin,
|
2020-07-21 22:36:17 +00:00
|
|
|
glutin::dpi::PhysicalPosition,
|
2019-03-12 21:59:55 +00:00
|
|
|
};
|
|
|
|
|
2020-05-30 09:04:40 +00:00
|
|
|
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
2020-05-10 19:13:39 +00:00
|
|
|
struct Window {
|
|
|
|
pos: Option<Pos2>,
|
|
|
|
size: Option<Vec2>,
|
|
|
|
}
|
|
|
|
|
2020-05-23 07:57:39 +00:00
|
|
|
fn read_json<T>(memory_json_path: impl AsRef<std::path::Path>) -> Option<T>
|
|
|
|
where
|
|
|
|
T: serde::de::DeserializeOwned,
|
|
|
|
{
|
2020-05-10 19:13:39 +00:00
|
|
|
match std::fs::File::open(memory_json_path) {
|
|
|
|
Ok(file) => {
|
|
|
|
let reader = std::io::BufReader::new(file);
|
|
|
|
match serde_json::from_reader(reader) {
|
|
|
|
Ok(value) => Some(value),
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("ERROR: Failed to parse json: {}", err);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_err) => {
|
|
|
|
// File probably doesn't exist. That's fine.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
fn main() {
|
2020-05-10 19:13:39 +00:00
|
|
|
// TODO: combine
|
2020-05-30 08:22:35 +00:00
|
|
|
let memory_path = "egui.json";
|
2020-05-10 19:13:39 +00:00
|
|
|
let settings_json_path: &str = "window.json";
|
2020-07-21 22:36:17 +00:00
|
|
|
let app_json_path: &str = "egui_example_app.json";
|
2020-05-10 19:13:39 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
let mut egui_example_app: ExampleApp = read_json(app_json_path).unwrap_or_default();
|
2020-05-23 07:57:39 +00:00
|
|
|
let mut window_settings: Window = read_json(settings_json_path).unwrap_or_default();
|
2020-05-10 19:13:39 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
let event_loop = glutin::event_loop::EventLoop::new();
|
|
|
|
let window = glutin::window::WindowBuilder::new()
|
|
|
|
.with_decorations(true)
|
|
|
|
.with_resizable(true)
|
|
|
|
.with_title("Egui glium example")
|
|
|
|
.with_transparent(false);
|
|
|
|
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();
|
2019-03-12 21:59:55 +00:00
|
|
|
|
2020-05-30 12:56:38 +00:00
|
|
|
let size = window_settings.size.unwrap_or_else(|| vec2(1024.0, 800.0));
|
2020-05-10 19:13:39 +00:00
|
|
|
|
2020-04-22 18:01:49 +00:00
|
|
|
display
|
|
|
|
.gl_window()
|
2020-07-21 22:36:17 +00:00
|
|
|
.window()
|
|
|
|
.set_inner_size(glutin::dpi::PhysicalSize {
|
2020-05-10 19:13:39 +00:00
|
|
|
width: size.x as f64,
|
|
|
|
height: size.y as f64,
|
2020-04-22 18:01:49 +00:00
|
|
|
});
|
2020-05-10 19:13:39 +00:00
|
|
|
|
|
|
|
if let Some(pos) = window_settings.pos {
|
|
|
|
display
|
|
|
|
.gl_window()
|
2020-07-21 22:36:17 +00:00
|
|
|
.window()
|
|
|
|
.set_outer_position(PhysicalPosition::new(pos.x as f64, pos.y as f64));
|
2020-05-10 19:13:39 +00:00
|
|
|
}
|
2020-04-22 18:01:49 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
let pixels_per_point = display.gl_window().window().scale_factor() as f32;
|
2019-03-12 21:59:55 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
let mut ctx = egui::Context::new();
|
|
|
|
let mut painter = egui_glium::Painter::new(&display);
|
2019-03-12 21:59:55 +00:00
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
let mut raw_input = egui::RawInput {
|
2019-03-12 21:59:55 +00:00
|
|
|
screen_size: {
|
|
|
|
let (width, height) = display.get_framebuffer_dimensions();
|
2019-03-16 11:57:44 +00:00
|
|
|
vec2(width as f32, height as f32) / pixels_per_point
|
2019-03-12 21:59:55 +00:00
|
|
|
},
|
2020-05-20 19:19:42 +00:00
|
|
|
pixels_per_point: Some(pixels_per_point),
|
2019-04-21 08:13:05 +00:00
|
|
|
..Default::default()
|
2019-03-12 21:59:55 +00:00
|
|
|
};
|
|
|
|
|
2020-04-21 12:46:42 +00:00
|
|
|
// used to keep track of time for animations
|
|
|
|
let start_time = Instant::now();
|
2020-05-30 08:22:35 +00:00
|
|
|
let mut frame_times = egui::MovementTracker::new(1000, 1.0);
|
|
|
|
let mut clipboard = egui_glium::init_clipboard();
|
2020-04-29 19:25:49 +00:00
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
egui_glium::read_memory(&ctx, memory_path);
|
2020-05-02 09:37:12 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
*control_flow = glutin::event_loop::ControlFlow::Wait;
|
|
|
|
|
|
|
|
match event {
|
|
|
|
glutin::event::Event::RedrawRequested(_) => {
|
|
|
|
let egui_start = Instant::now();
|
|
|
|
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
|
|
|
|
raw_input.seconds_since_midnight = Some(egui_glium::local_time_of_day());
|
|
|
|
|
|
|
|
let mut ui = ctx.begin_frame(raw_input.take());
|
|
|
|
egui_example_app.ui(&mut ui, "");
|
|
|
|
{
|
|
|
|
use egui::*;
|
|
|
|
let mut ui = ui.centered_column(ui.available().width().min(480.0));
|
|
|
|
ui.set_layout(Layout::vertical(Align::Min));
|
|
|
|
ui.add(label!("Egui running inside of Glium").text_style(TextStyle::Heading));
|
|
|
|
if ui.add(Button::new("Quit")).clicked {
|
|
|
|
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.add(
|
|
|
|
label!(
|
|
|
|
"CPU usage: {:.2} ms (excludes painting)",
|
|
|
|
1e3 * frame_times.average().unwrap_or_default()
|
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
|
|
|
);
|
|
|
|
ui.add(
|
|
|
|
label!(
|
|
|
|
"FPS: {:.1}",
|
|
|
|
1.0 / frame_times.mean_time_interval().unwrap_or_default()
|
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
|
|
|
);
|
|
|
|
}
|
2019-03-12 21:59:55 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
let (output, paint_jobs) = ctx.end_frame();
|
2019-11-18 19:06:41 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
frame_times.add(
|
|
|
|
raw_input.time,
|
|
|
|
(Instant::now() - egui_start).as_secs_f64() as f32,
|
|
|
|
);
|
2020-04-21 18:48:31 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
painter.paint_jobs(&display, paint_jobs, ctx.texture());
|
|
|
|
egui_glium::handle_output(output, &display, clipboard.as_mut());
|
2020-05-02 09:37:12 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
display.gl_window().window().request_redraw(); // TODO: only if needed (new events etc)
|
|
|
|
}
|
|
|
|
glutin::event::Event::WindowEvent { event, .. } => {
|
|
|
|
let mut running = true;
|
|
|
|
egui_glium::input_to_egui(event, clipboard.as_mut(), &mut raw_input, &mut running);
|
|
|
|
if !running {
|
|
|
|
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glutin::event::Event::LoopDestroyed => {
|
|
|
|
// Save state to disk:
|
|
|
|
window_settings.pos = display
|
|
|
|
.gl_window()
|
|
|
|
.window()
|
|
|
|
.outer_position()
|
|
|
|
.ok()
|
|
|
|
.map(|p| pos2(p.x as f32, p.y as f32));
|
|
|
|
window_settings.size = Some(vec2(
|
|
|
|
display.gl_window().window().inner_size().width as f32,
|
|
|
|
display.gl_window().window().inner_size().height as f32,
|
|
|
|
));
|
|
|
|
|
|
|
|
if let Err(err) = egui_glium::write_memory(&ctx, memory_path) {
|
|
|
|
eprintln!("ERROR: Failed to save egui state: {}", err);
|
|
|
|
}
|
2020-05-17 10:26:17 +00:00
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
serde_json::to_writer_pretty(
|
|
|
|
std::fs::File::create(app_json_path).unwrap(),
|
|
|
|
&egui_example_app,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
serde_json::to_writer_pretty(
|
|
|
|
std::fs::File::create(settings_json_path).unwrap(),
|
|
|
|
&window_settings,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
2020-05-17 10:26:17 +00:00
|
|
|
}
|