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
57
58
59
60
61
62
63
64
65
66
67
//! The [`InputManager`] struct.

use std::collections::BTreeSet;

use crate::Vec2;

/// A store for Input-related data.
///
/// The [`InputManager`] stores and handles the current input states.
///
/// # Examples
/// ```
/// use moon_engine::input::InputManager;
///
/// let mut input = InputManager::new();
///
/// input.key_down(b'w');
///
/// assert!(input.get_key_state(b'w'));
/// ```
#[derive(Default)]
pub struct InputManager {
    /// Set of Keyboard key states.
    ///
    /// If a key is present, then it is being pressed, and otherwise it is not.
    keyboard_states: BTreeSet<u8>,
    /// Position of the Mouse.
    ///
    /// The Screen-Space position of the Mouse as a [`Vec2`].
    pub mouse_position: Vec2,
}

impl InputManager {
    /// Default [`InputManager`] instance.
    ///
    /// Creates a new [`InputManager`] with default keyboard and mouse input states.
    pub fn new() -> Self {
        Default::default()
    }

    /// Key Down State.
    ///
    /// Sets the key in the [`BTreeSet`].
    pub fn key_down(&mut self, key_code: u8) {
        self.keyboard_states.insert(key_code);
    }

    /// Key Up State.
    ///
    /// Resets the key in the [`BTreeSet`].
    pub fn key_up(&mut self, key_code: u8) {
        self.keyboard_states.remove(&key_code);
    }

    /// Get the state of a key as a [`bool`].
    ///
    /// Returns true if the key is currently pressed, or false.
    pub fn get_key_state(&self, key_code: u8) -> bool {
        self.keyboard_states.contains(&key_code)
    }

    /// Set the mouse position.
    pub fn set_mouse_position(&mut self, x: f32, y: f32) {
        self.mouse_position.x = x;
        self.mouse_position.y = y;
    }
}