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
use abstutil::Tags;
use map_gui::tools::PopupMsg;
use map_model::{BufferType, Direction, EditCmd, EditRoad, LaneSpec, LaneType, RoadID};
use widgetry::{
Choice, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Outcome, Panel, State, TextExt,
VerticalAlignment, Widget,
};
use crate::app::{App, Transition};
use crate::common::RouteSketcher;
use crate::edit::apply_map_edits;
use crate::ungap::layers::render_edits;
use crate::ungap::magnifying::MagnifyingGlass;
pub struct QuickSketch {
top_panel: Panel,
network_layer: Drawable,
edits_layer: Drawable,
magnifying_glass: MagnifyingGlass,
route_sketcher: RouteSketcher,
}
impl QuickSketch {
pub fn new_state(ctx: &mut EventCtx, app: &mut App) -> Box<dyn State<App>> {
let mut qs = QuickSketch {
top_panel: Panel::empty(ctx),
magnifying_glass: MagnifyingGlass::new(ctx),
network_layer: crate::ungap::render_network_layer(ctx, app),
edits_layer: render_edits(ctx, app),
route_sketcher: RouteSketcher::new(ctx, app),
};
qs.update_top_panel(ctx);
Box::new(qs)
}
fn update_top_panel(&mut self, ctx: &mut EventCtx) {
let default_buffer = if self.top_panel.has_widget("buffer type") {
self.top_panel.dropdown_value("buffer type")
} else {
Some(BufferType::FlexPosts)
};
self.top_panel = Panel::new_builder(Widget::col(vec![
self.route_sketcher.get_widget_to_describe(ctx),
Widget::row(vec![
"Protect the new bike lanes?"
.text_widget(ctx)
.centered_vert(),
Widget::dropdown(
ctx,
"buffer type",
default_buffer,
vec![
Choice::new("diagonal stripes", Some(BufferType::Stripes)),
Choice::new("flex posts", Some(BufferType::FlexPosts)),
Choice::new("planters", Some(BufferType::Planters)),
Choice::new("no -- just paint", None),
],
),
]),
Widget::custom_row(vec![
ctx.style()
.btn_solid_primary
.text("Add bike lanes")
.hotkey(Key::Enter)
.disabled(!self.route_sketcher.is_route_started())
.build_def(ctx),
ctx.style()
.btn_solid_destructive
.text("Cancel")
.hotkey(Key::Escape)
.build_def(ctx),
])
.evenly_spaced(),
]))
.aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
.build(ctx);
}
}
impl State<App> for QuickSketch {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
self.magnifying_glass.event(ctx, app);
match self.top_panel.event(ctx) {
Outcome::Clicked(x) => match x.as_ref() {
"Cancel" => {
return Transition::Pop;
}
"Add bike lanes" => {
let messages = make_quick_changes(
ctx,
app,
self.route_sketcher.all_roads(app),
self.top_panel.dropdown_value("buffer type"),
);
return Transition::Replace(PopupMsg::new_state(ctx, "Changes made", messages));
}
_ => unreachable!(),
},
_ => {}
}
if self.route_sketcher.event(ctx, app) {
self.update_top_panel(ctx);
}
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
self.top_panel.draw(g);
if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
g.redraw(&self.network_layer);
self.magnifying_glass.draw(g, app);
}
g.redraw(&self.edits_layer);
self.route_sketcher.draw(g);
}
}
fn make_quick_changes(
ctx: &mut EventCtx,
app: &mut App,
roads: Vec<RoadID>,
buffer_type: Option<BufferType>,
) -> Vec<String> {
let mut edits = app.primary.map.get_edits().clone();
let already_modified_roads = edits.changed_roads.clone();
let mut num_changes = 0;
for r in roads {
if already_modified_roads.contains(&r) {
continue;
}
let old = app.primary.map.get_r_edit(r);
let mut new = old.clone();
maybe_add_bike_lanes(&mut new, buffer_type);
if old != new {
num_changes += 1;
edits.commands.push(EditCmd::ChangeRoad { r, old, new });
}
}
apply_map_edits(ctx, app, edits);
vec![format!("Changed {} segments", num_changes)]
}
fn maybe_add_bike_lanes(r: &mut EditRoad, buffer_type: Option<BufferType>) {
let dummy_tags = Tags::empty();
let mut lanes_ltr = Vec::new();
for spec in r.lanes_ltr.drain(..) {
if spec.lt != LaneType::Parking {
lanes_ltr.push(spec);
continue;
}
if let Some(buffer) = buffer_type {
let replacements = if spec.dir == Direction::Fwd {
[LaneType::Buffer(buffer), LaneType::Biking]
} else {
[LaneType::Biking, LaneType::Buffer(buffer)]
};
for lt in replacements {
lanes_ltr.push(LaneSpec {
lt,
dir: spec.dir,
width: LaneSpec::typical_lane_widths(lt, &dummy_tags)[0].0,
});
}
} else {
lanes_ltr.push(LaneSpec {
lt: LaneType::Biking,
dir: spec.dir,
width: LaneSpec::typical_lane_widths(LaneType::Biking, &dummy_tags)[0].0,
});
}
}
r.lanes_ltr = lanes_ltr;
}