diff options
author | Nao Pross <naopross@thearcway.org> | 2019-01-10 11:25:12 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2019-01-10 11:25:12 +0100 |
commit | 005206229d45701a44b12d20f6d7a0662a2b002a (patch) | |
tree | a1a6c868f49a2c7d7eecb83ec6865732e50f3a46 /src | |
parent | Add pan and partial zoom implementation (diff) | |
download | Subconscious-rs-005206229d45701a44b12d20f6d7a0662a2b002a.tar.gz Subconscious-rs-005206229d45701a44b12d20f6d7a0662a2b002a.zip |
Update pan to use less Graphics members
Diffstat (limited to '')
-rw-r--r-- | src/graphics.rs | 33 |
1 files 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<u32, Image>, textures: HashMap<u32, Texture>, @@ -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); } } |