Reducing glutin dependency in egui_glow (#1151)
Use winit wherever possible
This commit is contained in:
parent
fa43d16c41
commit
9d596967b4
4 changed files with 28 additions and 31 deletions
|
@ -6,7 +6,8 @@ All notable changes to the `egui_glow` integration will be noted in this file.
|
||||||
* `EguiGlow::run` no longer returns the shapes to paint, but stores them internally until you call `EguiGlow::paint` ([#1110](https://github.com/emilk/egui/pull/1110)).
|
* `EguiGlow::run` no longer returns the shapes to paint, but stores them internally until you call `EguiGlow::paint` ([#1110](https://github.com/emilk/egui/pull/1110)).
|
||||||
* Added `set_texture_filter` method to `Painter` ([#1041](https://github.com/emilk/egui/pull/1041)).
|
* Added `set_texture_filter` method to `Painter` ([#1041](https://github.com/emilk/egui/pull/1041)).
|
||||||
* Fix failure to run in Chrome ([#1092](https://github.com/emilk/egui/pull/1092)).
|
* Fix failure to run in Chrome ([#1092](https://github.com/emilk/egui/pull/1092)).
|
||||||
|
* `EguiGlow::new` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)).
|
||||||
|
* `EguiGlow::paint` now takes `&winit::Window` because there are no reason to use `&glutin::WindowedContext` ([#1151](https://github.com/emilk/egui/pull/1151)).
|
||||||
|
|
||||||
## 0.16.0 - 2021-12-29
|
## 0.16.0 - 2021-12-29
|
||||||
* Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)).
|
* Made winit/glutin an optional dependency ([#868](https://github.com/emilk/egui/pull/868)).
|
||||||
|
|
|
@ -44,7 +44,7 @@ fn main() {
|
||||||
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
||||||
let (gl_window, gl) = create_display(&event_loop);
|
let (gl_window, gl) = create_display(&event_loop);
|
||||||
|
|
||||||
let mut egui_glow = egui_glow::EguiGlow::new(&gl_window, &gl);
|
let mut egui_glow = egui_glow::EguiGlow::new(gl_window.window(), &gl);
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
let mut redraw = || {
|
let mut redraw = || {
|
||||||
|
@ -78,7 +78,7 @@ fn main() {
|
||||||
|
|
||||||
// draw things behind egui here
|
// draw things behind egui here
|
||||||
|
|
||||||
egui_glow.paint(&gl_window, &gl);
|
egui_glow.paint(gl_window.window(), &gl);
|
||||||
|
|
||||||
// draw things on top of egui here
|
// draw things on top of egui here
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
use egui_winit::winit;
|
||||||
|
|
||||||
struct RequestRepaintEvent;
|
struct RequestRepaintEvent;
|
||||||
|
|
||||||
struct GlowRepaintSignal(std::sync::Mutex<glutin::event_loop::EventLoopProxy<RequestRepaintEvent>>);
|
struct GlowRepaintSignal(std::sync::Mutex<winit::event_loop::EventLoopProxy<RequestRepaintEvent>>);
|
||||||
|
|
||||||
impl epi::backend::RepaintSignal for GlowRepaintSignal {
|
impl epi::backend::RepaintSignal for GlowRepaintSignal {
|
||||||
fn request_repaint(&self) {
|
fn request_repaint(&self) {
|
||||||
|
@ -12,8 +13,8 @@ impl epi::backend::RepaintSignal for GlowRepaintSignal {
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
fn create_display(
|
fn create_display(
|
||||||
window_builder: glutin::window::WindowBuilder,
|
window_builder: winit::window::WindowBuilder,
|
||||||
event_loop: &glutin::event_loop::EventLoop<RequestRepaintEvent>,
|
event_loop: &winit::event_loop::EventLoop<RequestRepaintEvent>,
|
||||||
) -> (
|
) -> (
|
||||||
glutin::WindowedContext<glutin::PossiblyCurrent>,
|
glutin::WindowedContext<glutin::PossiblyCurrent>,
|
||||||
glow::Context,
|
glow::Context,
|
||||||
|
@ -51,7 +52,7 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||||
let window_settings = persistence.load_window_settings();
|
let window_settings = persistence.load_window_settings();
|
||||||
let window_builder =
|
let window_builder =
|
||||||
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name());
|
egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name());
|
||||||
let event_loop = glutin::event_loop::EventLoop::with_user_event();
|
let event_loop = winit::event_loop::EventLoop::with_user_event();
|
||||||
let (gl_window, gl) = create_display(window_builder, &event_loop);
|
let (gl_window, gl) = create_display(window_builder, &event_loop);
|
||||||
|
|
||||||
let repaint_signal = std::sync::Arc::new(GlowRepaintSignal(std::sync::Mutex::new(
|
let repaint_signal = std::sync::Arc::new(GlowRepaintSignal(std::sync::Mutex::new(
|
||||||
|
@ -116,12 +117,12 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||||
|
|
||||||
{
|
{
|
||||||
*control_flow = if integration.should_quit() {
|
*control_flow = if integration.should_quit() {
|
||||||
glutin::event_loop::ControlFlow::Exit
|
winit::event_loop::ControlFlow::Exit
|
||||||
} else if needs_repaint {
|
} else if needs_repaint {
|
||||||
gl_window.window().request_redraw();
|
gl_window.window().request_redraw();
|
||||||
glutin::event_loop::ControlFlow::Poll
|
winit::event_loop::ControlFlow::Poll
|
||||||
} else {
|
} else {
|
||||||
glutin::event_loop::ControlFlow::Wait
|
winit::event_loop::ControlFlow::Wait
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,30 +133,30 @@ pub fn run(app: Box<dyn epi::App>, native_options: &epi::NativeOptions) -> ! {
|
||||||
// Platform-dependent event handlers to workaround a winit bug
|
// 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/987
|
||||||
// See: https://github.com/rust-windowing/winit/issues/1619
|
// See: https://github.com/rust-windowing/winit/issues/1619
|
||||||
glutin::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
|
winit::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
|
||||||
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
winit::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
||||||
|
|
||||||
glutin::event::Event::WindowEvent { event, .. } => {
|
winit::event::Event::WindowEvent { event, .. } => {
|
||||||
if let glutin::event::WindowEvent::Focused(new_focused) = event {
|
if let winit::event::WindowEvent::Focused(new_focused) = event {
|
||||||
is_focused = new_focused;
|
is_focused = new_focused;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let glutin::event::WindowEvent::Resized(physical_size) = event {
|
if let winit::event::WindowEvent::Resized(physical_size) = event {
|
||||||
gl_window.resize(physical_size);
|
gl_window.resize(physical_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
integration.on_event(&event);
|
integration.on_event(&event);
|
||||||
if integration.should_quit() {
|
if integration.should_quit() {
|
||||||
*control_flow = glutin::event_loop::ControlFlow::Exit;
|
*control_flow = winit::event_loop::ControlFlow::Exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
|
gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
|
||||||
}
|
}
|
||||||
glutin::event::Event::LoopDestroyed => {
|
winit::event::Event::LoopDestroyed => {
|
||||||
integration.on_exit(gl_window.window());
|
integration.on_exit(gl_window.window());
|
||||||
painter.destroy(&gl);
|
painter.destroy(&gl);
|
||||||
}
|
}
|
||||||
glutin::event::Event::UserEvent(RequestRepaintEvent) => {
|
winit::event::Event::UserEvent(RequestRepaintEvent) => {
|
||||||
gl_window.window().request_redraw();
|
gl_window.window().request_redraw();
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
|
@ -88,6 +88,8 @@
|
||||||
#![allow(clippy::manual_range_contains)]
|
#![allow(clippy::manual_range_contains)]
|
||||||
|
|
||||||
pub mod painter;
|
pub mod painter;
|
||||||
|
#[cfg(feature = "winit")]
|
||||||
|
use egui_winit::winit;
|
||||||
pub use glow;
|
pub use glow;
|
||||||
pub use painter::Painter;
|
pub use painter::Painter;
|
||||||
#[cfg(feature = "winit")]
|
#[cfg(feature = "winit")]
|
||||||
|
@ -119,10 +121,7 @@ pub struct EguiGlow {
|
||||||
|
|
||||||
#[cfg(feature = "winit")]
|
#[cfg(feature = "winit")]
|
||||||
impl EguiGlow {
|
impl EguiGlow {
|
||||||
pub fn new(
|
pub fn new(window: &winit::window::Window, gl: &glow::Context) -> Self {
|
||||||
gl_window: &glutin::WindowedContext<glutin::PossiblyCurrent>,
|
|
||||||
gl: &glow::Context,
|
|
||||||
) -> Self {
|
|
||||||
let painter = crate::Painter::new(gl, None, "")
|
let painter = crate::Painter::new(gl, None, "")
|
||||||
.map_err(|error| {
|
.map_err(|error| {
|
||||||
crate::misc_util::glow_print_error(format!(
|
crate::misc_util::glow_print_error(format!(
|
||||||
|
@ -134,7 +133,7 @@ impl EguiGlow {
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
egui_ctx: Default::default(),
|
egui_ctx: Default::default(),
|
||||||
egui_winit: egui_winit::State::new(painter.max_texture_side(), gl_window.window()),
|
egui_winit: egui_winit::State::new(painter.max_texture_side(), window),
|
||||||
painter,
|
painter,
|
||||||
shapes: Default::default(),
|
shapes: Default::default(),
|
||||||
textures_delta: Default::default(),
|
textures_delta: Default::default(),
|
||||||
|
@ -147,7 +146,7 @@ impl EguiGlow {
|
||||||
/// and only when this returns `false` pass on the events to your game.
|
/// and only when this returns `false` pass on the events to your game.
|
||||||
///
|
///
|
||||||
/// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs.
|
/// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs.
|
||||||
pub fn on_event(&mut self, event: &glutin::event::WindowEvent<'_>) -> bool {
|
pub fn on_event(&mut self, event: &winit::event::WindowEvent<'_>) -> bool {
|
||||||
self.egui_winit.on_event(&self.egui_ctx, event)
|
self.egui_winit.on_event(&self.egui_ctx, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +155,7 @@ impl EguiGlow {
|
||||||
/// Call [`Self::paint`] later to paint.
|
/// Call [`Self::paint`] later to paint.
|
||||||
pub fn run(
|
pub fn run(
|
||||||
&mut self,
|
&mut self,
|
||||||
window: &glutin::window::Window,
|
window: &winit::window::Window,
|
||||||
run_ui: impl FnMut(&egui::Context),
|
run_ui: impl FnMut(&egui::Context),
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let raw_input = self.egui_winit.take_egui_input(window);
|
let raw_input = self.egui_winit.take_egui_input(window);
|
||||||
|
@ -172,11 +171,7 @@ impl EguiGlow {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Paint the results of the last call to [`Self::run`].
|
/// Paint the results of the last call to [`Self::run`].
|
||||||
pub fn paint(
|
pub fn paint(&mut self, window: &winit::window::Window, gl: &glow::Context) {
|
||||||
&mut self,
|
|
||||||
gl_window: &glutin::WindowedContext<glutin::PossiblyCurrent>,
|
|
||||||
gl: &glow::Context,
|
|
||||||
) {
|
|
||||||
let shapes = std::mem::take(&mut self.shapes);
|
let shapes = std::mem::take(&mut self.shapes);
|
||||||
let mut textures_delta = std::mem::take(&mut self.textures_delta);
|
let mut textures_delta = std::mem::take(&mut self.textures_delta);
|
||||||
|
|
||||||
|
@ -185,7 +180,7 @@ impl EguiGlow {
|
||||||
}
|
}
|
||||||
|
|
||||||
let clipped_meshes = self.egui_ctx.tessellate(shapes);
|
let clipped_meshes = self.egui_ctx.tessellate(shapes);
|
||||||
let dimensions: [u32; 2] = gl_window.window().inner_size().into();
|
let dimensions: [u32; 2] = window.inner_size().into();
|
||||||
self.painter.paint_meshes(
|
self.painter.paint_meshes(
|
||||||
gl,
|
gl,
|
||||||
dimensions,
|
dimensions,
|
||||||
|
|
Loading…
Reference in a new issue