Skip to content

Commit

Permalink
Update egui to 0.15 (#217)
Browse files Browse the repository at this point in the history
- Replaces `egui_winit_platform` with `egui-winit`
- Renames the example package to avoid crate name collision
  • Loading branch information
parasyte authored Oct 27, 2021
1 parent efeec7f commit eb07e61
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The Minimum Supported Rust Version for `pixels` will always be made available in
- [Conway's Game of Life](./examples/conway)
- [Custom Shader](./examples/custom-shader)
- [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 `winit`](./examples/minimal-winit)
- [Minimal example with `fltk`](./examples/minimal-fltk)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "egui-winit"
name = "minimal-egui"
version = "0.1.0"
authors = ["Jay Oster <[email protected]>"]
edition = "2018"
Expand All @@ -11,9 +11,9 @@ optimize = ["log/release_max_level_warn"]
default = ["optimize"]

[dependencies]
egui = "0.14"
egui_wgpu_backend = "0.13"
egui_winit_platform = { version = "0.10", features = ["webbrowser"] }
egui = "0.15"
egui_wgpu_backend = { git = "https://github.com/hasenbanck/egui_wgpu_backend.git", rev = "961125e7bd2c71c5ead1d61a7ca7ffa8c0d17f48" }
egui-winit = { version = "0.15", default-features = false, features = ["links"] }
env_logger = "0.9"
log = "0.4"
pixels = { path = "../.." }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Egui Example

![Egui Example](../../img/egui-winit.png)
![Egui Example](../../img/minimal-egui.png)

Minimal example with `egui` and `winit`.

## Running

```bash
cargo run --release --package egui-winit
cargo run --release --package minimal-egui
```

## About
Expand Down
128 changes: 66 additions & 62 deletions examples/egui-winit/src/gui.rs → examples/minimal-egui/src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
use egui::{ClippedMesh, FontDefinitions};
use egui::{ClippedMesh, CtxRef};
use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor};
use egui_winit_platform::{Platform, PlatformDescriptor};
use pixels::{wgpu, PixelsContext};
use std::time::Instant;
use winit::window::Window;

/// Manages all state required for rendering egui over `Pixels`.
pub(crate) struct Gui {
pub(crate) struct Framework {
// State for egui.
start_time: Instant,
platform: Platform,
egui_ctx: CtxRef,
egui_state: egui_winit::State,
screen_descriptor: ScreenDescriptor,
rpass: RenderPass,
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,
}

impl Gui {
impl Framework {
/// Create egui.
pub(crate) fn new(width: u32, height: u32, scale_factor: f64, pixels: &pixels::Pixels) -> Self {
let platform = Platform::new(PlatformDescriptor {
physical_width: width,
physical_height: height,
scale_factor,
font_definitions: FontDefinitions::default(),
style: Default::default(),
});
pub(crate) fn new(width: u32, height: u32, scale_factor: f32, pixels: &pixels::Pixels) -> Self {
let egui_ctx = CtxRef::default();
let egui_state = egui_winit::State::from_pixels_per_point(scale_factor);
let screen_descriptor = ScreenDescriptor {
physical_width: width,
physical_height: height,
scale_factor: scale_factor as f32,
scale_factor,
};
let rpass = RenderPass::new(pixels.device(), pixels.render_texture_format(), 1);
let gui = Gui::new();

Self {
start_time: Instant::now(),
platform,
egui_ctx,
egui_state,
screen_descriptor,
rpass,
paint_jobs: Vec::new(),
window_open: true,
gui,
}
}

/// Handle input events from the window manager.
pub(crate) fn handle_event(&mut self, event: &winit::event::Event<'_, ()>) {
self.platform.handle_event(event);
pub(crate) fn handle_event(&mut self, event: &winit::event::WindowEvent) {
self.egui_state.on_event(&self.egui_ctx, event);
}

/// Resize egui.
Expand All @@ -65,46 +65,18 @@ impl Gui {

/// Prepare egui.
pub(crate) fn prepare(&mut self, window: &Window) {
self.platform
.update_time(self.start_time.elapsed().as_secs_f64());

// 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.
self.ui(&self.platform.context());
self.gui.ui(&self.egui_ctx);

// End the egui frame and create all paint jobs to prepare for rendering.
let (_output, paint_commands) = self.platform.end_frame(Some(window));
self.paint_jobs = self.platform.context().tessellate(paint_commands);
}

/// Create the UI using egui.
fn ui(&mut self, ctx: &egui::CtxRef) {
egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
egui::menu::menu(ui, "File", |ui| {
if ui.button("About...").clicked() {
self.window_open = true;
}
})
});
});

egui::Window::new("Hello, egui!")
.open(&mut self.window_open)
.show(ctx, |ui| {
ui.label("This example demonstrates using egui with pixels.");
ui.label("Made with 💖 in San Francisco!");

ui.separator();

ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x /= 2.0;
ui.label("Learn more about egui at");
ui.hyperlink("https://docs.rs/egui");
});
});
let (output, paint_commands) = self.egui_ctx.end_frame();
self.egui_state
.handle_output(window, &self.egui_ctx, output);
self.paint_jobs = self.egui_ctx.tessellate(paint_commands);
}

/// Render egui.
Expand All @@ -115,11 +87,8 @@ impl Gui {
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_texture(&context.device, &context.queue, &self.egui_ctx.texture());
self.rpass
.update_user_textures(&context.device, &context.queue);
self.rpass.update_buffers(
Expand All @@ -139,3 +108,38 @@ impl Gui {
)
}
}

impl Gui {
/// Create a `Gui`.
fn new() -> Self {
Self { window_open: true }
}

/// Create the UI using egui.
fn ui(&mut self, ctx: &CtxRef) {
egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
egui::menu::menu(ui, "File", |ui| {
if ui.button("About...").clicked() {
self.window_open = true;
}
})
});
});

egui::Window::new("Hello, egui!")
.open(&mut self.window_open)
.show(ctx, |ui| {
ui.label("This example demonstrates using egui with pixels.");
ui.label("Made with 💖 in San Francisco!");

ui.separator();

ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x /= 2.0;
ui.label("Learn more about egui at");
ui.hyperlink("https://docs.rs/egui");
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(clippy::all)]
#![forbid(unsafe_code)]

use crate::gui::Gui;
use crate::gui::Framework;
use log::error;
use pixels::{Error, Pixels, SurfaceTexture};
use winit::dpi::LogicalSize;
Expand Down Expand Up @@ -38,50 +38,19 @@ fn main() -> Result<(), Error> {
.unwrap()
};

let (mut pixels, mut gui) = {
let (mut pixels, mut framework) = {
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 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();

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
if input.update(&event) {
// Close events
Expand All @@ -92,19 +61,54 @@ fn main() -> Result<(), Error> {

// Update the scale factor
if let Some(scale_factor) = input.scale_factor() {
gui.scale_factor(scale_factor);
framework.scale_factor(scale_factor);
}

// Resize the window
if let Some(size) = input.window_resized() {
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
world.update();
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;
}
}
_ => (),
}
});
}

Expand Down
File renamed without changes

0 comments on commit eb07e61

Please sign in to comment.