Skip to content

Commit

Permalink
Move winit code to maplibre-winit (#86)
Browse files Browse the repository at this point in the history
* Extract winit code to maplibre-winit

* Run clippy

* Fix android and apple

* Remove import

* Fix android compilation

* Fix android

* Update dependencies

* Fix android

* Fix android

* Add map window config

* Fix feature flag

* Add title to config

* Remove unused export

* Fix for android
  • Loading branch information
maxammann committed May 10, 2022
1 parent 30cdb3d commit feff320
Show file tree
Hide file tree
Showing 38 changed files with 563 additions and 447 deletions.
1 change: 1 addition & 0 deletions .idea/maplibre-rs.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -4,6 +4,7 @@ resolver = "2"

members = [
"maplibre",
"maplibre-winit",
"maplibre-build-tools",
"maplibre-demo",

Expand Down
1 change: 1 addition & 0 deletions android/Cargo.toml
Expand Up @@ -8,6 +8,7 @@ publish = false

[dependencies]
maplibre = { path = "../maplibre" }
maplibre-winit = { path = "../maplibre-winit", version = "0.0.1" }
env_logger = "0.9"
log = "0.4.16"
ndk-glue = "0.5.0" # version is required by winit
Expand Down
18 changes: 12 additions & 6 deletions android/src/lib.rs
Expand Up @@ -2,9 +2,10 @@ use jni::objects::JClass;
use jni::JNIEnv;
use log::Level;
use maplibre::platform::http_client::ReqwestHttpClient;
use maplibre::platform::run_multithreaded;
use maplibre::platform::schedule_method::TokioScheduleMethod;
use maplibre::window::FromWindow;
use maplibre::MapBuilder;
use maplibre_winit::winit::{WinitEventLoop, WinitMapWindow, WinitMapWindowConfig, WinitWindow};
use std::ffi::CString;

#[cfg(not(target_os = "android"))]
Expand All @@ -14,11 +15,16 @@ compile_error!("android works only on android.");
pub fn android_main() {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));

MapBuilder::from_window("A fantastic window!")
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.run_sync();
run_multithreaded(async {
MapBuilder::new()
.with_map_window_config(WinitMapWindowConfig::new("maplibre android".to_string()))
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.initialize()
.await
.run()
})
}

#[no_mangle]
Expand Down
2 changes: 2 additions & 0 deletions apple/Cargo.toml
Expand Up @@ -8,6 +8,8 @@ publish = false

[dependencies]
maplibre = { path = "../maplibre" }
maplibre-winit = { path = "../maplibre-winit", version = "0.0.1" }

env_logger = "0.9"

[lib]
Expand Down
18 changes: 12 additions & 6 deletions apple/src/lib.rs
@@ -1,7 +1,8 @@
use maplibre::platform::http_client::ReqwestHttpClient;
use maplibre::platform::run_multithreaded;
use maplibre::platform::schedule_method::TokioScheduleMethod;
use maplibre::window::FromWindow;
use maplibre::MapBuilder;
use maplibre_winit::winit::{WinitEventLoop, WinitMapWindow, WinitMapWindowConfig, WinitWindow};

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
compile_error!("apple works only on macOS and iOS.");
Expand All @@ -10,9 +11,14 @@ compile_error!("apple works only on macOS and iOS.");
pub fn maplibre_apple_main() {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));

