Skip to content

Commit

Permalink
Merge branch 'podusowski:main' into add-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans authored Jul 2, 2024
2 parents 4c7eaab + 708d4c4 commit 9f1977f
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 185 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

All notable changes to this project will be documented in this file.

## Unreleased
## 0.23.0

* New functions in `MapMemory` for getting and setting the zoom level: `zoom` and `set_zoom`.
* In-memory cache is now limited to 256 tiles. Previously it grew indefinitely.
* `TilesManager` trait is now called `Tiles` and `Tiles` struct is now called `HttpTiles`.

## 0.22.0

Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ members = [
resolver = "2"

[workspace.package]
version = "0.22.0"
version = "0.23.0"

[workspace.dependencies]
image = "0.25"
image = { version = "0.25", default-features = false }
log = "0.4"
egui = "0.27"
egui_extras = { version = "0.27", features = ["svg"] }
Expand Down
16 changes: 8 additions & 8 deletions demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::HashMap;

use crate::plugins::ImagesPluginData;
use egui::Context;
use walkers::{HttpOptions, Map, MapMemory, Tiles, TilesManager};
use walkers::{HttpOptions, HttpTiles, Map, MapMemory, Tiles};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Provider {
Expand All @@ -31,12 +31,12 @@ fn http_options() -> HttpOptions {
}
}

fn providers(egui_ctx: Context) -> HashMap<Provider, Box<dyn TilesManager + Send>> {
let mut providers: HashMap<Provider, Box<dyn TilesManager + Send>> = HashMap::default();
fn providers(egui_ctx: Context) -> HashMap<Provider, Box<dyn Tiles + Send>> {
let mut providers: HashMap<Provider, Box<dyn Tiles + Send>> = HashMap::default();

providers.insert(
Provider::OpenStreetMap,
Box::new(Tiles::with_options(
Box::new(HttpTiles::with_options(
walkers::sources::OpenStreetMap,
http_options(),
egui_ctx.to_owned(),
Expand All @@ -45,7 +45,7 @@ fn providers(egui_ctx: Context) -> HashMap<Provider, Box<dyn TilesManager + Send

providers.insert(
Provider::Geoportal,
Box::new(Tiles::with_options(
Box::new(HttpTiles::with_options(
walkers::sources::Geoportal,
http_options(),
egui_ctx.to_owned(),
Expand All @@ -65,7 +65,7 @@ fn providers(egui_ctx: Context) -> HashMap<Provider, Box<dyn TilesManager + Send
if let Some(token) = mapbox_access_token {
providers.insert(
Provider::MapboxStreets,
Box::new(Tiles::with_options(
Box::new(HttpTiles::with_options(
walkers::sources::Mapbox {
style: walkers::sources::MapboxStyle::Streets,
access_token: token.to_string(),
Expand All @@ -77,7 +77,7 @@ fn providers(egui_ctx: Context) -> HashMap<Provider, Box<dyn TilesManager + Send
);
providers.insert(
Provider::MapboxSatellite,
Box::new(Tiles::with_options(
Box::new(HttpTiles::with_options(
walkers::sources::Mapbox {
style: walkers::sources::MapboxStyle::Satellite,
access_token: token.to_string(),
Expand All @@ -93,7 +93,7 @@ fn providers(egui_ctx: Context) -> HashMap<Provider, Box<dyn TilesManager + Send
}

pub struct MyApp {
providers: HashMap<Provider, Box<dyn TilesManager + Send>>,
providers: HashMap<Provider, Box<dyn Tiles + Send>>,
selected_provider: Provider,
map_memory: MapMemory,
images_plugin_data: ImagesPluginData,
Expand Down
4 changes: 2 additions & 2 deletions demo/src/local_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use egui::Context;
use walkers::sources::Attribution;
use walkers::Texture;
use walkers::TileId;
use walkers::TilesManager;
use walkers::Tiles;

pub struct LocalTiles {
egui_ctx: Context,
Expand All @@ -15,7 +15,7 @@ impl LocalTiles {
}
}

impl TilesManager for LocalTiles {
impl Tiles for LocalTiles {
fn at(&mut self, _tile_id: TileId) -> Option<Texture> {
let image = ColorImage::example();

Expand Down
2 changes: 1 addition & 1 deletion demo_android/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish = false

[dependencies]
log = "0.4"
android_logger = "0.13"
android_logger = "0.14"
jni = "*"
eframe = { workspace = true, features = ["wgpu", "android-game-activity"] }
egui-winit.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion hypermocker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl AnticipatedRequest {
self.payload_tx.send(response).unwrap();
}

/// Respond to this request with given status, and empty body.
/// Similar to [AnticipatedRequest], but with status and empty body.
pub async fn respond_with_status(self, status: hyper::StatusCode) {
log::info!(
"Saving response (with status: {}) for '{}'.",
Expand Down
2 changes: 1 addition & 1 deletion walkers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ log.workspace = true
egui.workspace = true
egui_extras.workspace = true
thiserror = "1"
image = { version = "0.25", features = ["jpeg", "png"] }
image = { version = "0.25", default-features = false, features = ["jpeg", "png"] }
geo-types = { version = "0.7" }
reqwest = { version = "0.11", default-features = false, features = [
"rustls-tls",
Expand Down
7 changes: 4 additions & 3 deletions walkers/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Walkers, a map widget for Rust

[![crates.io](https://img.shields.io/crates/v/walkers.svg)](https://crates.io/crates/walkers)
[![docs.rs](https://img.shields.io/docsrs/walkers/latest)](https://docs.rs/walkers/latest/)

Walkers is a slippy maps widget for [egui](https://github.com/emilk/egui),
similar to very popular [Leaflet](https://leafletjs.com/), but written in Rust.
Expand All @@ -22,19 +23,19 @@ such as OpenStreetMap and stores them in a cache, `MapMemory` keeps track of
the widget's state and `Map` is the widget itself.

```rust
use walkers::{Tiles, Map, MapMemory, Position, sources::OpenStreetMap};
use walkers::{HttpTiles, Map, MapMemory, Position, sources::OpenStreetMap};
use egui::{Context, CentralPanel};
use eframe::{App, Frame};

struct MyApp {
tiles: Tiles,
tiles: HttpTiles,
map_memory: MapMemory,
}

impl MyApp {
fn new(egui_ctx: Context) -> Self {
Self {
tiles: Tiles::new(OpenStreetMap, egui_ctx),
tiles: HttpTiles::new(OpenStreetMap, egui_ctx),
map_memory: MapMemory::default(),
}
}
Expand Down
6 changes: 5 additions & 1 deletion walkers/src/download.rs
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion walkers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ mod zoom;
pub use download::{HeaderValue, HttpOptions};
pub use map::{Map, MapMemory, Plugin, Projector};
pub use mercator::{screen_to_position, Position, TileId};
pub use tiles::{Texture, Tiles, TilesManager};
pub use tiles::{HttpTiles, Texture, Tiles};
pub use zoom::InvalidZoom;
26 changes: 19 additions & 7 deletions walkers/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
mercator::{screen_to_position, Pixels, PixelsExt, TileId},
tiles,
zoom::{InvalidZoom, Zoom},
Position, TilesManager,
Position, Tiles,
};

/// Plugins allow drawing custom shapes on the map. After implementing this trait for your type,
Expand All @@ -18,14 +18,14 @@ pub trait Plugin {
}

/// The actual map widget. Instances are to be created on each frame, as all necessary state is
/// stored in [`TilesManager`] and [`MapMemory`].
/// stored in [`Tiles`] and [`MapMemory`].
///
/// # Examples
///
/// ```
/// # use walkers::{Map, Tiles, MapMemory, Position};
///
/// fn update(ui: &mut egui::Ui, tiles: &mut Tiles, map_memory: &mut MapMemory) {
/// fn update(ui: &mut egui::Ui, tiles: &mut dyn Tiles, map_memory: &mut MapMemory) {
/// ui.add(Map::new(
/// Some(tiles), // `None`, if you don't want to show any tiles.
/// map_memory,
Expand All @@ -34,7 +34,7 @@ pub trait Plugin {
/// }
/// ```
pub struct Map<'a, 'b, 'c> {
tiles: Option<&'b mut dyn TilesManager>,
tiles: Option<&'b mut dyn Tiles>,
memory: &'a mut MapMemory,
my_position: Position,
plugins: Vec<Box<dyn Plugin + 'c>>,
Expand All @@ -45,7 +45,7 @@ pub struct Map<'a, 'b, 'c> {

impl<'a, 'b, 'c> Map<'a, 'b, 'c> {
pub fn new(
tiles: Option<&'b mut dyn TilesManager>,
tiles: Option<&'b mut dyn Tiles>,
memory: &'a mut MapMemory,
my_position: Position,
) -> Self {
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Projector {
let projected_position = position.project(self.memory.zoom.into());

// We need the precision of f64 here,
// since some "gaps" between tiles are noticable on large zoom levels (e.g. 16+)
// since some "gaps" between tiles are noticeable on large zoom levels (e.g. 16+)
let zoom: f64 = self.memory.zoom.into();

// We also need to know where the map center is.
Expand Down Expand Up @@ -290,6 +290,18 @@ impl MapMemory {
self.zoom.zoom_out()
}

/// Set exact zoom level
pub fn set_zoom(&mut self, zoom: f32) -> Result<(), InvalidZoom> {
self.center_mode = self.center_mode.clone().zero_offset(self.zoom.into());
self.zoom = Zoom::try_from(zoom)?;
Ok(())
}

/// Returns the current zoom level
pub fn zoom(&self) -> f32 {
self.zoom.into()
}

/// Returns exact position if map is detached (i.e. not following `my_position`),
/// `None` otherwise.
pub fn detached(&self) -> Option<Position> {
Expand All @@ -316,7 +328,7 @@ fn flood_fill_tiles(
tile_id: TileId,
map_center_projected_position: Pixels,
zoom: f64,
tiles: &mut dyn TilesManager,
tiles: &mut dyn Tiles,
meshes: &mut HashMap<TileId, Option<Mesh>>,
) {
// We need to make up the difference between integer and floating point zoom levels.
Expand Down
2 changes: 1 addition & 1 deletion walkers/src/mercator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub struct TileId {
pub y: u32,

/// Zoom level, where 0 means no zoom.
/// See: https://wiki.openstreetmap.org/wiki/Zoom_levels
/// See: <https://wiki.openstreetmap.org/wiki/Zoom_levels>
pub zoom: u8,
}

Expand Down
Loading

0 comments on commit 9f1977f

Please sign in to comment.