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
use std::collections::BTreeSet;
use abstutil::Timer;
use geom::Duration;
use map_gui::tools::ChooseSomething;
use map_model::IntersectionID;
use widgetry::{
Choice, EventCtx, GfxCtx, HorizontalAlignment, Key, Outcome, Panel, State, TextExt, UpdateType,
VerticalAlignment, Widget,
};
use crate::app::{App, Transition};
use crate::sandbox::{spawn_agents_around, TimePanel};
struct PreviewTrafficSignal {
panel: Panel,
time_panel: TimePanel,
}
impl PreviewTrafficSignal {
fn new_state(ctx: &mut EventCtx, app: &App) -> Box<dyn State<App>> {
Box::new(PreviewTrafficSignal {
panel: Panel::new_builder(Widget::col(vec![
"Previewing traffic signal".text_widget(ctx),
ctx.style()
.btn_outline
.text("back to editing")
.hotkey(Key::Escape)
.build_def(ctx),
]))
.aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
.build(ctx),
time_panel: TimePanel::new(ctx, app),
})
}
}
impl State<App> for PreviewTrafficSignal {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
ctx.canvas_movement();
if let Outcome::Clicked(x) = self.panel.event(ctx) {
match x.as_ref() {
"back to editing" => {
app.primary.clear_sim();
return Transition::Pop;
}
_ => unreachable!(),
}
}
if let Some(t) = self.time_panel.event(ctx, app, None) {
return t;
}
if self.time_panel.is_paused() {
Transition::Keep
} else {
ctx.request_update(UpdateType::Game);
Transition::Keep
}
}
fn draw(&self, g: &mut GfxCtx, _: &App) {
self.panel.draw(g);
self.time_panel.draw(g);
}
}
pub fn make_previewer(
ctx: &mut EventCtx,
app: &App,
members: BTreeSet<IntersectionID>,
stage: usize,
) -> Box<dyn State<App>> {
let random = "random agents around these intersections".to_string();
let right_now = format!(
"change the traffic signal live at {}",
app.primary.suspended_sim.as_ref().unwrap().time()
);
ChooseSomething::new_state(
ctx,
"Preview the traffic signal with what kind of traffic?",
Choice::strings(vec![random, right_now]),
Box::new(move |x, ctx, app| {
if x == "random agents around these intersections" {
for (idx, i) in members.into_iter().enumerate() {
if idx == 0 {
let signal = app.primary.map.get_traffic_signal(i);
let mut step = Duration::ZERO;
for idx in 0..stage {
step += signal.stages[idx].stage_type.simple_duration();
}
app.primary.sim.timed_step(
&app.primary.map,
step,
&mut app.primary.sim_cb,
&mut Timer::throwaway(),
);
}
spawn_agents_around(i, app);
}
} else {
app.primary.sim = app.primary.suspended_sim.as_ref().unwrap().clone();
app.primary
.sim
.handle_live_edited_traffic_signals(&app.primary.map);
}
Transition::Replace(PreviewTrafficSignal::new_state(ctx, app))
}),
)
}