Skip to content

Commit

Permalink
Move system cursors back to bevy_window
Browse files Browse the repository at this point in the history
  • Loading branch information
eero-lehtinen committed Jul 18, 2024
1 parent 814f7cc commit 8538a3c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 62 deletions.
5 changes: 1 addition & 4 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 7 additions & 55 deletions crates/bevy_render/src/view/window/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,7 +29,7 @@ pub enum CursorIcon {

impl Default for CursorIcon {
fn default() -> Self {
CursorIcon::System(SystemCursorIcon::Default)
CursorIcon::System(Default::default())
}
}

Expand All @@ -51,11 +47,6 @@ impl From<CustomCursor> 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 {
Expand Down Expand Up @@ -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))
}
};

Expand Down Expand Up @@ -180,42 +171,3 @@ fn image_to_rgba_pixels(image: &Image) -> Option<Vec<u8>> {
_ => 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,
}
}
3 changes: 2 additions & 1 deletion crates/bevy_render/src/view/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -26,7 +27,6 @@ use wgpu::{

pub mod cursor;
pub mod screenshot;
pub mod system_cursor;

use screenshot::{
ScreenshotManager, ScreenshotPlugin, ScreenshotPreparedState, ScreenshotToScreenPipeline,
Expand All @@ -41,6 +41,7 @@ pub struct WindowRenderPlugin;
impl Plugin for WindowRenderPlugin {
fn build(&self, app: &mut App) {
app.add_plugins(ScreenshotPlugin)
.init_resource::<CustomCursorCache>()
.add_systems(Last, update_cursors);

if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
File renamed without changes.
42 changes: 41 additions & 1 deletion crates/bevy_winit/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion examples/window/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 8538a3c

Please sign in to comment.