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
//! A widget for plotting a series of lines using the given function *x -> y*.
use crate::;
use cratewidget;
/// A widget that plots a series of lines using the given function *x -> y*.
///
/// The function is sampled once per pixel and the result is mapped to the widget's height.
///
/// The resulting "path" is drawn using carbide's `PointPath` primitive widget.
/// Unique styling parameters for the `PlotPath` widget.
widget_ids!
/// Unique state stored between updates for the `PlotPath` widget.
/*impl<X, Y, F> OldWidget for PlotPath<X, Y, F>
where X: num::NumCast + Clone,
Y: num::NumCast + Clone,
F: FnMut(X) -> Y,
{
type State = State;
type Style = Style;
type Event = ();
fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
State {
ids: Ids::new(id_gen),
}
}
fn style(&self) -> Self::Style {
self.style.clone()
}
fn is_over(&self) -> widget::IsOverFn {
fn is_over_widget(widget: &graph::Container, _: Point, _: &Theme) -> widget::IsOver {
let unique = widget.state_and_style::<State, Style>().unwrap();
unique.state.ids.point_path.into()
}
is_over_widget
}
/// Update the state of the PlotPath.
fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
let widget::UpdateArgs { id, state, style, rect, ui, .. } = args;
let PlotPath { min_x, max_x, min_y, max_y, mut f, .. } = self;
let y_to_scalar =
|y| utils::map_range(y, min_y.clone(), max_y.clone(), rect.bottom(), rect.top());
let scalar_to_x =
|s| utils::map_range(s, rect.left(), rect.right(), min_x.clone(), max_x.clone());
let point_iter = (0 .. rect.w() as usize)
.map(|x_scalar| {
let x_scalar = x_scalar as Scalar + rect.x.start;
let x = scalar_to_x(x_scalar);
let y = f(x);
let y_scalar = y_to_scalar(y);
[x_scalar, y_scalar]
});
let thickness = style.thickness(ui.theme());
let color = style.color(ui.theme());
widget::PointPath::new(point_iter)
.wh(rect.dim())
.xy(rect.xy())
.color(color)
.thickness(thickness)
.parent(id)
.graphics_for(id)
.set(state.ids.point_path, ui);
}
}*/