use egui_backend::glfw;
use egui_gl_glfw as egui_backend;
use glfw::{GlfwReceiver, Key, MouseButton};
use nalgebra_glm as glm; use std::collections::HashSet;
pub struct InputManager {
glfw: glfw::Glfw,
event_receiver: GlfwReceiver<(f64, glfw::WindowEvent)>,
pub events: Vec<(f64, glfw::WindowEvent)>,
pub keys: HashSet<Key>,
pub key_just_pressed: HashSet<Key>,
pub mouse_buttons: HashSet<MouseButton>,
pub mouse_button_just_pressed: HashSet<MouseButton>,
pub mouse_position: glm::Vec2,
pub last_mouse_position: glm::Vec2,
pub mouse_delta: glm::Vec2,
}
impl InputManager {
pub fn new(events: GlfwReceiver<(f64, glfw::WindowEvent)>, glfw: glfw::Glfw) -> InputManager {
InputManager {
glfw,
event_receiver: events,
events: Vec::new(), keys: HashSet::new(),
key_just_pressed: HashSet::new(),
mouse_buttons: HashSet::new(),
mouse_button_just_pressed: HashSet::new(),
mouse_position: glm::vec2(0.0, 0.0),
last_mouse_position: glm::vec2(0.0, 0.0),
mouse_delta: glm::vec2(0.0, 0.0),
}
}
pub fn update(&mut self) {
self.glfw.poll_events();
self.mouse_delta = self.mouse_position - self.last_mouse_position;
self.last_mouse_position = self.mouse_position;
self.key_just_pressed.clear(); self.mouse_button_just_pressed.clear();
self.events.clear(); self.events = glfw::flush_messages(&self.event_receiver).collect();
for (_, event) in self.events.iter() {
match event {
glfw::WindowEvent::Key(key, _, action, _) => {
if *action == glfw::Action::Press {
self.keys.insert(*key);
self.key_just_pressed.insert(*key); } else if *action == glfw::Action::Release {
self.keys.remove(key);
}
}
glfw::WindowEvent::MouseButton(button, action, _) => {
if *action == glfw::Action::Press {
self.mouse_buttons.insert(*button);
self.mouse_button_just_pressed.insert(*button); } else if *action == glfw::Action::Release {
self.mouse_buttons.remove(button);
}
}
glfw::WindowEvent::CursorPos(x, y) => {
self.mouse_position = glm::vec2(*x as f32, *y as f32);
}
_ => {}
}
}
}
}