diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-27 21:39:34 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-27 21:39:34 +0200 |
commit | b8f437e48777e32eaf4f518b9d1a37c507eb0aac (patch) | |
tree | c5bad17b976855fefd418a2640411f7a668d978d /glfw | |
parent | 89fad401fbcca7d059045bae4419da0c55a0d855 (diff) |
feat(glfw): add get cursor position & set input and cursor mode
Diffstat (limited to 'glfw')
-rw-r--r-- | glfw/src/window.rs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/glfw/src/window.rs b/glfw/src/window.rs index b0c07ff..0c69a6b 100644 --- a/glfw/src/window.rs +++ b/glfw/src/window.rs @@ -150,6 +150,88 @@ impl Window } }) } + + /// Returns the position of the cursor, in screen coordinates, relative to the + /// upper-left corner of the content area of the window. + /// + /// # Errors + /// Will return `Err` if a GLFW error occurs. + pub fn get_cursor_position(&self) -> Result<CursorPosition, Error> + { + let mut x_pos = 0.0; + let mut y_pos = 0.0; + + // SAFETY: The initialize function (called when the window is created) makes sure + // the current thread is the main thread + unsafe { + crate::ffi::glfwGetCursorPos(self.handle, &mut x_pos, &mut y_pos); + } + + get_glfw_error()?; + + Ok(CursorPosition { x: x_pos, y: y_pos }) + } + + /// Sets a input mode option. + /// + /// # Errors + /// Will return `Err` if a GLFW error occurs. + pub fn set_input_mode( + &self, + input_mode: InputMode, + enabled: bool, + ) -> Result<(), Error> + { + // SAFETY: The initialize function (called when the window is created) makes sure + // the current thread is the main thread + unsafe { + crate::ffi::glfwSetInputMode( + self.handle, + input_mode as i32, + i32::from(enabled), + ); + } + + get_glfw_error()?; + + Ok(()) + } + + /// Sets the cursor mode. + /// + /// # Errors + /// Will return `Err` if a GLFW error occurs. + pub fn set_cursor_mode(&self, cursor_mode: CursorMode) -> Result<(), Error> + { + // SAFETY: The initialize function (called when the window is created) makes sure + // the current thread is the main thread + unsafe { + crate::ffi::glfwSetInputMode( + self.handle, + crate::ffi::GLFW_CURSOR, + cursor_mode as i32, + ); + } + + get_glfw_error()?; + + Ok(()) + } + + /// Returns whether or not raw mouse motion is supported. + /// + /// # Errors + /// Will return `Err` if a GLFW error occurs. + pub fn is_raw_mouse_motion_supported(&self) -> Result<bool, Error> + { + // SAFETY: The initialize function (called when the window is created) makes sure + // the current thread is the main thread + let supported = unsafe { crate::ffi::glfwRawMouseMotionSupported() }; + + get_glfw_error()?; + + Ok(supported == crate::ffi::GLFW_TRUE) + } } /// [`Window`] builder. @@ -252,6 +334,25 @@ pub struct Size #[derive(Debug, Clone, Copy)] #[repr(i32)] +pub enum InputMode +{ + StickyKeys = crate::ffi::GLFW_STICKY_KEYS, + StickyMouseButtons = crate::ffi::GLFW_STICKY_MOUSE_BUTTONS, + LockKeyMods = crate::ffi::GLFW_LOCK_KEY_MODS, + RawMouseMotion = crate::ffi::GLFW_RAW_MOUSE_MOTION, +} + +#[derive(Debug, Clone, Copy)] +#[repr(i32)] +pub enum CursorMode +{ + Disabled = crate::ffi::GLFW_CURSOR_DISABLED, + Hidden = crate::ffi::GLFW_CURSOR_HIDDEN, + Normal = crate::ffi::GLFW_CURSOR_NORMAL, +} + +#[derive(Debug, Clone, Copy)] +#[repr(i32)] pub enum Key { // Unknown = crate::ffi::GLFW_KEY_UNKNOWN, @@ -384,6 +485,13 @@ pub enum KeyState Released, } +#[derive(Debug, Clone)] +pub struct CursorPosition +{ + pub x: f64, + pub y: f64, +} + type FramebufferSizeCb = Box<dyn Fn(Size)>; thread_local! { |