rstnode: implement viewport scrolling and zooming

wip/yesman
Katharina Fey 3 years ago
parent b53b19ee46
commit 8dfaa3b256
Signed by: kookie
GPG Key ID: 90734A9E619C8A6C
  1. 58
      games/rstnode/rst-client/src/graphics/vector2.rs
  2. 8
      games/rstnode/rst-client/src/input.rs
  3. 9
      games/rstnode/rst-client/src/state.rs
  4. 18
      games/rstnode/rst-client/src/viewport.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,
}

@ -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!");
}

@ -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));

@ -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;
}
}

Loading…
Cancel
Save