Skip to content

Commit

Permalink
Respect hidpi factor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatekii committed Jul 27, 2019
1 parent d61fafb commit a440829
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ impl AppState {
width: u32,
height: u32,
zoom: f32,
hidpi_factor: f64,
) -> Self {
Self {
tile_cache: TileCache::new(),
css_cache: RulesCache::try_load_from_file(style).expect("Unable to load the style file. Please consult the log."),
screen: Screen::new(center, width, height),
screen: Screen::new(center, width, height, hidpi_factor),
tile_field: TileField::new(TileId::new(8, 0, 0), TileId::new(8, 0, 0)),
zoom,
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ fn main() {
let size = 600;

let mut events_loop = wgpu::winit::EventsLoop::new();
let hdpi_factor = events_loop.get_available_monitors().next().expect("No monitors found").get_hidpi_factor();

let mut app_state = app_state::AppState::new("config/style.css", zurich.clone(), size, size, z);
let mut app_state = app_state::AppState::new("config/style.css", zurich.clone(), size, size, z, hdpi_factor);

let mut painter = drawing::Painter::init(&events_loop, size, size, &app_state);

Expand Down Expand Up @@ -72,7 +73,7 @@ fn main() {
}
},
WindowEvent::CursorMoved { position, .. } => {
let size = crate::config::CONFIG.renderer.tile_size as f32;
let size = app_state.screen.get_tile_size();
let mut delta = vector((position.x - last_pos.x) as f32, (position.y - last_pos.y) as f32);
let zoom_x = (app_state.screen.width as f32) / size / 2f32.powf(app_state.zoom) / size / 2.0 / 1.3;
let zoom_y = (app_state.screen.height as f32) / size / 2f32.powf(app_state.zoom) / size / 2.0 / 1.3;
Expand Down
22 changes: 14 additions & 8 deletions src/vector_tile/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,29 @@ pub struct Screen {
pub center: Point,
pub width: u32,
pub height: u32,
hidpi_factor: f64,
tile_size: u32,
}

impl Screen {
pub fn new(center: Point, width: u32, height: u32) -> Self {
pub fn new(center: Point, width: u32, height: u32, hidpi_factor: f64) -> Self {
Self {
center,
width,
height,
hidpi_factor,
tile_size: (crate::config::CONFIG.renderer.tile_size as f64 * hidpi_factor) as u32,
}
}

pub fn get_tile_size(&self) -> f32 {
self.tile_size as f32
}

pub fn get_tile_boundaries_for_zoom_level(&self, z: f32, scale: u32) -> TileField {
let z = z.min(14.0);
let size = crate::config::CONFIG.renderer.tile_size as f32;
let px_to_world = self.width as f32 / size / 2.0 / 2f32.powi(z as i32) / scale as f32;
let py_to_world = self.height as f32 / size / 2.0 / 2f32.powi(z as i32) / scale as f32;
let px_to_world = self.width as f32 / self.get_tile_size() / 2.0 / 2f32.powi(z as i32) / scale as f32;
let py_to_world = self.height as f32 / self.get_tile_size() / 2.0 / 2f32.powi(z as i32) / scale as f32;

let top_left: TileId = global_to_num_space(&(self.center - vector(px_to_world, py_to_world)), z as u32).into();
let bottom_right: TileId = global_to_num_space(&(self.center + vector(px_to_world, py_to_world)), z as u32).into();
Expand All @@ -96,9 +103,8 @@ impl Screen {
}

pub fn global_to_screen(&self, z: f32) -> glm::TMat4<f32> {
let size = crate::config::CONFIG.renderer.tile_size as f32;
let zoom_x = 2.0f32.powf(z) / (self.width as f32 / 2.0) * size;
let zoom_y = 2.0f32.powf(z) / (self.height as f32 / 2.0) * size;
let zoom_x = 2.0f32.powf(z) / (self.width as f32 / 2.0) * self.get_tile_size();
let zoom_y = 2.0f32.powf(z) / (self.height as f32 / 2.0) * self.get_tile_size();
let zoom = glm::scaling(&glm::vec3(zoom_x, zoom_y, 1.0));
let position = glm::translation(&glm::vec3(-self.center.x, -self.center.y, 0.0));
zoom * position
Expand Down Expand Up @@ -260,7 +266,7 @@ impl<'a> Iterator for TileIterator<'a> {

#[test]
fn get_tile_boundaries_for_8_zoom() {
let bb = Screen::new(point(47.607371, 6.114297), 800, 800);
let bb = Screen::new(point(47.607371, 6.114297), 800, 800, 1.0);
let tile_field = bb.get_tile_boundaries_for_zoom_level(8.0, 1);
let tiles = tile_field.iter().collect::<Vec<_>>();

Expand Down

0 comments on commit a440829

Please sign in to comment.