-
Notifications
You must be signed in to change notification settings - Fork 50
Add egui example #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeerooth
wants to merge
12
commits into
master
Choose a base branch
from
egui-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add egui example #48
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
99c2c05
add egui example
zeerooth 549addd
fix egui template on desktop
zeerooth 3b2e730
Merge branch 'master' into egui-example
zeerooth ff8916e
bump egui_wgpu_backend to 0.14 in egui template
zeerooth a394f85
egui template: handle Resumed/Suspended events, start without a surfa…
zeerooth a6783d3
enable fullscreen in android by default
zeerooth 8c045d0
Merge remote-tracking branch 'origin/master' into egui-example
zeerooth 5ce078a
update dependencies and adapt to winit=0.26 and egui=0.16
zeerooth 86570c9
use utc timestamps in simple_logger and update dependencies to newst …
zeerooth 6fbfac6
[egui example] disable optional features for egui-winit breaking the …
zeerooth 70bb196
Merge branch 'master' into egui-example
zeerooth 7a39f84
Merge branch 'master' into egui-example
zeerooth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Rust | ||
| target/ | ||
| **/*.rs.bk | ||
|
|
||
| # cargo-mobile | ||
| .cargo/ | ||
| /gen | ||
|
|
||
| # macOS | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| [package] | ||
| name = "{{app.name}}" | ||
| version = "0.1.0" | ||
| authors = ["{{author}}"] | ||
| edition = "2018" | ||
| resolver = "2" | ||
|
|
||
| [lib] | ||
| crate-type = ["staticlib", "cdylib", "rlib"] | ||
|
|
||
| [[bin]] | ||
| name = "{{app.name}}-desktop" | ||
| path = "gen/bin/desktop.rs" | ||
|
|
||
| [dependencies] | ||
| egui_wgpu_backend = "0.16" | ||
| chrono = "0.4" | ||
| pollster = "0.2" | ||
| egui = "0.16.1" | ||
| epi = "0.16" | ||
| wgpu = "0.12" | ||
| winit = "0.26" | ||
| egui_demo_lib = "0.16" | ||
| mobile-entry-point = "0.1" | ||
| egui-winit = "0.16" | ||
|
|
||
| [target.'cfg(target_os = "android")'.dependencies] | ||
| android_logger = "0.10.1" | ||
| log = "0.4.14" | ||
| ndk-glue = "0.5.0" | ||
|
|
||
| [target.'cfg(not(target_os = "android"))'.dependencies] | ||
| simple_logger = "1.16.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # egui | ||
|
|
||
| This is an example inspired by [this repo](https://github.com/hasenbanck/egui_example), using `egui`, `winit` and `wgpu` to run [egui_demo_app](https://github.com/emilk/egui/tree/master/egui_demo_app). | ||
|
|
||
| To run this on desktop, just do `cargo run` like normal! For mobile, use `cargo android run` and `cargo apple run` respectively (or use `cargo android open` and `cargo apple open` to open in Android Studio and Xcode respectively). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| fn main() { | ||
| #[cfg(not(any(target_os = "android", target_os = "ios")))] | ||
| {{snake-case app.name}}::start_app(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| use std::iter; | ||
| use std::time::Instant; | ||
|
|
||
| use egui_wgpu_backend::{RenderPass, ScreenDescriptor}; | ||
| use epi::*; | ||
| use winit::event::Event::*; | ||
| use winit::event_loop::ControlFlow; | ||
|
|
||
| use mobile_entry_point::mobile_entry_point; | ||
| #[cfg(target_os = "android")] | ||
| use ndk_glue; | ||
|
|
||
| /// A custom event type for the winit app. | ||
| #[derive(Debug)] | ||
| enum Event { | ||
| RequestRedraw, | ||
| } | ||
|
|
||
| static INITIAL_WIDTH: u32 = 1280; | ||
| static INITIAL_HEIGHT: u32 = 720; | ||
|
|
||
| /// This is the repaint signal type that egui needs for requesting a repaint from another thread. | ||
| /// It sends the custom RequestRedraw event to the winit event loop. | ||
| struct ExampleRepaintSignal(std::sync::Mutex<winit::event_loop::EventLoopProxy<Event>>); | ||
|
|
||
| impl epi::backend::RepaintSignal for ExampleRepaintSignal { | ||
| fn request_repaint(&self) { | ||
| self.0.lock().unwrap().send_event(Event::RequestRedraw).ok(); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(target_os = "android")] | ||
| fn init_logging() { | ||
| android_logger::init_once( | ||
| android_logger::Config::default() | ||
| .with_min_level(log::Level::Trace) | ||
| .with_tag("{{app.name}}"), | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "android"))] | ||
| fn init_logging() { | ||
| simple_logger::SimpleLogger::new().with_utc_timestamps().init().unwrap(); | ||
| } | ||
|
|
||
| /// A simple egui + wgpu + winit based example. | ||
| #[mobile_entry_point] | ||
| fn main() { | ||
| init_logging(); | ||
| let event_loop = winit::event_loop::EventLoop::with_user_event(); | ||
| let window = winit::window::WindowBuilder::new() | ||
| .with_title("A fantastic window!") | ||
| .with_inner_size(winit::dpi::LogicalSize::new(INITIAL_WIDTH, INITIAL_HEIGHT)) | ||
| .build(&event_loop) | ||
| .unwrap(); | ||
|
|
||
| let instance = wgpu::Instance::new(wgpu::Backends::PRIMARY); | ||
|
|
||
| let mut surface = if cfg!(target_os = "android") { | ||
| None | ||
| } else { | ||
| Some(unsafe { instance.create_surface(&window) }) | ||
| }; | ||
|
|
||
| // WGPU 0.11+ support force fallback (if HW implementation not supported), set it to true or false (optional). | ||
| let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { | ||
| power_preference: wgpu::PowerPreference::HighPerformance, | ||
| compatible_surface: surface.as_ref(), | ||
| force_fallback_adapter: false, | ||
| })) | ||
| .unwrap(); | ||
|
|
||
| let (device, queue) = pollster::block_on(adapter.request_device( | ||
| &wgpu::DeviceDescriptor { | ||
| features: wgpu::Features::default(), | ||
| limits: wgpu::Limits::default(), | ||
| label: None, | ||
| }, | ||
| None, | ||
| )) | ||
| .unwrap(); | ||
|
|
||
| let surface_format = if let Some(surface) = &surface { | ||
| surface.get_preferred_format(&adapter).unwrap() | ||
| } else { | ||
| // if Surface is none, we're guaranteed to be on android | ||
| wgpu::TextureFormat::Rgba8UnormSrgb | ||
| }; | ||
|
|
||
| let mut surface_config = wgpu::SurfaceConfiguration { | ||
| usage: wgpu::TextureUsages::RENDER_ATTACHMENT, | ||
| format: surface_format, | ||
| width: INITIAL_WIDTH, | ||
| height: INITIAL_HEIGHT, | ||
| present_mode: wgpu::PresentMode::Mailbox, | ||
| }; | ||
|
|
||
| if let Some(surface) = &mut surface { | ||
| surface.configure(&device, &surface_config); | ||
| } | ||
|
|
||
| let repaint_signal = std::sync::Arc::new(ExampleRepaintSignal(std::sync::Mutex::new( | ||
| event_loop.create_proxy(), | ||
| ))); | ||
|
|
||
| // We use the egui_wgpu_backend crate as the render backend. | ||
| let mut egui_rpass = RenderPass::new(&device, surface_format, 1); | ||
|
|
||
| // Display the demo application that ships with egui. | ||
| let mut demo_app = egui_demo_lib::WrapApp::default(); | ||
|
|
||
| let mut previous_frame_time = None; | ||
|
|
||
| let mut state = egui_winit::State::new(&window); | ||
| let mut ctx = egui::CtxRef::default(); | ||
|
|
||
| event_loop.run(move |event, _, control_flow| { | ||
| *control_flow = ControlFlow::Poll; | ||
|
|
||
| let mut redraw = || { | ||
| if let Some(surface) = &surface { | ||
| let output_frame = match surface.get_current_texture() { | ||
| Ok(frame) => frame, | ||
| Err(e) => { | ||
| eprintln!("Dropped frame with error: {}", e); | ||
| return; | ||
| } | ||
| }; | ||
| let output_view = output_frame | ||
| .texture | ||
| .create_view(&wgpu::TextureViewDescriptor::default()); | ||
|
|
||
| let egui_start = Instant::now(); | ||
| let raw_input: egui::RawInput = state.take_egui_input(&window); | ||
| ctx.begin_frame(raw_input); | ||
|
|
||
| let app_output = epi::backend::AppOutput { | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let frame_data = epi::backend::FrameData { | ||
| info: epi::IntegrationInfo { | ||
| name: "egui_winit", | ||
| web_info: None, | ||
| cpu_usage: previous_frame_time, | ||
| native_pixels_per_point: Some(window.scale_factor() as _), | ||
| prefer_dark_mode: None, | ||
| }, | ||
| output: app_output, | ||
| repaint_signal: repaint_signal.clone(), | ||
| }; | ||
|
|
||
| let frame = Frame::new(frame_data); | ||
|
|
||
| // Draw the demo application. | ||
| demo_app.update(&ctx, &frame); | ||
|
|
||
| // End the UI frame. We could now handle the output and draw the UI with the backend. | ||
| let (_output, paint_commands) = ctx.end_frame(); | ||
| let paint_jobs = ctx.tessellate(paint_commands); | ||
|
|
||
| let frame_time = (Instant::now() - egui_start).as_secs_f64() as f32; | ||
| previous_frame_time = Some(frame_time); | ||
|
|
||
| let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { | ||
| label: Some("encoder"), | ||
| }); | ||
|
|
||
| // Upload all resources for the GPU. | ||
| let screen_descriptor = ScreenDescriptor { | ||
| physical_width: surface_config.width, | ||
| physical_height: surface_config.height, | ||
| scale_factor: window.scale_factor() as f32, | ||
| }; | ||
| egui_rpass.update_texture(&device, &queue, &ctx.font_image()); | ||
| egui_rpass.update_user_textures(&device, &queue); | ||
| egui_rpass.update_buffers(&device, &queue, &paint_jobs, &screen_descriptor); | ||
|
|
||
| // Record all render passes. | ||
| egui_rpass | ||
| .execute( | ||
| &mut encoder, | ||
| &output_view, | ||
| &paint_jobs, | ||
| &screen_descriptor, | ||
| Some(wgpu::Color::BLACK), | ||
| ) | ||
| .unwrap(); | ||
| // Submit the commands. | ||
| queue.submit(iter::once(encoder.finish())); | ||
|
|
||
| // Redraw egui | ||
| output_frame.present(); | ||
| }; | ||
| }; | ||
| match event { | ||
| RedrawRequested(..) | MainEventsCleared => { | ||
| redraw(); | ||
| } | ||
| Resumed => { | ||
| let s = unsafe { instance.create_surface(&window) }; | ||
| surface_config.format = s.get_preferred_format(&adapter).unwrap(); | ||
| s.configure(&device, &surface_config); | ||
| surface = Some(s); | ||
| } | ||
| Suspended => { | ||
| surface = None; | ||
| } | ||
| WindowEvent { event, .. } => { | ||
| match event { | ||
| winit::event::WindowEvent::Resized(size) => { | ||
| if size.width != 0 && size.height != 0 { | ||
| // Recreate the swap chain with the new size | ||
| surface_config.width = size.width; | ||
| surface_config.height = size.height; | ||
| if let Some(surface) = &surface { | ||
| surface.configure(&device, &surface_config); | ||
| } | ||
| } | ||
| } | ||
| winit::event::WindowEvent::CloseRequested => { | ||
| *control_flow = ControlFlow::Exit; | ||
| } | ||
| _ => { | ||
| state.on_event(&ctx, &event); | ||
| } | ||
| }; | ||
| } | ||
| _ => (), | ||
| } | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's so that apps are in fullscreen by default and the status bar doesn't overlay the app's content (which I've seen was also an issue in other examples, such as the wgpu one)