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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::collections::BTreeSet;

use map_gui::ID;
use map_model::{IntersectionID, RoadID};
use widgetry::{
    Color, Drawable, EventCtx, GeomBatch, GfxCtx, Key, RewriteColor, StyledButtons, Widget,
};

use crate::app::App;
use crate::common::{intersections_from_roads, CommonState};

/// A tool for selecting multiple roads.
pub struct RoadSelector {
    pub roads: BTreeSet<RoadID>,
    /// Intersections can't be selected directly. If all roads connected to an intersection are
    /// selected, then the intersection will be too.
    pub intersections: BTreeSet<IntersectionID>,
    pub preview: Option<Drawable>,
    mode: Mode,
    dragging: bool,
}

pub enum Mode {
    /// No selecting, just normal click-and-drag controls.
    Pan,
    /// The user is choosing two intersections, to select the route between.
    Route {
        i1: Option<IntersectionID>,
        preview_path: Option<(IntersectionID, Vec<RoadID>, Drawable)>,
    },
    /// Click and drag to select roads
    Paint,
    /// Click and drag to unselect roads
    Erase,
}

impl RoadSelector {
    pub fn new(ctx: &mut EventCtx, app: &mut App, roads: BTreeSet<RoadID>) -> RoadSelector {
        app.primary.current_selection = None;
        let mut rs = RoadSelector {
            roads,
            intersections: BTreeSet::new(),
            preview: None,
            mode: Mode::Paint,
            dragging: false,
        };
        rs.roads_changed(ctx, app);
        rs
    }

    pub fn make_controls(&self, ctx: &mut EventCtx) -> Widget {
        Widget::custom_row(vec![
            ctx.style()
                .btn_plain_light_icon("system/assets/tools/pencil.svg")
                .disabled(matches!(self.mode, Mode::Paint))
                .hotkey(Key::P)
                .build_widget(ctx, "paint"),
            ctx.style()
                .btn_plain_light_icon("system/assets/tools/eraser.svg")
                .hotkey(Key::Backspace)
                .disabled(matches!(self.mode, Mode::Erase))
                .build_widget(ctx, "erase"),
            ctx.style()
                .btn_plain_light_icon("system/assets/timeline/start_pos.svg")
                .hotkey(Key::R)
                .disabled(matches!(self.mode, Mode::Route { .. }))
                .build_widget(ctx, "select along route"),
            ctx.style()
                .btn_plain_light_icon("system/assets/tools/pan.svg")
                .hotkey(Key::Escape)
                .disabled(matches!(self.mode, Mode::Pan))
                .build_widget(ctx, "pan"),
        ])
        .evenly_spaced()
    }

    fn roads_changed(&mut self, ctx: &mut EventCtx, app: &App) {
        let mut batch = GeomBatch::new();
        for r in &self.roads {
            batch.push(
                Color::BLUE.alpha(0.5),
                app.primary
                    .map
                    .get_r(*r)
                    .get_thick_polygon(&app.primary.map),
            );
        }
        self.intersections.clear();
        for i in intersections_from_roads(&self.roads, &app.primary.map) {
            self.intersections.insert(i);
            batch.push(
                Color::BLUE.alpha(0.5),
                app.primary.map.get_i(i).polygon.clone(),
            );
        }
        self.preview = Some(ctx.upload(batch));
    }

