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
use std::{cell::RefCell, marker::PhantomData, mem, rc::Rc};

use crate::{
    context::{MapContext, ViewState},
    coords::{LatLon, WorldCoords, Zoom, TILE_SIZE},
    error::Error,
    io::{
        scheduler::Scheduler,
        source_client::{HttpClient, HttpSourceClient},
        tile_repository::TileRepository,
    },
    render::{create_default_render_graph, register_default_render_stages},
    schedule::{Schedule, Stage},
    stages::register_stages,
    style::Style,
    Environment, HeadedMapWindow, MapWindowConfig, Renderer, RendererSettings, WgpuSettings,
    WindowSize,
};

/// Stores the state of the map, dispatches tile fetching and caching, tessellation and drawing.
pub struct InteractiveMapSchedule<E: Environment> {
    map_window_config: E::MapWindowConfig,

    // FIXME (wasm-executor): avoid RefCell, change ownership model
    pub apc: Rc<RefCell<E::AsyncProcedureCall>>,

    map_context: EventuallyMapContext,

    schedule: Schedule,

    suspended: bool,
}

impl<E: Environment> InteractiveMapSchedule<E> {
    pub fn new(
        map_window_config: E::MapWindowConfig,
        window_size: WindowSize,
        renderer: Option<Renderer>,
        scheduler: E::Scheduler, // TODO: unused
        apc: E::AsyncProcedureCall,
        http_client: E::HttpClient,
        style: Style,
        wgpu_settings: WgpuSettings,
        renderer_settings: RendererSettings,
    ) -> Self {
        let zoom = style.zoom.map(|zoom| Zoom::new(zoom)).unwrap_or_default();
        let position = style
            .center
            .map(|center| WorldCoords::from_lat_lon(LatLon::new(center[0], center[1]), zoom))
            .unwrap_or_default();
        let pitch = style.pitch.unwrap_or_default();
        let view_state = ViewState::new(&window_size, position, zoom, pitch, cgmath::Deg(110.0));

        let tile_repository = TileRepository::new();
        let mut schedule = Schedule::default();

        let apc = Rc::new(RefCell::new(apc));

        let http_source_client: HttpSourceClient<E::HttpClient> =
            HttpSourceClient::new(http_client);
        register_stages::<E>(&mut schedule, http_source_client, apc.clone());

        let graph = create_default_render_graph().unwrap(); // TODO: Remove unwrap
        register_default_render_stages(graph, &mut schedule);

        Self {
            apc,
            map_window_config,
            map_context: match renderer {
                None => EventuallyMapContext::Premature(PrematureMapContext {
                    view_state,
                    style,
                    tile_repository,
                    wgpu_settings,
                    renderer_settings,
                }),
                Some(renderer) => EventuallyMapContext::Full(MapContext {
                    view_state,
                    style,
                    tile_repository,
                    renderer,
                }),
            },
            schedule,
            suspended: false,
        }
    }

    #[tracing::instrument(name = "update_and_redraw", skip_all)]
    pub fn update_and_redraw(&mut self) -> Result<(), Error> {
        if self.suspended {
            return Ok(());
        }

        if let EventuallyMapContext::Full(map_context) = &mut self.map_context {
            self.schedule.run(map_context)
        }

        Ok(())
    }

    pub fn resize(&mut self, width: u32, height: u32) {
        if let EventuallyMapContext::Full(map_context) = &mut self.map_context {
            let view_state = &mut map_context.view_state;
            view_state.perspective.resize(width, height);
            view_state.camera.resize(width, height);

            map_context.renderer.resize(width, height)
        }
    }

    pub fn is_initialized(&self) -> bool {
        match &self.map_context {
            EventuallyMapContext::Full(_) => true,
            _ => false,
        }
    }

    pub fn view_state_mut(&mut self) -> &mut ViewState {
        match &mut self.map_context {
            EventuallyMapContext::Full(MapContext { view_state, .. }) => view_state,
            EventuallyMapContext::Premature(PrematureMapContext { view_state, .. }) => view_state,
            _ => panic!("should not happen"),
        }
    }

    pub fn apc(&self) -> &Rc<RefCell<E::AsyncProcedureCall>> {
        &self.apc
    }
}

impl<E: Environment> InteractiveMapSchedule<E>
where
    <E::MapWindowConfig as MapWindowConfig>::MapWindow: HeadedMapWindow,
{
    pub fn suspend(&mut self) {
        self.suspended = true;
    }

    pub fn resume(&mut self, window: &<E::MapWindowConfig as MapWindowConfig>::MapWindow) {
        if let EventuallyMapContext::Full(map_context) = &mut self.map_context {
            let renderer = &mut map_context.renderer;
            renderer.state.recreate_surface(window, &renderer.instance);
            self.suspended = false;
        }
    }

    pub async fn late_init(&mut self) -> bool {
        match &self.map_context {
            EventuallyMapContext::Full(_) => false,
            EventuallyMapContext::Premature(PrematureMapContext {
                wgpu_settings,
                renderer_settings,
                ..
            }) => {
                let window = self.map_window_config.create();
                let renderer =
                    Renderer::initialize(&window, wgpu_settings.clone(), renderer_settings.clone())
                        .await
                        .unwrap(); // TODO: Remove unwrap
                self.map_context.make_full(renderer);
                true
            }
            EventuallyMapContext::_Uninitialized => false,
        }
    }
}

pub struct PrematureMapContext {
    view_state: ViewState,
    style: Style,

    tile_repository: TileRepository,

    wgpu_settings: WgpuSettings,
    renderer_settings: RendererSettings,
}

pub enum EventuallyMapContext {
    Full(MapContext),
    Premature(PrematureMapContext),
    _Uninitialized,
}

impl EventuallyMapContext {
    pub fn make_full(&mut self, renderer: Renderer) {
        let context = mem::replace(self, EventuallyMapContext::_Uninitialized);

        match context {
            EventuallyMapContext::Full(_) => {}
            EventuallyMapContext::Premature(PrematureMapContext {
                view_state,
                style,
                tile_repository,
                ..
            }) => {
                *self = EventuallyMapContext::Full(MapContext {
                    view_state,
                    style,
                    tile_repository,
                    renderer,
                });
            }
            EventuallyMapContext::_Uninitialized => {}
        }
    }
}