-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
gui.rs
293 lines (258 loc) · 9.8 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! GUI-related code
//!
//! If at some point you use `Painter` or similar and you get this error:
//!
//! `VK_ERROR_NATIVE_WINDOW_IN_USE_KHR`
//!
//! and/or:
//!
//! `wgpu_core::device: surface configuration failed: Native window is in use`
//!
//! it's *probably(?)* because the swap chain has already been created for the
//! window (e.g. by an integration) and *not* because of a regression of this
//! issue (probably):
//!
//! <https://github.com/gfx-rs/wgpu/issues/1492>
use fj_interop::status_report::StatusReport;
use fj_math::{Aabb, Scalar};
use crate::graphics::DrawConfig;
/// The GUI
pub struct Gui {
context: egui::Context,
render_pass: egui_wgpu::renderer::RenderPass,
options: Options,
}
impl Gui {
pub(crate) fn new(
device: &wgpu::Device,
texture_format: wgpu::TextureFormat,
) -> Self {
// The implementation of the integration with `egui` is likely to need
// to change "significantly" depending on what architecture approach is
// chosen going forward.
//
// The current implementation is somewhat complicated by virtue of
// "sitting somewhere in the middle" in relation to being neither a
// standalone integration nor fully using `egui` as a framework.
//
// This is a result of a combination of the current integration being
// "proof of concept" level, and using `egui-winit` & `egui-wgpu`, which
// are both relatively new additions to the core `egui` ecosystem.
//
// It is recommended to read the following for additional helpful
// context for choosing an architecture:
//
// - https://github.com/emilk/egui/blob/eeae485629fca24a81a7251739460b671e1420f7/README.md#what-is-the-difference-between-egui-and-eframe
// - https://github.com/emilk/egui/blob/eeae485629fca24a81a7251739460b671e1420f7/README.md#how-do-i-render-3d-stuff-in-an-egui-area
let context = egui::Context::default();
// We need to hold on to this, otherwise it might cause the egui font
// texture to get dropped after drawing one frame.
//
// This then results in an `egui_wgpu_backend` error of
// `BackendError::Internal` with message:
//
// ```
// Texture 0 used but not live
// ```
//
// See also: <https://github.com/hasenbanck/egui_wgpu_backend/blob/b2d3e7967351690c6425f37cd6d4ffb083a7e8e6/src/lib.rs#L373>
let render_pass =
egui_wgpu::renderer::RenderPass::new(device, texture_format, 1);
Self {
context,
render_pass,
options: Default::default(),
}
}
/// Access the egui context
pub fn context(&self) -> &egui::Context {
&self.context
}
pub(crate) fn update(
&mut self,
egui_input: egui::RawInput,
config: &mut DrawConfig,
aabb: &Aabb<3>,
status: &StatusReport,
line_drawing_available: bool,
) {
self.context.begin_frame(egui_input);
let bounding_box_size = {
let [x, y, z] = aabb.size().components.map(Scalar::into_f32);
format!("Model bounding box size:\n{x:0.1} {y:0.1} {z:0.1}")
};
egui::SidePanel::left("fj-left-panel").show(&self.context, |ui| {
ui.add_space(16.0);
ui.group(|ui| {
ui.checkbox(&mut config.draw_model, "Render model")
.on_hover_text_at_pointer("Toggle with 1");
ui.add_enabled(line_drawing_available, egui::Checkbox::new(&mut config.draw_mesh, "Render mesh"))
.on_hover_text_at_pointer("Toggle with 2")
.on_disabled_hover_text(
"Rendering device does not have line rendering feature support",
);
ui.add_enabled(line_drawing_available, egui::Checkbox::new(&mut config.draw_debug, "Render debug"))
.on_hover_text_at_pointer("Toggle with 3")
.on_disabled_hover_text(
"Rendering device does not have line rendering feature support"
);
ui.add_space(16.0);
ui.strong(bounding_box_size);
});
ui.add_space(16.0);
{
ui.group(|ui| {
ui.checkbox(
&mut self.options.show_settings_ui,
"Show egui settings UI",
);
if self.options.show_settings_ui {
self.context.settings_ui(ui);
}
});
ui.add_space(16.0);
ui.group(|ui| {
ui.checkbox(
&mut self.options.show_inspection_ui,
"Show egui inspection UI",
);
if self.options.show_inspection_ui {
ui.indent("indent-inspection-ui", |ui| {
self.context.inspection_ui(ui);
});
}
});
}
ui.add_space(16.0);
{
//
// Originally this was only meant to be a simple demonstration
// of the `egui` `trace!()` macro...
//
// ...but it seems the trace feature can't be enabled
// separately from the layout debug feature, which all
// gets a bit messy...
//
// ...so, this instead shows one possible way to implement
// "trace only" style debug text on hover.
//
ui.group(|ui| {
let label_text = format!(
"Show debug text demo.{}",
if self.options.show_debug_text_example {
" (Hover me.)"
} else {
""
}
);
ui.style_mut().wrap = Some(false);
if ui
.checkbox(
&mut self.options.show_debug_text_example,
label_text,
)
.hovered()
&& self.options.show_debug_text_example
{
let hover_pos =
ui.input().pointer.hover_pos().unwrap_or_default();
ui.painter().debug_text(
hover_pos,
egui::Align2::LEFT_TOP,
egui::Color32::DEBUG_COLOR,
format!("{:#?}", &config),
);
}
});
}
ui.add_space(16.0);
{
//
// Demonstration of the `egui` layout debug functionality.
//
ui.group(|ui| {
//
if ui
.checkbox(
&mut self.options.show_layout_debug_on_hover,
"Show layout debug on hover.",
)
.changed()
{
ui.ctx().set_debug_on_hover(
self.options.show_layout_debug_on_hover,
);
}
ui.scope(|ui| {
if self.options.show_trace {
egui::trace!(ui, format!("{:?}", &config));
}
});
ui.indent("indent-show-trace", |ui| {
ui.set_enabled(
self.options.show_layout_debug_on_hover,
);
ui.checkbox(
&mut self.options.show_trace,
"Also show egui trace.",
);
//
});
});
}
ui.add_space(16.0);
});
egui::Area::new("fj-status-message").show(&self.context, |ui| {
ui.group(|ui| {
ui.add(egui::Label::new(
egui::RichText::new(format!("Status:{}", status.status()))
.color(egui::Color32::BLACK),
))
})
});
}
pub(crate) fn draw(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
color_view: &wgpu::TextureView,
screen_descriptor: egui_wgpu::renderer::ScreenDescriptor,
) {
let egui_output = self.context.end_frame();
let clipped_primitives = self.context.tessellate(egui_output.shapes);
for (id, image_delta) in &egui_output.textures_delta.set {
self.render_pass
.update_texture(device, queue, *id, image_delta);
}
for id in &egui_output.textures_delta.free {
self.render_pass.free_texture(id);
}
self.render_pass.update_buffers(
device,
queue,
&clipped_primitives,
&screen_descriptor,
);
self.render_pass.execute(
encoder,
color_view,
&clipped_primitives,
&screen_descriptor,
None,
);
}
}
impl std::fmt::Debug for Gui {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Gui {}")
}
}
#[derive(Default)]
pub struct Options {
pub show_trace: bool,
pub show_layout_debug_on_hover: bool,
pub show_debug_text_example: bool,
pub show_settings_ui: bool,
pub show_inspection_ui: bool,
}