-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
gui.rs
144 lines (127 loc) · 4.39 KB
/
gui.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use egui::{ClippedMesh, CtxRef};
use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor};
use pixels::{wgpu, PixelsContext};
use winit::window::Window;
/// Manages all state required for rendering egui over `Pixels`.
pub(crate) struct Framework {
// State for egui.
egui_ctx: CtxRef,
egui_state: egui_winit::State,
screen_descriptor: ScreenDescriptor,
rpass: RenderPass,
paint_jobs: Vec<ClippedMesh>,
// 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 Framework {
/// Create egui.
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,
};
let rpass = RenderPass::new(pixels.device(), pixels.render_texture_format(), 1);
let gui = Gui::new();
Self {
egui_ctx,
egui_state,
screen_descriptor,
rpass,
paint_jobs: Vec::new(),
gui,
}
}
/// Handle input events from the window manager.
pub(crate) fn handle_event(&mut self, event: &winit::event::WindowEvent) {
self.egui_state.on_event(&self.egui_ctx, event);
}
/// Resize egui.
pub(crate) fn resize(&mut self, width: u32, height: u32) {
if width > 0 && height > 0 {
self.screen_descriptor.physical_width = width;
self.screen_descriptor.physical_height = height;
}
}
/// Update scaling factor.
pub(crate) fn scale_factor(&mut self, scale_factor: f64) {
self.screen_descriptor.scale_factor = scale_factor as f32;
}
/// Prepare egui.
pub(crate) fn prepare(&mut self, window: &Window) {
// Run the egui frame and create all paint jobs to prepare for rendering.
let raw_input = self.egui_state.take_egui_input(window);
let (output, paint_commands) = self.egui_ctx.run(raw_input, |egui_ctx| {
// Draw the demo application.
self.gui.ui(egui_ctx);
});
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.font_image());
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.
fn ui(&mut self, ctx: &CtxRef) {
egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("About...").clicked() {
self.window_open = true;
ui.close_menu();
}
})
});
});
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");
});
});
}
}