    // Pass None. Returns true if anything changed.
    pub fn event(&mut self, ctx: &mut EventCtx, app: &mut App, clicked: Option<&str>) -> bool {
        if ctx.redo_mouseover() {
            match self.mode {
                Mode::Pan => {
                    app.primary.current_selection = None;
                }
                Mode::Route { .. } => {
                    app.primary.current_selection = app.mouseover_unzoomed_intersections(ctx);
                }
                Mode::Paint | Mode::Erase => {
                    app.primary.current_selection =
                        match app.mouseover_unzoomed_roads_and_intersections(ctx) {
                            Some(ID::Road(r)) => Some(r),
                            Some(ID::Lane(l)) => Some(app.primary.map.get_l(l).parent),
                            _ => None,
                        }
                        .and_then(|r| {
                            if app.primary.map.get_r(r).is_light_rail() {
                                None
                            } else {
                                Some(ID::Road(r))
                            }
                        });
                }
            }
        }

        match self.mode {
            Mode::Pan | Mode::Route { .. } => {
                ctx.canvas_movement();
            }
            Mode::Paint | Mode::Erase => {
                if self.dragging && ctx.input.left_mouse_button_released() {
                    self.dragging = false;
                } else if !self.dragging && ctx.input.left_mouse_button_pressed() {
                    self.dragging = true;
                }
            }
        }

        if self.dragging {
            if let Some(ID::Road(r)) = app.primary.current_selection {
                let change = match self.mode {
                    Mode::Paint => {
                        if self.roads.contains(&r) {
                            false
                        } else {
                            self.roads.insert(r);
                            true
                        }
                    }
                    Mode::Erase => {
                        if self.roads.contains(&r) {
                            self.roads.remove(&r);
                            true
                        } else {
                            false
                        }
                    }
                    Mode::Route { .. } | Mode::Pan => unreachable!(),
                };
                if change {
                    self.roads_changed(ctx, app);
                    return true;
                }
            }
        }

        match clicked {
            Some(x) => match x {
                "paint" => {
                    self.dragging = false;
                    self.mode = Mode::Paint;
                    return true;
                }
                "erase" => {
                    self.dragging = false;
                    self.mode = Mode::Erase;
                    return true;
                }
                "pan" => {
                    app.primary.current_selection = None;
                    self.dragging = false;
                    self.mode = Mode::Pan;
                    return true;
                }
                "select along route" => {
                    app.primary.current_selection = None;
                    self.dragging = false;
                    self.mode = Mode::Route {
                        i1: None,
                        preview_path: None,
                    };
                    return true;
                }
                _ => unreachable!(),
            },
            None => {}
        }

        if let Mode::Route {
            ref mut i1,
            ref mut preview_path,
        } = self.mode
        {
            if let Some(ID::Intersection(i)) = app.primary.current_selection {
                if i1.is_none() && app.per_obj.left_click(ctx, "start here") {
                    *i1 = Some(i);
                }
            }

            if let Some(i1) = *i1 {
                if let Some(ID::Intersection(i2)) = app.primary.current_selection {
                    if preview_path
                        .as_ref()
                        .map(|(i, _, _)| *i != i2)
                        .unwrap_or(true)
                    {
                        let mut batch = GeomBatch::new();
                        let roads = if let Some(roads) = app.primary.map.simple_path_btwn(i1, i2) {
                            let mut intersections = BTreeSet::new();
                            for r in &roads {
                                let r = app.primary.map.get_r(*r);
                                batch.push(
                                    Color::RED.alpha(0.5),
                                    r.get_thick_polygon(&app.primary.map),
                                );
                                intersections.insert(r.src_i);
                                intersections.insert(r.dst_i);
                            }
                            for i in intersections {
                                batch.push(
                                    Color::RED.alpha(0.5),
                                    app.primary.map.get_i(i).polygon.clone(),
                                );
                            }
                            roads
                        } else {
                            Vec::new()
                        };
                        *preview_path = Some((i2, roads, ctx.upload(batch)));
                    }

                    if preview_path
                        .as_ref()
                        .map(|(_, roads, _)| !roads.is_empty())
                        .unwrap_or(false)
                        && app.per_obj.left_click(ctx, "end here")
                    {
                        let (_, roads, _) = preview_path.take().unwrap();
                        self.roads.extend(roads);
                        self.mode = Mode::Pan;
                        self.roads_changed(ctx, app);
                        return true;
                    }
                } else {
                    *preview_path = None;
                }
            }
        }

        false
    }

    pub fn draw(&self, g: &mut GfxCtx, app: &App, show_preview: bool) {
        if show_preview {
            if let Some(ref p) = self.preview {
                g.redraw(p);
            }
        }
        if g.canvas.get_cursor_in_map_space().is_some() {
            if let Some(cursor) = match self.mode {
                Mode::Pan => None,
                Mode::Paint => Some("system/assets/tools/pencil.svg"),
                Mode::Erase => Some("system/assets/tools/eraser.svg"),
                Mode::Route { .. } => Some("system/assets/timeline/start_pos.svg"),
            } {
                let mut batch = GeomBatch::new();
                batch.append(
                    GeomBatch::load_svg(g, cursor)
                        .centered_on(g.canvas.get_cursor().to_pt())
                        .color(RewriteColor::ChangeAll(Color::GREEN)),
                );
                g.fork_screenspace();
                batch.draw(g);
                g.unfork();
            }
        }

        if let Mode::Route {
            ref i1,
            ref preview_path,
        } = self.mode
        {
            if let Some(i) = i1 {
                g.draw_polygon(Color::GREEN, app.primary.map.get_i(*i).polygon.clone());
            }
            if let Some((_, _, ref p)) = preview_path {
                g.redraw(p);
            }
        }

        CommonState::draw_osd(g, app);
    }
}