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
use geom::{Distance, Polygon};
use map_model::NORMAL_LANE_THICKNESS;
use sim::{TripEndpoint, TripMode};
use widgetry::mapspace::{ObjectID, ToggleZoomed, World};
use widgetry::{
Color, EventCtx, GfxCtx, HorizontalAlignment, Key, Outcome, Panel, State, VerticalAlignment,
Widget,
};
use super::Neighborhood;
use crate::app::{App, Transition};
use crate::common::{InputWaypoints, WaypointID};
pub struct RoutePlanner {
panel: Panel,
waypoints: InputWaypoints,
world: World<ID>,
neighborhood: Neighborhood,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum ID {
MainRoute,
Waypoint(WaypointID),
}
impl ObjectID for ID {}
impl RoutePlanner {
pub fn new_state(
ctx: &mut EventCtx,
app: &mut App,
neighborhood: Neighborhood,
) -> Box<dyn State<App>> {
let mut rp = RoutePlanner {
panel: Panel::empty(ctx),
waypoints: InputWaypoints::new(app),
world: World::bounded(app.primary.map.get_bounds()),
neighborhood,
};
rp.update(ctx, app);
Box::new(rp)
}
fn update(&mut self, ctx: &mut EventCtx, app: &App) {
self.panel = Panel::new_builder(Widget::col(vec![
ctx.style()
.btn_outline
.text("Back to editing modal filters")
.hotkey(Key::Escape)
.build_def(ctx),
self.waypoints.get_panel_widget(ctx),
]))
.aligned(HorizontalAlignment::Left, VerticalAlignment::Top)
.ignore_initial_events()
.build(ctx);
let mut world = self.calculate_paths(ctx, app);
self.waypoints
.rebuild_world(ctx, &mut world, ID::Waypoint, 1);
world.initialize_hover(ctx);
world.rebuilt_during_drag(&self.world);
self.world = world;
}
fn calculate_paths(&self, ctx: &mut EventCtx, app: &App) -> World<ID> {
let map = &app.primary.map;
let mut world = World::bounded(map.get_bounds());
let mut params = map.routing_params().clone();
params
.avoid_roads
.extend(app.session.modal_filters.roads.keys().cloned());
let cache_custom = true;
let mut draw_route = ToggleZoomed::builder();
let mut hitbox_pieces = Vec::new();
for pair in self.waypoints.get_waypoints().windows(2) {
if let Some(pl) = TripEndpoint::path_req(pair[0], pair[1], TripMode::Drive, map)
.and_then(|req| map.pathfind_with_params(req, ¶ms, cache_custom).ok())
.and_then(|path| path.trace(map))
{
let shape = pl.make_polygons(5.0 * NORMAL_LANE_THICKNESS);
draw_route
.unzoomed
.push(Color::RED.alpha(0.8), shape.clone());
draw_route.zoomed.push(Color::RED.alpha(0.5), shape.clone());
hitbox_pieces.push(shape);
}
}
if !hitbox_pieces.is_empty() {
world
.add(ID::MainRoute)
.hitbox(Polygon::union_all(hitbox_pieces))
.draw(draw_route)
.hover_outline(Color::BLACK, Distance::meters(2.0))
.build(ctx);
}
world
}
}
impl State<App> for RoutePlanner {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
let world_outcome_for_waypoints = match self.world.event(ctx) {
x => x.map_id(|id| match id {
ID::Waypoint(id) => id,
_ => unreachable!(),
}),
};
let panel_outcome = self.panel.event(ctx);
if let Outcome::Clicked(ref x) = panel_outcome {
if x == "Back to editing modal filters" {
return Transition::ConsumeState(Box::new(|state, ctx, app| {
let state = state.downcast::<RoutePlanner>().ok().unwrap();
vec![super::viewer::Viewer::new_state(
ctx,
app,
state.neighborhood,
)]
}));
}
}
if self
.waypoints
.event(app, panel_outcome, world_outcome_for_waypoints)
{
self.update(ctx, app);
}
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
self.panel.draw(g);
g.redraw(&self.neighborhood.fade_irrelevant);
g.redraw(&self.neighborhood.draw_filters);
if g.canvas.is_unzoomed() {
self.neighborhood.labels.draw(g, app);
}
self.world.draw(g);
}
}