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
use geom::{Circle, Distance, FindClosest, Polygon};
use sim::TripEndpoint;
use widgetry::{
Color, Drawable, EventCtx, GeomBatch, GfxCtx, Image, Line, Outcome, Text, TextExt, Widget,
};
use crate::app::App;
pub struct InputWaypoints {
waypoints: Vec<Waypoint>,
draw_waypoints: Drawable,
hovering_on_waypt: Option<usize>,
draw_hover: Drawable,
dragging: bool,
snap_to_endpts: FindClosest<TripEndpoint>,
}
struct Waypoint {
order: char,
at: TripEndpoint,
label: String,
geom: GeomBatch,
hitbox: Polygon,
}
impl InputWaypoints {
pub fn new(ctx: &mut EventCtx, app: &App) -> InputWaypoints {
let map = &app.primary.map;
let mut snap_to_endpts = FindClosest::new(map.get_bounds());
for i in map.all_intersections() {
if i.is_border() {
snap_to_endpts.add(TripEndpoint::Border(i.id), i.polygon.points());
}
}
for b in map.all_buildings() {
snap_to_endpts.add(TripEndpoint::Bldg(b.id), b.polygon.points());
}
InputWaypoints {
waypoints: Vec::new(),
draw_waypoints: Drawable::empty(ctx),
hovering_on_waypt: None,
draw_hover: Drawable::empty(ctx),
dragging: false,
snap_to_endpts,
}
}
pub fn get_panel_widget(&self, ctx: &mut EventCtx) -> Widget {
let mut col = Vec::new();
for (idx, waypt) in self.waypoints.iter().enumerate() {
col.push(Widget::row(vec![
format!("{}) {}", waypt.order, waypt.label)
.text_widget(ctx)
.centered_vert(),
ctx.style()
.btn_plain_destructive
.text("X")
.build_widget(ctx, &format!("delete waypoint {}", idx)),
]));
}
col.push(Widget::row(vec![
Image::from_path("system/assets/tools/mouse.svg").into_widget(ctx),
Text::from_all(vec![
Line("Click").fg(ctx.style().text_hotkey_color),
Line(" to add a waypoint, "),
Line("drag").fg(ctx.style().text_hotkey_color),
Line(" a waypoint to move it"),
])
.into_widget(ctx),
]));
Widget::col(col)
}
pub fn get_waypoints(&self) -> Vec<TripEndpoint> {
self.waypoints.iter().map(|w| w.at).collect()
}
pub fn event(&mut self, ctx: &mut EventCtx, app: &mut App, outcome: Outcome) -> bool {
if self.dragging {
if ctx.redo_mouseover() {
if self.update_dragging(ctx, app) == Some(true) {
return true;
}
}
if ctx.input.left_mouse_button_released() {
self.dragging = false;
self.update_hover(ctx);
}
} else {
if ctx.redo_mouseover() {
self.update_hover(ctx);
}
if self.hovering_on_waypt.is_none() {
ctx.canvas_movement();
} else if let Some((_, dy)) = ctx.input.get_mouse_scroll() {
ctx.canvas.zoom(dy, ctx.canvas.get_cursor());
}
if self.hovering_on_waypt.is_some() && ctx.input.left_mouse_button_pressed() {
self.dragging = true;
}
if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
if self.hovering_on_waypt.is_none() && ctx.normal_left_click() {
if let Some((at, _)) =
self.snap_to_endpts.closest_pt(pt, Distance::meters(30.0))
{
self.waypoints
.push(Waypoint::new(ctx, app, at, self.waypoints.len()));
self.update_waypoints_drawable(ctx);
self.update_hover(ctx);
return true;
}
}
}
}
if let Outcome::Clicked(x) = outcome {
if let Some(x) = x.strip_prefix("delete waypoint ") {
let idx = x.parse::<usize>().unwrap();
self.waypoints.remove(idx);
for (idx, waypt) in self.waypoints.iter_mut().enumerate() {
*waypt = Waypoint::new(ctx, app, waypt.at, idx);
}
self.update_waypoints_drawable(ctx);
return true;
} else {
panic!("Unknown InputWaypoints click {}", x);
}
}
false
}
pub fn draw(&self, g: &mut GfxCtx) {
g.redraw(&self.draw_waypoints);
g.redraw(&self.draw_hover);
}
fn update_waypoints_drawable(&mut self, ctx: &mut EventCtx) {
let mut batch = GeomBatch::new();
for waypt in &self.waypoints {
batch.append(waypt.geom.clone());
}
self.draw_waypoints = ctx.upload(batch);
}
fn update_hover(&mut self, ctx: &EventCtx) {
self.hovering_on_waypt = None;
if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
self.hovering_on_waypt = self
.waypoints
.iter()
.position(|waypt| waypt.hitbox.contains_pt(pt));
}
let mut batch = GeomBatch::new();
if let Some(idx) = self.hovering_on_waypt {
batch.push(Color::BLUE.alpha(0.5), self.waypoints[idx].hitbox.clone());
}
self.draw_hover = ctx.upload(batch);
}
fn update_dragging(&mut self, ctx: &mut EventCtx, app: &App) -> Option<bool> {
let pt = ctx.canvas.get_cursor_in_map_space()?;
let (at, _) = self.snap_to_endpts.closest_pt(pt, Distance::meters(30.0))?;
let mut changed = false;
let idx = self.hovering_on_waypt.unwrap();
if self.waypoints[idx].at != at {
self.waypoints[idx] = Waypoint::new(ctx, app, at, idx);
self.update_waypoints_drawable(ctx);
changed = true;
}
let mut batch = GeomBatch::new();
batch.push(Color::BLUE.alpha(0.5), self.waypoints[idx].hitbox.clone());
self.draw_hover = ctx.upload(batch);
Some(changed)
}
}
impl Waypoint {
fn new(ctx: &mut EventCtx, app: &App, at: TripEndpoint, idx: usize) -> Waypoint {
let order = char::from_u32('A' as u32 + idx as u32).unwrap();
let map = &app.primary.map;
let (center, label) = match at {
TripEndpoint::Bldg(b) => {
let b = map.get_b(b);
(b.polygon.center(), b.address.clone())
}
TripEndpoint::Border(i) => {
let i = map.get_i(i);
(i.polygon.center(), i.name(app.opts.language.as_ref(), map))
}
TripEndpoint::SuddenlyAppear(pos) => (pos.pt(map), pos.to_string()),
};
let circle = Circle::new(center, Distance::meters(30.0)).to_polygon();
let mut geom = GeomBatch::new();
geom.push(Color::RED, circle.clone());
geom.append(
Text::from(Line(format!("{}", order)).fg(Color::WHITE))
.render(ctx)
.centered_on(center),
);
let hitbox = circle;
Waypoint {
order,
at,
label,
geom,
hitbox,
}
}
}