diff options
author | Katharina Fey <kookie@spacekookie.de> | 2021-02-07 23:43:43 +0100 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2021-02-07 23:43:43 +0100 |
commit | 8dfaa3b256b0e35efa7c5711e541b882b7a83f4b (patch) | |
tree | 54ee41cf0eed62555e1ed79679fd1679d710dcaf /games | |
parent | b53b19ee469801b2aaa02dc7ee244892269550c4 (diff) |
rstnode: implement viewport scrolling and zooming
Diffstat (limited to 'games')
-rw-r--r-- | games/rstnode/rst-client/src/graphics/vector2.rs | 58 | ||||
-rw-r--r-- | games/rstnode/rst-client/src/input.rs | 8 | ||||
-rw-r--r-- | games/rstnode/rst-client/src/state.rs | 9 | ||||
-rw-r--r-- | games/rstnode/rst-client/src/viewport.rs | 18 |
4 files changed, 80 insertions, 13 deletions
diff --git a/games/rstnode/rst-client/src/graphics/vector2.rs b/games/rstnode/rst-client/src/graphics/vector2.rs index 5204e77fa443..f1ca0c902e34 100644 --- a/games/rstnode/rst-client/src/graphics/vector2.rs +++ b/games/rstnode/rst-client/src/graphics/vector2.rs @@ -1,5 +1,5 @@ use std::fmt::{self, Display, Formatter}; -use std::ops::{Add, Mul, Sub, SubAssign}; +use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign}; /// Just a vector #[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)] @@ -27,6 +27,29 @@ impl Vector2 { } } + +impl Add for Vector2 { + type Output = Vector2; + + fn add(self, o: Vector2) -> Self::Output { + Vector2 { + x: self.x + o.x, + y: self.y + o.y, + } + } +} + + +impl AddAssign for Vector2 { + fn add_assign(&mut self, o: Self) { + *self = Self { + x: self.x + o.x, + y: self.y + o.y, + } + } +} + + impl Sub for Vector2 { type Output = Vector2; @@ -47,22 +70,43 @@ impl SubAssign for Vector2 { } } -impl Add for Vector2 { + +impl Mul for Vector2 { type Output = Vector2; - fn add(self, o: Vector2) -> Self::Output { + fn mul(self, o: Vector2) -> Self::Output { Vector2 { - x: self.x + o.x, - y: self.y + o.y, + x: self.x * o.x, + y: self.y * o.y, } } } -impl Mul for Vector2 { +impl Div<f32> for Vector2 { type Output = Vector2; - fn mul(self, o: Vector2) -> Self::Output { + fn div(self, o: f32) -> Self::Output { Vector2 { + x: self.x / o, + y: self.y / o, + } + } +} + +impl Mul<f32> for Vector2 { + type Output = Vector2; + + fn mul(self, o: f32) -> Self::Output { + Self { + x: self.x * o, + y: self.y * o, + } + } +} + +impl MulAssign for Vector2 { + fn mul_assign(&mut self, o: Self) { + *self = Self { x: self.x * o.x, y: self.y * o.y, } diff --git a/games/rstnode/rst-client/src/input.rs b/games/rstnode/rst-client/src/input.rs index 281386986434..e9d2516a94c3 100644 --- a/games/rstnode/rst-client/src/input.rs +++ b/games/rstnode/rst-client/src/input.rs @@ -18,6 +18,8 @@ pub struct InputHandle { pub right_pressed: bool, /// Set when pressing left mouse and unset when releasing it pub drag_point: Option<Vector2>, + /// Get the scroll-wheel position this frame + pub scroll_offset: f32, } impl InputHandle { @@ -28,6 +30,7 @@ impl InputHandle { middle_pressed: false, right_pressed: false, drag_point: None, + scroll_offset: 1.0, } } @@ -55,6 +58,11 @@ impl EventHandler for InputHandle { Ok(()) } + + fn mouse_wheel_event(&mut self, _ctx: &mut Context, _: f32, y: f32) { + self.scroll_offset += y * 0.1; + } + fn draw(&mut self, _: &mut Context) -> GameResult<()> { panic!("Don't draw the input handle!"); } diff --git a/games/rstnode/rst-client/src/state.rs b/games/rstnode/rst-client/src/state.rs index d3b5bbcc3d7b..8099cf9add96 100644 --- a/games/rstnode/rst-client/src/state.rs +++ b/games/rstnode/rst-client/src/state.rs @@ -55,7 +55,7 @@ impl ClientState { pub fn viewport(&mut self) -> &mut Viewport { &mut self.vp } - + pub fn assets(&self) -> &Assets { &self.assets } @@ -76,6 +76,13 @@ impl EventHandler for ClientState { Ok(()) } + fn mouse_wheel_event(&mut self, ctx: &mut Context, x: f32, y: f32) { + self.input.mouse_wheel_event(ctx, x, y); + let zoom = self.input.scroll_offset; + self.viewport().zoom(zoom); + + } + fn draw(&mut self, ctx: &mut Context) -> GameResult<()> { graphics::clear(ctx, graphics::Color::from_rgb(15, 15, 15)); diff --git a/games/rstnode/rst-client/src/viewport.rs b/games/rstnode/rst-client/src/viewport.rs index 3f9e803e387b..017ba7d2a6e1 100644 --- a/games/rstnode/rst-client/src/viewport.rs +++ b/games/rstnode/rst-client/src/viewport.rs @@ -12,6 +12,7 @@ pub struct Viewport { start: Vector2, prev_start: Vector2, size: Vector2, + orig_size: Vector2, } impl Viewport { @@ -19,10 +20,6 @@ impl Viewport { Self::default() } - pub fn start(&self) -> &Vector2 { - &self.start - } - /// Must be called at least once before calling /// [`update`](Self::update) pub fn init(&mut self, ctx: &mut Context) { @@ -30,6 +27,7 @@ impl Viewport { self.start = Vector2::new(x, y); self.prev_start = self.start; self.size = Vector2::new(w, h); + self.orig_size = self.size; } /// Update the game state with the curent viewport data @@ -49,15 +47,25 @@ impl Viewport { /// Apply changes from the input handle to the viewport pub fn apply(&mut self, _: &mut Context, input: &InputHandle) -> GameResult<()> { + + // Move the viewport around if input.middle_pressed { let drag = input.drag_point.as_ref().unwrap().clone(); let pos = input.mouse_pos.clone(); self.start = self.prev_start + (drag - pos); - debug!("Changing VP start: {}", self.start); } else { self.prev_start = self.start; } + // Compute the scroll level Ok(()) } + + /// Called when the input handler scroll event is handled + pub fn zoom(&mut self, zoom: f32) { + let new_size = self.orig_size * zoom; + let diff = self.size - new_size; + self.start += diff / 2.0; + self.size = new_size; + } } |