# context.rs - source
Source of the Rust file `maplibre/src/context.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

    use crate::coords::{Zoom, ZoomLevel, TILE_SIZE};
    use crate::io::tile_repository::TileRepository;
    use crate::render::camera::{Camera, Perspective, ViewProjection};
    use crate::util::ChangeObserver;
    use crate::{Renderer, ScheduleMethod, Style, WindowSize};
    use std::sync::mpsc;

    /// Stores the camera configuration.
    pub struct ViewState {
        pub zoom: ChangeObserver<Zoom>,
        pub camera: ChangeObserver<Camera>,
        pub perspective: Perspective,
    }

    impl ViewState {
        pub fn new(window_size: &WindowSize) -> Self {
            let camera = Camera::new(
                (TILE_SIZE / 2.0, TILE_SIZE / 2.0, 150.0),
                cgmath::Deg(-90.0),
                cgmath::Deg(0.0),
                window_size.width(),
                window_size.height(),
            );

            let perspective = Perspective::new(
                window_size.width(),
                window_size.height(),
                cgmath::Deg(110.0),
                100.0,
                2000.0,
            );

            Self {
                zoom: ChangeObserver::default(),
                camera: ChangeObserver::new(camera),
                perspective,
            }
        }

        pub fn view_projection(&self) -> ViewProjection {
            self.camera.calc_view_proj(&self.perspective)
        }

        pub fn visible_level(&self) -> ZoomLevel {
            self.zoom.level()
        }

        pub fn zoom(&self) -> Zoom {
            *self.zoom
        }

        pub fn update_zoom(&mut self, new_zoom: Zoom) {
            *self.zoom = new_zoom;
            log::info!("zoom: {}", new_zoom);
        }
    }

    /// Stores the context of the map.
    pub struct MapContext {
        pub view_state: ViewState,
        pub style: Style,

        pub tile_repository: TileRepository,
        pub renderer: Renderer,
    }