# Objective - Add custom images as cursors - Fixes #9557 ## Solution - Change cursor type to accommodate both native and image cursors - I don't really like this solution because I couldn't use `Handle<Image>` directly. I would need to import `bevy_assets` and that causes a circular dependency. Alternatively we could use winit's `CustomCursor` smart pointers, but that seems hard because the event loop is needed to create those and is not easily accessable for users. So now I need to copy around rgba buffers which is sad. - I use a cache because especially on the web creating cursor images is really slow - Sorry to #14196 for yoinking, I just wanted to make a quick solution for myself and thought that I should probably share it too. Update: - Now uses `Handle<Image>`, reads rgba data in `bevy_render` and uses resources to send the data to `bevy_winit`, where the final cursors are created. ## Testing - Added example which works fine at least on Linux Wayland (winit side has been tested with all platforms). - I haven't tested if the url cursor works. ## Migration Guide - `CursorIcon` is no longer a field in `Window`, but a separate component can be inserted to a window entity. It has been changed to an enum that can hold custom images in addition to system icons. - `Cursor` is renamed to `CursorOptions` and `cursor` field of `Window` is renamed to `cursor_options` - `CursorIcon` is renamed to `SystemCursorIcon` --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Jan Hohenheim <jan@hohenheim.ch>
		
			
				
	
	
		
			31 lines
		
	
	
		
			855 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			855 B
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! Demonstrates how to grab and hide the mouse cursor.
 | 
						|
 | 
						|
use bevy::{prelude::*, window::CursorGrabMode};
 | 
						|
 | 
						|
fn main() {
 | 
						|
    App::new()
 | 
						|
        .add_plugins(DefaultPlugins)
 | 
						|
        .add_systems(Update, grab_mouse)
 | 
						|
        .run();
 | 
						|
}
 | 
						|
 | 
						|
// This system grabs the mouse when the left mouse button is pressed
 | 
						|
// and releases it when the escape key is pressed
 | 
						|
fn grab_mouse(
 | 
						|
    mut windows: Query<&mut Window>,
 | 
						|
    mouse: Res<ButtonInput<MouseButton>>,
 | 
						|
    key: Res<ButtonInput<KeyCode>>,
 | 
						|
) {
 | 
						|
    let mut window = windows.single_mut();
 | 
						|
 | 
						|
    if mouse.just_pressed(MouseButton::Left) {
 | 
						|
        window.cursor_options.visible = false;
 | 
						|
        window.cursor_options.grab_mode = CursorGrabMode::Locked;
 | 
						|
    }
 | 
						|
 | 
						|
    if key.just_pressed(KeyCode::Escape) {
 | 
						|
        window.cursor_options.visible = true;
 | 
						|
        window.cursor_options.grab_mode = CursorGrabMode::None;
 | 
						|
    }
 | 
						|
}
 |