Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_feathers/src/cursor.rs
6595 views
1
//! Provides a way to automatically set the mouse cursor based on hovered entity.
2
use bevy_app::{App, Plugin, PreUpdate};
3
use bevy_ecs::{
4
component::Component,
5
entity::Entity,
6
hierarchy::ChildOf,
7
query::{With, Without},
8
reflect::{ReflectComponent, ReflectResource},
9
resource::Resource,
10
schedule::IntoScheduleConfigs,
11
system::{Commands, Query, Res},
12
};
13
use bevy_picking::{hover::HoverMap, pointer::PointerId, PickingSystems};
14
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
15
#[cfg(feature = "custom_cursor")]
16
use bevy_window::CustomCursor;
17
use bevy_window::{CursorIcon, SystemCursorIcon, Window};
18
19
/// A resource that specifies the cursor icon to be used when the mouse is not hovering over
20
/// any other entity. This is used to set the default cursor icon for the window.
21
#[derive(Resource, Debug, Clone, Default, Reflect)]
22
#[reflect(Resource, Debug, Default)]
23
pub struct DefaultCursor(pub EntityCursor);
24
25
/// A component that specifies the cursor shape to be used when the pointer hovers over an entity.
26
/// This is copied to the windows's [`CursorIcon`] component.
27
///
28
/// This is effectively the same type as [`CustomCursor`] but with different methods, and used
29
/// in different places.
30
#[derive(Component, Debug, Clone, Reflect, PartialEq, Eq)]
31
#[reflect(Component, Debug, Default, PartialEq, Clone)]
32
pub enum EntityCursor {
33
#[cfg(feature = "custom_cursor")]
34
/// Custom cursor image.
35
Custom(CustomCursor),
36
/// System provided cursor icon.
37
System(SystemCursorIcon),
38
}
39
40
impl EntityCursor {
41
/// Convert the [`EntityCursor`] to a [`CursorIcon`] so that it can be inserted into a
42
/// window.
43
pub fn to_cursor_icon(&self) -> CursorIcon {
44
match self {
45
#[cfg(feature = "custom_cursor")]
46
EntityCursor::Custom(custom_cursor) => CursorIcon::Custom(custom_cursor.clone()),
47
EntityCursor::System(icon) => CursorIcon::from(*icon),
48
}
49
}
50
51
/// Compare the [`EntityCursor`] to a [`CursorIcon`] so that we can see whether or not
52
/// the window cursor needs to be changed.
53
pub fn eq_cursor_icon(&self, cursor_icon: &CursorIcon) -> bool {
54
// If feature custom_cursor is not enabled in bevy_feathers, we can't know if it is or not
55
// in bevy_window. So we use the wrapper function `as_system` to let bevy_window check its own feature.
56
// Otherwise it is not possible to have a match that both covers all cases and doesn't have unreachable
57
// branches under all feature combinations.
58
match (self, cursor_icon, cursor_icon.as_system()) {
59
#[cfg(feature = "custom_cursor")]
60
(EntityCursor::Custom(custom), CursorIcon::Custom(other), _) => custom == other,
61
(EntityCursor::System(system), _, Some(cursor_icon)) => *system == *cursor_icon,
62
_ => false,
63
}
64
}
65
}
66
67
impl Default for EntityCursor {
68
fn default() -> Self {
69
EntityCursor::System(Default::default())
70
}
71
}
72
73
/// System which updates the window cursor icon whenever the mouse hovers over an entity with
74
/// a [`CursorIcon`] component. If no entity is hovered, the cursor icon is set to
75
/// the cursor in the [`DefaultCursor`] resource.
76
pub(crate) fn update_cursor(
77
mut commands: Commands,
78
hover_map: Option<Res<HoverMap>>,
79
parent_query: Query<&ChildOf>,
80
cursor_query: Query<&EntityCursor, Without<Window>>,
81
q_windows: Query<(Entity, Option<&CursorIcon>), With<Window>>,
82
r_default_cursor: Res<DefaultCursor>,
83
) {
84
let cursor = hover_map
85
.and_then(|hover_map| match hover_map.get(&PointerId::Mouse) {
86
Some(hover_set) => hover_set.keys().find_map(|entity| {
87
cursor_query.get(*entity).ok().or_else(|| {
88
parent_query
89
.iter_ancestors(*entity)
90
.find_map(|e| cursor_query.get(e).ok())
91
})
92
}),
93
None => None,
94
})
95
.unwrap_or(&r_default_cursor.0);
96
97
for (entity, prev_cursor) in q_windows.iter() {
98
if let Some(prev_cursor) = prev_cursor
99
&& cursor.eq_cursor_icon(prev_cursor)
100
{
101
continue;
102
}
103
commands.entity(entity).insert(cursor.to_cursor_icon());
104
}
105
}
106
107
/// Plugin that supports automatically changing the cursor based on the hovered entity.
108
pub struct CursorIconPlugin;
109
110
impl Plugin for CursorIconPlugin {
111
fn build(&self, app: &mut App) {
112
if app.world().get_resource::<DefaultCursor>().is_none() {
113
app.init_resource::<DefaultCursor>();
114
}
115
app.add_systems(PreUpdate, update_cursor.in_set(PickingSystems::Last));
116
}
117
}
118
119