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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! The default input system by the engine.

use crate::Layer;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use winit::dpi::PhysicalSize;
use winit::event::{ElementState, Event, ModifiersState, WindowEvent};
pub use winit::event::{MouseButton, VirtualKeyCode};

use crossbeam::atomic::AtomicCell;
use glam::f32::{vec2, Vec2};
use hashbrown::HashSet;
use parking_lot::Mutex;

/// Holds the input information to be used in game.
///
/// Updates each frame.
#[derive(Clone)]
pub struct Input {
    //pressed keyboard buttons.
    keyboard_down: Arc<Mutex<HashSet<u32>>>,
    //pressed keyboard keycodes.
    keys_down: Arc<Mutex<HashSet<VirtualKeyCode>>>,
    //pressed keyboard modifiers
    keyboard_modifiers: Arc<Mutex<ModifiersState>>,
    //pressed mouse buttons
    mouse_down: Arc<Mutex<HashSet<MouseButton>>>,
    //mouse position
    cursor_position: Arc<AtomicCell<Vec2>>,
    cursor_inside: Arc<AtomicBool>,
    //dimensions of the window
    dimensions: Arc<AtomicCell<(f32, f32)>>, // lazylock future
}

impl Input {
    pub(crate) fn new() -> Self {
        Self {
            keyboard_down: Arc::new(Mutex::new(HashSet::new())),
            keys_down: Arc::new(Mutex::new(HashSet::new())),
            keyboard_modifiers: Arc::new(Mutex::new(ModifiersState::empty())),
            mouse_down: Arc::new(Mutex::new(HashSet::new())),
            cursor_position: Arc::new(AtomicCell::new(vec2(0.0, 0.0))),
            cursor_inside: Arc::new(AtomicBool::new(false)),
            dimensions: Arc::new(AtomicCell::new((0.0, 0.0))),
        }
    }
    /// Updates the input with the event.
    pub(crate) fn update<T: 'static>(&mut self, event: &Event<T>, dimensions: PhysicalSize<u32>) {
        self.dimensions
            .store((dimensions.width as f32, dimensions.height as f32));
        if let Event::WindowEvent { event, .. } = event {
            match event {
                WindowEvent::KeyboardInput { input, .. } => {
                    if input.state == ElementState::Pressed {
                        self.keyboard_down.lock().insert(input.scancode);
                        if let Some(code) = input.virtual_keycode {
                            self.keys_down.lock().insert(code);
                        }
                    } else {
                        self.keyboard_down.lock().remove(&input.scancode);
                        if let Some(code) = input.virtual_keycode {
                            self.keys_down.lock().remove(&code);
                        }
                    }
                }
                WindowEvent::ModifiersChanged(modifiers) => {
                    *self.keyboard_modifiers.lock() = *modifiers;
                }
                WindowEvent::MouseInput { state, button, .. } => {
                    if state == &ElementState::Pressed {
                        self.mouse_down.lock().insert(*button);
                    } else {
                        self.mouse_down.lock().remove(button);
                    }
                }
                WindowEvent::CursorMoved { position, .. } => {
                    self.cursor_position.store(vec2(
                        (position.x as f32 / dimensions.width as f32) * 2.0 - 1.0,
                        (position.y as f32 / dimensions.height as f32) * 2.0 - 1.0,
                    ));
                }
                WindowEvent::CursorEntered { .. } => {
                    self.cursor_inside.store(true, Ordering::Release)
                }
                WindowEvent::CursorLeft { .. } => {
                    self.cursor_inside.store(false, Ordering::Release)
                }
                _ => (),
            }
        }
    }

    /// Returns true if the given key is pressed on the keyboard.
    pub fn is_down(&self, key: u32) -> bool {
        self.keyboard_down.lock().contains(&key)
    }
    /// Returns true if the given keycode is pressed on the keyboard.
    pub fn key_down(&self, key: VirtualKeyCode) -> bool {
        self.keys_down.lock().contains(&key)
    }

    /// Returns true if the given mouse button is pressed.
    pub fn mouse_down(&self, button: &MouseButton) -> bool {
        self.mouse_down.lock().contains(button)
    }

    /// Returns the cursor position going from -1.0 to 1.0 x and y.
    pub fn cursor_position(&self) -> Vec2 {
        let cp = self.cursor_position.load();
        vec2(cp[0], cp[1])
    }

    /// Returns the cursor position going from -1.0 to 1.0 x and y scaled with the inserted layers scaling properties.
    pub fn scaled_cursor(&self, layer: &Layer) -> Vec2 {
        let (width, height) = crate::utils::scale(layer.camera_scaling(), self.dimensions.load());
        let cp = self.cursor_position.load();
        vec2(cp[0] * width, cp[1] * height)
    }

    /// Returns the cursor position in layer world space.
    pub fn cursor_to_world(&self, layer: &Layer) -> Vec2 {
        let dims = self.dimensions.load();
        let (width, height) = crate::utils::scale(layer.camera_scaling(), dims);
        let cp = self.cursor_position.load();
        let cam = layer.camera_position();
        let zoom = 1.0 / layer.zoom();
        vec2(
            cp[0] * (width * zoom) + cam[0] * 2.0,
            cp[1] * (height * zoom) + cam[1] * 2.0,
        )
    }

    /// Returns true if shift is pressed on the keyboard.
    pub fn shift(&self) -> bool {
        self.keyboard_modifiers.lock().shift()
    }

    /// Returns true if ctrl is pressed on the keyboard.
    pub fn ctrl(&self) -> bool {
        self.keyboard_modifiers.lock().ctrl()
    }

    /// Returns true if alt is pressed on the keyboard.
    pub fn alt(&self) -> bool {
        self.keyboard_modifiers.lock().alt()
    }

    /// Returns true if the super key is pressed on the keyboard.
    ///
    /// Super key also means "Windows" key on Windows or "Command" key on Mac.
    pub fn super_key(&self) -> bool {
        self.keyboard_modifiers.lock().logo()
    }

    /// Returns true if the cursor is located in the window.
    pub fn cursor_inside(&self) -> bool {
        self.cursor_inside.load(Ordering::Acquire)
    }
}

impl Default for Input {
    fn default() -> Self {
        Self::new()
    }
}