A bit of a rant: it's fucking ridiculous how much work it is to subtract two points from each other with mint + nalgebra. I ended up adding a complete `Vector2` implementation in this commit because I got fed up with it. Consider this the unofficial start of librgx (rust graphics x), which will basically become a libgdx in Rust, and the basis for the little game engine. Not to mention that ggez probably won't be maintained past version 0.6.0 unless some other maintainer takes over (I guess that could always happen). Anyway, I'm annoyed!wip/yesman
parent
f9d4d7ec01
commit
b53b19ee46
@ -0,0 +1,82 @@ |
||||
use std::fmt::{self, Display, Formatter}; |
||||
use std::ops::{Add, Mul, Sub, SubAssign}; |
||||
|
||||
/// Just a vector
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)] |
||||
pub struct Vector2 { |
||||
pub x: f32, |
||||
pub y: f32, |
||||
} |
||||
|
||||
impl Display for Vector2 { |
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
||||
write!(f, "[{}, {}]", self.x, self.y) |
||||
} |
||||
} |
||||
|
||||
impl Vector2 { |
||||
pub fn new(x: f32, y: f32) -> Self { |
||||
Self { x, y } |
||||
} |
||||
|
||||
pub fn abs(self) -> Self { |
||||
Self { |
||||
x: self.x.abs(), |
||||
y: self.y.abs(), |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl Sub for Vector2 { |
||||
type Output = Vector2; |
||||
|
||||
fn sub(self, o: Vector2) -> Self::Output { |
||||
Vector2 { |
||||
x: self.x - o.x, |
||||
y: self.y - o.y, |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl SubAssign for Vector2 { |
||||
fn sub_assign(&mut self, o: Self) { |
||||
*self = Self { |
||||
x: self.x - o.x, |
||||
y: self.y - o.y, |
||||
} |
||||
} |
||||
} |
||||
|
||||
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 Mul for Vector2 { |
||||
type Output = Vector2; |
||||
|
||||
fn mul(self, o: Vector2) -> Self::Output { |
||||
Vector2 { |
||||
x: self.x * o.x, |
||||
y: self.y * o.y, |
||||
} |
||||
} |
||||
} |
||||
|
||||
impl From<Vector2> for mint::Point2<f32> { |
||||
fn from(v: Vector2) -> Self { |
||||
[v.x, v.y].into() |
||||
} |
||||
} |
||||
|
||||
impl From<mint::Point2<f32>> for Vector2 { |
||||
fn from(v: mint::Point2<f32>) -> Self { |
||||
Self { x: v.x, y: v.y } |
||||
} |
||||
} |
@ -0,0 +1,63 @@ |
||||
//! Viewport utilities
|
||||
|
||||
use crate::{graphics::Vector2, input::InputHandle}; |
||||
use ggez::{ |
||||
error::GameResult, |
||||
graphics::{self, Rect}, |
||||
Context, |
||||
}; |
||||
|
||||
#[derive(Default)] |
||||
pub struct Viewport { |
||||
start: Vector2, |
||||
prev_start: Vector2, |
||||
size: Vector2, |
||||
} |
||||
|
||||
impl Viewport { |
||||
pub fn new() -> Self { |
||||
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) { |
||||
let Rect { x, y, w, h } = graphics::screen_coordinates(&ctx); |
||||
self.start = Vector2::new(x, y); |
||||
self.prev_start = self.start; |
||||
self.size = Vector2::new(w, h); |
||||
} |
||||
|
||||
/// Update the game state with the curent viewport data
|
||||
pub fn update(&self, ctx: &mut Context) -> GameResult<()> { |
||||
graphics::set_screen_coordinates( |
||||
ctx, |
||||
Rect { |
||||
x: self.start.x, |
||||
y: self.start.y, |
||||
w: self.size.x, |
||||
h: self.size.y, |
||||
}, |
||||
)?; |
||||
|
||||
Ok(()) |
||||
} |
||||
|
||||
/// Apply changes from the input handle to the viewport
|
||||
pub fn apply(&mut self, _: &mut Context, input: &InputHandle) -> GameResult<()> { |
||||
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; |
||||
} |
||||
|
||||
Ok(()) |
||||
} |
||||
} |
Loading…
Reference in new issue