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
//! After a single road has been edited, these states let the changes be copied to all similar road
//! segments. Note that only lane configuration is copied, not speed limit or access restrictions.

use std::collections::HashSet;

use geom::Distance;
use map_gui::ID;
use map_model::{EditRoad, MapEdits, RoadID};
use widgetry::tools::PopupMsg;
use widgetry::{
    Color, Drawable, EventCtx, Fill, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome,
    Panel, State, Text, TextExt, Texture, VerticalAlignment, Widget,
};

use crate::app::App;
use crate::app::Transition;
use crate::common::Warping;
use crate::edit::apply_map_edits;

pub struct SelectSegments {
    new_state: EditRoad,
    candidates: HashSet<RoadID>,
    base_road: RoadID,
    base_edits: MapEdits,

    current: HashSet<RoadID>,
    draw: Drawable,
    panel: Panel,
    selected: Option<RoadID>,
}

impl SelectSegments {
    pub fn new_state(
        ctx: &mut EventCtx,
        app: &mut App,
        base_road: RoadID,
        orig_state: EditRoad,
        new_state: EditRoad,
        base_edits: MapEdits,
    ) -> Box<dyn State<App>> {
        app.primary.current_selection = None;

        // Find all road segments matching the original state and name. base_road has already
        // changed to new_state, so no need to exclude it.
        let map = &app.primary.map;
        let base_name = map.get_r(base_road).get_name(None);
        let mut candidates = HashSet::new();
        for r in map.all_roads() {
            if map.get_r_edit(r.id).lanes_ltr == orig_state.lanes_ltr
                && r.get_name(None) == base_name
            {
                candidates.insert(r.id);
            }
        }

        if candidates.is_empty() {
            return PopupMsg::new_state(
                ctx,
                "Error",
                vec!["No other roads resemble the one you changed"],
            );
        }

        let current = candidates.clone();
        let mut state = SelectSegments {
            new_state,
            candidates,
            base_road,
            base_edits,

            current,
            draw: Drawable::empty(ctx),
            panel: Panel::empty(ctx),
            selected: None,
        };
        state.recalculate(ctx, app);
        Box::new(state)
    }

    fn recalculate(&mut self, ctx: &mut EventCtx, app: &App) {
        // Update the drawn view
        let mut batch = GeomBatch::new();
        let map = &app.primary.map;
        let color = Color::CYAN;
        // Point out the road we're using as the template
        if let Ok(outline) = map
            .get_r(self.base_road)
            .get_thick_polygon()
            .to_outline(Distance::meters(3.0))
        {
            batch.push(color.alpha(0.9), outline);
        }
        for r in &self.candidates {
            let alpha = if self.current.contains(r) { 0.9 } else { 0.5 };
            batch.push(
                Fill::ColoredTexture(Color::CYAN.alpha(alpha), Texture::CROSS_HATCH),
                map.get_r(*r).get_thick_polygon(),
            );
        }
        self.draw = ctx.upload(batch);

        // Update the panel
        self.panel = Panel::new_builder(Widget::col(vec![
            Line("Apply changes to similar roads")
                .small_heading()
                .into_widget(ctx),
            Widget::row(vec![
                format!(
                    "{} / {} roads similar to",
                    self.current.len(),
                    self.candidates.len(),
                )
                .text_widget(ctx)
                .centered_vert(),
                ctx.style()
                    .btn_plain
                    .icon_text(
                        "system/assets/tools/location.svg",
                        format!("#{}", self.base_road.0),
                    )
                    .build_widget(ctx, "jump to changed road"),
                "are selected".text_widget(ctx).centered_vert(),
            ]),
            // TODO Explain that this is only for lane configuration, NOT speed limit
            Widget::row(vec![
                "Click to select/unselect".text_widget(ctx).centered_vert(),
                ctx.style()
                    .btn_plain
                    .text("Select all")
                    .disabled(self.current.len() == self.candidates.len())
                    .build_def(ctx),
                ctx.style()
                    .btn_plain
                    .text("Unselect all")
                    .disabled(self.current.is_empty())
                    .build_def(ctx),
            ]),
            Widget::row(vec![
                ctx.style()
                    .btn_solid_primary
                    .text(format!("Apply changes to {} roads", self.current.len()))
                    .hotkey(Key::Enter)
                    .build_widget(ctx, "Apply"),
                ctx.style()
                    .btn_plain
                    .text("Cancel")
                    .hotkey(Key::Escape)
                    .build_def(ctx),
            ]),
        ]))
        .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
        .build(ctx);
    }
}

