diff options
Diffstat (limited to 'games/rstnode/rst-client/src/viewport.rs')
-rw-r--r-- | games/rstnode/rst-client/src/viewport.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/games/rstnode/rst-client/src/viewport.rs b/games/rstnode/rst-client/src/viewport.rs new file mode 100644 index 000000000000..3f9e803e387b --- /dev/null +++ b/games/rstnode/rst-client/src/viewport.rs @@ -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(()) + } +} |