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
use map_gui::render::Renderable;
use map_gui::ID;
use map_model::{EditCmd, LaneID, LaneType, Map};
use widgetry::{
    Choice, Color, ControlState, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Panel,
    SimpleState, State, StyledButtons, TextExt, VerticalAlignment, Widget,
};

use crate::app::App;
use crate::app::Transition;
use crate::common::CommonState;
use crate::edit::zones::ZoneEditor;
use crate::edit::{
    apply_map_edits, can_edit_lane, maybe_edit_intersection, speed_limit_choices, try_change_lt,
};
use crate::sandbox::GameplayMode;

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

impl LaneEditor {
    pub fn new(
        ctx: &mut EventCtx,
        app: &App,
        l: LaneID,
        mode: GameplayMode,
    ) -> Box<dyn State<App>> {
        let mut row = Vec::new();
        let current_lt = app.primary.map.get_l(l).lane_type;
        for (icon, label, key, lt) in vec![
            (
                "driving",
                "convert to a driving lane",
                Key::D,
                LaneType::Driving,
            ),
            (
                "bike",
                "convert to a protected bike lane",
                Key::B,
                LaneType::Biking,
            ),
            ("bus", "convert to a bus-only lane", Key::T, LaneType::Bus),
            (
                "parking",
                "convert to an on-street parking lane",
                Key::P,
                LaneType::Parking,
            ),
            (
                "construction",
                "close for construction",
                Key::C,
                LaneType::Construction,
            ),
        ] {
            row.push(
                ctx.style()
                    .btn_plain_light_icon(&format!("system/assets/edit/{}.svg", icon))
                    .hotkey(key)
                    .disabled(current_lt == lt)
                    .build_widget(ctx, label),
            );
        }

        let parent = app.primary.map.get_parent(l);
        let col = vec![
            Widget::row(vec![
                Line(format!("Editing {}", l)).small_heading().draw(ctx),
                ctx.style()
                    .btn_plain_light_text("+ Edit multiple")
                    .label_color(Color::hex("#4CA7E9"), ControlState::Default)
                    .hotkey(Key::M)
                    .build_widget(ctx, "Edit multiple lanes"),
            ]),
            "Type of lane".draw_text(ctx),
            Widget::custom_row(row).centered(),
            ctx.style()
                .btn_outline_light_text("reverse direction")
                .hotkey(Key::F)
                .build_def(ctx),
            {
                let mut choices = speed_limit_choices(app);
                if !choices.iter().any(|c| c.data == parent.speed_limit) {
                    choices.push(Choice::new(
                        parent.speed_limit.to_string(&app.opts.units),
                        parent.speed_limit,
                    ));
                }
                Widget::row(vec![
                    "Change speed limit:".draw_text(ctx).centered_vert(),
                    Widget::dropdown(ctx, "speed limit", parent.speed_limit, choices),
                ])
            },
            ctx.style()
                .btn_outline_light_text("Change access restrictions")
                .hotkey(Key::A)
                .build_def(ctx),
            ctx.style()
                .btn_solid_dark_text("Finish")
                .hotkey(Key::Escape)
                .build_def(ctx),
        ];
        let panel = Panel::new(Widget::col(col))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx);

        SimpleState::new(panel, Box::new(LaneEditor { l, mode }))
    }
}

impl SimpleState<App> for LaneEditor {
    fn on_click(&mut self, ctx: &mut EventCtx, app: &mut App, x: &str, _: &Panel) -> Transition {
        match x {
            "Edit multiple lanes" => Transition::Replace(crate::edit::bulk::BulkSelect::new(
                ctx,
                app,
                app.primary.map.get_l(self.l).parent,
            )),
            "Change access restrictions" => Transition::Push(ZoneEditor::new(
                ctx,
                app,
                app.primary.map.get_l(self.l).parent,
            )),
            "Finish" => Transition::Pop,
            x => {
                let map = &mut app.primary.map;
                let result = match x {
                    "reverse direction" => Ok(reverse_lane(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);

                        Transition::Replace(LaneEditor::new(ctx, app, self.l, self.mode.clone()))
                    }
                    Err(err) => Transition::Push(err),
                }
            }
        }
    }

    fn panel_changed(
        &mut self,
        ctx: &mut EventCtx,
        app: &mut App,
        panel: &Panel,
    ) -> Option<Transition> {
        let mut edits = app.primary.map.get_edits().clone();
        edits.commands.push(app.primary.map.edit_road_cmd(
            app.primary.map.get_l(self.l).parent,
            |new| {
                new.speed_limit = panel.dropdown_value("speed limit");
            },
        ));
        apply_map_edits(ctx, app, edits);
        Some(Transition::Replace(LaneEditor::new(
            ctx,
            app,
            self.l,
            self.mode.clone(),
        )))
    }

    fn on_mouseover(&mut self, ctx: &mut EventCtx, app: &mut App) {
        app.recalculate_current_selection(ctx);
        app.recalculate_current_selection(ctx);
        if match app.primary.current_selection {
            Some(ID::Lane(l)) => !can_edit_lane(&self.mode, l, app),
            Some(ID::Intersection(i)) => {
                !self.mode.can_edit_stop_signs() && app.primary.map.maybe_get_stop_sign(i).is_some()
            }
            _ => true,
        } {
            app.primary.current_selection = None;
        }
    }

    fn other_event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        ctx.canvas_movement();
        if let Some(l) = app.click_on_lane(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);
            }
        }

        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),
        );
        CommonState::draw_osd(g, app);
    }
}

// Allow doing this anywhere. Players can create really wacky roads with many direction changes,
// but it's not really useful to limit creativity. ;)
fn reverse_lane(map: &Map, l: LaneID) -> EditCmd {
    let r = map.get_parent(l);
    let idx = r.offset(l);
    map.edit_road_cmd(r.id, |new| {
        new.lanes_ltr[idx].1 = new.lanes_ltr[idx].1.opposite();
    })
}