Skip to main content

bevy_window/cursor/
mod.rs

1//! Cursor handling for windows.
2//!
3//! Note: [`CursorSystems`] lives in `bevy_window` rather than `bevy_winit` so
4//! that systems which need to order themselves around cursor updates don't have
5//! to depend on `bevy_winit` just for this set. The system set is intended to
6//! be backend-agnostic and consumable by any windowing backend (`bevy_winit`,
7//! or a future one).
8
9#[cfg(feature = "custom_cursor")]
10mod custom_cursor;
11mod system_cursor;
12
13#[cfg(feature = "custom_cursor")]
14pub use custom_cursor::*;
15pub use system_cursor::*;
16
17#[cfg(feature = "bevy_reflect")]
18use bevy_ecs::reflect::ReflectComponent;
19use bevy_ecs::{component::Component, schedule::SystemSet};
20#[cfg(feature = "bevy_reflect")]
21use bevy_reflect::{std_traits::ReflectDefault, Reflect};
22
23#[cfg(feature = "custom_cursor")]
24pub use crate::cursor::{CustomCursor, CustomCursorImage};
25
26/// System sets for cursor-related systems.
27#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
28#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
29pub enum CursorSystems {
30    /// Reads changes to [`CursorIcon`] and queues the corresponding cursor to
31    /// be applied to the window by the windowing backend.
32    ///
33    /// Order your systems before this set to set [`CursorIcon`] and have it
34    /// take effect on the same frame.
35    Update,
36}
37
38/// Insert into a window entity to set the cursor for that window.
39#[derive(Component, Debug, Clone, PartialEq, Eq)]
40#[cfg_attr(
41    feature = "bevy_reflect",
42    derive(Reflect),
43    reflect(Component, Debug, Default, PartialEq, Clone)
44)]
45pub enum CursorIcon {
46    #[cfg(feature = "custom_cursor")]
47    /// Custom cursor image.
48    Custom(CustomCursor),
49    /// System provided cursor icon.
50    System(SystemCursorIcon),
51}
52
53impl Default for CursorIcon {
54    fn default() -> Self {
55        CursorIcon::System(Default::default())
56    }
57}
58
59impl From<SystemCursorIcon> for CursorIcon {
60    fn from(icon: SystemCursorIcon) -> Self {
61        CursorIcon::System(icon)
62    }
63}
64
65impl CursorIcon {
66    /// Returns the system cursor icon if this is a system cursor.
67    pub fn as_system(&self) -> Option<&SystemCursorIcon> {
68        #[cfg(feature = "custom_cursor")]
69        {
70            if let CursorIcon::System(icon) = self {
71                Some(icon)
72            } else {
73                None
74            }
75        }
76        #[cfg(not(feature = "custom_cursor"))]
77        {
78            let CursorIcon::System(icon) = self;
79            Some(icon)
80        }
81    }
82}