From f94c1c531a1f9874e4303fb702f22b7e953c2a29 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 26 Oct 2023 00:56:51 +0200 Subject: feat(engine): add get keyboard input --- engine/src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'engine/src/lib.rs') diff --git a/engine/src/lib.rs b/engine/src/lib.rs index bec1e30..cc629b2 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use std::time::{Duration, Instant}; +use glfw::window::KeyState; use glfw::{Window, WindowBuilder}; use crate::camera::Camera; @@ -21,6 +22,7 @@ pub mod object; pub mod vector; pub mod vertex; +pub use glfw::window::Key; pub use glfw::WindowSize; #[derive(Debug)] @@ -134,6 +136,18 @@ impl Engine &self.delta_time } + /// Returns whether or not a keyboard key is currently pressed. + /// + /// # Errors + /// Will return `Err` if getting the key state fails. + pub fn is_key_pressed(&self, key: Key) -> Result + { + self.window + .get_key(key) + .map(|key_state| matches!(key_state, KeyState::Pressed)) + .map_err(Error::GetKeyStateFailed) + } + fn update_delta_time(&mut self, prev_frame_start: &mut Option) { let frame_start_time = Instant::now(); @@ -164,4 +178,7 @@ pub enum Error #[error("Failed to get window size")] GetWindowSizeFailed(#[source] glfw::Error), + + #[error("Failed to get key state")] + GetKeyStateFailed(#[source] glfw::Error), } -- cgit v1.2.3-18-g5258