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
use ggez::{
glam::Vec2,
graphics::{Canvas, Color, Mesh, MeshBuilder, Rect},
*,
};
#[derive(Clone, Copy, Debug, PartialEq)]
/// A struct that describes the additional visual elements of the background added to an element. This background will be drawn first and also contain the padding if any.
pub struct Visuals {
/// The color of the background.
pub background: Color,
/// The color of the border, if present.
pub border: Color,
/// The width of the borders.
/// Layout:
/// ```text
/// 0
/// +------+
/// 3 | | 1
/// +------+
/// 2
pub border_widths: [f32; 4],
/// The radius of the corners.
/// Layout:
/// ```text
/// 3 0
/// +------+
/// | |
/// +------+
/// 2 1
pub corner_radii: [f32; 4],
}
impl Visuals {
/// Returns a new Visuals with the specified contents.
pub fn new(background: Color, border: Color, border_width: f32, corner_radius: f32) -> Self {
Self {
background,
border,
border_widths: [border_width; 4],
corner_radii: [corner_radius; 4],
}
}
/// Returns a new Visuals with the specified contents.
pub fn new_custom(
background: Color,
border: Color,
border_widths: [f32; 4],
corner_radii: [f32; 4],
) -> Self {
Self {
background,
border,
border_widths,
corner_radii,
}
}
/// Draws the background to the canvas, filling the rectangle target.
pub(crate) fn draw(&self, ctx: &Context, canvas: &mut Canvas, param: super::UiDrawParam) {
canvas.draw(
&Mesh::from_data(
ctx,
self.create_mesh(param.target).unwrap_or_default().build(),
),
param.param,
);
}
/// Creates a mesh that can be used to draw this visual based on its attributes.
fn create_mesh(&self, target: Rect) -> Result<MeshBuilder, GameError> {
let tolerance = 1.;
let inner_radius: Vec<f32> = (0..4)
.map(|i| {
(self.corner_radii[i] - self.border_widths[i].max(self.border_widths[(i + 1) % 4]))
.max(0.)
})
.collect();
// --- Circles for outer corners
let mut mesh_builder = MeshBuilder::new();
mesh_builder
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + target.w - self.corner_radii[0],
target.y + self.corner_radii[0],
),
self.corner_radii[0],
tolerance,
self.border,
)?
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + target.w - self.corner_radii[1],
target.y + target.h - self.corner_radii[1],
),
self.corner_radii[1],
tolerance,
self.border,
)?
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + self.corner_radii[2],
target.y + target.h - self.corner_radii[2],
),
self.corner_radii[2],
tolerance,
self.border,
)?
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + self.corner_radii[3],
target.y + self.corner_radii[3],
),
self.corner_radii[3],
tolerance,
self.border,
)?;
// --- Outer Borders
mesh_builder
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x + self.corner_radii[3],
target.y,
target.w - self.corner_radii[3] - self.corner_radii[0],
target.h / 2.,
),
self.border,
)?
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x + target.w / 2.,
target.y + self.corner_radii[0],
target.w / 2.,
target.h - self.corner_radii[0] - self.corner_radii[1],
),
self.border,
)?
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x + self.corner_radii[2],
target.y + target.h / 2.,
target.w - self.corner_radii[1] - self.corner_radii[2],
target.h / 2.,
),
self.border,
)?
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x,
target.y + self.corner_radii[3],
target.w / 2.,
target.h - self.corner_radii[2] - self.corner_radii[3],
),
self.border,
)?;
// Circles for inner corners
mesh_builder
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + target.w - self.border_widths[1] - inner_radius[0],
target.y + self.border_widths[0] + inner_radius[0],
),
inner_radius[0],
tolerance,
self.background,
)?
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + target.w - self.border_widths[1] - inner_radius[1],
target.y + target.h - self.border_widths[2] - inner_radius[1],
),
inner_radius[1],
tolerance,
self.background,
)?
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + self.border_widths[3] + inner_radius[2],
target.y + target.h - self.border_widths[2] - inner_radius[2],
),
inner_radius[2],
tolerance,
self.background,
)?
.circle(
graphics::DrawMode::fill(),
Vec2::new(
target.x + self.border_widths[3] + inner_radius[3],
target.y + self.border_widths[0] + inner_radius[3],
),
inner_radius[3],
tolerance,
self.background,
)?;
// --- Rectangles for inner area.
mesh_builder
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x + self.border_widths[3] + inner_radius[3],
target.y + self.border_widths[0],
target.w
- self.border_widths[1]
- self.border_widths[3]
- inner_radius[3]
- inner_radius[0],
target.h / 2. - self.border_widths[0],
),
self.background,
)?
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x + target.w / 2.,
target.y + self.border_widths[0] + inner_radius[0],
target.w / 2. - self.border_widths[1],
target.h
- self.border_widths[0]
- self.border_widths[2]
- inner_radius[0]
- inner_radius[1],
),
self.background,
)?
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x + self.border_widths[3] + inner_radius[2],
target.y + target.h / 2.,
target.w
- self.border_widths[1]
- self.border_widths[3]
- inner_radius[1]
- inner_radius[2],
target.h / 2. - self.border_widths[2],
),
self.background,
)?
.rectangle(
graphics::DrawMode::fill(),
Rect::new(
target.x + self.border_widths[3],
target.y + self.border_widths[0] + inner_radius[3],
target.w / 2. - self.border_widths[3],
target.h
- self.border_widths[0]
- self.border_widths[2]
- inner_radius[2]
- inner_radius[3],
),
self.background,
)?;
Ok(mesh_builder)
}
/// Returns another visual that is the weighted (by ratio) average between ```self``` and ```other```.
/// ```ratio=0``` will return ```self```, ```ratio=1``` will return ```other```.
pub fn average(&self, other: Self, ratio: f32) -> Self {
Self {
background: Color::from_rgba(
((self.background.r * (1. - ratio) + other.background.r * ratio) * 256.) as u8,
((self.background.g * (1. - ratio) + other.background.g * ratio) * 256.) as u8,
((self.background.b * (1. - ratio) + other.background.b * ratio) * 256.) as u8,
((self.background.a * (1. - ratio) + other.background.a * ratio) * 256.) as u8,
),
border: Color::from_rgba(
((self.border.r * (1. - ratio) + other.border.r * ratio) * 256.) as u8,
((self.border.g * (1. - ratio) + other.border.g * ratio) * 256.) as u8,
((self.border.b * (1. - ratio) + other.border.b * ratio) * 256.) as u8,
((self.border.a * (1. - ratio) + other.border.a * ratio) * 256.) as u8,
),
border_widths: {
let mut bw = [0.; 4];
for (i, bw_c) in bw.iter_mut().enumerate() {
*bw_c = self.border_widths[i] * (1. - ratio) + other.border_widths[i] * ratio;
}
bw
},
corner_radii: {
let mut rc = [0.; 4];
for (i, rc_c) in rc.iter_mut().enumerate() {
*rc_c = self.corner_radii[i] * (1. - ratio) + other.corner_radii[i] * ratio;
}
rc
},
}
}
}
impl Default for Visuals {
fn default() -> Self {
Self {
background: Color::from_rgba(0, 0, 0, 0),
border: Color::from_rgba(0, 0, 0, 0),
border_widths: [0.; 4],
corner_radii: [0.; 4],
}
}
}