From 005206229d45701a44b12d20f6d7a0662a2b002a Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Thu, 10 Jan 2019 11:25:12 +0100 Subject: Update pan to use less Graphics members --- src/graphics.rs | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index d50562d..7e8a0e1 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -4,6 +4,7 @@ use sfml::window::mouse::Button; use sfml::system::{ Vector2f, + Vector2i }; use sfml::window::{ @@ -45,9 +46,6 @@ pub struct Graphics { panning: bool, // pan pan_start: Vector2f, - pan_start_center: Vector2f, - // absolute zoom - zoom: f32, // loaded resources tilesets: HashMap, textures: HashMap, @@ -93,11 +91,6 @@ impl Graphics { panning: false, // zoom and pan pan_start: Vector2f::new(0.0, 0.0), - pan_start_center: Vector2f::new( - default_window_size.0 as f32 / 2.0, - default_window_size.1 as f32 / 2.0 - ), - zoom: 1.0, // resources tilesets: HashMap::new(), textures: HashMap::new(), @@ -140,6 +133,7 @@ impl Graphics { let tmx_tileset = match state.map.get_tileset_by_gid(tmx_tile) { Some(tileset) => tileset, None => { + // TODO: missing_tile texture panic!("Failed to get tileset with get_tileset_by_gid"); } }; @@ -194,7 +188,7 @@ impl Graphics { self.tilesets.insert(tmx_tileset.first_gid, image); } - // draw! + // draw tiles // TODO: check map render order let texture = &self.textures.get(&tmx_tile).unwrap(); let mut tile_rect = RectangleShape::with_texture(texture); @@ -209,11 +203,15 @@ impl Graphics { y: (y * tmx_tileset.tile_height) as f32 }); + self.window.draw(&tile_rect); } } } + // draw panels + + self.window.display(); } @@ -225,14 +223,9 @@ impl Graphics { self.running = false; }, Event::MouseWheelScrolled { wheel: _, delta, x: _, y: _ } => { - // TODO: zoom towards (x, y) - // delta is probalby a value between -1 and 1 - let local_zoom = 1.0 + delta * 0.1; - // update absolute zoom - self.zoom *= local_zoom; - - self.view.zoom(local_zoom); + let zoom = 1.0 + delta * 0.1; + self.view.zoom(zoom); self.window.set_view(&self.view); }, Event::MouseButtonPressed { button, x, y} => { @@ -241,7 +234,7 @@ impl Graphics { Button::Left => { // set pan start position self.panning = true; - self.pan_start = Vector2f::new(x as f32, y as f32); + self.pan_start = self.window.map_pixel_to_coords_current_view(&Vector2i::new(x, y)); } _ => {}, } @@ -251,7 +244,6 @@ impl Graphics { Button::Right => {} Button::Left => { self.panning = false; - self.pan_start_center = self.view.center(); }, _ => {}, } @@ -259,11 +251,10 @@ impl Graphics { Event::MouseMoved { x, y } => { if self.panning { // get delta of vectors - // (pan_start_pos - current_mouse_pos) * zoom - let delta = (self.pan_start - Vector2f::new(x as f32, y as f32)) * self.zoom; + let delta = self.pan_start - self.window.map_pixel_to_coords_current_view(&Vector2i::new(x, y)); // move view by moving the center by delta - self.view.set_center(self.pan_start_center + delta); + self.view.move_(delta); self.window.set_view(&self.view); } } -- cgit v1.2.1