My personal project and infrastructure archive
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nomicon/games/rstnode/rst-client/src/viewport.rs

71 lines
1.9 KiB

//! Viewport utilities
use crate::{graphics::Vector2, input::InputArbiter};
use ggez::{
error::GameResult,
graphics::{self, Rect},
Context,
};
#[derive(Default)]
pub struct Viewport {
start: Vector2,
prev_start: Vector2,
size: Vector2,
orig_size: Vector2,
}
impl Viewport {
pub fn new() -> Self {
Self::default()
}
/// 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);
self.orig_size = self.size;
}
/// 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: &InputArbiter) -> 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);
// } 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;
}
}