rich_sdl2_rust/event/mouse/
cursor.rs

1//! Controlling cursor to show/hide.
2
3use static_assertions::assert_not_impl_all;
4use std::marker::PhantomData;
5use std::os::raw::c_int;
6
7use crate::{bind, window::Window, Sdl};
8
9/// This controls the cursor on the window. It can show/hide the cursor.
10#[derive(Clone)]
11pub struct Cursor<'window> {
12    window: PhantomData<&'window Window<'window>>,
13}
14
15impl std::fmt::Debug for Cursor<'_> {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("Cursor").finish()
18    }
19}
20
21assert_not_impl_all!(Cursor: Send, Sync);
22
23impl<'window> Cursor<'window> {
24    /// Constructs cursor control from the window.
25    #[must_use]
26    pub fn new(_: &'window Window) -> Self {
27        Self {
28            window: PhantomData,
29        }
30    }
31
32    /// Shows the cursor on the window.
33    pub fn show(&self) {
34        let ret = unsafe { bind::SDL_ShowCursor(bind::SDL_ENABLE as c_int) };
35        if ret < 0 {
36            eprintln!("{}", Sdl::error());
37        }
38    }
39
40    /// Hides the cursor on the window.
41    pub fn hide(&self) {
42        let ret = unsafe { bind::SDL_ShowCursor(bind::SDL_DISABLE as c_int) };
43        if ret < 0 {
44            eprintln!("{}", Sdl::error());
45        }
46    }
47
48    /// Returns whether the cursor is shown.
49    #[must_use]
50    pub fn is_shown(&self) -> bool {
51        let ret = unsafe { bind::SDL_ShowCursor(bind::SDL_QUERY as c_int) };
52        if ret < 0 {
53            eprintln!("{}", Sdl::error());
54            return true;
55        }
56        ret as u32 == bind::SDL_ENABLE
57    }
58}