2021-10-18 21:13:32 +00:00
|
|
|
use crate::*;
|
2022-01-24 16:08:27 +00:00
|
|
|
use egui_winit::winit;
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
struct RequestRepaintEvent;
|
|
|
|
|
2022-01-24 16:08:27 +00:00
|
|
|
struct GlowRepaintSignal(std::sync::Mutex<winit::event_loop::EventLoopProxy<RequestRepaintEvent>>);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
impl epi::backend::RepaintSignal for GlowRepaintSignal {
|
2021-10-18 21:13:32 +00:00
|
|
|
fn request_repaint(&self) {
|
|
|
|
self.0.lock().unwrap().send_event(RequestRepaintEvent).ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
fn create_display(
|
2022-01-24 16:08:27 +00:00
|
|
|
window_builder: winit::window::WindowBuilder,
|
|
|
|
event_loop: &winit::event_loop::EventLoop<RequestRepaintEvent>,
|
2021-10-18 21:13:32 +00:00
|
|
|
) -> (
|
|
|
|
glutin::WindowedContext<glutin::PossiblyCurrent>,
|
|
|
|
glow::Context,
|
|
|
|
) {
|
|
|
|
let gl_window = unsafe {
|
|
|
|
glutin::ContextBuilder::new()
|
|
|
|
.with_depth_buffer(0)
|
|
|
|
.with_srgb(true)
|
|
|
|
.with_stencil_buffer(0)
|
|
|
|
.with_vsync(true)
|
|
|
|
.build_windowed(window_builder, event_loop)
|
|
|
|
.unwrap()
|
|
|
|
.make_current()
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let gl = unsafe { glow::Context::from_loader_function(|s| gl_window.get_proc_address(s)) };
|
|
|
|
|
|
|
|
unsafe {
|
2021-10-19 19:40:55 +00:00
|
|
|
use glow::HasContext as _;
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.enable(glow::FRAMEBUFFER_SRGB);
|
|
|
|
}
|
|
|
|
|
|
|
|
(gl_window, gl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-10-19 19:40:55 +00:00
|
|
|
pub use epi::NativeOptions;
|
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
/// Run an egui app
|
|
|
|
#[allow(unsafe_code)]
|
2021-11-03 12:45:51 +00:00
|
|
|
pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
|
|
|
let persistence = egui_winit::epi::Persistence::from_app_name(app.name());
|
2021-10-19 19:40:55 +00:00
|
|
|
let window_settings = persistence.load_window_settings();
|
|
|
|
let window_builder =
|
|
|
|
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name());
|
2022-01-24 16:08:27 +00:00
|
|
|
let event_loop = winit::event_loop::EventLoop::with_user_event();
|
2021-10-19 19:40:55 +00:00
|
|
|
let (gl_window, gl) = create_display(window_builder, &event_loop);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
let repaint_signal = std::sync::Arc::new(GlowRepaintSignal(std::sync::Mutex::new(
|
|
|
|
event_loop.create_proxy(),
|
|
|
|
)));
|
|
|
|
|
2021-11-13 11:32:01 +00:00
|
|
|
let mut painter = crate::Painter::new(&gl, None, "")
|
2022-02-01 11:27:39 +00:00
|
|
|
.unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error));
|
2021-11-03 12:45:51 +00:00
|
|
|
let mut integration = egui_winit::epi::EpiIntegration::new(
|
|
|
|
"egui_glow",
|
2022-01-24 13:32:36 +00:00
|
|
|
painter.max_texture_side(),
|
2021-11-03 12:45:51 +00:00
|
|
|
gl_window.window(),
|
|
|
|
repaint_signal,
|
|
|
|
persistence,
|
|
|
|
app,
|
|
|
|
);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
let mut is_focused = true;
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
let mut redraw = || {
|
|
|
|
if !is_focused {
|
|
|
|
// On Mac, a minimized Window uses up all CPU: https://github.com/emilk/egui/issues/325
|
|
|
|
// We can't know if we are minimized: https://github.com/rust-windowing/winit/issues/208
|
|
|
|
// But we know if we are focused (in foreground). When minimized, we are not focused.
|
|
|
|
// However, a user may want an egui with an animation in the background,
|
|
|
|
// so we still need to repaint quite fast.
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:13:53 +00:00
|
|
|
let egui::FullOutput {
|
|
|
|
platform_output,
|
|
|
|
needs_repaint,
|
|
|
|
textures_delta,
|
|
|
|
shapes,
|
|
|
|
} = integration.update(gl_window.window());
|
|
|
|
|
|
|
|
integration.handle_platform_output(gl_window.window(), platform_output);
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
let clipped_meshes = integration.egui_ctx.tessellate(shapes);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
// paint:
|
2021-10-18 21:13:32 +00:00
|
|
|
{
|
2021-11-03 12:45:51 +00:00
|
|
|
let color = integration.app.clear_color();
|
2021-10-18 21:13:32 +00:00
|
|
|
unsafe {
|
2021-10-19 19:40:55 +00:00
|
|
|
use glow::HasContext as _;
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.disable(glow::SCISSOR_TEST);
|
2021-10-19 19:40:55 +00:00
|
|
|
gl.clear_color(color[0], color[1], color[2], color[3]);
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.clear(glow::COLOR_BUFFER_BIT);
|
|
|
|
}
|
2022-02-21 20:49:52 +00:00
|
|
|
painter.paint_and_update_textures(
|
2021-11-03 12:45:51 +00:00
|
|
|
&gl,
|
2021-12-26 20:21:28 +00:00
|
|
|
gl_window.window().inner_size().into(),
|
2021-11-03 12:45:51 +00:00
|
|
|
integration.egui_ctx.pixels_per_point(),
|
|
|
|
clipped_meshes,
|
2022-02-21 20:49:52 +00:00
|
|
|
&textures_delta,
|
2021-11-03 12:45:51 +00:00
|
|
|
);
|
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
gl_window.swap_buffers().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-11-03 12:45:51 +00:00
|
|
|
*control_flow = if integration.should_quit() {
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event_loop::ControlFlow::Exit
|
2021-10-18 21:13:32 +00:00
|
|
|
} else if needs_repaint {
|
|
|
|
gl_window.window().request_redraw();
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event_loop::ControlFlow::Poll
|
2021-10-18 21:13:32 +00:00
|
|
|
} else {
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event_loop::ControlFlow::Wait
|
2021-10-18 21:13:32 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
integration.maybe_autosave(gl_window.window());
|
2021-10-18 21:13:32 +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
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
|
|
|
|
winit::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event::Event::WindowEvent { event, .. } => {
|
|
|
|
if let winit::event::WindowEvent::Focused(new_focused) = event {
|
2021-10-18 21:13:32 +00:00
|
|
|
is_focused = new_focused;
|
|
|
|
}
|
|
|
|
|
2022-01-24 16:08:27 +00:00
|
|
|
if let winit::event::WindowEvent::Resized(physical_size) = event {
|
2021-10-18 21:39:33 +00:00
|
|
|
gl_window.resize(physical_size);
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
integration.on_event(&event);
|
|
|
|
if integration.should_quit() {
|
2022-01-24 16:08:27 +00:00
|
|
|
*control_flow = winit::event_loop::ControlFlow::Exit;
|
2021-11-03 12:45:51 +00:00
|
|
|
}
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
|
|
|
|
}
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event::Event::LoopDestroyed => {
|
2021-11-03 12:45:51 +00:00
|
|
|
integration.on_exit(gl_window.window());
|
|
|
|
painter.destroy(&gl);
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event::Event::UserEvent(RequestRepaintEvent) => {
|
2021-10-18 21:13:32 +00:00
|
|
|
gl_window.window().request_redraw();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|