Skip to content

Commit 4d42534

Browse files
authored
Move CustomCursor to bevy_winit/src/custom_cursor.rs (#17381)
# Objective - Follow up work from #17121 (comment) to keep the `cursor.rs` file more manageable. ## Solution - Move `CustomCursor` and make it compile. ## Testing - Ran the example: `cargo run --example custom_cursor_image --features=custom_cursor` - CI
1 parent e8e2426 commit 4d42534

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

crates/bevy_winit/src/cursor.rs

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
};
1616
use bevy_app::{App, Last, Plugin};
1717
#[cfg(feature = "custom_cursor")]
18-
use bevy_asset::{Assets, Handle};
18+
use bevy_asset::Assets;
1919
#[cfg(feature = "custom_cursor")]
2020
use bevy_ecs::system::Res;
2121
use bevy_ecs::{
@@ -29,15 +29,16 @@ use bevy_ecs::{
2929
world::{OnRemove, Ref},
3030
};
3131
#[cfg(feature = "custom_cursor")]
32-
use bevy_image::{Image, TextureAtlas, TextureAtlasLayout};
33-
#[cfg(feature = "custom_cursor")]
34-
use bevy_math::URect;
32+
use bevy_image::{Image, TextureAtlasLayout};
3533
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
3634
use bevy_utils::HashSet;
3735
use bevy_window::{SystemCursorIcon, Window};
3836
#[cfg(feature = "custom_cursor")]
3937
use tracing::warn;
4038

39+
#[cfg(feature = "custom_cursor")]
40+
pub use crate::custom_cursor::CustomCursor;
41+
4142
pub(crate) struct CursorPlugin;
4243

4344
impl Plugin for CursorPlugin {
@@ -75,51 +76,6 @@ impl From<SystemCursorIcon> for CursorIcon {
7576
}
7677
}
7778

78-
#[cfg(feature = "custom_cursor")]
79-
impl From<CustomCursor> for CursorIcon {
80-
fn from(cursor: CustomCursor) -> Self {
81-
CursorIcon::Custom(cursor)
82-
}
83-
}
84-
85-
#[cfg(feature = "custom_cursor")]
86-
/// Custom cursor image data.
87-
#[derive(Debug, Clone, Reflect, PartialEq, Eq, Hash)]
88-
pub enum CustomCursor {
89-
/// Image to use as a cursor.
90-
Image {
91-
/// The image must be in 8 bit int or 32 bit float rgba. PNG images
92-
/// work well for this.
93-
handle: Handle<Image>,
94-
/// The (optional) texture atlas used to render the image.
95-
texture_atlas: Option<TextureAtlas>,
96-
/// Whether the image should be flipped along its x-axis.
97-
flip_x: bool,
98-
/// Whether the image should be flipped along its y-axis.
99-
flip_y: bool,
100-
/// An optional rectangle representing the region of the image to
101-
/// render, instead of rendering the full image. This is an easy one-off
102-
/// alternative to using a [`TextureAtlas`].
103-
///
104-
/// When used with a [`TextureAtlas`], the rect is offset by the atlas's
105-
/// minimal (top-left) corner position.
106-
rect: Option<URect>,
107-
/// X and Y coordinates of the hotspot in pixels. The hotspot must be
108-
/// within the image bounds.
109-
hotspot: (u16, u16),
110-
},
111-
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
112-
/// A URL to an image to use as the cursor.
113-
Url {
114-
/// Web URL to an image to use as the cursor. PNGs preferred. Cursor
115-
/// creation can fail if the image is invalid or not reachable.
116-
url: String,
117-
/// X and Y coordinates of the hotspot in pixels. The hotspot must be
118-
/// within the image bounds.
119-
hotspot: (u16, u16),
120-
},
121-
}
122-
12379
fn update_cursors(
12480
mut commands: Commands,
12581
windows: Query<(Entity, Ref<CursorIcon>), With<Window>>,

crates/bevy_winit/src/custom_cursor.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
11
use bevy_app::{App, Plugin};
2-
use bevy_asset::Assets;
2+
use bevy_asset::{Assets, Handle};
33
use bevy_image::{Image, TextureAtlas, TextureAtlasLayout, TextureAtlasPlugin};
44
use bevy_math::{ops, Rect, URect, UVec2, Vec2};
5+
use bevy_reflect::Reflect;
56
use wgpu_types::TextureFormat;
67

7-
use crate::state::CustomCursorCache;
8+
use crate::{cursor::CursorIcon, state::CustomCursorCache};
9+
10+
/// Custom cursor image data.
11+
#[derive(Debug, Clone, Reflect, PartialEq, Eq, Hash)]
12+
pub enum CustomCursor {
13+
/// Image to use as a cursor.
14+
Image {
15+
/// The image must be in 8 bit int or 32 bit float rgba. PNG images
16+
/// work well for this.
17+
handle: Handle<Image>,
18+
/// The (optional) texture atlas used to render the image.
19+
texture_atlas: Option<TextureAtlas>,
20+
/// Whether the image should be flipped along its x-axis.
21+
flip_x: bool,
22+
/// Whether the image should be flipped along its y-axis.
23+
flip_y: bool,
24+
/// An optional rectangle representing the region of the image to
25+
/// render, instead of rendering the full image. This is an easy one-off
26+
/// alternative to using a [`TextureAtlas`].
27+
///
28+
/// When used with a [`TextureAtlas`], the rect is offset by the atlas's
29+
/// minimal (top-left) corner position.
30+
rect: Option<URect>,
31+
/// X and Y coordinates of the hotspot in pixels. The hotspot must be
32+
/// within the image bounds.
33+
hotspot: (u16, u16),
34+
},
35+
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
36+
/// A URL to an image to use as the cursor.
37+
Url {
38+
/// Web URL to an image to use as the cursor. PNGs preferred. Cursor
39+
/// creation can fail if the image is invalid or not reachable.
40+
url: String,
41+
/// X and Y coordinates of the hotspot in pixels. The hotspot must be
42+
/// within the image bounds.
43+
hotspot: (u16, u16),
44+
},
45+
}
46+
47+
impl From<CustomCursor> for CursorIcon {
48+
fn from(cursor: CustomCursor) -> Self {
49+
CursorIcon::Custom(cursor)
50+
}
51+
}
852

953
/// Adds support for custom cursors.
1054
pub(crate) struct CustomCursorPlugin;

0 commit comments

Comments
 (0)