use crate::core::resources::inputs::{
keyboard::Keyboard,
mouse::{Mouse, MouseEvent},
types::{Input, InputState, KeyCode, KeyboardEvent, Shortcut},
};
#[derive(Default)]
pub struct InputsController {
mouse: Mouse,
keyboard: Keyboard,
}
impl InputsController {
pub fn key_pressed(&self, key: &KeyCode) -> bool {
self.keyboard.pressed_keys.contains(key)
}
pub fn on_key_pressed<Body>(&self, key: KeyCode, action: Body)
where
Body: FnMut(),
{
self.keyboard.on_key_pressed(key, action);
}
pub fn on_key_released<Body>(&self, key: KeyCode, action: Body)
where
Body: FnMut(),
{
self.keyboard.on_key_released(key, action);
}
pub fn on_left_click_pressed<Body>(&self, action: Body)
where
Body: FnMut(f64, f64),
{
self.mouse.on_left_click_pressed(action);
}
pub fn on_right_click_pressed<Body>(&self, action: Body)
where
Body: FnMut(f64, f64),
{
self.mouse.on_right_click_pressed(action);
}
pub fn on_middle_click_pressed<Body>(&self, action: Body)
where
Body: FnMut(f64, f64),
{
self.mouse.on_middle_click_pressed(action);
}
pub fn on_left_click_released<Body>(&self, action: Body)
where
Body: FnMut(f64, f64),
{
self.mouse.on_left_click_released(action);
}
pub fn on_right_click_released<Body>(&self, action: Body)
where
Body: FnMut(f64, f64),
{
self.mouse.on_right_click_released(action);
}
pub fn on_middle_click_released<Body>(&self, action: Body)
where
Body: FnMut(f64, f64),
{
self.mouse.on_middle_click_released(action);
}
pub fn all_pressed_events(&self) -> Vec<Input> {
self.all_events_for_state(InputState::Pressed)
}
pub fn all_released_events(&self) -> Vec<Input> {
self.all_events_for_state(InputState::Released)
}
pub fn all_pressed(&self) -> Vec<Input> {
let mut pressed = self.keyboard.all_pressed();
let mut mouse_pressed = self.mouse.all_pressed();
pressed.append(&mut mouse_pressed);
pressed
}
pub fn mouse_xy(&self) -> (f64, f64) {
self.mouse.xy()
}
pub fn shortcut_pressed(&self, shortcut: &Shortcut) -> bool {
shortcut.iter().all(|input| self.input_pressed(input))
}
pub fn shortcut_pressed_event(&self, shortcut: &Shortcut) -> bool {
shortcut.iter().all(|input| self.input_pressed(input))
&& shortcut.iter().any(|input| self.all_pressed_events().contains(input))
}
pub fn shortcut_released_event(&self, shortcut: &Shortcut) -> bool {
shortcut.iter().any(|input| self.all_released_events().contains(input))
&& shortcut.iter().all(|input| {
self.input_pressed(input) || self.all_released_events().contains(input)
})
}
fn all_events_for_state(&self, input_state: InputState) -> Vec<Input> {
let mut inputs = self.keyboard.all_keys_at_state(input_state);
let mut mouse_inputs = self.mouse.all_click_at_state(input_state);
inputs.append(&mut mouse_inputs);
inputs
}
fn input_pressed(&self, input: &Input) -> bool {
match input {
Input::Key(keycode) => self.key_pressed(keycode),
Input::Mouse(mouse_button) => self.mouse.button_pressed(mouse_button),
}
}
pub(crate) fn reset_inputs(&mut self) {
self.mouse.clear_events();
self.keyboard.clear_events();
}
pub(crate) fn set_mouse_position(&mut self, x: f64, y: f64) {
self.mouse.set_position(x, y);
}
pub(crate) fn add_click_event(&mut self, event: MouseEvent) {
self.mouse.add_click_event(event);
}
pub(crate) fn add_keyboard_event(&mut self, event: KeyboardEvent) {
self.keyboard.add_keyboard_event(event);
}
}