-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscroll.rs
More file actions
189 lines (166 loc) · 5.43 KB
/
Copy pathscroll.rs
File metadata and controls
189 lines (166 loc) · 5.43 KB
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
use std::ops::RangeInclusive;
use prism::chart::{
Chart, Labels,
series::{line_series, point_series},
};
use iced::{
Element, Length, Task, Theme,
widget::{Container, column, container, row, text},
};
fn main() -> Result<(), iced::Error> {
iced::application(App::new, App::update, App::view)
.title(App::title)
.theme(App::theme)
.antialiasing(true)
.run()
}
#[derive(Debug, Clone)]
enum Message {
OnMove(Option<iced::Point>),
MouseDown(Option<iced::Point>),
MouseUp(Option<iced::Point>),
}
#[derive(Debug)]
struct App {
x_offset: f32,
x_range: RangeInclusive<f32>,
data: Vec<(f32, f32)>,
data_1: Vec<Entry>,
dragging: Dragging,
}
#[derive(Debug)]
struct Entry {
x: f32,
y: f32,
}
#[derive(Debug, Default)]
enum Dragging {
CouldStillBeClick(iced::Point),
ForSure(iced::Point),
#[default]
None,
}
impl App {
pub fn new() -> (Self, Task<Message>) {
let data: Vec<_> = (-50..=50)
.map(|x| x as f32 / 50.0)
.map(|x| (x, x * x))
.collect();
let data_1 = data
.iter()
.copied()
.map(|(x, y)| Entry { x, y: y * 2.0 })
.collect();
(
Self {
data,
data_1,
x_range: -4.0..=4.0,
x_offset: 0.0,
dragging: Dragging::None,
},
Task::none(),
)
}
pub fn title(&self) -> String {
"pliced".to_string()
}
pub fn update(&mut self, msg: Message) -> Task<Message> {
let mut update_center = |prev_pos: iced::Point, pos: iced::Point| {
let shift_x = prev_pos.x - pos.x;
let new_start = self.x_range.start() + shift_x;
let new_end = self.x_range.end() + shift_x;
self.x_range = new_start..=new_end;
self.x_offset += shift_x;
};
match msg {
Message::MouseDown(pos) => {
let Dragging::None = self.dragging else {
return Task::none();
};
if let Some(pos) = pos {
self.dragging = Dragging::CouldStillBeClick(pos);
}
}
Message::OnMove(pos) => {
let Some(pos) = pos else {
dbg!("no pos: {:?}", &msg);
return Task::none();
};
match self.dragging {
Dragging::CouldStillBeClick(prev_pos) => {
if prev_pos == pos {
return Task::none();
} else {
update_center(prev_pos, pos);
self.dragging = Dragging::ForSure(pos);
}
}
Dragging::ForSure(prev_pos) => {
update_center(prev_pos, pos);
self.dragging = Dragging::ForSure(pos);
}
Dragging::None => {}
}
}
Message::MouseUp(pos) => {
let Some(pos) = pos else {
dbg!("no pos: {:?}", &msg);
return Task::none();
};
match self.dragging {
Dragging::CouldStillBeClick(_point) => {
self.dragging = Dragging::None;
}
Dragging::ForSure(prev_pos) => {
update_center(prev_pos, pos);
self.dragging = Dragging::None;
}
Dragging::None => {}
}
}
}
Task::none()
}
pub fn view(&self) -> Element<'_, Message> {
let palette = self.theme().palette();
let top = bound("Top").center_x(Length::Fill);
let bottom = bound("Bottom").center_x(Length::Fill);
let left = bound("Left").center_y(Length::Fill);
let right = bound("Right").center_y(Length::Fill);
let chart = Chart::<_, (), _>::new()
.width(Length::Fill)
.height(Length::Fill)
.x_range(self.x_range.clone())
.x_labels(Labels::default().format(&|v| format!("{v:.2}")))
.y_labels(Labels::default().format(&|v| format!("{v:.2}")))
.y_range(-1500.0..=1500.0)
// .push_series(line_series(self.data.iter().copied()).color(palette.primary)) // .push_series(
// .push_series(line_series(&self.data_1).color(palette.success))
.push_series(
point_series(self.data.iter().copied().map(|(x, y)| (x, y * 1000.5)))
.x(&|item| item.0)
.y(&|item| item.1)
.color(palette.danger),
)
.on_press(|state| Message::MouseDown(state.get_offset()))
.on_release(|state| Message::MouseUp(state.get_offset()))
.on_move(|state| Message::OnMove(state.get_offset()));
column![top, row![left, chart, right].spacing(10), bottom]
.spacing(10)
.into()
}
pub fn theme(&self) -> Theme {
Theme::TokyoNight
}
}
impl From<&Entry> for (f32, f32) {
fn from(entry: &Entry) -> Self {
(entry.x, entry.y)
}
}
fn bound<Message>(label: &str) -> Container<Message> {
container(text(label).center())
.padding(10)
.style(container::bordered_box)
}