From b8f437e48777e32eaf4f518b9d1a37c507eb0aac Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 27 Oct 2023 21:39:34 +0200 Subject: feat(glfw): add get cursor position & set input and cursor mode --- glfw/src/window.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) (limited to 'glfw/src') 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 + { + 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 + { + // 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. @@ -250,6 +332,25 @@ pub struct Size pub height: u32, } +#[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 @@ -384,6 +485,13 @@ pub enum KeyState Released, } +#[derive(Debug, Clone)] +pub struct CursorPosition +{ + pub x: f64, + pub y: f64, +} + type FramebufferSizeCb = Box; thread_local! { -- cgit v1.2.3-18-g5258