1// TODO Huge hack -- this is just a copy of the map_gui file. I want to remove the dependency on
2// map_gui from map_editor, but this file really feels like it belongs there. I'm choosing faster
3// build time for map_editor over duplicate code, for now. :\
45use serde::{Deserialize, Serialize};
67use abstio::MapName;
8use abstutil::Timer;
9use widgetry::{Canvas, EventCtx};
1011/// Represents the state of a widgetry Canvas.
12#[derive(Serialize, Deserialize, Debug)]
13pub struct CameraState {
14 cam_x: f64,
15 cam_y: f64,
16 cam_zoom: f64,
17}
1819/// Track the last map used, to resume next session.
20#[derive(Serialize, Deserialize, Debug)]
21pub struct DefaultMap {
22pub last_map: MapName,
23}
2425impl CameraState {
26/// Save the camera's configuration for the specified map, and also remember this map was the
27 /// last to be used.
28pub fn save(canvas: &Canvas, name: &MapName) {
29if name == &MapName::blank() {
30return;
31 }
3233let state = CameraState {
34 cam_x: canvas.cam_x,
35 cam_y: canvas.cam_y,
36 cam_zoom: canvas.cam_zoom,
37 };
38 abstio::write_json(abstio::path_camera_state(name), &state);
3940 abstio::write_json(
41 abstio::path_player("maps.json"),
42&DefaultMap {
43 last_map: name.clone(),
44 },
45 );
46 }
4748/// Load the camera's configuration for the specified map. Returns true if successful, has no
49 /// effect if the file is missing or broken.
50pub fn load(ctx: &mut EventCtx, name: &MapName) -> bool {
51match abstio::maybe_read_json::<CameraState>(
52 abstio::path_camera_state(name),
53&mut Timer::throwaway(),
54 ) {
55Ok(ref loaded) => {
56 ctx.canvas.cam_x = loaded.cam_x;
57 ctx.canvas.cam_y = loaded.cam_y;
58 ctx.canvas.cam_zoom = loaded.cam_zoom;
59true
60}
61Err(_) => false,
62 }
63 }
64}