parent
0472f0fb50
commit
f9d4d7ec01
@ -0,0 +1,56 @@ |
|||||||
|
//! Advanced input handler
|
||||||
|
|
||||||
|
use ggez::{ |
||||||
|
event::EventHandler, |
||||||
|
input::mouse::{self, MouseButton}, |
||||||
|
Context, GameResult, |
||||||
|
}; |
||||||
|
use mint::Point2; |
||||||
|
|
||||||
|
pub struct InputHandle { |
||||||
|
/// The mouse position on the viewport
|
||||||
|
pub mouse_pos: Point2<f32>, |
||||||
|
/// Whether the left mouse button is pressed this frame
|
||||||
|
pub left_pressed: bool, |
||||||
|
/// Whether the right mouse button is pressed this frame
|
||||||
|
pub right_pressed: bool, |
||||||
|
/// Set when pressing left mouse and unset when releasing it
|
||||||
|
pub drag_point: Option<Point2<f32>>, |
||||||
|
} |
||||||
|
|
||||||
|
impl InputHandle { |
||||||
|
pub fn new() -> Self { |
||||||
|
Self { |
||||||
|
mouse_pos: [0.0, 0.0].into(), |
||||||
|
left_pressed: false, |
||||||
|
right_pressed: false, |
||||||
|
drag_point: None, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// Get the unprojected mouse coordinates
|
||||||
|
pub fn unproject(&self) -> Point2<f32> { |
||||||
|
self.mouse_pos.clone(); |
||||||
|
todo!() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
impl EventHandler for InputHandle { |
||||||
|
fn update(&mut self, ctx: &mut Context) -> GameResult<()> { |
||||||
|
self.mouse_pos = mouse::position(&ctx); |
||||||
|
self.left_pressed = mouse::button_pressed(ctx, MouseButton::Left); |
||||||
|
self.right_pressed = mouse::button_pressed(ctx, MouseButton::Right); |
||||||
|
|
||||||
|
// Only set the drag_point once and unset when we release Left button
|
||||||
|
if self.left_pressed && self.drag_point.is_none() { |
||||||
|
self.drag_point = Some(self.mouse_pos.clone()); |
||||||
|
} else if !self.left_pressed { |
||||||
|
self.drag_point = None; |
||||||
|
} |
||||||
|
|
||||||
|
Ok(()) |
||||||
|
} |
||||||
|
fn draw(&mut self, _: &mut Context) -> GameResult<()> { |
||||||
|
panic!("Don't draw the input handle!"); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue