diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 68e6dad1c03bf9..9c20c16e3eee91 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -48,10 +48,7 @@ pub mod prelude { spatial_bundle::SpatialBundle, texture::{image_texture_conversion::IntoDynamicImageError, Image, ImagePlugin}, view::{ - window::{ - cursor::{CursorIcon, CustomCursor}, - system_cursor::SystemCursorIcon, - }, + window::cursor::{CursorIcon, CustomCursor}, InheritedVisibility, Msaa, ViewVisibility, Visibility, VisibilityBundle, }, ExtractSchedule, diff --git a/crates/bevy_render/src/view/window/cursor.rs b/crates/bevy_render/src/view/window/cursor.rs index 5b4984fabf87c5..c17ba44ec12471 100644 --- a/crates/bevy_render/src/view/window/cursor.rs +++ b/crates/bevy_render/src/view/window/cursor.rs @@ -8,21 +8,17 @@ use bevy_ecs::{ }; use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_utils::tracing::warn; -use bevy_window::Window; -use bevy_winit::{CursorSource, CustomCursorCache, CustomCursorCacheKey, PendingCursor}; +use bevy_window::{SystemCursorIcon, Window}; +use bevy_winit::{ + convert_system_cursor_icon, CursorSource, CustomCursorCache, CustomCursorCacheKey, + PendingCursor, +}; use wgpu::TextureFormat; use crate::prelude::Image; -use super::system_cursor::SystemCursorIcon; - /// Insert into a window entity to set the cursor for that window. #[derive(Component, Debug, Clone, Reflect, PartialEq, Eq)] -#[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), - reflect(Serialize, Deserialize) -)] #[reflect(Component, Debug, Default)] pub enum CursorIcon { /// Custom cursor image. @@ -33,7 +29,7 @@ pub enum CursorIcon { impl Default for CursorIcon { fn default() -> Self { - CursorIcon::System(SystemCursorIcon::Default) + CursorIcon::System(Default::default()) } } @@ -51,11 +47,6 @@ impl From for CursorIcon { /// Custom cursor image data. #[derive(Debug, Clone, Reflect, PartialEq, Eq, Hash)] -#[cfg_attr( - feature = "serialize", - derive(serde::Serialize, serde::Deserialize), - reflect(Serialize, Deserialize) -)] pub enum CustomCursor { /// Image to use as a cursor. Image { @@ -147,7 +138,7 @@ pub fn update_cursors( } } CursorIcon::System(system_cursor_icon) => { - CursorSource::System(convert_system_cursor(*system_cursor_icon)) + CursorSource::System(convert_system_cursor_icon(*system_cursor_icon)) } }; @@ -180,42 +171,3 @@ fn image_to_rgba_pixels(image: &Image) -> Option> { _ => None, } } - -pub fn convert_system_cursor(system_cursor_icon: SystemCursorIcon) -> bevy_winit::CursorIcon { - match system_cursor_icon { - SystemCursorIcon::Crosshair => bevy_winit::CursorIcon::Crosshair, - SystemCursorIcon::Pointer => bevy_winit::CursorIcon::Pointer, - SystemCursorIcon::Move => bevy_winit::CursorIcon::Move, - SystemCursorIcon::Text => bevy_winit::CursorIcon::Text, - SystemCursorIcon::Wait => bevy_winit::CursorIcon::Wait, - SystemCursorIcon::Help => bevy_winit::CursorIcon::Help, - SystemCursorIcon::Progress => bevy_winit::CursorIcon::Progress, - SystemCursorIcon::NotAllowed => bevy_winit::CursorIcon::NotAllowed, - SystemCursorIcon::ContextMenu => bevy_winit::CursorIcon::ContextMenu, - SystemCursorIcon::Cell => bevy_winit::CursorIcon::Cell, - SystemCursorIcon::VerticalText => bevy_winit::CursorIcon::VerticalText, - SystemCursorIcon::Alias => bevy_winit::CursorIcon::Alias, - SystemCursorIcon::Copy => bevy_winit::CursorIcon::Copy, - SystemCursorIcon::NoDrop => bevy_winit::CursorIcon::NoDrop, - SystemCursorIcon::Grab => bevy_winit::CursorIcon::Grab, - SystemCursorIcon::Grabbing => bevy_winit::CursorIcon::Grabbing, - SystemCursorIcon::AllScroll => bevy_winit::CursorIcon::AllScroll, - SystemCursorIcon::ZoomIn => bevy_winit::CursorIcon::ZoomIn, - SystemCursorIcon::ZoomOut => bevy_winit::CursorIcon::ZoomOut, - SystemCursorIcon::EResize => bevy_winit::CursorIcon::EResize, - SystemCursorIcon::NResize => bevy_winit::CursorIcon::NResize, - SystemCursorIcon::NeResize => bevy_winit::CursorIcon::NeResize, - SystemCursorIcon::NwResize => bevy_winit::CursorIcon::NwResize, - SystemCursorIcon::SResize => bevy_winit::CursorIcon::SResize, - SystemCursorIcon::SeResize => bevy_winit::CursorIcon::SeResize, - SystemCursorIcon::SwResize => bevy_winit::CursorIcon::SwResize, - SystemCursorIcon::WResize => bevy_winit::CursorIcon::WResize, - SystemCursorIcon::EwResize => bevy_winit::CursorIcon::EwResize, - SystemCursorIcon::NsResize => bevy_winit::CursorIcon::NsResize, - SystemCursorIcon::NeswResize => bevy_winit::CursorIcon::NeswResize, - SystemCursorIcon::NwseResize => bevy_winit::CursorIcon::NwseResize, - SystemCursorIcon::ColResize => bevy_winit::CursorIcon::ColResize, - SystemCursorIcon::RowResize => bevy_winit::CursorIcon::RowResize, - _ => bevy_winit::CursorIcon::Default, - } -} diff --git a/crates/bevy_render/src/view/window/mod.rs b/crates/bevy_render/src/view/window/mod.rs index a89b5696038ac6..4121e718e8bd15 100644 --- a/crates/bevy_render/src/view/window/mod.rs +++ b/crates/bevy_render/src/view/window/mod.rs @@ -14,6 +14,7 @@ use bevy_utils::{default, tracing::debug, HashSet}; use bevy_window::{ CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosing, }; +use bevy_winit::CustomCursorCache; use std::{ num::NonZeroU32, ops::{Deref, DerefMut}, @@ -26,7 +27,6 @@ use wgpu::{ pub mod cursor; pub mod screenshot; -pub mod system_cursor; use screenshot::{ ScreenshotManager, ScreenshotPlugin, ScreenshotPreparedState, ScreenshotToScreenPipeline, @@ -41,6 +41,7 @@ pub struct WindowRenderPlugin; impl Plugin for WindowRenderPlugin { fn build(&self, app: &mut App) { app.add_plugins(ScreenshotPlugin) + .init_resource::() .add_systems(Last, update_cursors); if let Some(render_app) = app.get_sub_app_mut(RenderApp) { diff --git a/crates/bevy_window/src/lib.rs b/crates/bevy_window/src/lib.rs index 9daa1692d308b7..db7215c384691a 100644 --- a/crates/bevy_window/src/lib.rs +++ b/crates/bevy_window/src/lib.rs @@ -18,12 +18,14 @@ use bevy_a11y::Focus; mod event; mod raw_handle; mod system; +mod system_cursor; mod window; pub use crate::raw_handle::*; pub use event::*; pub use system::*; +pub use system_cursor::*; pub use window::*; #[allow(missing_docs)] diff --git a/crates/bevy_render/src/view/window/system_cursor.rs b/crates/bevy_window/src/system_cursor.rs similarity index 100% rename from crates/bevy_render/src/view/window/system_cursor.rs rename to crates/bevy_window/src/system_cursor.rs diff --git a/crates/bevy_winit/src/converters.rs b/crates/bevy_winit/src/converters.rs index fc9bc305f344e9..78b52b8eb4dbad 100644 --- a/crates/bevy_winit/src/converters.rs +++ b/crates/bevy_winit/src/converters.rs @@ -6,7 +6,7 @@ use bevy_input::{ ButtonState, }; use bevy_math::Vec2; -use bevy_window::{EnabledButtons, WindowLevel, WindowTheme}; +use bevy_window::{EnabledButtons, SystemCursorIcon, WindowLevel, WindowTheme}; use winit::keyboard::{Key, NamedKey, NativeKey}; pub fn convert_keyboard_input( @@ -628,6 +628,46 @@ pub fn convert_native_key(native_key: &NativeKey) -> bevy_input::keyboard::Nativ } } +/// Converts a [`SystemCursorIcon`] to a [`winit::window::CursorIcon`]. +pub fn convert_system_cursor_icon(cursor_icon: SystemCursorIcon) -> winit::window::CursorIcon { + match cursor_icon { + SystemCursorIcon::Crosshair => winit::window::CursorIcon::Crosshair, + SystemCursorIcon::Pointer => winit::window::CursorIcon::Pointer, + SystemCursorIcon::Move => winit::window::CursorIcon::Move, + SystemCursorIcon::Text => winit::window::CursorIcon::Text, + SystemCursorIcon::Wait => winit::window::CursorIcon::Wait, + SystemCursorIcon::Help => winit::window::CursorIcon::Help, + SystemCursorIcon::Progress => winit::window::CursorIcon::Progress, + SystemCursorIcon::NotAllowed => winit::window::CursorIcon::NotAllowed, + SystemCursorIcon::ContextMenu => winit::window::CursorIcon::ContextMenu, + SystemCursorIcon::Cell => winit::window::CursorIcon::Cell, + SystemCursorIcon::VerticalText => winit::window::CursorIcon::VerticalText, + SystemCursorIcon::Alias => winit::window::CursorIcon::Alias, + SystemCursorIcon::Copy => winit::window::CursorIcon::Copy, + SystemCursorIcon::NoDrop => winit::window::CursorIcon::NoDrop, + SystemCursorIcon::Grab => winit::window::CursorIcon::Grab, + SystemCursorIcon::Grabbing => winit::window::CursorIcon::Grabbing, + SystemCursorIcon::AllScroll => winit::window::CursorIcon::AllScroll, + SystemCursorIcon::ZoomIn => winit::window::CursorIcon::ZoomIn, + SystemCursorIcon::ZoomOut => winit::window::CursorIcon::ZoomOut, + SystemCursorIcon::EResize => winit::window::CursorIcon::EResize, + SystemCursorIcon::NResize => winit::window::CursorIcon::NResize, + SystemCursorIcon::NeResize => winit::window::CursorIcon::NeResize, + SystemCursorIcon::NwResize => winit::window::CursorIcon::NwResize, + SystemCursorIcon::SResize => winit::window::CursorIcon::SResize, + SystemCursorIcon::SeResize => winit::window::CursorIcon::SeResize, + SystemCursorIcon::SwResize => winit::window::CursorIcon::SwResize, + SystemCursorIcon::WResize => winit::window::CursorIcon::WResize, + SystemCursorIcon::EwResize => winit::window::CursorIcon::EwResize, + SystemCursorIcon::NsResize => winit::window::CursorIcon::NsResize, + SystemCursorIcon::NeswResize => winit::window::CursorIcon::NeswResize, + SystemCursorIcon::NwseResize => winit::window::CursorIcon::NwseResize, + SystemCursorIcon::ColResize => winit::window::CursorIcon::ColResize, + SystemCursorIcon::RowResize => winit::window::CursorIcon::RowResize, + _ => winit::window::CursorIcon::Default, + } +} + pub fn convert_window_level(window_level: WindowLevel) -> winit::window::WindowLevel { match window_level { WindowLevel::AlwaysOnBottom => winit::window::WindowLevel::AlwaysOnBottom, diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 25c0b0915738c2..28f0877f12ed22 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -24,6 +24,7 @@ use bevy_app::{App, Last, Plugin}; use bevy_ecs::prelude::*; #[allow(deprecated)] use bevy_window::{exit_on_all_closed, Window, WindowCreated}; +pub use converters::convert_system_cursor_icon; pub use state::{CursorSource, CustomCursorCache, CustomCursorCacheKey, PendingCursor}; pub use system::create_windows; use system::{changed_windows, despawn_windows}; diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index 4a85ac63ddffe7..8b4c027a7fe0c1 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -5,7 +5,7 @@ use bevy::{ core::FrameCount, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, - window::{CursorGrabMode, PresentMode, WindowLevel, WindowTheme}, + window::{CursorGrabMode, PresentMode, SystemCursorIcon, WindowLevel, WindowTheme}, }; fn main() {