mirror of
https://github.com/maplibre/maplibre-rs.git
synced 2025-12-08 19:05:57 +00:00
Add window module and refactor more
This commit is contained in:
parent
c040d5bbaa
commit
3c7dddf336
@ -1,4 +1,5 @@
|
||||
use mapr::{FromWindow, MapBuilder, ScheduleMethod, TokioScheduleMethod};
|
||||
use mapr::window::FromWindow;
|
||||
use mapr::{MapBuilder, ScheduleMethod, TokioScheduleMethod};
|
||||
|
||||
#[cfg(feature = "enable-tracing")]
|
||||
fn enable_tracing() {
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
//! Errors which can happen in various parts of the library.
|
||||
|
||||
use lyon::tessellation::TessellationError;
|
||||
use std::sync::mpsc::SendError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Schedule,
|
||||
Network(String),
|
||||
File(String),
|
||||
Tesselation(TessellationError),
|
||||
}
|
||||
|
||||
@ -15,3 +15,9 @@ impl From<TessellationError> for Error {
|
||||
Error::Tesselation(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<SendError<T>> for Error {
|
||||
fn from(_e: SendError<T>) -> Self {
|
||||
Error::Schedule
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use crate::coords::{WorldCoords, Zoom};
|
||||
use crate::error::Error;
|
||||
use crate::io::geometry_index::{GeometryIndex, IndexProcessor, IndexedGeometry, TileIndex};
|
||||
use crate::io::tile_request_state::TileRequestState;
|
||||
use crate::io::{
|
||||
@ -6,7 +7,7 @@ use crate::io::{
|
||||
TileTessellateMessage,
|
||||
};
|
||||
use crate::tessellation::Tessellated;
|
||||
use std::sync::mpsc::{SendError};
|
||||
use std::sync::mpsc::SendError;
|
||||
use std::sync::{mpsc, Arc, Mutex};
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -25,11 +26,7 @@ impl SharedThreadState {
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub fn process_tile(
|
||||
&self,
|
||||
request_id: TileRequestID,
|
||||
data: Box<[u8]>,
|
||||
) -> Result<(), SendError<TessellateMessage>> {
|
||||
pub fn process_tile(&self, request_id: TileRequestID, data: Box<[u8]>) -> Result<(), Error> {
|
||||
if let Some(tile_request) = self.get_tile_request(request_id) {
|
||||
let tile_result = TileFetchResult::Tile {
|
||||
coords: tile_request.coords,
|
||||
@ -43,10 +40,7 @@ impl SharedThreadState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn tile_unavailable(
|
||||
&self,
|
||||
request_id: TileRequestID,
|
||||
) -> Result<(), SendError<TessellateMessage>> {
|
||||
pub fn tile_unavailable(&self, request_id: TileRequestID) -> Result<(), Error> {
|
||||
if let Some(tile_request) = self.get_tile_request(request_id) {
|
||||
let tile_result = TileFetchResult::Unavailable {
|
||||
coords: tile_request.coords,
|
||||
@ -112,7 +106,7 @@ impl SharedThreadState {
|
||||
tile_result: &TileFetchResult,
|
||||
tile_request: &TileRequest,
|
||||
request_id: TileRequestID,
|
||||
) -> Result<(), SendError<TessellateMessage>> {
|
||||
) -> Result<(), Error> {
|
||||
match tile_result {
|
||||
TileFetchResult::Unavailable { coords } => {
|
||||
for to_load in &tile_request.layers {
|
||||
|
||||
@ -39,7 +39,9 @@ impl StaticTileFetcher {
|
||||
|
||||
let tile = TILES
|
||||
.get_file(format!("{}/{}/{}.{}", coords.z, coords.x, coords.y, "pbf"))
|
||||
.ok_or_else(|| Error::File("Failed to load tile from within the binary".to_string()))?;
|
||||
.ok_or_else(|| {
|
||||
Error::Network("Failed to load tile from within the binary".to_string())
|
||||
})?;
|
||||
Ok(Vec::from(tile.contents())) // TODO: Unnecessary copy
|
||||
}
|
||||
}
|
||||
|
||||
18
src/lib.rs
18
src/lib.rs
@ -14,29 +14,15 @@ pub(crate) mod winit;
|
||||
|
||||
// Used for benchmarking
|
||||
pub mod benchmarking;
|
||||
pub mod window;
|
||||
|
||||
use crate::map_state::{MapState, Runnable};
|
||||
use crate::render::render_state::RenderState;
|
||||
use crate::window::{WindowFactory, WindowSize};
|
||||
pub use io::scheduler::ScheduleMethod;
|
||||
pub use platform::schedule_method::*;
|
||||
use style_spec::Style;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct WindowSize {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
pub type WindowFactory<W, E> = dyn FnOnce() -> (W, WindowSize, E);
|
||||
|
||||
pub trait FromWindow {
|
||||
fn from_window(title: &'static str) -> Self;
|
||||
}
|
||||
|
||||
pub trait FromCanvas {
|
||||
fn from_canvas(dom_id: &'static str) -> Self;
|
||||
}
|
||||
|
||||
pub struct Map<W, E> {
|
||||
map_state: MapState<W>,
|
||||
event_loop: E,
|
||||
|
||||
@ -10,6 +10,7 @@ use crate::WindowSize;
|
||||
use std::collections::HashSet;
|
||||
use std::sync::{mpsc, Arc, Mutex};
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::io::geometry_index::GeometryIndex;
|
||||
use crate::io::shared_thread_state::SharedThreadState;
|
||||
use crate::io::source_client::{HttpSourceClient, SourceClient};
|
||||
@ -55,13 +56,13 @@ impl<W> MapState<W> {
|
||||
(TILE_SIZE / 2.0, TILE_SIZE / 2.0, 150.0),
|
||||
cgmath::Deg(-90.0),
|
||||
cgmath::Deg(0.0),
|
||||
window_size.width,
|
||||
window_size.height,
|
||||
window_size.width(),
|
||||
window_size.height(),
|
||||
);
|
||||
|
||||
let perspective = camera::Perspective::new(
|
||||
window_size.width,
|
||||
window_size.height,
|
||||
window_size.width(),
|
||||
window_size.height(),
|
||||
cgmath::Deg(110.0),
|
||||
100.0,
|
||||
2000.0,
|
||||
@ -186,7 +187,7 @@ impl<W> MapState<W> {
|
||||
&mut self,
|
||||
coords: &WorldTileCoords,
|
||||
layers: &HashSet<String>,
|
||||
) -> Result<bool, mpsc::SendError<TileRequest>> {
|
||||
) -> Result<bool, Error> {
|
||||
if !self.tile_cache.is_layers_missing(coords, layers) {
|
||||
return Ok(false);
|
||||
}
|
||||
@ -242,10 +243,6 @@ impl<W> MapState<W> {
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, width: u32, height: u32) {
|
||||
if width <= 0 || height <= 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
self.perspective.resize(width, height);
|
||||
self.camera.resize(width, height);
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use crate::io::scheduler::ScheduleMethod;
|
||||
use crate::platform::schedule_method::TokioScheduleMethod;
|
||||
use crate::FromWindow;
|
||||
use crate::window::FromWindow;
|
||||
use crate::MapBuilder;
|
||||
pub use std::time::Instant;
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use crate::io::scheduler::ScheduleMethod;
|
||||
use crate::platform::schedule_method::TokioScheduleMethod;
|
||||
use crate::FromWindow;
|
||||
use crate::window::FromWindow;
|
||||
use crate::MapBuilder;
|
||||
pub use std::time::Instant;
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ use web_sys::Worker;
|
||||
use crate::io::scheduler::ScheduleMethod;
|
||||
use crate::io::scheduler::Scheduler;
|
||||
use crate::io::scheduler::ThreadLocalState;
|
||||
use crate::FromCanvas;
|
||||
use crate::window::FromCanvas;
|
||||
use crate::MapBuilder;
|
||||
|
||||
pub mod http_client;
|
||||
|
||||
@ -78,8 +78,8 @@ impl RenderState {
|
||||
let surface_config = wgpu::SurfaceConfiguration {
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||
format: crate::platform::COLOR_TEXTURE_FORMAT,
|
||||
width: window_size.width,
|
||||
height: window_size.height,
|
||||
width: window_size.width(),
|
||||
height: window_size.height(),
|
||||
// present_mode: wgpu::PresentMode::Mailbox,
|
||||
present_mode: wgpu::PresentMode::Fifo, // VSync
|
||||
};
|
||||
|
||||
32
src/window.rs
Normal file
32
src/window.rs
Normal file
@ -0,0 +1,32 @@
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct WindowSize {
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
impl WindowSize {
|
||||
pub fn new(width: u32, height: u32) -> Option<Self> {
|
||||
if width <= 0 || height <= 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Self { width, height })
|
||||
}
|
||||
|
||||
pub fn width(&self) -> u32 {
|
||||
self.width
|
||||
}
|
||||
pub fn height(&self) -> u32 {
|
||||
self.height
|
||||
}
|
||||
}
|
||||
|
||||
pub type WindowFactory<W, E> = dyn FnOnce() -> (W, WindowSize, E);
|
||||
|
||||
pub trait FromWindow {
|
||||
fn from_window(title: &'static str) -> Self;
|
||||
}
|
||||
|
||||
pub trait FromCanvas {
|
||||
fn from_canvas(dom_id: &'static str) -> Self;
|
||||
}
|
||||
12
src/winit.rs
12
src/winit.rs
@ -12,7 +12,8 @@ use crate::input::{InputController, UpdateState};
|
||||
use crate::map_state::{MapState, Runnable};
|
||||
use crate::platform::Instant;
|
||||
|
||||
use crate::{FromWindow, MapBuilder, WindowSize};
|
||||
use crate::window::FromWindow;
|
||||
use crate::{MapBuilder, WindowSize};
|
||||
|
||||
impl Runnable<winit::event_loop::EventLoop<()>> for MapState<winit::window::Window> {
|
||||
fn run(mut self, event_loop: winit::event_loop::EventLoop<()>, max_frames: Option<u64>) {
|
||||
@ -123,10 +124,7 @@ impl FromWindow for MapBuilder<winit::window::Window, winit::event_loop::EventLo
|
||||
let size = window.inner_size();
|
||||
(
|
||||
window,
|
||||
WindowSize {
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
},
|
||||
WindowSize::new(size.width, size.height).unwrap(),
|
||||
event_loop,
|
||||
)
|
||||
}))
|
||||
@ -158,7 +156,9 @@ pub fn get_canvas(element_id: &'static str) -> web_sys::HtmlCanvasElement {
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl crate::FromCanvas for MapBuilder<winit::window::Window, winit::event_loop::EventLoop<()>> {
|
||||
impl crate::window::FromCanvas
|
||||
for MapBuilder<winit::window::Window, winit::event_loop::EventLoop<()>>
|
||||
{
|
||||
fn from_canvas(dom_id: &'static str) -> Self {
|
||||
let event_loop = EventLoop::new();
|
||||
Self::new(Box::new(move || {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user