From 46c288be8cdda2086404d97076d14f8780a04210 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sun, 29 Jul 2018 22:58:40 +0200 Subject: [PATCH] Add panning with rmb --- src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main.rs b/src/main.rs index 5868494..6b24a3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use sdl2::keyboard::Keycode; use sdl2::pixels::Color; use sdl2::render::WindowCanvas; use sdl2::image::INIT_PNG; +use sdl2::mouse::MouseButton; use perlin_noise::PerlinNoise; @@ -22,11 +23,16 @@ struct GameState { camera_x: i32, camera_y: i32, + camera_panning: bool, +} + impl GameState { fn new() -> Self { Self { camera_x: 0, camera_y: 0, + + camera_panning: false, } } } @@ -58,6 +64,19 @@ fn update(event_pump: &mut EventPump, game_state: &mut GameState) -> bool { }; }, + Event::MouseButtonDown { mouse_btn: MouseButton::Right, .. } => { + game_state.camera_panning = true; + }, + Event::MouseButtonUp { mouse_btn: MouseButton::Right, .. } => { + game_state.camera_panning = false; + }, + Event::MouseMotion { xrel: x, yrel: y, .. } => { + if game_state.camera_panning { + game_state.camera_x -= x; + game_state.camera_y -= y; + } + } + _ => {}, }; }