mirror of
https://github.com/maplibre/maplibre-rs.git
synced 2025-12-08 19:05:57 +00:00
Rename runnable to eventloop and fix variable names
This commit is contained in:
parent
675e62bae1
commit
810954f49d
@ -1,3 +1,4 @@
|
||||
use maplibre::coords::WorldTileCoords;
|
||||
use maplibre::error::Error;
|
||||
use maplibre::io::scheduler::ScheduleMethod;
|
||||
use maplibre::io::source_client::{HttpClient, HttpSourceClient};
|
||||
@ -6,7 +7,7 @@ use maplibre::platform::http_client::ReqwestHttpClient;
|
||||
use maplibre::platform::run_multithreaded;
|
||||
use maplibre::platform::schedule_method::TokioScheduleMethod;
|
||||
use maplibre::render::settings::RendererSettings;
|
||||
use maplibre::window::{MapWindow, MapWindowConfig, Runnable, WindowSize};
|
||||
use maplibre::window::{EventLoop, MapWindow, MapWindowConfig, WindowSize};
|
||||
use maplibre::MapBuilder;
|
||||
use maplibre_winit::winit::{WinitEventLoop, WinitMapWindow, WinitMapWindowConfig, WinitWindow};
|
||||
use wgpu::TextureFormat;
|
||||
@ -38,26 +39,6 @@ impl MapWindow for HeadlessMapWindow {
|
||||
}
|
||||
}
|
||||
|
||||
impl<MWC, SM, HC> Runnable<MWC, SM, HC> for HeadlessMapWindow
|
||||
where
|
||||
MWC: MapWindowConfig<MapWindow = HeadlessMapWindow>,
|
||||
SM: ScheduleMethod,
|
||||
HC: HTTPClient,
|
||||
{
|
||||
fn run(mut self, mut map_state: MapSchedule<MWC, SM, HC>, max_frames: Option<u64>) {
|
||||
for i in 0..3 {
|
||||
match map_state.update_and_redraw() {
|
||||
Ok(_) => {}
|
||||
Err(Error::Render(e)) => {
|
||||
eprintln!("{}", e);
|
||||
if e.should_exit() {}
|
||||
}
|
||||
e => eprintln!("{:?}", e),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_in_window() {
|
||||
run_multithreaded(async {
|
||||
MapBuilder::new()
|
||||
@ -73,7 +54,7 @@ fn run_in_window() {
|
||||
|
||||
fn run_headless() {
|
||||
run_multithreaded(async {
|
||||
MapBuilder::new()
|
||||
let mut map = MapBuilder::new()
|
||||
.with_map_window_config(HeadlessMapWindowConfig)
|
||||
.with_http_client(ReqwestHttpClient::new(None))
|
||||
.with_schedule_method(TokioScheduleMethod::new())
|
||||
@ -83,8 +64,37 @@ fn run_headless() {
|
||||
})
|
||||
.build()
|
||||
.initialize_headless()
|
||||
.await
|
||||
.run()
|
||||
.await;
|
||||
|
||||
let http_source_client: HttpSourceClient<HC> =
|
||||
HttpSourceClient::new(ReqwestHttpClient::new(None));
|
||||
|
||||
let coords = WorldTileCoords::from((0, 0, 0));
|
||||
let request_id = 0;
|
||||
|
||||
let x = match http_source_client.fetch(&coords).await {
|
||||
Ok(data) => state.process_tile(0, data.into_boxed_slice()).unwrap(),
|
||||
Err(e) => {
|
||||
log::error!("{:?}", &e);
|
||||
|
||||
state.tile_unavailable(&coords, request_id).unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
match map.map_schedule_mut().map_context {
|
||||
EventuallyMapContext::Full(a) => a.tile_cache.put_tessellated_layer(),
|
||||
EventuallyMapContext::Premature(_) => {}
|
||||
EventuallyMapContext::_Uninitialized => {}
|
||||
}
|
||||
|
||||
match map.map_schedule_mut().update_and_redraw() {
|
||||
Ok(_) => {}
|
||||
Err(Error::Render(e)) => {
|
||||
eprintln!("{}", e);
|
||||
if e.should_exit() {}
|
||||
}
|
||||
e => eprintln!("{:?}", e),
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ use winit::event_loop::ControlFlow;
|
||||
|
||||
use crate::input::{InputController, UpdateState};
|
||||
use maplibre::map_schedule::MapSchedule;
|
||||
use maplibre::window::{HeadedMapWindow, MapWindow, MapWindowConfig, Runnable};
|
||||
use maplibre::window::{EventLoop, HeadedMapWindow, MapWindow, MapWindowConfig};
|
||||
use winit::event::Event;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
@ -65,13 +65,13 @@ impl WinitMapWindow {
|
||||
///* Input (Mouse/Keyboard)
|
||||
///* Platform Events like suspend/resume
|
||||
///* Render a new frame
|
||||
impl<MWC, SM, HC> Runnable<MWC, SM, HC> for WinitMapWindow
|
||||
impl<MWC, SM, HC> EventLoop<MWC, SM, HC> for WinitMapWindow
|
||||
where
|
||||
MWC: MapWindowConfig<MapWindow = WinitMapWindow>,
|
||||
SM: ScheduleMethod,
|
||||
HC: HttpClient,
|
||||
{
|
||||
fn run(mut self, mut map_state: MapSchedule<MWC, SM, HC>, max_frames: Option<u64>) {
|
||||
fn run(mut self, mut map_schedule: MapSchedule<MWC, SM, HC>, max_frames: Option<u64>) {
|
||||
let mut last_render_time = Instant::now();
|
||||
let mut current_frame: u64 = 0;
|
||||
|
||||
@ -81,13 +81,13 @@ where
|
||||
.unwrap()
|
||||
.run(move |event, _, control_flow| {
|
||||
#[cfg(target_os = "android")]
|
||||
if !map_state.is_initialized() && event == Event::Resumed {
|
||||
if !map_schedule.is_initialized() && event == Event::Resumed {
|
||||
use tokio::runtime::Handle;
|
||||
use tokio::task;
|
||||
|
||||
let state = task::block_in_place(|| {
|
||||
Handle::current().block_on(async {
|
||||
map_state.late_init().await;
|
||||
map_schedule.late_init().await;
|
||||
})
|
||||
});
|
||||
return;
|
||||
@ -118,10 +118,10 @@ where
|
||||
..
|
||||
} => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::Resized(physical_size) => {
|
||||
map_state.resize(physical_size.width, physical_size.height);
|
||||
map_schedule.resize(physical_size.width, physical_size.height);
|
||||
}
|
||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||
map_state.resize(new_inner_size.width, new_inner_size.height);
|
||||
map_schedule.resize(new_inner_size.width, new_inner_size.height);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@ -133,10 +133,10 @@ where
|
||||
last_render_time = now;
|
||||
|
||||
{
|
||||
input_controller.update_state(map_state.view_state_mut(), dt);
|
||||
input_controller.update_state(map_schedule.view_state_mut(), dt);
|
||||
}
|
||||
|
||||
match map_state.update_and_redraw() {
|
||||
match map_schedule.update_and_redraw() {
|
||||
Ok(_) => {}
|
||||
Err(Error::Render(e)) => {
|
||||
eprintln!("{}", e);
|
||||
@ -157,10 +157,10 @@ where
|
||||
}
|
||||
}
|
||||
Event::Suspended => {
|
||||
map_state.suspend();
|
||||
map_schedule.suspend();
|
||||
}
|
||||
Event::Resumed => {
|
||||
map_state.resume(&self);
|
||||
map_schedule.resume(&self);
|
||||
}
|
||||
Event::MainEventsCleared => {
|
||||
// RedrawRequested will only trigger once, unless we manually
|
||||
|
||||
@ -28,7 +28,6 @@ impl MapWindow for WinitMapWindow {
|
||||
}
|
||||
}
|
||||
impl HeadedMapWindow for WinitMapWindow {
|
||||
type EventLoop = WinitEventLoop;
|
||||
type RawWindow = WinitWindow;
|
||||
|
||||
fn inner(&self) -> &Self::RawWindow {
|
||||
|
||||
@ -22,7 +22,7 @@ use crate::map_schedule::MapSchedule;
|
||||
use crate::render::settings::{RendererSettings, WgpuSettings};
|
||||
use crate::render::{RenderState, Renderer};
|
||||
use crate::style::Style;
|
||||
use crate::window::{HeadedMapWindow, MapWindow, MapWindowConfig, Runnable, WindowSize};
|
||||
use crate::window::{EventLoop, HeadedMapWindow, MapWindow, MapWindowConfig, WindowSize};
|
||||
|
||||
pub mod context;
|
||||
pub mod coords;
|
||||
@ -51,30 +51,35 @@ pub struct Map<MWC, SM, HC>
|
||||
where
|
||||
MWC: MapWindowConfig,
|
||||
SM: ScheduleMethod,
|
||||
HC: HTTPClient,
|
||||
HC: HttpClient,
|
||||
{
|
||||
map_state: MapSchedule<MWC, SM, HC>,
|
||||
map_schedule: MapSchedule<MWC, SM, HC>,
|
||||
window: MWC::MapWindow,
|
||||
}
|
||||
|
||||
impl<MWC, SM, HC> Map<MWC, SM, HC>
|
||||
where
|
||||
MWC: MapWindowConfig,
|
||||
MWC::MapWindow: Runnable<MWC, SM, HC>,
|
||||
SM: ScheduleMethod,
|
||||
HC: HTTPClient,
|
||||
HC: HttpClient,
|
||||
{
|
||||
/// Starts the [`crate::map_state::MapState`] Runnable with the configured event loop.
|
||||
pub fn run(self) {
|
||||
/// 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_state::MapState`] Runnable with the configured event loop.
|
||||
/// 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) {
|
||||
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));
|
||||
}
|
||||
|
||||
@ -83,15 +88,23 @@ where
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `max_frames` - Optional maximum number of frames per second.
|
||||
pub fn run_with_optionally_max_frames(self, max_frames: Option<u64>) {
|
||||
self.window.run(self.map_state, max_frames);
|
||||
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) -> &MapSchedule<MWC, SM, HC> {
|
||||
&self.map_schedule
|
||||
}
|
||||
|
||||
pub fn map_schedule_mut(&mut self) -> &mut MapSchedule<MWC, SM, HC> {
|
||||
&mut self.map_schedule
|
||||
}
|
||||
}
|
||||
|
||||
/// Stores the map configuration before the map's state has been fully initialized.
|
||||
///
|
||||
/// FIXME: We could maybe remove this class, and store the render_state in an Optional in [`crate::map_state::MapState`].
|
||||
/// FIXME: I think we can find a workaround so that this class doesn't exist.
|
||||
pub struct UninitializedMap<MWC, SM, HC>
|
||||
where
|
||||
MWC: MapWindowConfig,
|
||||
@ -134,7 +147,7 @@ where
|
||||
.await
|
||||
.ok();
|
||||
Map {
|
||||
map_state: MapSchedule::new(
|
||||
map_schedule: MapSchedule::new(
|
||||
self.map_window_config,
|
||||
window_size,
|
||||
renderer,
|
||||
@ -160,7 +173,7 @@ where
|
||||
.await
|
||||
.ok();
|
||||
Map {
|
||||
map_state: MapSchedule::new(
|
||||
map_schedule: MapSchedule::new(
|
||||
self.map_window_config,
|
||||
window_size,
|
||||
renderer,
|
||||
@ -193,7 +206,7 @@ impl<MWC, SM, HC> MapBuilder<MWC, SM, HC>
|
||||
where
|
||||
MWC: MapWindowConfig,
|
||||
SM: ScheduleMethod,
|
||||
HC: HTTPClient,
|
||||
HC: HttpClient,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
||||
@ -9,7 +9,6 @@ pub trait MapWindow {
|
||||
}
|
||||
|
||||
pub trait HeadedMapWindow: MapWindow {
|
||||
type EventLoop;
|
||||
type RawWindow: HasRawWindowHandle;
|
||||
|
||||
fn inner(&self) -> &Self::RawWindow;
|
||||
@ -21,13 +20,13 @@ pub trait MapWindowConfig: 'static {
|
||||
fn create(&self) -> Self::MapWindow;
|
||||
}
|
||||
|
||||
pub trait Runnable<MWC, SM, HC>
|
||||
pub trait EventLoop<MWC, SM, HC>
|
||||
where
|
||||
MWC: MapWindowConfig,
|
||||
SM: ScheduleMethod,
|
||||
HC: HttpClient,
|
||||
{
|
||||
fn run(self, map_state: MapSchedule<MWC, SM, HC>, max_frames: Option<u64>);
|
||||
fn run(self, map_schedule: MapSchedule<MWC, SM, HC>, max_frames: Option<u64>);
|
||||
}
|
||||
|
||||
/// Window size with a width and an height in pixels.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user