use std::cell::Ref;
use std::rc::Rc;
use std::sync::Arc;
use mln_map_types::{
AnimationOptions, BoundOptions, CameraOptions, ConstrainMode, MapDebugOptions, MapMode, NorthOrientation,
ProjectionMode, TileLodMode, TransformState, ViewportMode,
};
use mln_renderer::{ExceptionPtr, RendererFrontend};
use mln_storage::{ClientOptions, FileSource, FileSourceManager, FileSourceType, ResourceOptions};
use mln_style::Style;
use mln_util::exception::MisuseException;
use mln_util::geo::{EdgeInsets, LatLng, LatLngBounds, ScreenCoordinate, WrapMode};
use mln_util::size::Size;
use crate::map_impl::{MapImpl, StillImageCallback, StillImageRequest, StyleMut};
use crate::map_observer::MapObserver;
use crate::map_options::MapOptions;
pub struct Map {
impl_: Rc<MapImpl>,
}
impl Map {
pub fn new<F>(frontend: &F, observer: Rc<dyn MapObserver>, map_options: MapOptions) -> Map
where
F: RendererFrontend + Clone + 'static,
{
Self::with_resource_options(
frontend,
observer,
map_options,
&ResourceOptions::default(),
&ClientOptions::default(),
)
}
pub fn with_resource_options<F>(
frontend: &F,
observer: Rc<dyn MapObserver>,
map_options: MapOptions,
resource_options: &ResourceOptions,
client_options: &ClientOptions,
) -> Map
where
F: RendererFrontend + Clone + 'static,
{
let file_source =
FileSourceManager::get().get_file_source(FileSourceType::ResourceLoader, resource_options, client_options);
Map { impl_: MapImpl::new(Box::new(frontend.clone()), observer, file_source, &map_options) }
}
pub fn with_file_source<F>(
frontend: &F,
observer: Rc<dyn MapObserver>,
file_source: Arc<dyn FileSource>,
map_options: MapOptions,
) -> Map
where
F: RendererFrontend + Clone + 'static,
{
Map { impl_: MapImpl::new(Box::new(frontend.clone()), observer, Some(file_source), &map_options) }
}
pub fn get_client_options(&self) -> ClientOptions {
match &self.impl_.file_source {
Some(file_source) => file_source.get_client_options(),
None => ClientOptions::default(),
}
}
pub fn render_still(&mut self, callback: StillImageCallback) {
let impl_ = &self.impl_;
if impl_.mode != MapMode::Static && impl_.mode != MapMode::Tile {
callback(Some(misuse("Map is not in static or tile image render modes")));
return;
}
if impl_.still_image_request.borrow().is_some() {
callback(Some(misuse("Map is currently rendering an image")));
return;
}
let last_error = impl_.style.borrow().impl_.get_last_error();
if let Some(error) = last_error {
callback(Some(error));
return;
}
*impl_.still_image_request.borrow_mut() = Some(StillImageRequest { callback });
impl_.on_update();
}
pub fn render_still_with_camera(
&mut self,
camera: &CameraOptions,
debug_options: MapDebugOptions,
callback: StillImageCallback,
) {
self.impl_.camera_mutated.set(true);
self.impl_.debug_options.set(debug_options);
self.impl_.transform.borrow_mut().jump_to(camera);
self.render_still(callback);
}
pub fn trigger_repaint(&mut self) {
self.impl_.on_update();
}
pub fn get_style(&self) -> Ref<'_, Style> {
self.impl_.style.borrow()
}
pub fn get_style_mut(&mut self) -> StyleMut<'_> {
StyleMut::new(&self.impl_)
}
pub fn cancel_transitions(&mut self) {
self.impl_.transform.borrow_mut().cancel_transitions();
self.impl_.on_update();
}
pub fn set_gesture_in_progress(&mut self, in_progress: bool) {
self.impl_.transform.borrow_mut().set_gesture_in_progress(in_progress);
self.impl_.on_update();
}
pub fn is_gesture_in_progress(&self) -> bool {
self.impl_.transform.borrow().is_gesture_in_progress()
}
pub fn is_rotating(&self) -> bool {
self.impl_.transform.borrow().is_rotating()
}
pub fn is_scaling(&self) -> bool {
self.impl_.transform.borrow().is_scaling()
}
pub fn is_panning(&self) -> bool {
self.impl_.transform.borrow().is_panning()
}
pub fn get_camera_options(&self, padding: Option<EdgeInsets>) -> CameraOptions {
self.impl_.transform.borrow().get_camera_options(padding)
}
pub fn jump_to(&mut self, camera: &CameraOptions) {
self.impl_.jump_to(camera);
}
pub fn lat_lng_bounds_for_camera(&self, camera: &CameraOptions) -> mln_util::geo::LatLngBounds {
let mut shallow = crate::transform::Transform::from_state(self.impl_.transform.borrow().get_state().clone());
let size = shallow.get_state().get_size();
shallow.jump_to(camera);
mln_util::geo::LatLngBounds::hull(
shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(0.0, 0.0), mln_util::geo::WrapMode::Wrapped),
shallow.screen_coordinate_to_lat_lng(
&ScreenCoordinate::new(f64::from(size.width), f64::from(size.height)),
mln_util::geo::WrapMode::Wrapped,
),
)
}
pub fn ease_to(&mut self, camera: &CameraOptions, animation: &AnimationOptions) {
self.impl_.camera_mutated.set(true);
self.impl_.transform.borrow_mut().ease_to(camera, animation);
self.impl_.on_update();
}
pub fn fly_to(&mut self, camera: &CameraOptions, animation: &AnimationOptions) {
self.impl_.camera_mutated.set(true);
self.impl_.transform.borrow_mut().fly_to(camera, animation, false);
self.impl_.on_update();
}
pub fn move_by(&mut self, point: &ScreenCoordinate, animation: &AnimationOptions) {
self.impl_.camera_mutated.set(true);
self.impl_.transform.borrow_mut().move_by(point, animation);
self.impl_.on_update();
}
pub fn pitch_by(&mut self, pitch: f64, animation: &AnimationOptions) {
let current = mln_math::rad2deg(self.impl_.transform.borrow().get_pitch());
self.ease_to(&CameraOptions::default().with_pitch(Some(current - pitch)), animation);
}
pub fn scale_by(&mut self, scale: f64, anchor: Option<ScreenCoordinate>, animation: &AnimationOptions) {
let zoom = {
let transform = self.impl_.transform.borrow();
transform.get_zoom() + transform.get_state().scale_zoom(scale)
};
self.ease_to(&CameraOptions::default().with_zoom(Some(zoom)).with_anchor(anchor), animation);
}
pub fn rotate_by(&mut self, first: &ScreenCoordinate, second: &ScreenCoordinate, animation: &AnimationOptions) {
self.impl_.camera_mutated.set(true);
self.impl_.transform.borrow_mut().rotate_by(first, second, animation);
self.impl_.on_update();
}
pub fn camera_for_lat_lng_bounds(
&self,
bounds: &LatLngBounds,
padding: &EdgeInsets,
bearing: Option<f64>,
pitch: Option<f64>,
) -> CameraOptions {
self.camera_for_lat_lngs(
&[bounds.northwest(), bounds.southwest(), bounds.southeast(), bounds.northeast()],
padding,
bearing,
pitch,
)
}
pub fn camera_for_lat_lngs(
&self,
lat_lngs: &[LatLng],
padding: &EdgeInsets,
bearing: Option<f64>,
pitch: Option<f64>,
) -> CameraOptions {
if bearing.is_none() && pitch.is_none() {
return camera_for_lat_lngs(lat_lngs, &self.impl_.transform.borrow(), padding);
}
let mut transform = crate::transform::Transform::from_state(self.impl_.transform.borrow().get_state().clone());
transform.jump_to(&CameraOptions::default().with_bearing(bearing).with_pitch(pitch));
camera_for_lat_lngs(lat_lngs, &transform, padding)
.with_bearing(Some(mln_math::rad2deg(-transform.get_bearing())))
.with_pitch(Some(mln_math::rad2deg(transform.get_pitch())))
}
pub fn lat_lng_bounds_for_camera_unwrapped(&self, camera: &CameraOptions) -> LatLngBounds {
let mut shallow = crate::transform::Transform::from_state(self.impl_.transform.borrow().get_state().clone());
let size = shallow.get_state().get_size();
shallow.jump_to(camera);
let width = f64::from(size.width);
let height = f64::from(size.height);
let mut nw = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(0.0, 0.0), WrapMode::Wrapped);
let mut se = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(width, height), WrapMode::Wrapped);
let mut ne = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(width, 0.0), WrapMode::Wrapped);
let mut sw = shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(0.0, height), WrapMode::Wrapped);
let center =
shallow.screen_coordinate_to_lat_lng(&ScreenCoordinate::new(width / 2.0, height / 2.0), WrapMode::Wrapped);
nw.unwrap_for_shortest_path(¢er);
se.unwrap_for_shortest_path(¢er);
ne.unwrap_for_shortest_path(¢er);
sw.unwrap_for_shortest_path(¢er);
let mut bounds = LatLngBounds::hull(nw, se);
bounds.extend(&ne);
bounds.extend(&sw);
bounds.extend(¢er);
bounds
}
pub fn set_bounds(&mut self, options: &BoundOptions) -> Result<(), String> {
let mut change_camera = false;
let mut camera_options = CameraOptions::default();
{
let mut transform = self.impl_.transform.borrow_mut();
if let Some(bounds) = options.bounds {
change_camera = true;
transform.set_lat_lng_bounds(bounds)?;
}
if let Some(min_zoom) = options.min_zoom {
transform.set_min_zoom(min_zoom);
if transform.get_zoom() < min_zoom {
change_camera = true;
camera_options = camera_options.with_zoom(options.min_zoom);
}
}
if let Some(max_zoom) = options.max_zoom {
transform.set_max_zoom(max_zoom);
if transform.get_zoom() > max_zoom {
change_camera = true;
camera_options = camera_options.with_zoom(options.max_zoom);
}
}
if let Some(max_pitch) = options.max_pitch {
transform.set_max_pitch(max_pitch);
if transform.get_pitch() > transform.get_state().get_max_pitch() {
change_camera = true;
camera_options = camera_options.with_pitch(options.max_pitch);
}
}
if let Some(min_pitch) = options.min_pitch {
transform.set_min_pitch(min_pitch);
if transform.get_pitch() < transform.get_state().get_min_pitch() {
change_camera = true;
camera_options = camera_options.with_pitch(options.min_pitch);
}
}
}
if change_camera {
self.jump_to(&camera_options);
}
Ok(())
}
pub fn get_bounds(&self) -> BoundOptions {
let transform = self.impl_.transform.borrow();
let state = transform.get_state();
BoundOptions::default()
.with_lat_lng_bounds(state.get_lat_lng_bounds())
.with_min_zoom(state.get_min_zoom())
.with_max_zoom(state.get_max_zoom())
.with_min_pitch(mln_math::rad2deg(state.get_min_pitch()))
.with_max_pitch(mln_math::rad2deg(state.get_max_pitch()))
}
pub fn set_north_orientation(&mut self, orientation: NorthOrientation) {
self.impl_.transform.borrow_mut().set_north_orientation(orientation);
self.impl_.on_update();
}
pub fn set_constrain_mode(&mut self, mode: ConstrainMode) {
self.impl_.transform.borrow_mut().set_constrain_mode(mode);
self.impl_.on_update();
}
pub fn set_viewport_mode(&mut self, mode: ViewportMode) {
self.impl_.transform.borrow_mut().set_viewport_mode(mode);
self.impl_.on_update();
}
pub fn set_size(&mut self, size: Size) -> Result<(), String> {
self.impl_.transform.borrow_mut().resize(size)?;
self.impl_.on_update();
Ok(())
}
pub fn set_frustum_offset(&mut self, frustum_offset: &EdgeInsets) {
self.impl_.transform.borrow_mut().set_frustum_offset(frustum_offset);
self.impl_.on_update();
}
pub fn get_frustum_offset(&self) -> EdgeInsets {
self.impl_.transform.borrow().get_frustum_offset()
}
pub fn get_map_options(&self) -> MapOptions {
let transform = self.impl_.transform.borrow();
let state = transform.get_state();
MapOptions::new()
.with_north_orientation(transform.get_north_orientation())
.with_map_mode(self.impl_.mode)
.with_constrain_mode(state.get_constrain_mode())
.with_viewport_mode(state.get_viewport_mode())
.with_cross_source_collisions(self.impl_.cross_source_collisions)
.with_size(state.get_size())
.with_pixel_ratio(self.impl_.pixel_ratio)
}
pub fn set_projection_mode(&mut self, options: &ProjectionMode) {
self.impl_.transform.borrow_mut().set_projection_mode(options);
self.impl_.on_update();
}
pub fn get_projection_mode(&self) -> ProjectionMode {
self.impl_.transform.borrow().get_projection_mode()
}
pub fn get_transform_state(&self) -> TransformState {
self.impl_.transform.borrow().get_state().clone()
}
pub fn set_tile_lod_min_radius(&mut self, radius: f64) {
if self.impl_.tile_lod_min_radius.get() == radius {
return;
}
self.impl_.tile_lod_min_radius.set(radius);
self.impl_.on_update();
}
pub fn get_tile_lod_min_radius(&self) -> f64 {
self.impl_.tile_lod_min_radius.get()
}
pub fn set_tile_lod_scale(&mut self, scale: f64) {
if self.impl_.tile_lod_scale.get() == scale {
return;
}
self.impl_.tile_lod_scale.set(scale);
self.impl_.on_update();
}
pub fn get_tile_lod_scale(&self) -> f64 {
self.impl_.tile_lod_scale.get()
}
pub fn set_tile_lod_pitch_threshold(&mut self, threshold: f64) {
if self.impl_.tile_lod_pitch_threshold.get() == threshold {
return;
}
self.impl_.tile_lod_pitch_threshold.set(threshold);
self.impl_.on_update();
}
pub fn get_tile_lod_pitch_threshold(&self) -> f64 {
self.impl_.tile_lod_pitch_threshold.get()
}
pub fn set_tile_lod_zoom_shift(&mut self, shift: f64) {
if self.impl_.tile_lod_zoom_shift.get() == shift {
return;
}
self.impl_.tile_lod_zoom_shift.set(shift);
self.impl_.on_update();
}
pub fn get_tile_lod_zoom_shift(&self) -> f64 {
self.impl_.tile_lod_zoom_shift.get()
}
pub fn set_tile_lod_mode(&mut self, mode: TileLodMode) {
if self.impl_.tile_lod_mode.get() == mode {
return;
}
self.impl_.tile_lod_mode.set(mode);
self.impl_.on_update();
}
pub fn get_tile_lod_mode(&self) -> TileLodMode {
self.impl_.tile_lod_mode.get()
}
pub fn pixel_for_lat_lng(&self, lat_lng: &LatLng) -> ScreenCoordinate {
let transform = self.impl_.transform.borrow();
let mut unwrapped_lat_lng = lat_lng.wrapped();
unwrapped_lat_lng.unwrap_for_shortest_path(&transform.get_lat_lng(WrapMode::Wrapped));
transform.lat_lng_to_screen_coordinate(&unwrapped_lat_lng)
}
pub fn lat_lng_for_pixel(&self, pixel: &ScreenCoordinate, wrap_mode: WrapMode) -> LatLng {
self.impl_.transform.borrow().screen_coordinate_to_lat_lng(pixel, wrap_mode)
}
pub fn pixels_for_lat_lngs(&self, lat_lngs: &[LatLng]) -> Vec<ScreenCoordinate> {
lat_lngs.iter().map(|lat_lng| self.pixel_for_lat_lng(lat_lng)).collect()
}
pub fn lat_lngs_for_pixels(&self, screen_coords: &[ScreenCoordinate], wrap_mode: WrapMode) -> Vec<LatLng> {
screen_coords.iter().map(|point| self.lat_lng_for_pixel(point, wrap_mode)).collect()
}
pub fn set_prefetch_zoom_delta(&mut self, delta: u8) {
if self.impl_.prefetch_zoom_delta.get() == delta {
return;
}
self.impl_.prefetch_zoom_delta.set(delta);
self.impl_.on_update();
}
pub fn get_prefetch_zoom_delta(&self) -> u8 {
self.impl_.prefetch_zoom_delta.get()
}
pub fn get_free_camera_options(&self) -> mln_map_types::FreeCameraOptions {
self.impl_.transform.borrow().get_free_camera_options()
}
pub fn set_debug(&mut self, debug_options: MapDebugOptions) {
self.impl_.debug_options.set(debug_options);
self.impl_.on_update();
}
pub fn get_debug(&self) -> MapDebugOptions {
self.impl_.debug_options.get()
}
pub fn is_fully_loaded(&self) -> bool {
self.impl_.style.borrow().impl_.is_loaded() && self.impl_.renderer_fully_loaded.get()
}
}
fn misuse(message: &str) -> ExceptionPtr {
MisuseException::ptr(message)
}
pub(crate) fn camera_for_lat_lngs(
lat_lngs: &[LatLng],
transform: &crate::transform::Transform,
padding: &EdgeInsets,
) -> CameraOptions {
if lat_lngs.is_empty() {
return CameraOptions::default();
}
let size = transform.get_state().get_size();
let mut ne_pixel = ScreenCoordinate::new(f64::NEG_INFINITY, f64::NEG_INFINITY);
let mut sw_pixel = ScreenCoordinate::new(f64::INFINITY, f64::INFINITY);
for lat_lng in lat_lngs {
let pixel = transform.lat_lng_to_screen_coordinate(lat_lng);
sw_pixel.x = if pixel.x < sw_pixel.x { pixel.x } else { sw_pixel.x };
ne_pixel.x = if ne_pixel.x < pixel.x { pixel.x } else { ne_pixel.x };
sw_pixel.y = if pixel.y < sw_pixel.y { pixel.y } else { sw_pixel.y };
ne_pixel.y = if ne_pixel.y < pixel.y { pixel.y } else { ne_pixel.y };
}
let width = ne_pixel.x - sw_pixel.x;
let height = ne_pixel.y - sw_pixel.y;
let mut min_scale = f64::INFINITY;
if width > 0.0 || height > 0.0 {
let mut scale_x = f64::from(size.width) / width;
let mut scale_y = f64::from(size.height) / height;
scale_x -= (padding.left() + padding.right()) / width;
scale_y -= (padding.top() + padding.bottom()) / height;
min_scale = mln_math::min_f64(scale_x, scale_y);
}
let mut zoom = transform.get_zoom();
if min_scale > 0.0 {
zoom = mln_math::clamp_f64(
zoom + mln_math::log2(min_scale),
transform.get_state().get_min_zoom(),
transform.get_state().get_max_zoom(),
);
} else {
mln_util::logging::Log::error(
mln_util::logging::Event::General,
"Unable to calculate appropriate zoom level for bounds. Vertical or horizontal padding is greater than \
map's height or width.",
);
}
let center_pixel = ScreenCoordinate::new((ne_pixel.x + sw_pixel.x) / 2.0, (ne_pixel.y + sw_pixel.y) / 2.0);
CameraOptions::default()
.with_center(Some(transform.screen_coordinate_to_lat_lng(¢er_pixel, WrapMode::Wrapped)))
.with_padding(Some(*padding))
.with_zoom(Some(zoom))
}