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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
pub use self::conrod_core::event::Input;
pub use self::conrod_core::{
color, cursor, event, graph, image, input, position, scroll, text, theme, utils, widget,
widget_ids,
};
pub use self::conrod_core::{
Borderable, Bordering, Color, Colorable, Dimensions, FontSize, Labelable, Point, Positionable,
Range, Rect, Scalar, Sizeable, Theme, UiCell, Widget,
};
pub use crate::conrod_core;
pub use crate::conrod_wgpu;
pub use crate::conrod_winit;
pub mod prelude {
pub use super::{Borderable, Colorable, Labelable, Positionable, Sizeable, Widget};
pub use super::{
Bordering, Dimensions, FontSize, Input, Point, Range, Rect, Scalar, Theme, Ui, UiCell,
};
pub use super::{color, image, position, text, widget, widget_ids};
}
use self::conrod_core::text::rt::gpu_cache::CacheWriteErr;
use crate::frame::Frame;
use crate::text::{font, Font};
use crate::window::{self, Window};
use crate::{wgpu, App};
use std::cell::RefCell;
use std::collections::HashMap;
use std::error::Error as StdError;
use std::fmt;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::{mpsc, Arc, Mutex};
use winit;
pub(crate) struct Arrangement {
pub(super) windows: RefCell<HashMap<window::Id, Vec<Handle>>>,
}
pub(crate) struct Handle {
pub(crate) input_tx: Option<mpsc::SyncSender<Input>>,
}
pub struct Ui {
window_id: window::Id,
ui: conrod_core::Ui,
input_rx: Option<mpsc::Receiver<Input>>,
pub image_map: ImageMap,
renderer: Mutex<conrod_wgpu::Renderer>,
}
pub struct Builder<'a> {
app: &'a App,
window_id: Option<window::Id>,
dimensions: Option<[Scalar; 2]>,
theme: Option<Theme>,
automatically_handle_input: bool,
pending_input_limit: usize,
default_font_path: Option<PathBuf>,
glyph_cache_dimensions: Option<(u32, u32)>,
}
#[derive(Debug)]
pub enum BuildError {
InvalidWindow,
FailedToLoadFont(text::font::Error),
}
#[derive(Debug)]
pub enum DrawToFrameError {
InvalidWindow,
RendererPoisoned,
RenderModePoisoned,
InvalidRenderMode,
RendererFill(CacheWriteErr),
}
pub type ImageMap = conrod_core::image::Map<conrod_wgpu::Image>;
impl Arrangement {
pub(crate) fn new() -> Self {
let windows = RefCell::new(HashMap::new());
Arrangement { windows }
}
}
impl<'a> Builder<'a> {
pub(super) fn new(app: &'a App) -> Self {
Builder {
app,
window_id: None,
dimensions: None,
theme: None,
automatically_handle_input: true,
pending_input_limit: Ui::DEFAULT_PENDING_INPUT_LIMIT,
default_font_path: None,
glyph_cache_dimensions: None,
}
}
pub fn window(mut self, window_id: window::Id) -> Self {
self.window_id = Some(window_id);
self
}
pub fn with_dimensions(mut self, dimensions: [Scalar; 2]) -> Self {
self.dimensions = Some(dimensions);
self
}
pub fn with_theme(mut self, theme: Theme) -> Self {
self.theme = Some(theme);
self
}
pub fn automatically_handle_input(mut self, b: bool) -> Self {
self.automatically_handle_input = b;
self
}
pub fn pending_input_limit(mut self, limit: usize) -> Self {
self.pending_input_limit = limit;
self
}
pub fn default_font_path(mut self, path: PathBuf) -> Self {
self.default_font_path = Some(path);
self
}
pub fn with_glyph_cache_dimensions(mut self, width: u32, height: u32) -> Self {
self.glyph_cache_dimensions = Some((width, height));
self
}
pub fn build(self) -> Result<Ui, BuildError> {
let Builder {
app,
window_id,
dimensions,
theme,
pending_input_limit,
automatically_handle_input,
default_font_path,
glyph_cache_dimensions,
} = self;
let window_id = window_id.unwrap_or(app.window_id());
let window = app.window(window_id).ok_or(BuildError::InvalidWindow)?;
let msaa_samples = window.msaa_samples();
let dimensions = dimensions.unwrap_or_else(|| {
let (win_w, win_h) = window.inner_size_points();
[win_w as Scalar, win_h as Scalar]
});
let theme = theme.unwrap_or_else(Theme::default);
let ui = conrod_core::UiBuilder::new(dimensions).theme(theme).build();
let (input_rx, handle) = if automatically_handle_input {
let (input_tx, input_rx) = mpsc::sync_channel(pending_input_limit);
let input_tx = Some(input_tx);
let input_rx = Some(input_rx);
let handle = Handle { input_tx };
(input_rx, handle)
} else {
let input_tx = None;
let input_rx = None;
let handle = Handle { input_tx };
(input_rx, handle)
};
app.ui
.windows
.borrow_mut()
.entry(window_id)
.or_insert(Vec::new())
.push(handle);
let device = window.swap_chain_device().clone();
let texture_format = Frame::TEXTURE_FORMAT;
let renderer = match glyph_cache_dimensions {
Some((w, h)) => {
let dims = [w as _, h as _];
conrod_wgpu::Renderer::with_glyph_cache_dimensions(
device,
msaa_samples,
texture_format,
dims,
)
}
None => conrod_wgpu::Renderer::new(device, msaa_samples, texture_format),
};
let renderer = Mutex::new(renderer);
let image_map = image::Map::new();
let mut ui = Ui {
window_id,
ui,
input_rx,
image_map,
renderer,
};
let default_font = default_font(default_font_path.as_ref().map(|path| path.as_path()))?;
ui.fonts_mut().insert(default_font);
Ok(ui)
}
}
impl Ui {
pub const DEFAULT_PENDING_INPUT_LIMIT: usize = 1024;
pub fn generate_widget_id(&mut self) -> widget::Id {
self.widget_id_generator().next()
}
pub fn widget_id_generator(&mut self) -> widget::id::Generator {
self.ui.widget_id_generator()
}
pub fn handle_input(&mut self, input: Input) {
self.ui.handle_event(input)
}
pub fn handle_pending_input(&mut self) {
let Ui {
ref mut ui,
ref input_rx,
..
} = *self;
if let Some(ref rx) = *input_rx {
for input in rx.try_iter() {
ui.handle_event(input);
}
}
}
pub fn set_widgets(&mut self) -> UiCell {
self.handle_pending_input();
self.ui.set_widgets()
}
pub fn fonts_mut(&mut self) -> &mut text::font::Map {
&mut self.ui.fonts
}
pub fn theme_mut(&mut self) -> &mut Theme {
&mut self.ui.theme
}
pub fn clear_with(&mut self, color: Color) {
self.ui.clear_with(color)
}
pub fn draw_to_frame(&self, app: &App, frame: &Frame) -> Result<(), DrawToFrameError> {
let primitives = self.ui.draw();
let color_attachment_desc = frame.color_attachment_descriptor();
let mut command_encoder = frame.command_encoder();
let window = app
.window(self.window_id)
.ok_or(DrawToFrameError::InvalidWindow)?;
encode_render_pass(
self,
&*window,
primitives,
color_attachment_desc,
&mut *command_encoder,
)
}
pub fn draw_to_frame_if_changed(
&self,
app: &App,
frame: &Frame,
) -> Result<bool, DrawToFrameError> {
match self.ui.draw_if_changed() {
None => Ok(false),
Some(primitives) => {
let window = app
.window(self.window_id)
.ok_or(DrawToFrameError::InvalidWindow)?;
let color_attachment_desc = frame.color_attachment_descriptor();
let mut command_encoder = frame.command_encoder();
encode_render_pass(
self,
&*window,
primitives,
color_attachment_desc,
&mut *command_encoder,
)?;
Ok(true)
}
}
}
}
impl wgpu::Texture {
pub fn into_ui_image(self) -> conrod_wgpu::Image {
let texture_format = self.format();
let [width, height] = self.size();
let texture = Arc::try_unwrap(self.into_inner())
.expect("converting to UI image requires unique access to texture");
conrod_wgpu::Image {
texture,
texture_format,
width,
height,
}
}
}
pub fn encode_render_pass(
ui: &Ui,
window: &Window,
primitives: conrod_core::render::Primitives,
color_attachment_desc: wgpu::RenderPassColorAttachment,
encoder: &mut wgpu::CommandEncoder,
) -> Result<(), DrawToFrameError> {
let mut renderer = ui
.renderer
.lock()
.ok()
.ok_or(DrawToFrameError::RendererPoisoned)?;
let device = window.swap_chain_device();
let scale_factor = window.scale_factor();
let (win_w, win_h) = window.inner_size_pixels();
let viewport = [0.0, 0.0, win_w as f32, win_h as f32];
if let Some(cmd) = renderer
.fill(&ui.image_map, viewport, scale_factor as f64, primitives)
.unwrap()
{
cmd.load_buffer_and_encode(&device, encoder);
}
let render_pass_desc = wgpu::RenderPassDescriptor {
label: Some("nannou_ui_render_pass_descriptor"),
color_attachments: &[color_attachment_desc],
depth_stencil_attachment: None,
};
let render = renderer.render(&device, &ui.image_map);
{
let mut render_pass = encoder.begin_render_pass(&render_pass_desc);
render_pass.set_vertex_buffer(0, render.vertex_buffer.slice(..));
let instance_range = 0..1;
for cmd in render.commands {
match cmd {
conrod_wgpu::RenderPassCommand::SetPipeline { pipeline } => {
render_pass.set_pipeline(pipeline);
}
conrod_wgpu::RenderPassCommand::SetBindGroup { bind_group } => {
render_pass.set_bind_group(0, bind_group, &[]);
}
conrod_wgpu::RenderPassCommand::SetScissor {
top_left,
dimensions,
} => {
let [x, y] = top_left;
let [w, h] = dimensions;
render_pass.set_scissor_rect(x, y, w, h);
}
conrod_wgpu::RenderPassCommand::Draw { vertex_range } => {
render_pass.draw(vertex_range, instance_range.clone());
}
}
}
}
Ok(())
}
mod conrod_winit_conv {
conrod_winit::v023_conversion_fns!();
}
pub fn winit_window_event_to_input(
event: &winit::event::WindowEvent,
window: &Window,
) -> Option<Input> {
conrod_winit_conv::convert_window_event(event, &window.window)
}
impl Deref for Ui {
type Target = conrod_core::Ui;
fn deref(&self) -> &Self::Target {
&self.ui
}
}
impl From<text::font::Error> for BuildError {
fn from(err: text::font::Error) -> Self {
BuildError::FailedToLoadFont(err)
}
}
impl From<CacheWriteErr> for DrawToFrameError {
fn from(err: CacheWriteErr) -> Self {
DrawToFrameError::RendererFill(err)
}
}
impl StdError for BuildError {
fn cause(&self) -> Option<&dyn StdError> {
match *self {
BuildError::InvalidWindow => None,
BuildError::FailedToLoadFont(ref err) => Some(err),
}
}
}
impl StdError for DrawToFrameError {
fn cause(&self) -> Option<&dyn StdError> {
match *self {
DrawToFrameError::InvalidWindow => None,
DrawToFrameError::RendererPoisoned => None,
DrawToFrameError::RenderModePoisoned => None,
DrawToFrameError::InvalidRenderMode => None,
DrawToFrameError::RendererFill(ref err) => Some(err),
}
}
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BuildError::InvalidWindow => {
write!(f, "no open window associated with the given `window_id`")
}
BuildError::FailedToLoadFont(ref err) => fmt::Display::fmt(err, f),
}
}
}
impl fmt::Display for DrawToFrameError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match *self {
DrawToFrameError::InvalidWindow => {
"no open window associated with the given `window_id`"
}
DrawToFrameError::RendererPoisoned => "`Mutex` containing `Renderer` was poisoned",
DrawToFrameError::RenderModePoisoned => "`Mutex` containing `RenderMode` was poisoned",
DrawToFrameError::InvalidRenderMode => {
"`draw_to_frame` was called while `Ui` was in `Subpass` render mode"
}
DrawToFrameError::RendererFill(ref err) => return fmt::Display::fmt(err, f),
};
write!(f, "{}", s)
}
}
fn default_font(default_font_path: Option<&Path>) -> Result<Font, text::font::Error> {
fn conv_err(err: font::Error) -> text::font::Error {
match err {
font::Error::Io(err) => text::font::Error::IO(err),
font::Error::NoFont => text::font::Error::NoFont,
}
}
let font = match default_font_path {
None => {
#[cfg(feature = "notosans")]
{
font::default_notosans()
}
#[cfg(not(feature = "notosans"))]
{
match crate::app::find_assets_path() {
Err(_err) => return Err(text::font::Error::NoFont)?,
Ok(assets) => font::default(&assets).map_err(conv_err)?,
}
}
}
Some(path) => {
let font = font::from_file(path).map_err(conv_err)?;
font
}
};
Ok(font)
}