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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
use {
CharacterCache,
Circle,
Color,
Colorable,
Dimension,
Direction,
Edge,
Frameable,
FramedRectangle,
FontSize,
IndexSlot,
Labelable,
Mouse,
NodeIndex,
Point,
PointPath,
Positionable,
Scalar,
Sizeable,
Text,
Theme,
Ui,
Widget,
};
use num::Float;
use std::any::Any;
use std::cmp::Ordering;
use std::default::Default;
use std::fmt::Debug;
use utils::{clamp, map_range, percentage, val_to_string};
use widget;
pub struct EnvelopeEditor<'a, E:'a, F> where E: EnvelopePoint {
common: widget::CommonBuilder,
env: &'a mut Vec<E>,
skew_y_range: f32,
min_x: E::X, max_x: E::X,
min_y: E::Y, max_y: E::Y,
maybe_react: Option<F>,
maybe_label: Option<&'a str>,
style: Style,
enabled: bool,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Style {
pub maybe_color: Option<Color>,
pub maybe_frame: Option<f64>,
pub maybe_frame_color: Option<Color>,
pub maybe_label_color: Option<Color>,
pub maybe_label_font_size: Option<FontSize>,
pub maybe_value_font_size: Option<FontSize>,
pub maybe_point_radius: Option<f64>,
pub maybe_line_thickness: Option<f64>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct State<E> where E: EnvelopePoint {
interaction: Interaction,
env: Vec<E>,
min_x: E::X,
max_x: E::X,
min_y: E::Y,
max_y: E::Y,
skew_y_range: f32,
rectangle_idx: IndexSlot,
label_idx: IndexSlot,
value_label_idx: IndexSlot,
point_path_idx: IndexSlot,
point_indices: Vec<NodeIndex>,
}
pub const KIND: widget::Kind = "EnvelopeEditor";
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Interaction {
Normal,
Highlighted(Elem),
Clicked(Elem, MouseButton),
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Elem {
Rect,
Pad,
EnvPoint(usize, (f64, f64)),
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum MouseButton {
Left,
Right,
}
pub trait EnvelopePoint: Any + Clone + Debug + PartialEq {
type X: Any + Debug + Default + Float + ToString;
type Y: Any + Debug + Default + Float + ToString;
fn get_x(&self) -> Self::X;
fn get_y(&self) -> Self::Y;
fn set_x(&mut self, _x: Self::X);
fn set_y(&mut self, _y: Self::Y);
fn get_curve(&self) -> f32 { 1.0 }
fn set_curve(&mut self, _curve: f32) {}
fn new(_x: Self::X, _y: Self::Y) -> Self;
}
impl EnvelopePoint for Point {
type X = Scalar;
type Y = Scalar;
fn get_x(&self) -> Scalar { self[0] }
fn get_y(&self) -> Scalar { self[1] }
fn set_x(&mut self, x: Scalar) { self[0] = x }
fn set_y(&mut self, y: Scalar) { self[1] = y }
fn new(x: Scalar, y: Scalar) -> Point { [x, y] }
}
impl Interaction {
fn color(&self, color: Color) -> Color {
match *self {
Interaction::Normal => color,
Interaction::Highlighted(_) => color.highlighted(),
Interaction::Clicked(_, _) => color.clicked(),
}
}
}
fn get_new_interaction(is_over_elem: Option<Elem>,
prev: Interaction,
mouse: Mouse) -> Interaction {
use mouse::ButtonPosition::{Down, Up};
use self::Elem::{EnvPoint};
use self::MouseButton::{Left, Right};
use self::Interaction::{Normal, Highlighted, Clicked};
match (is_over_elem, prev, mouse.left.position, mouse.right.position) {
(Some(_), Normal, Down, Up) => Normal,
(Some(elem), _, Up, Up) => Highlighted(elem),
(Some(elem), Highlighted(_), Down, Up) => Clicked(elem, Left),
(Some(_), Clicked(p_elem, m_button), Down, Up) |
(Some(_), Clicked(p_elem, m_button), Up, Down) => {
match p_elem {
EnvPoint(idx, _) => Clicked(EnvPoint(idx, (mouse.xy[0], mouse.xy[1])), m_button),
_ => Clicked(p_elem, m_button),
}
},
(None, Clicked(p_elem, m_button), Down, Up) => {
match (p_elem, m_button) {
(EnvPoint(idx, _), Left) =>
Clicked(EnvPoint(idx, (mouse.xy[0], mouse.xy[1])), Left),
_ => Clicked(p_elem, Left),
}
},
(Some(_), Highlighted(p_elem), Up, Down) => {
match p_elem {
EnvPoint(idx, _) => Clicked(EnvPoint(idx, (mouse.xy[0], mouse.xy[1])), Right),
_ => Clicked(p_elem, Right),
}
},
_ => Normal,
}
}
impl<'a, E, F> EnvelopeEditor<'a, E, F> where E: EnvelopePoint {
#[inline]
pub fn point_radius(mut self, radius: f64) -> EnvelopeEditor<'a, E, F> {
self.style.maybe_point_radius = Some(radius);
self
}
#[inline]
pub fn line_thickness(mut self, width: f64) -> EnvelopeEditor<'a, E, F> {
self.style.maybe_line_thickness = Some(width);
self
}
#[inline]
pub fn value_font_size(mut self, size: FontSize) -> EnvelopeEditor<'a, E, F> {
self.style.maybe_value_font_size = Some(size);
self
}
#[inline]
pub fn skew_y(self, skew: f32) -> EnvelopeEditor<'a, E, F> {
EnvelopeEditor { skew_y_range: skew, ..self }
}
pub fn new(env: &'a mut Vec<E>, min_x: E::X, max_x: E::X, min_y: E::Y, max_y: E::Y)
-> EnvelopeEditor<'a, E, F> {
EnvelopeEditor {
common: widget::CommonBuilder::new(),
env: env,
skew_y_range: 1.0,
min_x: min_x, max_x: max_x,
min_y: min_y, max_y: max_y,
maybe_react: None,
maybe_label: None,
style: Style::new(),
enabled: true,
}
}
pub fn react(mut self, reaction: F) -> EnvelopeEditor<'a, E, F> {
self.maybe_react = Some(reaction);
self
}
pub fn enabled(mut self, flag: bool) -> Self {
self.enabled = flag;
self
}
}
impl<'a, E, F> Widget for EnvelopeEditor<'a, E, F>
where
E: EnvelopePoint,
E::X: Any,
E::Y: Any,
F: FnMut(&mut Vec<E>, usize),
{
type State = State<E>;
type Style = Style;
fn common(&self) -> &widget::CommonBuilder {
&self.common
}
fn common_mut(&mut self) -> &mut widget::CommonBuilder {
&mut self.common
}
fn unique_kind(&self) -> widget::Kind {
KIND
}
fn init_state(&self) -> State<E> {
State {
interaction: Interaction::Normal,
env: Vec::new(),
min_x: self.min_x,
max_x: self.max_x,
min_y: self.min_y,
max_y: self.max_y,
skew_y_range: self.skew_y_range,
rectangle_idx: IndexSlot::new(),
label_idx: IndexSlot::new(),
value_label_idx: IndexSlot::new(),
point_path_idx: IndexSlot::new(),
point_indices: Vec::new(),
}
}
fn style(&self) -> Style {
self.style.clone()
}
fn default_x_dimension<C: CharacterCache>(&self, ui: &Ui<C>) -> Dimension {
widget::default_x_dimension(self, ui).unwrap_or(Dimension::Absolute(256.0))
}
fn default_y_dimension<C: CharacterCache>(&self, ui: &Ui<C>) -> Dimension {
widget::default_y_dimension(self, ui).unwrap_or(Dimension::Absolute(128.0))
}
fn update<C: CharacterCache>(self, args: widget::UpdateArgs<Self, C>) {
use self::Interaction::{Clicked, Highlighted, Normal};
let widget::UpdateArgs { idx, state, rect, style, mut ui, .. } = args;
let EnvelopeEditor {
env,
skew_y_range,
min_x, max_x,
min_y, max_y,
mut maybe_react,
maybe_label,
enabled,
..
} = self;
let maybe_mouse = ui.input().maybe_mouse;
let skew = skew_y_range;
let point_radius = style.point_radius(ui.theme());
let frame = style.frame(ui.theme());
let inner_rect = rect.pad(frame);
let map_x_to = |x: E::X, start: Scalar, end: Scalar| -> Scalar {
map_range(x, min_x, max_x, start, end)
};
let map_y_to = |y: E::Y, start: Scalar, end: Scalar| -> Scalar {
let skewed_perc = percentage(y, min_y, max_y).powf(1.0 / skew_y_range);
map_range(skewed_perc, 0.0, 1.0, start, end)
};
let map_to_x = |value: Scalar, start: Scalar, end: Scalar| -> E::X {
map_range(value, start, end, min_x, max_x)
};
let map_to_y = |value: Scalar, start: Scalar, end: Scalar| -> E::Y {
let unskewed_perc = percentage(value, start, end).powf(skew_y_range);
map_range(unskewed_perc, 0.0, 1.0, min_y, max_y)
};
let get_x_bounds = |env: &[E], idx: usize| -> (E::X, E::X) {
let len = env.len();
let right_bound = if len > 0 && len - 1 > idx { env[idx + 1].get_x() } else { max_x };
let left_bound = if len > 0 && idx > 0 { env[idx - 1].get_x() } else { min_x };
(left_bound, right_bound)
};
let mut new_interaction = match (enabled, maybe_mouse) {
(false, _) | (true, None) => Normal,
(true, Some(mouse)) => {
let is_over_elem = |env: &[E]| if rect.is_over(mouse.xy) {
if inner_rect.is_over(mouse.xy) {
for (i, p) in env.iter().enumerate() {
let px = p.get_x();
let py = p.get_y();
let x = map_x_to(px, inner_rect.left(), inner_rect.right());
let y = map_y_to(py, inner_rect.bottom(), inner_rect.top());
let distance = (mouse.xy[0] - x).powf(2.0)
+ (mouse.xy[1] - y).powf(2.0);
if distance <= point_radius.powf(2.0) {
return Some(Elem::EnvPoint(i, (x, y)));
}
}
Some(Elem::Pad)
} else {
Some(Elem::Rect)
}
} else {
None
};
get_new_interaction(is_over_elem(&env), state.view().interaction, mouse)
},
};
match (state.view().interaction, new_interaction) {
(Highlighted(_), Clicked(_, _)) => { ui.capture_mouse(); },
(Clicked(_, _), Highlighted(_)) |
(Clicked(_, _), Normal) => { ui.uncapture_mouse(); },
_ => (),
}
let is_clicked_env_point = match new_interaction {
Clicked(elem, _) | Highlighted(elem) => {
if let Elem::EnvPoint(idx, _) = elem { Some(idx) } else { None }
},
_ => None,
};
if let Some(mouse) = maybe_mouse {
let get_new_value = |env: &[E], idx: usize| -> (E::X, E::Y) {
let mouse_x_clamped = inner_rect.x.clamp_value(mouse.xy[0]);
let mouse_y_clamped = inner_rect.y.clamp_value(mouse.xy[1]);
let unbounded_x = map_to_x(mouse_x_clamped, inner_rect.left(), inner_rect.right());
let (left_bound, right_bound) = get_x_bounds(&env, idx);
let new_x = clamp(unbounded_x, left_bound, right_bound);
let new_y = map_to_y(mouse_y_clamped, inner_rect.bottom(), inner_rect.top());
(new_x, new_y)
};
if let Some(idx) = is_clicked_env_point {
match (state.view().interaction, new_interaction) {
(Clicked(_, m_button), Highlighted(_)) |
(Clicked(_, m_button), Normal) => {
match m_button {
MouseButton::Left => {
let (new_x, new_y) = get_new_value(&env, idx);
env[idx].set_x(new_x);
env[idx].set_y(new_y);
if let Some(ref mut react) = maybe_react {
react(env, idx);
}
},
MouseButton::Right => {
env.remove(idx);
if let Highlighted(ref mut elem) = new_interaction {
if env.is_empty() {
*elem = Elem::Pad;
} else if let Elem::EnvPoint(p_idx, p) = *elem {
if p_idx >= env.len() {
*elem = Elem::EnvPoint(env.len() - 1, p);
}
}
}
if let Some(ref mut react) = maybe_react {
react(env, idx);
}
},
}
},
(Clicked(_, prev_m_button), Clicked(_, m_button)) => {
if let (MouseButton::Left, MouseButton::Left) = (prev_m_button, m_button) {
let (new_x, new_y) = get_new_value(&env, idx);
let current_x = env[idx].get_x();
let current_y = env[idx].get_y();
if new_x != current_x || new_y != current_y {
env[idx].set_x(new_x);
env[idx].set_y(new_y);
if let Some(ref mut react) = maybe_react {
react(env, idx);
}
}
}
},
_ => (),
}
} else {
if env.len() == 0 {
if let (Clicked(elem, m_button), Highlighted(_)) =
(state.view().interaction, new_interaction) {
if let (Elem::Pad, MouseButton::Left) = (elem, m_button) {
let (new_x, new_y) = get_new_value(&env, 0);
let new_point = EnvelopePoint::new(new_x, new_y);
env.push(new_point);
if let Some(ref mut react) = maybe_react {
react(env, 0);
}
}
}
}
else {
if let (Clicked(elem, m_button), Highlighted(_)) =
(state.view().interaction, new_interaction) {
if let (Elem::Pad, MouseButton::Left) = (elem, m_button) {
let clamped_mouse_x = inner_rect.x.clamp_value(mouse.xy[0]);
let clamped_mouse_y = inner_rect.y.clamp_value(mouse.xy[1]);
let (left, right, bottom, top) = inner_rect.l_r_b_t();
let new_x = map_to_x(clamped_mouse_x, left, right);
let new_y = map_to_y(clamped_mouse_y, bottom, top);
let new_point = EnvelopePoint::new(new_x, new_y);
env.push(new_point);
env.sort_by(|a, b| if a.get_x() > b.get_x() { Ordering::Greater }
else if a.get_x() < b.get_x() { Ordering::Less }
else { Ordering::Equal });
if let Some(ref mut react) = maybe_react {
let idx = env.iter().enumerate().find(|&(_, point)| {
point.get_x() == new_x && point.get_y() == new_y
}).map(|(idx, _)| idx).unwrap();
react(env, idx)
}
}
}
}
}
}
let closest_elem = |env: &[E], target: Point| {
let mut closest_distance = ::std::f64::MAX;
let mut closest_elem = Elem::Pad;
for (i, p) in env.iter().enumerate() {
let px = p.get_x();
let py = p.get_y();
let x = map_x_to(px, inner_rect.left(), inner_rect.right());
let y = map_y_to(py, inner_rect.bottom(), inner_rect.top());
let distance = (target[0] - x).powf(2.0)
+ (target[1] - y).powf(2.0);
if distance < closest_distance {
closest_distance = distance;
closest_elem = Elem::EnvPoint(i, (x, y));
}
}
closest_elem
};
let maybe_closest_point = match new_interaction {
Clicked(Elem::EnvPoint(idx, p), _) |
Highlighted(Elem::EnvPoint(idx, p)) => Some((idx, p)),
Clicked(_, _) | Highlighted(_) =>
maybe_mouse.and_then(|mouse| match closest_elem(&env, mouse.xy) {
Elem::EnvPoint(idx, p) => Some((idx, p)),
_ => None,
}),
_ => None,
};
if state.view().interaction != new_interaction {
state.update(|state| state.interaction = new_interaction);
}
if &state.view().env[..] != &env[..] {
state.update(|state| state.env = env.clone());
}
let bounds_have_changed = {
let view = state.view();
view.min_x != min_x || view.max_x != max_x || view.min_y != min_y || view.max_y != max_y
};
if bounds_have_changed {
state.update(|state| {
state.min_x = min_x;
state.max_x = max_x;
state.min_y = min_y;
state.max_y = max_y;
});
}
if state.view().skew_y_range != skew {
state.update(|state| state.skew_y_range = skew);
}
let rectangle_idx = state.view().rectangle_idx.get(&mut ui);
let dim = rect.dim();
let frame = style.frame(ui.theme());
let color = new_interaction.color(style.color(ui.theme()));
let frame_color = style.frame_color(ui.theme());
FramedRectangle::new(dim)
.middle_of(idx)
.graphics_for(idx)
.color(color)
.frame(frame)
.frame_color(frame_color)
.set(rectangle_idx, &mut ui);
let label_color = style.label_color(ui.theme());
if let Some(label) = maybe_label {
let label_idx = state.view().label_idx.get(&mut ui);
let font_size = style.label_font_size(ui.theme());
Text::new(label)
.middle_of(rectangle_idx)
.graphics_for(idx)
.color(label_color)
.font_size(font_size)
.set(label_idx, &mut ui);
}
let line_color = label_color.with_alpha(1.0);
{
let point_path_idx = state.view().point_path_idx.get(&mut ui);
let thickness = style.line_thickness(ui.theme());
let points = env.iter().map(|point| {
let x = map_x_to(point.get_x(), inner_rect.left(), inner_rect.right());
let y = map_y_to(point.get_y(), inner_rect.bottom(), inner_rect.top());
[x, y]
});
PointPath::new(points)
.wh(inner_rect.dim())
.xy(inner_rect.xy())
.graphics_for(idx)
.parent(idx)
.color(line_color)
.thickness(thickness)
.set(point_path_idx, &mut ui);
}
let num_point_indices = state.view().point_indices.len();
let len = env.len();
if num_point_indices < len {
let new_indices = (num_point_indices..len).map(|_| ui.new_unique_node_index());
state.update(|state| state.point_indices.extend(new_indices));
}
let iter = state.view().point_indices.iter().zip(env.iter()).enumerate();
for (i, (&point_idx, point)) in iter {
let x = map_x_to(point.get_x(), inner_rect.left(), inner_rect.right());
let y = map_y_to(point.get_y(), inner_rect.bottom(), inner_rect.top());
let point_color = match new_interaction {
Clicked(Elem::EnvPoint(idx, _), MouseButton::Left) if idx == i =>
line_color.clicked(),
Highlighted(Elem::EnvPoint(idx, _)) if idx == i =>
line_color.highlighted(),
_ => line_color,
};
Circle::fill(point_radius)
.color(point_color)
.x_y(x, y)
.graphics_for(idx)
.parent(idx)
.set(point_idx, &mut ui);
}
if let Some((closest_idx, (x, y))) = maybe_closest_point {
let x_range = max_x - min_x;
let y_range = max_y - min_y;
let x_px_range = inner_rect.w() as usize;
let y_px_range = inner_rect.h() as usize;
let x_string = val_to_string(env[closest_idx].get_x(), max_x, x_range, x_px_range);
let y_string = val_to_string(env[closest_idx].get_y(), max_y, y_range, y_px_range);
let xy_string = format!("{}, {}", x_string, y_string);
let x_direction = match inner_rect.x.closest_edge(x) {
Edge::End => Direction::Backwards,
Edge::Start => Direction::Forwards,
};
let y_direction = match inner_rect.y.closest_edge(y) {
Edge::End => Direction::Backwards,
Edge::Start => Direction::Forwards,
};
let value_font_size = style.value_font_size(ui.theme());
let value_label_idx = state.view().value_label_idx.get(&mut ui);
let closest_point_idx = state.view().point_indices[closest_idx];
const VALUE_TEXT_PAD: f64 = 5.0;
Text::new(&xy_string)
.x_direction_from(closest_point_idx, x_direction, VALUE_TEXT_PAD)
.y_direction_from(closest_point_idx, y_direction, VALUE_TEXT_PAD)
.color(line_color)
.graphics_for(idx)
.parent(idx)
.font_size(value_font_size)
.set(value_label_idx, &mut ui);
}
}
}
impl Style {
pub fn new() -> Style {
Style {
maybe_color: None,
maybe_frame: None,
maybe_frame_color: None,
maybe_label_color: None,
maybe_label_font_size: None,
maybe_value_font_size: None,
maybe_point_radius: None,
maybe_line_thickness: None,
}
}
pub fn color(&self, theme: &Theme) -> Color {
self.maybe_color.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_color.unwrap_or(theme.shape_color)
})).unwrap_or(theme.shape_color)
}
pub fn frame(&self, theme: &Theme) -> f64 {
self.maybe_frame.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_frame.unwrap_or(theme.frame_width)
})).unwrap_or(theme.frame_width)
}
pub fn frame_color(&self, theme: &Theme) -> Color {
self.maybe_frame_color.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_frame_color.unwrap_or(theme.frame_color)
})).unwrap_or(theme.frame_color)
}
pub fn label_color(&self, theme: &Theme) -> Color {
self.maybe_label_color.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_label_color.unwrap_or(theme.label_color)
})).unwrap_or(theme.label_color)
}
pub fn label_font_size(&self, theme: &Theme) -> FontSize {
self.maybe_label_font_size.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_label_font_size.unwrap_or(theme.font_size_medium)
})).unwrap_or(theme.font_size_medium)
}
pub fn value_font_size(&self, theme: &Theme) -> FontSize {
const DEFAULT_VALUE_FONT_SIZE: u32 = 14;
self.maybe_value_font_size.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_value_font_size.unwrap_or(DEFAULT_VALUE_FONT_SIZE)
})).unwrap_or(DEFAULT_VALUE_FONT_SIZE)
}
pub fn point_radius(&self, theme: &Theme) -> f64 {
const DEFAULT_POINT_RADIUS: f64 = 6.0;
self.maybe_point_radius.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_point_radius.unwrap_or(DEFAULT_POINT_RADIUS)
})).unwrap_or(DEFAULT_POINT_RADIUS)
}
pub fn line_thickness(&self, theme: &Theme) -> f64 {
const DEFAULT_LINE_THICKNESS: f64 = 2.0;
self.maybe_line_thickness.or(theme.widget_style::<Self>(KIND).map(|default| {
default.style.maybe_line_thickness.unwrap_or(DEFAULT_LINE_THICKNESS)
})).unwrap_or(DEFAULT_LINE_THICKNESS)
}
}
impl<'a, E, F> Colorable for EnvelopeEditor<'a, E, F>
where
E: EnvelopePoint
{
fn color(mut self, color: Color) -> Self {
self.style.maybe_color = Some(color);
self
}
}
impl<'a, E, F> Frameable for EnvelopeEditor<'a, E, F>
where
E: EnvelopePoint
{
fn frame(mut self, width: f64) -> Self {
self.style.maybe_frame = Some(width);
self
}
fn frame_color(mut self, color: Color) -> Self {
self.style.maybe_frame_color = Some(color);
self
}
}
impl<'a, E, F> Labelable<'a> for EnvelopeEditor<'a, E, F>
where
E: EnvelopePoint
{
fn label(mut self, text: &'a str) -> Self {
self.maybe_label = Some(text);
self
}
fn label_color(mut self, color: Color) -> Self {
self.style.maybe_label_color = Some(color);
self
}
fn label_font_size(mut self, size: FontSize) -> Self {
self.style.maybe_label_font_size = Some(size);
self
}
}