Skip to content

Commit

Permalink
add custom provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jun 15, 2024
1 parent 2575d84 commit 00daf57
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
12 changes: 11 additions & 1 deletion demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use crate::plugins::ImagesPluginData;
use egui::Context;
use walkers::{HttpOptions, Map, MapMemory, Tiles, TilesManager};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Provider {
OpenStreetMap,
Geoportal,
MapboxStreets,
MapboxSatellite,
LocalTiles,
Custom(String),
}

fn http_options() -> HttpOptions {
Expand Down Expand Up @@ -114,6 +115,15 @@ impl MyApp {
click_watcher: Default::default(),
}
}
pub fn with_provider(
mut self,
name: Provider,
new_provider: Box<dyn TilesManager + Send>,
) -> Self {
self.providers.insert(name, new_provider);

self
}
}

impl eframe::App for MyApp {
Expand Down
6 changes: 5 additions & 1 deletion demo/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ pub fn controls(
.selected_text(format!("{:?}", selected_provider))
.show_ui(ui, |ui| {
for p in possible_providers {
ui.selectable_value(selected_provider, *p, format!("{:?}", p));
let text = match p {
Provider::Custom(name) => name.clone(),
_ => format!("{:?}", p),
};
ui.selectable_value(selected_provider, p.clone(), text);
}
});
});
Expand Down
1 change: 1 addition & 0 deletions demo_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ demo = { path = "../demo" }
eframe.workspace = true
egui.workspace = true
log.workspace = true
walkers = { path = "../walkers" }
wasm-bindgen-futures = "0.4"
3 changes: 3 additions & 0 deletions demo_web/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]

watch = ["src", "../demo/", "../walkers/"]
40 changes: 39 additions & 1 deletion demo_web/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
use demo::Provider;
use walkers::sources::{Attribution, TileSource};
use walkers::{TileId, Tiles};

pub struct CustomSource {}

impl CustomSource {
pub fn new() -> Self {
Self {}
}
pub fn get_provider_name() -> Provider {
Provider::Custom("Humanitarian OpenStreepMap".to_owned())
}
}

impl TileSource for CustomSource {
fn tile_url(&self, tile_id: TileId) -> String {
format!(
"https://tile-b.openstreetmap.fr/hot/{}/{}/{}.png",
tile_id.zoom, tile_id.x, tile_id.y
)
}

fn attribution(&self) -> Attribution {
Attribution {
text: "Humanitarian OpenStreetMap contributors",
url: "https://www.openstreetmap.org/copyright",
logo_light: None,
logo_dark: None,
}
}
}

#[cfg(target_arch = "wasm32")]
fn main() {
// Redirect `log` message to `console.log` and friends:
Expand All @@ -10,7 +43,12 @@ fn main() {
.start(
"the_canvas_id", // hardcode it
web_options,
Box::new(|cc| Box::new(demo::MyApp::new(cc.egui_ctx.clone()))),
Box::new(|cc| {
Box::new(demo::MyApp::new(cc.egui_ctx.clone()).with_provider(
CustomSource::get_provider_name(),
Box::new(Tiles::new(CustomSource::new(), cc.egui_ctx.to_owned())),
))
}),
)
.await
.expect("failed to start eframe");
Expand Down
8 changes: 6 additions & 2 deletions walkers/src/download.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{path::PathBuf, pin::Pin};
use std::{env, path::PathBuf, pin::Pin};

use egui::Context;
use futures::{
Expand Down Expand Up @@ -33,7 +33,11 @@ impl Default for HttpOptions {
fn default() -> Self {
Self {
cache: None,
user_agent: HeaderValue::from_static("Walkers"),
user_agent: HeaderValue::from_static(concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
)),
}
}
}
Expand Down

0 comments on commit 00daf57

Please sign in to comment.