diff options
author | HampusM <hampus@hampusmat.com> | 2023-11-27 22:01:18 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-11-27 22:01:18 +0100 |
commit | 89c6739581cce30dda05ad8016a553a1dd10a5d8 (patch) | |
tree | 05a93953de70b1fe33fed528a1ee6bc3c03c0bd0 /glfw/src | |
parent | a20192060679ee05d5a567eb4179e60d80f7f21e (diff) |
feat(glfw): add get mouse button input
Diffstat (limited to 'glfw/src')
-rw-r--r-- | glfw/src/window.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/glfw/src/window.rs b/glfw/src/window.rs index 0c69a6b..bd8b78b 100644 --- a/glfw/src/window.rs +++ b/glfw/src/window.rs @@ -151,6 +151,35 @@ impl Window }) } + /// Returns the last reported state of a mouse button. + /// + /// # Errors + /// Will return `Err` if a GLFW error occurs. + pub fn get_mouse_button( + &self, + mouse_button: MouseButton, + ) -> Result<MouseButtonState, Error> + { + // SAFETY: The initialize function (called when the window is created) makes sure + // the current thread is the main thread + let state = unsafe { + crate::ffi::glfwGetMouseButton(self.handle, mouse_button.into_raw()) + }; + + get_glfw_error()?; + + Ok(match state { + crate::ffi::GLFW_PRESS => MouseButtonState::Pressed, + crate::ffi::GLFW_RELEASE => MouseButtonState::Released, + _ => { + // SAFETY: glfwGetMouseButton can only return GLFW_PRESS or GLFW_RELEASE + unsafe { + unreachable_unchecked(); + } + } + }) + } + /// Returns the position of the cursor, in screen coordinates, relative to the /// upper-left corner of the content area of the window. /// @@ -485,6 +514,47 @@ pub enum KeyState Released, } +#[derive(Debug, Clone, Copy)] +pub enum MouseButton +{ + One, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, +} + +impl MouseButton +{ + pub const LEFT: Self = Self::One; + pub const MIDDLE: Self = Self::Three; + pub const RIGHT: Self = Self::Two; + + fn into_raw(self) -> i32 + { + match self { + Self::One => crate::ffi::GLFW_MOUSE_BUTTON_1, + Self::Two => crate::ffi::GLFW_MOUSE_BUTTON_2, + Self::Three => crate::ffi::GLFW_MOUSE_BUTTON_3, + Self::Four => crate::ffi::GLFW_MOUSE_BUTTON_4, + Self::Five => crate::ffi::GLFW_MOUSE_BUTTON_5, + Self::Six => crate::ffi::GLFW_MOUSE_BUTTON_6, + Self::Seven => crate::ffi::GLFW_MOUSE_BUTTON_7, + Self::Eight => crate::ffi::GLFW_MOUSE_BUTTON_8, + } + } +} + +#[derive(Debug, Clone, Copy)] +pub enum MouseButtonState +{ + Pressed, + Released, +} + #[derive(Debug, Clone)] pub struct CursorPosition { |