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
mod button;
mod checkbox;
mod image;
mod panel;
mod progress_bar;
mod radio;
mod slider;
mod text;
use crate::graphics::{Batch, Color, Font, Frame, Image, Mesh, Shape};
use crate::load::{Join, Task};
use crate::ui::core;
use std::cell::RefCell;
use std::rc::Rc;
pub struct Renderer {
pub(crate) sprites: Batch,
pub(crate) images: Vec<Batch>,
pub(crate) font: Rc<RefCell<Font>>,
explain_mesh: Mesh,
}
impl std::fmt::Debug for Renderer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Renderer")
.field("sprites", &self.sprites)
.field("images", &self.images)
.finish()
}
}
impl core::Renderer for Renderer {
type Configuration = Configuration;
fn load(config: Configuration) -> Task<Renderer> {
(config.sprites, config.font)
.join()
.map(|(sprites, font)| Renderer {
sprites: Batch::new(sprites),
images: Vec::new(),
font: Rc::new(RefCell::new(font)),
explain_mesh: Mesh::new(),
})
}
fn explain(&mut self, layout: &core::Layout<'_>, color: Color) {
self.explain_mesh
.stroke(Shape::Rectangle(layout.bounds()), color, 1.0);
layout
.children()
.for_each(|layout| self.explain(&layout, color));
}
fn flush(&mut self, frame: &mut Frame<'_>) {
let target = &mut frame.as_target();
self.sprites.draw(target);
self.sprites.clear();
for image in &self.images {
image.draw(target);
}
self.images.clear();
self.font.borrow_mut().draw(target);
if !self.explain_mesh.is_empty() {
self.explain_mesh.draw(target);
self.explain_mesh = Mesh::new();
}
}
}
#[derive(Debug)]
pub struct Configuration {
pub sprites: Task<Image>,
pub font: Task<Font>,
}
impl Default for Configuration {
fn default() -> Self {
Self {
sprites: Task::using_gpu(|gpu| {
Image::from_image(
gpu,
&::image::load_from_memory(include_bytes!(
"../../resources/ui.png"
))?,
)
}),
font: Font::load_from_bytes(include_bytes!(
"../../resources/font/Inconsolata-Regular.ttf"
)),
}
}
}