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
use {Color, Colorable, Point, Positionable, Rect, Scalar, Sizeable, Theme};
use graph;
use utils::{vec2_add, vec2_sub};
use widget::{self, Widget};
use widget::triangles::Triangle;
#[derive(Copy, Clone, Debug, WidgetCommon_)]
pub struct Line {
#[conrod(common_builder)]
pub common: widget::CommonBuilder,
pub start: Point,
pub end: Point,
pub style: Style,
pub should_centre_points: bool,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct State {
pub start: Point,
pub end: Point,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Style {
pub maybe_pattern: Option<Pattern>,
pub maybe_color: Option<Color>,
pub maybe_thickness: Option<Scalar>,
pub maybe_cap: Option<Cap>,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Pattern {
Solid,
Dashed,
Dotted,
}
#[allow(dead_code)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Cap {
Flat,
Round,
}
impl Line {
pub fn styled(start: Point, end: Point, style: Style) -> Self {
Line {
start: start,
end: end,
common: widget::CommonBuilder::default(),
style: style,
should_centre_points: false,
}
}
pub fn new(start: Point, end: Point) -> Self {
Line::styled(start, end, Style::new())
}
pub fn abs(start: Point, end: Point) -> Self {
Line::abs_styled(start, end, Style::new())
}
pub fn abs_styled(start: Point, end: Point, style: Style) -> Self {
let (xy, dim) = Rect::from_corners(start, end).xy_dim();
Line::styled(start, end, style).wh(dim).xy(xy)
}
pub fn centred(start: Point, end: Point) -> Self {
Line::centred_styled(start, end, Style::new())
}
pub fn centred_styled(start: Point, end: Point, style: Style) -> Self {
let dim = Rect::from_corners(start, end).dim();
let mut line = Line::styled(start, end, style).wh(dim);
line.should_centre_points = true;
line
}
pub fn thickness(mut self, thickness: Scalar) -> Self {
self.style.set_thickness(thickness);
self
}
pub fn solid(mut self) -> Self {
self.style.set_pattern(Pattern::Solid);
self
}
pub fn dashed(mut self) -> Self {
self.style.set_pattern(Pattern::Dashed);
self
}
pub fn dotted(mut self) -> Self {
self.style.set_pattern(Pattern::Dotted);
self
}
}
impl Style {
pub fn new() -> Self {
Style {
maybe_pattern: None,
maybe_color: None,
maybe_thickness: None,
maybe_cap: None,
}
}
pub fn solid() -> Self {
Style::new().pattern(Pattern::Solid)
}
pub fn dashed() -> Self {
Style::new().pattern(Pattern::Dashed)
}
pub fn dotted() -> Self {
Style::new().pattern(Pattern::Dotted)
}
pub fn pattern(mut self, pattern: Pattern) -> Self {
self.set_pattern(pattern);
self
}
pub fn color(mut self, color: Color) -> Self {
self.set_color(color);
self
}
pub fn thickness(mut self, thickness: Scalar) -> Self {
self.set_thickness(thickness);
self
}
pub fn cap(mut self, cap: Cap) -> Self {
self.set_cap(cap);
self
}
pub fn set_pattern(&mut self, pattern: Pattern) {
self.maybe_pattern = Some(pattern);
}
pub fn set_color(&mut self, color: Color) {
self.maybe_color = Some(color);
}
pub fn set_thickness(&mut self, thickness: Scalar) {
self.maybe_thickness = Some(thickness);
}
pub fn set_cap(&mut self, cap: Cap) {
self.maybe_cap = Some(cap);
}
pub fn get_pattern(&self, theme: &Theme) -> Pattern {
const DEFAULT_PATTERN: Pattern = Pattern::Solid;
self.maybe_pattern.or_else(|| theme.widget_style::<Style>().map(|default| {
default.style.maybe_pattern.unwrap_or(DEFAULT_PATTERN)
})).unwrap_or(DEFAULT_PATTERN)
}
pub fn get_color(&self, theme: &Theme) -> Color {
self.maybe_color.or_else(|| theme.widget_style::<Style>().map(|default| {
default.style.maybe_color.unwrap_or(theme.shape_color)
})).unwrap_or(theme.shape_color)
}
pub fn get_thickness(&self, theme: &Theme) -> Scalar {
const DEFAULT_THICKNESS: Scalar = 1.0;
self.maybe_thickness.or_else(|| theme.widget_style::<Style>().map(|default| {
default.style.maybe_thickness.unwrap_or(DEFAULT_THICKNESS)
})).unwrap_or(DEFAULT_THICKNESS)
}
pub fn get_cap(&self, theme: &Theme) -> Cap {
const DEFAULT_CAP: Cap = Cap::Flat;
self.maybe_cap.or_else(|| theme.widget_style::<Style>().map(|default| {
default.style.maybe_cap.unwrap_or(DEFAULT_CAP)
})).unwrap_or(DEFAULT_CAP)
}
}
impl Widget for Line {
type State = State;
type Style = Style;
type Event = ();
fn init_state(&self, _: widget::id::Generator) -> Self::State {
State {
start: [0.0, 0.0],
end: [0.0, 0.0],
}
}
fn style(&self) -> Self::Style {
self.style.clone()
}
fn is_over(&self) -> widget::IsOverFn {
is_over_widget
}
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { rect, state, .. } = args;
let Line { mut start, mut end, should_centre_points, .. } = self;
if should_centre_points {
let original = Rect::from_corners(start, end).xy();
let xy = rect.xy();
let difference = vec2_sub(xy, original);
start = vec2_add(start, difference);
end = vec2_add(end, difference);
}
if state.start != start {
state.update(|state| state.start = start);
}
if state.end != end {
state.update(|state| state.end = end);
}
}
}
impl Colorable for Line {
fn color(mut self, color: Color) -> Self {
self.style.maybe_color = Some(color);
self
}
}
pub fn rect_corners(a: Point, b: Point, half_thickness: Scalar) -> [Point; 4] {
let direction = [b[0] - a[0], b[1] - a[1]];
let mag = (direction[0] * direction[0] + direction[1] * direction[1]).sqrt();
let unit = [direction[0] / mag, direction[1] / mag];
let normal = [-unit[1], unit[0]];
let n = [normal[0] * half_thickness, normal[1] * half_thickness];
let r1 = [a[0] + n[0], a[1] + n[1]];
let r2 = [a[0] - n[0], a[1] - n[1]];
let r3 = [b[0] + n[0], b[1] + n[1]];
let r4 = [b[0] - n[0], b[1] - n[1]];
[r1, r2, r3, r4]
}
pub fn triangles(a: Point, b: Point, half_thickness: Scalar) -> [Triangle<Point>; 2] {
let r = rect_corners(a, b, half_thickness);
let t1 = Triangle([r[0], r[3], r[1]]);
let t2 = Triangle([r[0], r[3], r[2]]);
[t1, t2]
}
pub fn is_over(a: Point, b: Point, thickness: Scalar, point: Point) -> bool {
let half_thickness = thickness * 0.5;
let tris = triangles(a, b, half_thickness);
widget::triangles::is_over(tris.iter().cloned(), point)
}
pub fn is_over_widget(widget: &graph::Container, point: Point, theme: &Theme) -> widget::IsOver {
widget
.unique_widget_state::<Line>()
.map(|widget| {
let thickness = widget.style.get_thickness(theme);
let (a, b) = (widget.state.start, widget.state.end);
is_over(a, b, thickness, point)
})
.unwrap_or_else(|| widget.rect.is_over(point))
.into()
}