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
use crate::app::App;
use crate::common::CommonState;
use crate::edit::zones::ZoneEditor;
use crate::edit::{
    apply_map_edits, can_edit_lane, change_speed_limit, maybe_edit_intersection, try_change_lt,
    try_reverse,
};
use crate::game::{State, Transition};
use crate::helpers::ID;
use crate::render::Renderable;
use crate::sandbox::GameplayMode;
use ezgui::{
    hotkey, Btn, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, Outcome,
    RewriteColor, TextExt, VerticalAlignment, Widget,
};
use map_model::{EditCmd, LaneID, LaneType};

pub struct LaneEditor {
    l: LaneID,
    mode: GameplayMode,
    composite: Composite,
}

impl LaneEditor {
    pub fn new(ctx: &mut EventCtx, app: &App, l: LaneID, mode: GameplayMode) -> Box<dyn State> {
        let mut row = Vec::new();
        let lt = app.primary.map.get_l(l).lane_type;
        for (icon, label, key, active) in vec![
            (
                "driving",
                "convert to a driving lane",
                Key::D,
                lt != LaneType::Driving,
            ),
            (
                "bike",
                "convert to a protected bike lane",
                Key::B,
                lt != LaneType::Biking,
            ),
            (
                "bus",
                "convert to a bus-only lane",
                Key::T,
                lt != LaneType::Bus,
            ),
            (
                "parking",
                "convert to an on-street parking lane",
                Key::P,
                lt != LaneType::Parking,
            ),
            (
                "construction",
                "close for construction",
                Key::C,
                lt != LaneType::Construction,
            ),
            ("contraflow", "reverse lane direction", Key::F, true),
        ] {
            row.push(if active {
                Btn::svg_def(format!("system/assets/edit/{}.svg", icon)).build(
                    ctx,
                    label,
                    hotkey(key),
                )
            } else {
                Widget::draw_svg_transform(
                    ctx,
                    &format!("system/assets/edit/{}.svg", icon),
                    RewriteColor::ChangeAll(Color::WHITE.alpha(0.5)),
                )
            });
        }

        let parent = app.primary.map.get_parent(l);
        let col = vec![
            format!(
                "Convert this lane of {} to what type?",
                parent.get_name(app.opts.language.as_ref())
            )
            .draw_text(ctx)
            .centered_horiz(),
            Widget::custom_row(row).centered(),
            change_speed_limit(ctx, parent.speed_limit),
            Btn::text_fg("Change access restrictions").build_def(ctx, hotkey(Key::A)),
            Widget::custom_row(vec![
                Btn::text_fg("Finish").build_def(ctx, hotkey(Key::Escape)),
                // TODO Handle reverting speed limit too...
                if app.primary.map.get_edits().original_lts.contains_key(&l)
                    || app.primary.map.get_edits().reversed_lanes.contains(&l)
                {
                    Btn::text_fg("Revert").build_def(ctx, hotkey(Key::R))
                } else {
                    Btn::text_fg("Revert").inactive(ctx)
                },
            ])
            .centered(),
        ];

        let composite = Composite::new(Widget::col(col))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx);

        Box::new(LaneEditor { l, mode, composite })
    }
}

impl State for LaneEditor {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        ctx.canvas_movement();
        // Restrict what can be selected.
        if ctx.redo_mouseover() {
            app.recalculate_current_selection(ctx);
            if let Some(ID::Lane(l)) = app.primary.current_selection {
                if !can_edit_lane(&self.mode, l, app) {
                    app.primary.current_selection = None;
                }
            } else if let Some(ID::Intersection(i)) = app.primary.current_selection {
                if app.primary.map.maybe_get_stop_sign(i).is_some()
                    && !self.mode.can_edit_stop_signs()
                {
                    app.primary.current_selection = None;
                }
            } else {
                app.primary.current_selection = None;
            }
        }
        if let Some(ID::Lane(l)) = app.primary.current_selection {
            if app.per_obj.left_click(ctx, "edit this lane") {
                return Transition::Replace(LaneEditor::new(ctx, app, l, self.mode.clone()));
            }
        }
        if let Some(ID::Intersection(id)) = app.primary.current_selection {
            if let Some(state) = maybe_edit_intersection(ctx, app, id, &self.mode) {
                return Transition::Replace(state);
            }
        }

        match self.composite.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "Change access restrictions" => {
                    return Transition::Push(ZoneEditor::new(
                        ctx,
                        app,
                        app.primary.map.get_l(self.l).parent,
                    ));
                }
                "Finish" => {
                    return Transition::Pop;
                }
                x => {
                    let map = &mut app.primary.map;
                    let result = match x {
                        "Revert" => {
                            // TODO It's hard to revert both changes at once.
                            if let Some(lt) = map.get_edits().original_lts.get(&self.l).cloned() {
                                try_change_lt(ctx, map, self.l, lt)
                            } else {
                                try_reverse(ctx, map, self.l)
                            }
                        }
                        "reverse lane direction" => try_reverse(ctx, map, self.l),
                        "convert to a driving lane" => {
                            try_change_lt(ctx, map, self.l, LaneType::Driving)
                        }
                        "convert to a protected bike lane" => {
                            try_change_lt(ctx, map, self.l, LaneType::Biking)
                        }
                        "convert to a bus-only lane" => {
                            try_change_lt(ctx, map, self.l, LaneType::Bus)
                        }
                        "convert to an on-street parking lane" => {
                            try_change_lt(ctx, map, self.l, LaneType::Parking)
                        }
                        "close for construction" => {
                            try_change_lt(ctx, map, self.l, LaneType::Construction)
                        }
                        _ => unreachable!(),
                    };
                    match result {
                        Ok(cmd) => {
                            let mut edits = map.get_edits().clone();
                            edits.commands.push(cmd);
                            apply_map_edits(ctx, app, edits);

                            return Transition::Replace(LaneEditor::new(
                                ctx,
                                app,
                                self.l,
                                self.mode.clone(),
                            ));
                        }
                        Err(err) => {
                            return Transition::Push(err);
                        }
                    }
                }
            },
            Outcome::Changed => {
                let parent = app.primary.map.get_parent(self.l);
                let new = self.composite.dropdown_value("speed limit");
                let old = parent.speed_limit;
                let mut edits = app.primary.map.get_edits().clone();
                edits.commands.push(EditCmd::ChangeSpeedLimit {
                    id: parent.id,
                    new,
                    old,
                });
                apply_map_edits(ctx, app, edits);
                return Transition::Replace(LaneEditor::new(ctx, app, self.l, self.mode.clone()));
            }
            _ => {}
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        g.draw_polygon(
            app.cs.perma_selected_object,
            app.primary
                .draw_map
                .get_l(self.l)
                .get_outline(&app.primary.map),
        );
        self.composite.draw(g);
        CommonState::draw_osd(g, app);
    }
}