aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/input.rs
blob: 4e901b8ade0621419b1ddb83e28e892b6026423d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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!");
    }
}