# lib.rs - source
Source of the Rust file `maplibre/src/lib.rs`.
Keywords: rust, rustlang, rust-lang

      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
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295

    //! # Maplibre-rs
    //!
    //! A multi-platform library for rendering vector tile maps with WebGPU.
    //!
    //! Maplibre-rs is a map renderer that can run natively on MacOS, Linux, Windows, Android, iOS and the web.
    //! It takes advantage of Lyon to tessellate vector tiles and WebGPU to display them efficiently.
    //! Maplibre-rs also has an headless mode (*work in progress*) that can generate rasters.
    //!
    //! The official guide book can be found [here](https://maxammann.org/maplibre-rs/docs/).
    //!
    //! ### Example
    //!
    //! To import maplibre-rs in your `Cargo.toml`:
    //!
    //! ```toml
    //! maplibre = "0.0.2"
    //! ```

    use crate::io::scheduler::{ScheduleMethod, Scheduler};
    use crate::io::source_client::HttpClient;
    use crate::map_schedule::{InteractiveMapSchedule, SimpleMapSchedule};
    use crate::render::settings::{RendererSettings, WgpuSettings};
    use crate::render::{RenderState, Renderer};
    use crate::style::Style;
    use crate::window::{EventLoop, HeadedMapWindow, MapWindow, MapWindowConfig, WindowSize};

    pub mod context;
    pub mod coords;
    pub mod error;
    pub mod io;
    // Exposed because of input handlers in maplibre-winit
    pub mod map_schedule;
    pub mod platform;
    // Exposed because of camera
    pub mod render;
    pub mod style;
    pub mod window;
    // Exposed because of doc-strings
    pub mod schedule;
    // Exposed because of SharedThreadState
    pub mod stages;

    // Used for benchmarking
    pub mod benchmarking;

    // Internal modules
    pub(crate) mod tessellation;
    pub mod util;

    /// The [`Map`] defines the public interface of the map renderer.
    // DO NOT IMPLEMENT INTERNALS ON THIS STRUCT.
    pub struct Map<MWC, SM, HC>
    where
        MWC: MapWindowConfig,
        SM: ScheduleMethod,
        HC: HttpClient,
    {
        map_schedule: InteractiveMapSchedule<MWC, SM, HC>,
        window: MWC::MapWindow,
    }

    impl<MWC, SM, HC> Map<MWC, SM, HC>
    where
        MWC: MapWindowConfig,
        SM: ScheduleMethod,
        HC: HttpClient,
    {
        /// Starts the [`crate::map_schedule::MapState`] Runnable with the configured event loop.
        pub fn run(self)
        where
            MWC::MapWindow: EventLoop<MWC, SM, HC>,
        {
            self.run_with_optionally_max_frames(None);
        }

        /// Starts the [`crate::map_schedule::MapState`] Runnable with the configured event loop.
        ///
        /// # Arguments
        ///
        /// * `max_frames` - Maximum number of frames per second.
        pub fn run_with_max_frames(self, max_frames: u64)
        where
            MWC::MapWindow: EventLoop<MWC, SM, HC>,
        {
            self.run_with_optionally_max_frames(Some(max_frames));
        }

        /// Starts the MapState Runnable with the configured event loop.
        ///
        /// # Arguments
        ///
        /// * `max_frames` - Optional maximum number of frames per second.
        pub fn run_with_optionally_max_frames(self, max_frames: Option<u64>)
        where
            MWC::MapWindow: EventLoop<MWC, SM, HC>,
        {
            self.window.run(self.map_schedule, max_frames);
        }

        pub fn map_schedule(&self) -> &InteractiveMapSchedule<MWC, SM, HC> {
            &self.map_schedule
        }

        pub fn map_schedule_mut(&mut self) -> &mut InteractiveMapSchedule<MWC, SM, HC> {
            &mut self.map_schedule
        }
    }

    pub struct HeadlessMap<MWC, SM, HC>
    where
        MWC: MapWindowConfig,
        SM: ScheduleMethod,
        HC: HttpClient,
    {
        map_schedule: SimpleMapSchedule<MWC, SM, HC>,
        window: MWC::MapWindow,
    }

    impl<MWC, SM, HC> HeadlessMap<MWC, SM, HC>
    where
        MWC: MapWindowConfig,
        SM: ScheduleMethod,
        HC: HttpClient,
    {
        pub fn map_schedule_mut(&mut self) -> &mut SimpleMapSchedule<MWC, SM, HC> {
            &mut self.map_schedule
        }
    }

    /// Stores the map configuration before the map's state has been fully initialized.
    pub struct UninitializedMap<MWC, SM, HC>
    where
        MWC: MapWindowConfig,
        SM: ScheduleMethod,
        HC: HttpClient,
    {
        scheduler: Scheduler<SM>,
        http_client: HC,
        style: Style,

        wgpu_settings: WgpuSettings,
        renderer_settings: RendererSettings,
        map_window_config: MWC,
    }

    impl<MWC, SM, HC> UninitializedMap<MWC, SM, HC>
    where
        MWC: MapWindowConfig,
        SM: ScheduleMethod,
        HC: HttpClient,
    {
        /// Initializes the whole rendering pipeline for the given configuration.
        /// Returns the initialized map, ready to be run.
        pub async fn initialize(self) -> Map<MWC, SM, HC>
        where
            MWC: MapWindowConfig,
            <MWC as MapWindowConfig>::MapWindow: HeadedMapWindow,
        {
            let window = self.map_window_config.create();
            let window_size = window.size();

            #[cfg(target_os = "android")]
            let renderer = None;
            #[cfg(not(target_os = "android"))]
            let renderer = Renderer::initialize(
                &window,
                self.wgpu_settings.clone(),
                self.renderer_settings.clone(),
            )
            .await
            .ok();
            Map {
                map_schedule: InteractiveMapSchedule::new(
                    self.map_window_config,
                    window_size,
                    renderer,
                    self.scheduler,
                    self.http_client,
                    self.style,
                    self.wgpu_settings,
                    self.renderer_settings,
                ),
                window,
            }
        }

        pub async fn initialize_headless(self) -> HeadlessMap<MWC, SM, HC> {
            let window = self.map_window_config.create();
            let window_size = window.size();

            let renderer = Renderer::initialize_headless(
                &window,
                self.wgpu_settings.clone(),
                self.renderer_settings.clone(),
            )
            .await
            .expect("Failed to initialize renderer");
            HeadlessMap {
                map_schedule: SimpleMapSchedule::new(
                    self.map_window_config,
                    window_size,
                    renderer,
                    self.scheduler,
                    self.http_client,
                    self.style,
                ),
                window,
            }
        }
    }

    pub struct MapBuilder<MWC, SM, HC>
    where
        SM: ScheduleMethod,
    {
        schedule_method: Option<SM>,
        scheduler: Option<Scheduler<SM>>,
        http_client: Option<HC>,
        style: Option<Style>,

        map_window_config: Option<MWC>,
        wgpu_settings: Option<WgpuSettings>,
        renderer_settings: Option<RendererSettings>,
    }

    impl<MWC, SM, HC> MapBuilder<MWC, SM, HC>
    where
        MWC: MapWindowConfig,
        SM: ScheduleMethod,
        HC: HttpClient,
    {
        pub fn new() -> Self {
            Self {
                schedule_method: None,
                scheduler: None,
                http_client: None,
                style: None,
                map_window_config: None,
                wgpu_settings: None,
                renderer_settings: None,
            }
        }

        pub fn with_map_window_config(mut self, map_window_config: MWC) -> Self {
            self.map_window_config = Some(map_window_config);
            self
        }

        pub fn with_renderer_settings(mut self, renderer_settings: RendererSettings) -> Self {
            self.renderer_settings = Some(renderer_settings);
            self
        }

        pub fn with_wgpu_settings(mut self, wgpu_settings: WgpuSettings) -> Self {
            self.wgpu_settings = Some(wgpu_settings);
            self
        }

        pub fn with_schedule_method(mut self, schedule_method: SM) -> Self {
            self.schedule_method = Some(schedule_method);
            self
        }

        pub fn with_http_client(mut self, http_client: HC) -> Self {
            self.http_client = Some(http_client);
            self
        }

        pub fn with_existing_scheduler(mut self, scheduler: Scheduler<SM>) -> Self {
            self.scheduler = Some(scheduler);
            self
        }

        pub fn with_style(mut self, style: Style) -> Self {
            self.style = Some(style);
            self
        }

        /// Builds the UninitializedMap with the given configuration.
        pub fn build(self) -> UninitializedMap<MWC, SM, HC> {
            let scheduler = self
                .scheduler
                .unwrap_or_else(|| Scheduler::new(self.schedule_method.unwrap()));
            let style = self.style.unwrap_or_default();

            UninitializedMap {
                scheduler,
                http_client: self.http_client.unwrap(),
                style,
                wgpu_settings: self.wgpu_settings.unwrap_or_default(),
                renderer_settings: self.renderer_settings.unwrap_or_default(),
                map_window_config: self.map_window_config.unwrap(),
            }
        }
    }