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
use {Color, Colorable, Dimensions, Point, Rect, Scalar, Sizeable, Widget};
use super::Style as Style;
use widget;
#[derive(Copy, Clone, Debug, WidgetCommon_)]
pub struct Oval {
#[conrod(common_builder)]
pub common: widget::CommonBuilder,
pub style: Style,
pub resolution: usize,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct State {
pub resolution: usize,
}
pub const DEFAULT_RESOLUTION: usize = 50;
impl Oval {
pub fn styled(dim: Dimensions, style: Style) -> Self {
Oval {
common: widget::CommonBuilder::default(),
style: style,
resolution: DEFAULT_RESOLUTION,
}.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))
}
pub fn resolution(mut self, resolution: usize) -> Self {
self.resolution = resolution;
self
}
}
impl Widget for Oval {
type State = State;
type Style = Style;
type Event = ();
fn init_state(&self, _: widget::id::Generator) -> Self::State {
State {
resolution: DEFAULT_RESOLUTION,
}
}
fn style(&self) -> Self::Style {
self.style.clone()
}
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);
}
}
}
impl Colorable for Oval {
fn color(mut self, color: Color) -> Self {
self.style.set_color(color);
self
}
}
pub fn circumference(rect: Rect, resolution: usize) -> Circumference {
use std::f64::consts::PI;
let (x, y, w, h) = rect.x_y_w_h();
Circumference {
index: 0,
num_points: resolution + 1,
point: [x, y],
half_w: w / 2.0,
half_h: h / 2.0,
rad_step: 2.0 * PI / resolution as Scalar,
}
}
pub fn triangles(rect: Rect, resolution: usize) -> Option<Triangles> {
widget::polygon::triangles(circumference(rect, resolution))
}
#[derive(Clone)]
#[allow(missing_copy_implementations)]
pub struct Circumference {
index: usize,
num_points: usize,
point: Point,
rad_step: Scalar,
half_w: Scalar,
half_h: Scalar,
}
pub type Triangles = widget::polygon::Triangles<Circumference>;
impl Iterator for Circumference {
type Item = Point;
fn next(&mut self) -> Option<Self::Item> {
let Circumference { ref mut index, num_points, point, rad_step, half_w, half_h } = *self;
if *index >= num_points { return None; } else { *index += 1; }
let x = point[0] + half_w * (rad_step * *index as Scalar).cos();
let y = point[1] + half_h * (rad_step * *index as Scalar).sin();
Some([x, y])
}
}