MapBuilder::from_window("A fantastic window!")
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.run_sync();
run_multithreaded(async {
MapBuilder::new()
.with_map_window_config(WinitMapWindowConfig::new("maplibre apple".to_string()))
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.initialize()
.await
.run()
})
}
1 change: 1 addition & 0 deletions maplibre-demo/Cargo.toml
Expand Up @@ -15,6 +15,7 @@ enable-tracing = ["maplibre/enable-tracing", "tracing-subscriber", "tracing-trac
[dependencies]
env_logger = "0.9"
maplibre = { path = "../maplibre", version = "0.0.2" }
maplibre-winit = { path = "../maplibre-winit", version = "0.0.1" }

tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", optional = true }
Expand Down
32 changes: 13 additions & 19 deletions maplibre-demo/src/main.rs
@@ -1,7 +1,8 @@
use maplibre::platform::http_client::ReqwestHttpClient;
use maplibre::platform::run_multithreaded;
use maplibre::platform::schedule_method::TokioScheduleMethod;
use maplibre::window::FromWindow;
use maplibre::MapBuilder;
use maplibre_winit::winit::{WinitEventLoop, WinitMapWindow, WinitMapWindowConfig, WinitWindow};

#[cfg(feature = "enable-tracing")]
fn enable_tracing() {
Expand All @@ -14,19 +15,16 @@ fn enable_tracing() {
}

fn run_in_window() {
MapBuilder::from_window("A fantastic window!")
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.run_sync();
}

fn run_headless() {
MapBuilder::from_window("A fantastic window!")
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.run_sync();
run_multithreaded(async {
MapBuilder::new()
.with_map_window_config(WinitMapWindowConfig::new("maplibre".to_string()))
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.initialize()
.await
.run()
})
}

fn main() {
Expand All @@ -35,9 +33,5 @@ fn main() {
#[cfg(feature = "enable-tracing")]
enable_tracing();

MapBuilder::from_window("A fantastic window!")
.with_http_client(ReqwestHttpClient::new(None))
.with_schedule_method(TokioScheduleMethod::new())
.build()
.run_sync();
run_in_window()
}
25 changes: 25 additions & 0 deletions maplibre-winit/Cargo.toml
@@ -0,0 +1,25 @@
[package]
name = "maplibre-winit"
version = "0.0.1"
edition = "2021"

[target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "linux", target_os = "android"))'.dependencies]
tokio = { version = "1.17", features = ["rt"] }

[target.'cfg(target_os = "android")'.dependencies]
winit = { version = "0.26", default-features = false }

[target.'cfg(target_os = "linux")'.dependencies]
winit = { version = "0.26", default-features = false, features = ["x11", "wayland"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3", features = ["Window"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"

[dependencies]
maplibre = { path = "../maplibre", version = "0.0.2" }
winit = { version = "0.26", default-features = false }
cgmath = "0.18.0"
instant = { version = "0.1", features = ["wasm-bindgen"] } # FIXME: Untrusted dependency
log = "0.4"
7 changes: 2 additions & 5 deletions maplibre/src/input/mod.rs → maplibre-winit/src/input/mod.rs
Expand Up @@ -2,8 +2,8 @@

use std::time::Duration;

use crate::coords::Zoom;
use cgmath::Vector2;

use winit::event::{DeviceEvent, KeyboardInput, TouchPhase, WindowEvent};

use crate::input::pan_handler::PanHandler;
Expand All @@ -12,9 +12,7 @@ use crate::input::query_handler::QueryHandler;
use crate::input::shift_handler::ShiftHandler;
use crate::input::tilt_handler::TiltHandler;
use crate::input::zoom_handler::ZoomHandler;
use crate::map_state::{MapState, ViewState};
use crate::render::camera::Camera;
use crate::MapWindow;
use maplibre::map_state::ViewState;

mod pan_handler;
mod pinch_handler;
Expand Down Expand Up @@ -131,7 +129,6 @@ pub trait UpdateState {
}

impl UpdateState for InputController {
#[tracing::instrument(skip_all)]
fn update_state(&mut self, state: &mut ViewState, dt: Duration) {
self.pan_handler.update_state(state, dt);
self.pinch_handler.update_state(state, dt);
Expand Down
@@ -1,10 +1,10 @@
use super::UpdateState;

use crate::map_state::{MapState, ViewState};
use crate::render::camera::Camera;
use maplibre::map_state::ViewState;
use maplibre::render::camera::Camera;

use crate::MapWindow;
use cgmath::{EuclideanSpace, Point3, Vector2, Vector3, Zero};

use std::time::Duration;
use winit::event::{ElementState, MouseButton};

Expand Down
@@ -1,7 +1,6 @@
use super::UpdateState;

use crate::map_state::ViewState;
use crate::{MapState, MapWindow};
use maplibre::map_state::ViewState;
use std::time::Duration;

pub struct PinchHandler {}
Expand Down
@@ -1,8 +1,7 @@
use cgmath::Vector2;

use crate::input::UpdateState;
use crate::map_state::{MapState, ViewState};
use crate::MapWindow;
use maplibre::map_state::ViewState;
use std::time::Duration;
use winit::event::{ElementState, MouseButton};

Expand Down
@@ -1,8 +1,7 @@
use super::UpdateState;

use crate::map_state::ViewState;
use crate::{MapState, MapWindow};
use cgmath::{Vector3, Zero};
use maplibre::map_state::ViewState;
use std::time::Duration;

pub struct ShiftHandler {
Expand Down
@@ -1,11 +1,9 @@
use super::UpdateState;

use crate::map_state::{MapState, ViewState};
use maplibre::map_state::ViewState;

use crate::coords::Zoom;
use crate::render::camera::Camera;
use crate::MapWindow;
use cgmath::{Deg, Rad, Zero};

use std::time::Duration;

pub struct TiltHandler {
Expand Down
@@ -1,11 +1,10 @@
use super::UpdateState;

use crate::coords::Zoom;
use crate::map_state::{MapState, ViewState};
use maplibre::coords::Zoom;
use maplibre::map_state::ViewState;

use crate::render::camera::Camera;
use crate::MapWindow;
use cgmath::{Vector2, Vector3};

use std::time::Duration;

pub struct ZoomHandler {
Expand All @@ -15,7 +14,7 @@ pub struct ZoomHandler {
}

impl UpdateState for ZoomHandler {
fn update_state(&mut self, state: &mut ViewState, dt: Duration) {
fn update_state(&mut self, state: &mut ViewState, _dt: Duration) {
if let Some(zoom_delta) = self.zoom_delta {
if let Some(window_position) = self.window_position {
let current_zoom = state.zoom();
Expand Down
2 changes: 2 additions & 0 deletions maplibre-winit/src/lib.rs
@@ -0,0 +1,2 @@
pub mod input;
pub mod winit;

0 comments on commit feff320

Please sign in to comment.