Update egui to 0.15 (#217)
- Replaces `egui_winit_platform` with `egui-winit` - Renames the example package to avoid crate name collision
This commit is contained in:
parent
efeec7f727
commit
eb07e61f5c
|
@ -30,7 +30,7 @@ The Minimum Supported Rust Version for `pixels` will always be made available in
|
||||||
- [Conway's Game of Life](./examples/conway)
|
- [Conway's Game of Life](./examples/conway)
|
||||||
- [Custom Shader](./examples/custom-shader)
|
- [Custom Shader](./examples/custom-shader)
|
||||||
- [Dear ImGui example with `winit`](./examples/imgui-winit)
|
- [Dear ImGui example with `winit`](./examples/imgui-winit)
|
||||||
- [Egui example with `winit`](./examples/egui-winit)
|
- [Egui example with `winit`](./examples/minimal-egui)
|
||||||
- [Minimal example with SDL2](./examples/minimal-sdl2)
|
- [Minimal example with SDL2](./examples/minimal-sdl2)
|
||||||
- [Minimal example with `winit`](./examples/minimal-winit)
|
- [Minimal example with `winit`](./examples/minimal-winit)
|
||||||
- [Minimal example with `fltk`](./examples/minimal-fltk)
|
- [Minimal example with `fltk`](./examples/minimal-fltk)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "egui-winit"
|
name = "minimal-egui"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Jay Oster <jay@kodewerx.org>"]
|
authors = ["Jay Oster <jay@kodewerx.org>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
@ -11,9 +11,9 @@ optimize = ["log/release_max_level_warn"]
|
||||||
default = ["optimize"]
|
default = ["optimize"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
egui = "0.14"
|
egui = "0.15"
|
||||||
egui_wgpu_backend = "0.13"
|
egui_wgpu_backend = { git = "https://github.com/hasenbanck/egui_wgpu_backend.git", rev = "961125e7bd2c71c5ead1d61a7ca7ffa8c0d17f48" }
|
||||||
egui_winit_platform = { version = "0.10", features = ["webbrowser"] }
|
egui-winit = { version = "0.15", default-features = false, features = ["links"] }
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
pixels = { path = "../.." }
|
pixels = { path = "../.." }
|
|
@ -1,13 +1,13 @@
|
||||||
# Egui Example
|
# Egui Example
|
||||||
|
|
||||||
![Egui Example](../../img/egui-winit.png)
|
![Egui Example](../../img/minimal-egui.png)
|
||||||
|
|
||||||
Minimal example with `egui` and `winit`.
|
Minimal example with `egui` and `winit`.
|
||||||
|
|
||||||
## Running
|
## Running
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo run --release --package egui-winit
|
cargo run --release --package minimal-egui
|
||||||
```
|
```
|
||||||
|
|
||||||
## About
|
## About
|
|
@ -1,53 +1,53 @@
|
||||||
use egui::{ClippedMesh, FontDefinitions};
|
use egui::{ClippedMesh, CtxRef};
|
||||||
use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor};
|
use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor};
|
||||||
use egui_winit_platform::{Platform, PlatformDescriptor};
|
|
||||||
use pixels::{wgpu, PixelsContext};
|
use pixels::{wgpu, PixelsContext};
|
||||||
use std::time::Instant;
|
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
|
|
||||||
/// Manages all state required for rendering egui over `Pixels`.
|
/// Manages all state required for rendering egui over `Pixels`.
|
||||||
pub(crate) struct Gui {
|
pub(crate) struct Framework {
|
||||||
// State for egui.
|
// State for egui.
|
||||||
start_time: Instant,
|
egui_ctx: CtxRef,
|
||||||
platform: Platform,
|
egui_state: egui_winit::State,
|
||||||
screen_descriptor: ScreenDescriptor,
|
screen_descriptor: ScreenDescriptor,
|
||||||
rpass: RenderPass,
|
rpass: RenderPass,
|
||||||
paint_jobs: Vec<ClippedMesh>,
|
paint_jobs: Vec<ClippedMesh>,
|
||||||
|
|
||||||
// State for the demo app.
|
// State for the GUI
|
||||||
|
gui: Gui,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Example application state. A real application will need a lot more state than this.
|
||||||
|
struct Gui {
|
||||||
|
/// Only show the egui window when true.
|
||||||
window_open: bool,
|
window_open: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Gui {
|
impl Framework {
|
||||||
/// Create egui.
|
/// Create egui.
|
||||||
pub(crate) fn new(width: u32, height: u32, scale_factor: f64, pixels: &pixels::Pixels) -> Self {
|
pub(crate) fn new(width: u32, height: u32, scale_factor: f32, pixels: &pixels::Pixels) -> Self {
|
||||||
let platform = Platform::new(PlatformDescriptor {
|
let egui_ctx = CtxRef::default();
|
||||||
physical_width: width,
|
let egui_state = egui_winit::State::from_pixels_per_point(scale_factor);
|
||||||
physical_height: height,
|
|
||||||
scale_factor,
|
|
||||||
font_definitions: FontDefinitions::default(),
|
|
||||||
style: Default::default(),
|
|
||||||
});
|
|
||||||
let screen_descriptor = ScreenDescriptor {
|
let screen_descriptor = ScreenDescriptor {
|
||||||
physical_width: width,
|
physical_width: width,
|
||||||
physical_height: height,
|
physical_height: height,
|
||||||
scale_factor: scale_factor as f32,
|
scale_factor,
|
||||||
};
|
};
|
||||||
let rpass = RenderPass::new(pixels.device(), pixels.render_texture_format(), 1);
|
let rpass = RenderPass::new(pixels.device(), pixels.render_texture_format(), 1);
|
||||||
|
let gui = Gui::new();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
start_time: Instant::now(),
|
egui_ctx,
|
||||||
platform,
|
egui_state,
|
||||||
screen_descriptor,
|
screen_descriptor,
|
||||||
rpass,
|
rpass,
|
||||||
paint_jobs: Vec::new(),
|
paint_jobs: Vec::new(),
|
||||||
window_open: true,
|
gui,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle input events from the window manager.
|
/// Handle input events from the window manager.
|
||||||
pub(crate) fn handle_event(&mut self, event: &winit::event::Event<'_, ()>) {
|
pub(crate) fn handle_event(&mut self, event: &winit::event::WindowEvent) {
|
||||||
self.platform.handle_event(event);
|
self.egui_state.on_event(&self.egui_ctx, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resize egui.
|
/// Resize egui.
|
||||||
|
@ -65,22 +65,58 @@ impl Gui {
|
||||||
|
|
||||||
/// Prepare egui.
|
/// Prepare egui.
|
||||||
pub(crate) fn prepare(&mut self, window: &Window) {
|
pub(crate) fn prepare(&mut self, window: &Window) {
|
||||||
self.platform
|
|
||||||
.update_time(self.start_time.elapsed().as_secs_f64());
|
|
||||||
|
|
||||||
// Begin the egui frame.
|
// Begin the egui frame.
|
||||||
self.platform.begin_frame();
|
let raw_input = self.egui_state.take_egui_input(window);
|
||||||
|
self.egui_ctx.begin_frame(raw_input);
|
||||||
|
|
||||||
// Draw the demo application.
|
// Draw the demo application.
|
||||||
self.ui(&self.platform.context());
|
self.gui.ui(&self.egui_ctx);
|
||||||
|
|
||||||
// End the egui frame and create all paint jobs to prepare for rendering.
|
// End the egui frame and create all paint jobs to prepare for rendering.
|
||||||
let (_output, paint_commands) = self.platform.end_frame(Some(window));
|
let (output, paint_commands) = self.egui_ctx.end_frame();
|
||||||
self.paint_jobs = self.platform.context().tessellate(paint_commands);
|
self.egui_state
|
||||||
|
.handle_output(window, &self.egui_ctx, output);
|
||||||
|
self.paint_jobs = self.egui_ctx.tessellate(paint_commands);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render egui.
|
||||||
|
pub(crate) fn render(
|
||||||
|
&mut self,
|
||||||
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
|
render_target: &wgpu::TextureView,
|
||||||
|
context: &PixelsContext,
|
||||||
|
) -> Result<(), BackendError> {
|
||||||
|
// Upload all resources to the GPU.
|
||||||
|
self.rpass
|
||||||
|
.update_texture(&context.device, &context.queue, &self.egui_ctx.texture());
|
||||||
|
self.rpass
|
||||||
|
.update_user_textures(&context.device, &context.queue);
|
||||||
|
self.rpass.update_buffers(
|
||||||
|
&context.device,
|
||||||
|
&context.queue,
|
||||||
|
&self.paint_jobs,
|
||||||
|
&self.screen_descriptor,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Record all render passes.
|
||||||
|
self.rpass.execute(
|
||||||
|
encoder,
|
||||||
|
render_target,
|
||||||
|
&self.paint_jobs,
|
||||||
|
&self.screen_descriptor,
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Gui {
|
||||||
|
/// Create a `Gui`.
|
||||||
|
fn new() -> Self {
|
||||||
|
Self { window_open: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create the UI using egui.
|
/// Create the UI using egui.
|
||||||
fn ui(&mut self, ctx: &egui::CtxRef) {
|
fn ui(&mut self, ctx: &CtxRef) {
|
||||||
egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
|
egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
|
||||||
egui::menu::bar(ui, |ui| {
|
egui::menu::bar(ui, |ui| {
|
||||||
egui::menu::menu(ui, "File", |ui| {
|
egui::menu::menu(ui, "File", |ui| {
|
||||||
|
@ -106,36 +142,4 @@ impl Gui {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render egui.
|
|
||||||
pub(crate) fn render(
|
|
||||||
&mut self,
|
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
|
||||||
render_target: &wgpu::TextureView,
|
|
||||||
context: &PixelsContext,
|
|
||||||
) -> Result<(), BackendError> {
|
|
||||||
// Upload all resources to the GPU.
|
|
||||||
self.rpass.update_texture(
|
|
||||||
&context.device,
|
|
||||||
&context.queue,
|
|
||||||
&self.platform.context().texture(),
|
|
||||||
);
|
|
||||||
self.rpass
|
|
||||||
.update_user_textures(&context.device, &context.queue);
|
|
||||||
self.rpass.update_buffers(
|
|
||||||
&context.device,
|
|
||||||
&context.queue,
|
|
||||||
&self.paint_jobs,
|
|
||||||
&self.screen_descriptor,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Record all render passes.
|
|
||||||
self.rpass.execute(
|
|
||||||
encoder,
|
|
||||||
render_target,
|
|
||||||
&self.paint_jobs,
|
|
||||||
&self.screen_descriptor,
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
#![deny(clippy::all)]
|
#![deny(clippy::all)]
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
||||||
use crate::gui::Gui;
|
use crate::gui::Framework;
|
||||||
use log::error;
|
use log::error;
|
||||||
use pixels::{Error, Pixels, SurfaceTexture};
|
use pixels::{Error, Pixels, SurfaceTexture};
|
||||||
use winit::dpi::LogicalSize;
|
use winit::dpi::LogicalSize;
|
||||||
|
@ -38,50 +38,19 @@ fn main() -> Result<(), Error> {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
let (mut pixels, mut gui) = {
|
let (mut pixels, mut framework) = {
|
||||||
let window_size = window.inner_size();
|
let window_size = window.inner_size();
|
||||||
let scale_factor = window.scale_factor();
|
let scale_factor = window.scale_factor() as f32;
|
||||||
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
|
let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &window);
|
||||||
let pixels = Pixels::new(WIDTH, HEIGHT, surface_texture)?;
|
let pixels = Pixels::new(WIDTH, HEIGHT, surface_texture)?;
|
||||||
let gui = Gui::new(window_size.width, window_size.height, scale_factor, &pixels);
|
let framework =
|
||||||
|
Framework::new(window_size.width, window_size.height, scale_factor, &pixels);
|
||||||
|
|
||||||
(pixels, gui)
|
(pixels, framework)
|
||||||
};
|
};
|
||||||
let mut world = World::new();
|
let mut world = World::new();
|
||||||
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
// Update egui inputs
|
|
||||||
gui.handle_event(&event);
|
|
||||||
|
|
||||||
// Draw the current frame
|
|
||||||
if let Event::RedrawRequested(_) = event {
|
|
||||||
// Draw the world
|
|
||||||
world.draw(pixels.get_frame());
|
|
||||||
|
|
||||||
// Prepare egui
|
|
||||||
gui.prepare(&window);
|
|
||||||
|
|
||||||
// Render everything together
|
|
||||||
let render_result = pixels.render_with(|encoder, render_target, context| {
|
|
||||||
// Render the world texture
|
|
||||||
context.scaling_renderer.render(encoder, render_target);
|
|
||||||
|
|
||||||
// Render egui
|
|
||||||
gui.render(encoder, render_target, context)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
|
|
||||||
// Basic error handling
|
|
||||||
if render_result
|
|
||||||
.map_err(|e| error!("pixels.render() failed: {}", e))
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
*control_flow = ControlFlow::Exit;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle input events
|
// Handle input events
|
||||||
if input.update(&event) {
|
if input.update(&event) {
|
||||||
// Close events
|
// Close events
|
||||||
|
@ -92,19 +61,54 @@ fn main() -> Result<(), Error> {
|
||||||
|
|
||||||
// Update the scale factor
|
// Update the scale factor
|
||||||
if let Some(scale_factor) = input.scale_factor() {
|
if let Some(scale_factor) = input.scale_factor() {
|
||||||
gui.scale_factor(scale_factor);
|
framework.scale_factor(scale_factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resize the window
|
// Resize the window
|
||||||
if let Some(size) = input.window_resized() {
|
if let Some(size) = input.window_resized() {
|
||||||
pixels.resize_surface(size.width, size.height);
|
pixels.resize_surface(size.width, size.height);
|
||||||
gui.resize(size.width, size.height);
|
framework.resize(size.width, size.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update internal state and request a redraw
|
// Update internal state and request a redraw
|
||||||
world.update();
|
world.update();
|
||||||
window.request_redraw();
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::WindowEvent { event, .. } => {
|
||||||
|
// Update egui inputs
|
||||||
|
framework.handle_event(&event);
|
||||||
|
}
|
||||||
|
// Draw the current frame
|
||||||
|
Event::RedrawRequested(_) => {
|
||||||
|
// Draw the world
|
||||||
|
world.draw(pixels.get_frame());
|
||||||
|
|
||||||
|
// Prepare egui
|
||||||
|
framework.prepare(&window);
|
||||||
|
|
||||||
|
// Render everything together
|
||||||
|
let render_result = pixels.render_with(|encoder, render_target, context| {
|
||||||
|
// Render the world texture
|
||||||
|
context.scaling_renderer.render(encoder, render_target);
|
||||||
|
|
||||||
|
// Render egui
|
||||||
|
framework.render(encoder, render_target, context)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
// Basic error handling
|
||||||
|
if render_result
|
||||||
|
.map_err(|e| error!("pixels.render() failed: {}", e))
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
*control_flow = ControlFlow::Exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Before Width: | Height: | Size: 361 KiB After Width: | Height: | Size: 361 KiB |
Loading…
Reference in a new issue