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
use super::Style;
use graph;
use std;
use widget;
use widget::triangles::Triangle;
use {Color, Colorable, Dimensions, Point, Rect, Scalar, Sizeable, Theme, Widget};
#[derive(Copy, Clone, Debug, WidgetCommon_)]
pub struct Oval<S> {
#[conrod(common_builder)]
pub common: widget::CommonBuilder,
pub style: Style,
pub resolution: usize,
pub section: S,
}
pub trait OvalSection: 'static + Copy + PartialEq + Send {
const IS_OVER: widget::IsOverFn;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Full;
impl OvalSection for Full {
const IS_OVER: widget::IsOverFn = is_over_widget;
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Section {
pub radians: Scalar,
pub offset_radians: Scalar,
}
impl OvalSection for Section {
const IS_OVER: widget::IsOverFn = is_over_section_widget;
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct State<S> {
pub resolution: usize,
pub section: S,
}
pub const DEFAULT_RESOLUTION: usize = 50;
impl Oval<Full> {
pub fn styled(dim: Dimensions, style: Style) -> Self {
Oval {
common: widget::CommonBuilder::default(),
style: style,
resolution: DEFAULT_RESOLUTION,
section: Full,
}
.wh(dim)
}
pub fn fill(dim: Dimensions) -> Self {
Oval::styled(dim, Style::fill())
}
pub fn fill_with(dim: Dimensions, color: Color) -> Self {
Oval::styled(dim, Style::fill_with(color))
}
pub fn outline(dim: Dimensions) -> Self {
Oval::styled(dim, Style::outline())
}
pub fn outline_styled(dim: Dimensions, line_style: widget::line::Style) -> Self {
Oval::styled(dim, Style::outline_styled(line_style))
}
}
impl<S> Oval<S> {
pub fn resolution(mut self, resolution: usize) -> Self {
self.resolution = resolution;
self
}
pub fn section(self, radians: Scalar) -> Oval<Section> {
let Oval {
common,
style,
resolution,
..
} = self;
let section = Section {
radians,
offset_radians: 0.0,
};
Oval {
common,
style,
resolution,
section,
}
}
}
impl Oval<Section> {
pub fn offset_radians(mut self, offset_radians: Scalar) -> Self {
self.section.offset_radians = offset_radians;
self
}
}
impl<S> Widget for Oval<S>
where
S: OvalSection,
{
type State = State<S>;
type Style = Style;
type Event = ();
fn init_state(&self, _: widget::id::Generator) -> Self::State {
State {
resolution: DEFAULT_RESOLUTION,
section: self.section,
}
}
fn style(&self) -> Self::Style {
self.style.clone()
}
fn is_over(&self) -> widget::IsOverFn {
S::IS_OVER
}
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { state, .. } = args;
if state.resolution != self.resolution {
state.update(|state| state.resolution = self.resolution);
}
if state.section != self.section {
state.update(|state| state.section = self.section);
}
}
}
impl<S> Colorable for Oval<S> {
fn color(mut self, color: Color) -> Self {
self.style.set_color(color);
self
}
}
pub fn circumference(rect: Rect, resolution: usize) -> Circumference {
Circumference::new(rect, resolution)
}
pub fn triangles(rect: Rect, resolution: usize) -> Triangles {
circumference(rect, resolution).triangles()
}
#[derive(Clone)]
#[allow(missing_copy_implementations)]
pub struct Circumference {
index: usize,
num_points: usize,
point: Point,
rad_step: Scalar,
rad_offset: Scalar,
half_w: Scalar,
half_h: Scalar,
}
impl Circumference {
fn new_inner(rect: Rect, num_points: usize, rad_step: Scalar) -> Self {
let (x, y, w, h) = rect.x_y_w_h();
Circumference {
index: 0,
num_points: num_points,
point: [x, y],
half_w: w * 0.5,
half_h: h * 0.5,
rad_step: rad_step,
rad_offset: 0.0,
}
}
pub fn new(rect: Rect, mut resolution: usize) -> Self {
resolution = std::cmp::max(resolution, 1);
use std::f64::consts::PI;
let radians = 2.0 * PI;
Self::new_section(rect, resolution, radians)
}
pub fn new_section(rect: Rect, resolution: usize, radians: Scalar) -> Self {
Self::new_inner(rect, resolution + 1, radians / resolution as Scalar)
}
}
#[derive(Clone)]
pub struct Triangles {
last: Point,
points: Circumference,
}
impl Circumference {
pub fn section(mut self, radians: Scalar) -> Self {
let resolution = self.num_points - 1;
self.rad_step = radians / resolution as Scalar;
self
}
pub fn offset_radians(mut self, radians: Scalar) -> Self {
self.rad_offset = radians;
self
}
pub fn triangles(mut self) -> Triangles {
let last = self.next().unwrap_or(self.point);
Triangles { last, points: self }
}
}
impl Iterator for Circumference {
type Item = Point;
fn next(&mut self) -> Option<Self::Item> {
let Circumference {
ref mut index,
num_points,
point,
rad_step,
rad_offset,
half_w,
half_h,
} = *self;
if *index >= num_points {
return None;
}
let x = point[0] + half_w * (rad_offset + rad_step * *index as Scalar).cos();
let y = point[1] + half_h * (rad_offset + rad_step * *index as Scalar).sin();
*index += 1;
Some([x, y])
}
}
impl Iterator for Triangles {
type Item = Triangle<Point>;
fn next(&mut self) -> Option<Self::Item> {
let Triangles {
ref mut points,
ref mut last,
} = *self;
points.next().map(|next| {
let triangle = Triangle([points.point, *last, next]);
*last = next;
triangle
})
}
}
pub fn is_over(r: Rect, p: Point) -> bool {
let (px, py) = (p[0], p[1]);
let (ox, oy, w, h) = r.x_y_w_h();
let rx = w * 0.5;
let ry = h * 0.5;
((px - ox).powi(2) / rx.powi(2) + (py - oy).powi(2) / ry.powi(2)) < 1.0
}
pub fn is_over_widget(widget: &graph::Container, point: Point, _: &Theme) -> widget::IsOver {
is_over(widget.rect, point).into()
}
pub fn is_over_section(circumference: Circumference, p: Point) -> bool {
widget::triangles::is_over(circumference.triangles(), p)
}
pub fn is_over_section_widget(widget: &graph::Container, p: Point, _: &Theme) -> widget::IsOver {
widget
.state_and_style::<State<Section>, Style>()
.map(|unique| {
let res = unique.state.resolution;
let offset_rad = unique.state.section.offset_radians;
let rad = unique.state.section.radians;
let circumference =
Circumference::new_section(widget.rect, res, rad).offset_radians(offset_rad);
is_over_section(circumference, p)
})
.unwrap_or_else(|| widget.rect.is_over(p))
.into()
}