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 std::collections::HashSet;
use abstutil::prettyprint_usize;
use geom::{Circle, Distance, Pt2D, Time};
use map_gui::tools::{make_heatmap, HeatmapOptions};
use sim::PersonState;
use widgetry::mapspace::ToggleZoomed;
use widgetry::{
Choice, Color, EventCtx, GfxCtx, Line, Outcome, Panel, Text, TextExt, Toggle, Widget,
};
use crate::app::App;
use crate::layer::{header, Layer, LayerOutcome, PANEL_PLACEMENT};
pub struct Pandemic {
time: Time,
opts: Options,
draw: ToggleZoomed,
panel: Panel,
}
impl Layer for Pandemic {
fn name(&self) -> Option<&'static str> {
Some("pandemic model")
}
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Option<LayerOutcome> {
if app.primary.sim.time() != self.time {
let mut new = Pandemic::new(ctx, app, self.opts.clone());
new.panel.restore(ctx, &self.panel);
*self = new;
}
match self.panel.event(ctx) {
Outcome::Clicked(x) => match x.as_ref() {
"close" => {
return Some(LayerOutcome::Close);
}
_ => unreachable!(),
},
_ => {
let new_opts = self.options();
if self.opts != new_opts {
*self = Pandemic::new(ctx, app, new_opts);
}
}
}
None
}
fn draw(&self, g: &mut GfxCtx, _: &App) {
self.panel.draw(g);
self.draw.draw(g);
}
fn draw_minimap(&self, g: &mut GfxCtx) {
g.redraw(&self.draw.unzoomed);
}
}
impl Pandemic {
pub fn new(ctx: &mut EventCtx, app: &App, opts: Options) -> Pandemic {
let model = app.primary.sim.get_pandemic_model().unwrap();
let filter = |p| match opts.state {
Seir::Sane => model.is_sane(p),
Seir::Exposed => model.is_exposed(p),
Seir::Infected => model.is_exposed(p),
Seir::Recovered => model.is_recovered(p),
Seir::Dead => model.is_dead(p),
};
let mut pts = Vec::new();
for a in app.primary.sim.get_unzoomed_agents(&app.primary.map) {
if let Some(p) = a.person {
if filter(p) {
pts.push(a.pos);
}
}
}
let mut seen_bldgs = HashSet::new();
let mut repeat_pts = Vec::new();
for person in app.primary.sim.get_all_people() {
match person.state {
PersonState::Trip(_) => {}
PersonState::Inside(b) => {
if !filter(person.id) {
continue;
}
let pt = app.primary.map.get_b(b).polygon.center();
if seen_bldgs.contains(&b) {
repeat_pts.push(pt);
} else {
seen_bldgs.insert(b);
pts.push(pt);
}
}
PersonState::OffMap => {}
}
}
let mut draw = ToggleZoomed::builder();
let legend = if let Some(ref o) = opts.heatmap {
pts.extend(repeat_pts);
Some(make_heatmap(
ctx,
&mut draw.unzoomed,
app.primary.map.get_bounds(),
pts,
o,
))
} else {
let circle = Circle::new(Pt2D::new(0.0, 0.0), Distance::meters(10.0)).to_polygon();
for pt in pts {
draw.unzoomed
.push(Color::RED.alpha(0.8), circle.translate(pt.x(), pt.y()));
}
None
};
let controls = make_controls(ctx, app, &opts, legend);
Pandemic {
time: app.primary.sim.time(),
opts,
draw: draw.build(ctx),
panel: controls,
}
}
fn options(&self) -> Options {
let heatmap = if self.panel.is_checked("Show heatmap") {
Some(HeatmapOptions::from_controls(&self.panel))
} else {
None
};
Options {
heatmap,
state: self.panel.dropdown_value("seir"),
}
}
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Seir {
Sane,
Exposed,
Infected,
Recovered,
Dead,
}
#[derive(Clone, PartialEq)]
pub struct Options {
pub heatmap: Option<HeatmapOptions>,
pub state: Seir,
}
fn make_controls(ctx: &mut EventCtx, app: &App, opts: &Options, legend: Option<Widget>) -> Panel {
let model = app.primary.sim.get_pandemic_model().unwrap();
let pct = 100.0 / (model.count_total() as f64);
let mut col = vec![
header(ctx, "Pandemic model"),
Text::from_multiline(vec![
Line(format!(
"{} Sane ({:.1}%)",
prettyprint_usize(model.count_sane()),
(model.count_sane() as f64) * pct
)),
Line(format!(
"{} Exposed ({:.1}%)",
prettyprint_usize(model.count_exposed()),
(model.count_exposed() as f64) * pct
)),
Line(format!(
"{} Infected ({:.1}%)",
prettyprint_usize(model.count_infected()),
(model.count_infected() as f64) * pct
)),
Line(format!(
"{} Recovered ({:.1}%)",
prettyprint_usize(model.count_recovered()),
(model.count_recovered() as f64) * pct
)),
Line(format!(
"{} Dead ({:.1}%)",
prettyprint_usize(model.count_dead()),
(model.count_dead() as f64) * pct
)),
])
.into_widget(ctx),
Widget::row(vec![
"Filter:".text_widget(ctx),
Widget::dropdown(
ctx,
"seir",
opts.state,
vec![
Choice::new("sane", Seir::Sane),
Choice::new("exposed", Seir::Exposed),
Choice::new("infected", Seir::Infected),
Choice::new("recovered", Seir::Recovered),
Choice::new("dead", Seir::Dead),
],
),
]),
];
col.push(Toggle::switch(
ctx,
"Show heatmap",
None,
opts.heatmap.is_some(),
));
if let Some(ref o) = opts.heatmap {
col.extend(o.to_controls(ctx, legend.unwrap()));
}
Panel::new_builder(Widget::col(col))
.aligned_pair(PANEL_PLACEMENT)
.build(ctx)
}