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
use std::collections::BTreeSet;

use enumset::EnumSet;
use maplit::btreeset;

use map_gui::tools::ColorDiscrete;
use map_model::{AccessRestrictions, PathConstraints, RoadID};
use sim::TripMode;
use widgetry::{
    Color, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, Spinner,
    State, Text, TextExt, VerticalAlignment, Widget,
};

use crate::app::{App, Transition};
use crate::common::RoadSelector;
use crate::common::{checkbox_per_mode, intersections_from_roads, CommonState};
use crate::edit::apply_map_edits;

pub struct ZoneEditor {
    panel: Panel,
    selector: RoadSelector,
    allow_through_traffic: BTreeSet<TripMode>,
    unzoomed: Drawable,
    zoomed: Drawable,

    orig_members: BTreeSet<RoadID>,
}

impl ZoneEditor {
    pub fn new_state(ctx: &mut EventCtx, app: &mut App, start: RoadID) -> Box<dyn State<App>> {
        let start = app.primary.map.get_r(start);
        let members = if let Some(z) = start.get_zone(&app.primary.map) {
            z.members.clone()
        } else {
            // Starting a new zone
            btreeset! { start.id }
        };
        let allow_through_traffic = start
            .access_restrictions
            .allow_through_traffic
            .into_iter()
            .map(TripMode::from_constraints)
            .collect();
        let cap_vehicles_per_hour = start.access_restrictions.cap_vehicles_per_hour;

        let (unzoomed, zoomed, legend) = draw_zone(ctx, app, &members);
        let orig_members = members.clone();
        let selector = RoadSelector::new(ctx, app, members);

        Box::new(ZoneEditor {
            panel: Panel::new_builder(Widget::col(vec![
                Line("Editing restricted access zone")
                    .small_heading()
                    .into_widget(ctx),
                selector.make_controls(ctx).named("selector"),
                legend,
                make_instructions(ctx, &allow_through_traffic).named("instructions"),
                checkbox_per_mode(ctx, app, &allow_through_traffic),
                Widget::row(vec![
                    "Limit the number of vehicles passing through per hour (0 = unlimited):"
                        .text_widget(ctx)
                        .centered_vert(),
                    Spinner::widget(
                        ctx,
                        "cap_vehicles",
                        (0, 1000),
                        cap_vehicles_per_hour.unwrap_or(0),
                        1,
                    ),
                ]),
                Widget::custom_row(vec![
                    ctx.style()
                        .btn_solid_primary
                        .text("Apply")
                        .hotkey(Key::Enter)
                        .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),
            orig_members,
            selector,
            allow_through_traffic,
            unzoomed,
            zoomed,
        })
    }
}

// TODO Handle splitting/merging zones.
impl State<App> for ZoneEditor {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "Apply" => {
                    let mut edits = app.primary.map.get_edits().clone();

                    // Roads deleted from the zone
                    for r in self.orig_members.difference(&self.selector.roads) {
                        edits
                            .commands
                            .push(app.primary.map.edit_road_cmd(*r, |new| {
                                new.access_restrictions = AccessRestrictions::new();
                            }));
                    }

                    let mut allow_through_traffic = self
                        .allow_through_traffic
                        .iter()
                        .map(|m| m.to_constraints())
                        .collect::<EnumSet<_>>();
                    // The original allow_through_traffic always includes this, and there's no way
                    // to exclude it, so stay consistent.
                    allow_through_traffic.insert(PathConstraints::Train);
                    let new_access_restrictions = AccessRestrictions {
                        allow_through_traffic,
                        cap_vehicles_per_hour: {
                            let n = self.panel.spinner("cap_vehicles");
                            if n == 0 {
                                None
                            } else {
                                Some(n)
                            }
                        },
                    };
                    for r in &self.selector.roads {
                        let old_access_restrictions =
                            app.primary.map.get_r(*r).access_restrictions.clone();
                        if old_access_restrictions != new_access_restrictions {
                            edits
                                .commands
                                .push(app.primary.map.edit_road_cmd(*r, |new| {
                                    new.access_restrictions = new_access_restrictions.clone();
                                }));
                        }
                    }

                    apply_map_edits(ctx, app, edits);
                    return Transition::Pop;
                }
                "Cancel" => {
                    return Transition::Pop;
                }
                x => {
                    if self.selector.event(ctx, app, Some(x)) {
                        let new_controls = self.selector.make_controls(ctx);
                        self.panel.replace(ctx, "selector", new_controls);
                        let (unzoomed, zoomed, _) = draw_zone(ctx, app, &self.selector.roads);
                        self.unzoomed = unzoomed;
                        self.zoomed = zoomed;
                    }
                }
            },
            Outcome::Changed(_) => {
                let mut new_allow_through_traffic = BTreeSet::new();
                for m in TripMode::all() {
                    if self.panel.is_checked(m.ongoing_verb()) {
                        new_allow_through_traffic.insert(m);
                    }
                }
                let instructions = make_instructions(ctx, &new_allow_through_traffic);
                self.panel.replace(ctx, "instructions", instructions);
                self.allow_through_traffic = new_allow_through_traffic;
            }
            _ => {
                if self.selector.event(ctx, app, None) {
                    let new_controls = self.selector.make_controls(ctx);
                    self.panel.replace(ctx, "selector", new_controls);
                    let (unzoomed, zoomed, _) = draw_zone(ctx, app, &self.selector.roads);
                    self.unzoomed = unzoomed;
                    self.zoomed = zoomed;
                }
            }
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        // TODO The currently selected road is covered up pretty badly
        if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
            g.redraw(&self.unzoomed);
        } else {
            g.redraw(&self.zoomed);
        }
        self.panel.draw(g);
        self.selector.draw(g, app, false);
        CommonState::draw_osd(g, app);
    }
}

fn draw_zone(
    ctx: &mut EventCtx,
    app: &App,
    members: &BTreeSet<RoadID>,
) -> (Drawable, Drawable, Widget) {
    let mut colorer = ColorDiscrete::new(
        app,
        vec![
            ("restricted road", Color::CYAN),
            ("entrance/exit", Color::BLUE),
        ],
    );
    let map = &app.primary.map;
    for r in members {
        let r = map.get_r(*r);
        colorer.add_r(r.id, "restricted road");
        for next in map.get_next_roads(r.id) {
            if !members.contains(&next) {
                colorer.add_i(r.common_endpt(map.get_r(next)), "entrance/exit");
            }
        }
    }
    for i in intersections_from_roads(members, &app.primary.map) {
        colorer.add_i(i, "restricted road");
    }
    colorer.build(ctx)
}

fn make_instructions(ctx: &mut EventCtx, allow_through_traffic: &BTreeSet<TripMode>) -> Widget {
    if allow_through_traffic == &TripMode::all().into_iter().collect() {
        Text::from(
            "Through-traffic is allowed for everyone, meaning this is just a normal public road. \
             Would you like to restrict it?",
        )
        .wrap_to_pct(ctx, 30)
        .into_widget(ctx)
    } else {
        Line("Trips may start or end in this zone, but through-traffic is only allowed for:")
            .into_widget(ctx)
    }
}