Add panning with rmb

This commit is contained in:
Sijmen 2018-07-29 22:58:40 +02:00
parent 5dc1faad63
commit 46c288be8c

View file

@ -11,6 +11,7 @@ use sdl2::keyboard::Keycode;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::render::WindowCanvas; use sdl2::render::WindowCanvas;
use sdl2::image::INIT_PNG; use sdl2::image::INIT_PNG;
use sdl2::mouse::MouseButton;
use perlin_noise::PerlinNoise; use perlin_noise::PerlinNoise;
@ -22,11 +23,16 @@ struct GameState {
camera_x: i32, camera_x: i32,
camera_y: i32, camera_y: i32,
camera_panning: bool,
}
impl GameState { impl GameState {
fn new() -> Self { fn new() -> Self {
Self { Self {
camera_x: 0, camera_x: 0,
camera_y: 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;
}
}
_ => {}, _ => {},
}; };
} }