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
use crate::app::App;
use crate::edit::apply_map_edits;
use crate::game::{State, Transition};
use geom::{Duration, Time};
use map_model::{BusRouteID, EditCmd};
use widgetry::{
Btn, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, Spinner, TextExt,
VerticalAlignment, Widget,
};
pub struct RouteEditor {
panel: Panel,
route: BusRouteID,
}
impl RouteEditor {
pub fn new(ctx: &mut EventCtx, app: &mut App, id: BusRouteID) -> Box<dyn State> {
app.primary.current_selection = None;
let route = app.primary.map.get_br(id);
Box::new(RouteEditor {
panel: Panel::new(Widget::col(vec![
Widget::row(vec![
Line("Route editor").small_heading().draw(ctx),
Btn::plaintext("X")
.build(ctx, "close", Key::Escape)
.align_right(),
]),
Line(&route.full_name).draw(ctx),
Widget::row(vec![
"Frequency in minutes".draw_text(ctx),
Spinner::new(ctx, (1, 120), 60).named("freq_mins"),
]),
Btn::text_bg2("Apply").build_def(ctx, Key::Enter),
]))
.aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
.build(ctx),
route: id,
})
}
}
impl State for RouteEditor {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
ctx.canvas_movement();
match self.panel.event(ctx) {
Outcome::Clicked(x) => match x.as_ref() {
"close" => {
return Transition::Pop;
}
"Apply" => {
let freq = Duration::minutes(self.panel.spinner("freq_mins") as usize);
let mut now = Time::START_OF_DAY;
let mut hourly_times = Vec::new();
while now <= Time::START_OF_DAY + Duration::hours(24) {
hourly_times.push(now);
now += freq;
}
let mut edits = app.primary.map.get_edits().clone();
edits.commands.push(EditCmd::ChangeRouteSchedule {
id: self.route,
old: app.primary.map.get_br(self.route).spawn_times.clone(),
new: hourly_times,
});
apply_map_edits(ctx, app, edits);
return Transition::Pop;
}
_ => unreachable!(),
},
_ => {}
}
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, _: &App) {
self.panel.draw(g);
}
}