impl State<App> for SelectSegments {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "Apply" => {
                    let mut edits = std::mem::take(&mut self.base_edits);
                    for r in &self.current {
                        edits
                            .commands
                            .push(app.primary.map.edit_road_cmd(*r, |new| {
                                new.lanes_ltr = self.new_state.lanes_ltr.clone();
                            }));
                    }
                    apply_map_edits(ctx, app, edits);
                    app.primary.current_selection = None;
                    return Transition::Multi(vec![
                        Transition::Pop,
                        Transition::Replace(PopupMsg::new_state(
                            ctx,
                            "Success",
                            vec![format!(
                                "Changed {} other roads to match",
                                self.current.len()
                            )],
                        )),
                    ]);
                }
                "Select all" => {
                    self.current = self.candidates.clone();
                    self.recalculate(ctx, app);
                }
                "Unselect all" => {
                    self.current.clear();
                    self.recalculate(ctx, app);
                }
                "Cancel" => {
                    return Transition::Pop;
                }
                "jump to changed road" => {
                    return Transition::Push(Warping::new_state(
                        ctx,
                        app.primary
                            .canonical_point(ID::Road(self.base_road))
                            .unwrap(),
                        Some(10.0),
                        Some(ID::Road(self.base_road)),
                        &mut app.primary,
                    ));
                }
                _ => unreachable!(),
            }
        }

        if ctx.redo_mouseover() {
            self.selected = None;
            ctx.show_cursor();
            if let Some(r) = match app.mouseover_unzoomed_roads_and_intersections(ctx) {
                Some(ID::Road(r)) => Some(r),
                Some(ID::Lane(l)) => Some(l.road),
                _ => None,
            } {
                if self.candidates.contains(&r) {
                    self.selected = Some(r);
                    ctx.hide_cursor();
                }
                if r == self.base_road {
                    self.selected = Some(r);
                }
            }
        }

        ctx.canvas_movement();

        if let Some(r) = self.selected {
            if r != self.base_road {
                if self.current.contains(&r) && app.per_obj.left_click(ctx, "exclude road") {
                    self.current.remove(&r);
                    self.recalculate(ctx, app);
                } else if !self.current.contains(&r) && app.per_obj.left_click(ctx, "include road")
                {
                    self.current.insert(r);
                    self.recalculate(ctx, app);
                }
            }
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, _: &App) {
        g.redraw(&self.draw);
        self.panel.draw(g);

        if let Some(r) = self.selected {
            if let Some(cursor) = if self.current.contains(&r) {
                Some("system/assets/tools/exclude.svg")
            } else if self.candidates.contains(&r) {
                Some("system/assets/tools/include.svg")
            } else {
                None
            } {
                let mut batch = GeomBatch::new();
                batch.append(
                    GeomBatch::load_svg(g, cursor)
                        .scale(2.0)
                        .centered_on(g.canvas.get_cursor().to_pt()),
                );
                g.fork_screenspace();
                batch.draw(g);
                g.unfork();
            }

            if r == self.base_road {
                g.draw_mouse_tooltip(Text::from(format!("Edited {}", r)));
            }
        }
    }

    fn on_destroy(&mut self, ctx: &mut EventCtx, _: &mut App) {
        // Don't forget to do this!
        ctx.show_cursor();
    }
}