bevy/crates/bevy_window/src/system_cursor.rs
Eero Lehtinen 47c4e3084a
Add custom cursors (#14284)
# 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>
2024-08-12 15:49:03 +00:00

219 lines
8.1 KiB
Rust

// This file contains a portion of the CSS Basic User Interface Module Level 3
// specification. In particular, the names for the cursor from the #cursor
// section and documentation for some of the variants were taken.
//
// The original document is https://www.w3.org/TR/css-ui-3/#cursor.
// Copyright © 2018 W3C® (MIT, ERCIM, Keio, Beihang)
//
// These documents were used under the terms of the following license. This W3C
// license as well as the W3C short notice apply to the `CursorIcon` enum's
// variants and documentation attached to them.
// --------- BEGINNING OF W3C LICENSE
// --------------------------------------------------------------
//
// License
//
// By obtaining and/or copying this work, you (the licensee) agree that you have
// read, understood, and will comply with the following terms and conditions.
//
// Permission to copy, modify, and distribute this work, with or without
// modification, for any purpose and without fee or royalty is hereby granted,
// provided that you include the following on ALL copies of the work or portions
// thereof, including modifications:
//
// - The full text of this NOTICE in a location viewable to users of the
// redistributed or derivative work.
// - Any pre-existing intellectual property disclaimers, notices, or terms and
// conditions. If none exist, the W3C Software and Document Short Notice
// should be included.
// - Notice of any changes or modifications, through a copyright statement on
// the new code or document such as "This software or document includes
// material copied from or derived from [title and URI of the W3C document].
// Copyright © [YEAR] W3C® (MIT, ERCIM, Keio, Beihang)."
//
// Disclaimers
//
// THIS WORK IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS
// OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO, WARRANTIES
// OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
// THE SOFTWARE OR DOCUMENT WILL NOT INFRINGE ANY THIRD PARTY PATENTS,
// COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
//
// COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR
// CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENT.
//
// The name and trademarks of copyright holders may NOT be used in advertising
// or publicity pertaining to the work without specific, written prior
// permission. Title to copyright in this work will at all times remain with
// copyright holders.
//
// --------- END OF W3C LICENSE
// --------------------------------------------------------------------
// --------- BEGINNING OF W3C SHORT NOTICE
// ---------------------------------------------------------
//
// winit: https://github.com/rust-windowing/cursor-icon
//
// Copyright © 2023 World Wide Web Consortium, (Massachusetts Institute of
// Technology, European Research Consortium for Informatics and Mathematics,
// Keio University, Beihang). All Rights Reserved. This work is distributed
// under the W3C® Software License [1] in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// [1] http://www.w3.org/Consortium/Legal/copyright-software
//
// --------- END OF W3C SHORT NOTICE
// --------------------------------------------------------------
use bevy_reflect::{prelude::ReflectDefault, Reflect};
#[cfg(feature = "serialize")]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// The icon to display for a window.
///
/// Examples of all of these cursors can be found [here](https://www.w3schools.com/cssref/playit.php?filename=playcss_cursor&preval=crosshair).
/// This `enum` is simply a copy of a similar `enum` found in [`winit`](https://docs.rs/winit/latest/winit/window/enum.CursorIcon.html).
/// `winit`, in turn, is based upon the [CSS3 UI spec](https://www.w3.org/TR/css-ui-3/#cursor).
///
/// See the [`window_settings`] example for usage.
///
/// [`window_settings`]: https://github.com/bevyengine/bevy/blob/latest/examples/window/window_settings.rs
#[derive(Default, Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
#[reflect(Debug, PartialEq, Default)]
pub enum SystemCursorIcon {
/// The platform-dependent default cursor. Often rendered as arrow.
#[default]
Default,
/// A context menu is available for the object under the cursor. Often
/// rendered as an arrow with a small menu-like graphic next to it.
ContextMenu,
/// Help is available for the object under the cursor. Often rendered as a
/// question mark or a balloon.
Help,
/// The cursor is a pointer that indicates a link. Often rendered as the
/// backside of a hand with the index finger extended.
Pointer,
/// A progress indicator. The program is performing some processing, but is
/// different from [`SystemCursorIcon::Wait`] in that the user may still interact
/// with the program.
Progress,
/// Indicates that the program is busy and the user should wait. Often
/// rendered as a watch or hourglass.
Wait,
/// Indicates that a cell or set of cells may be selected. Often rendered as
/// a thick plus-sign with a dot in the middle.
Cell,
/// A simple crosshair (e.g., short line segments resembling a "+" sign).
/// Often used to indicate a two dimensional bitmap selection mode.
Crosshair,
/// Indicates text that may be selected. Often rendered as an I-beam.
Text,
/// Indicates vertical-text that may be selected. Often rendered as a
/// horizontal I-beam.
VerticalText,
/// Indicates an alias of/shortcut to something is to be created. Often
/// rendered as an arrow with a small curved arrow next to it.
Alias,
/// Indicates something is to be copied. Often rendered as an arrow with a
/// small plus sign next to it.
Copy,
/// Indicates something is to be moved.
Move,
/// Indicates that the dragged item cannot be dropped at the current cursor
/// location. Often rendered as a hand or pointer with a small circle with a
/// line through it.
NoDrop,
/// Indicates that the requested action will not be carried out. Often
/// rendered as a circle with a line through it.
NotAllowed,
/// Indicates that something can be grabbed (dragged to be moved). Often
/// rendered as the backside of an open hand.
Grab,
/// Indicates that something is being grabbed (dragged to be moved). Often
/// rendered as the backside of a hand with fingers closed mostly out of
/// view.
Grabbing,
/// The east border to be moved.
EResize,
/// The north border to be moved.
NResize,
/// The north-east corner to be moved.
NeResize,
/// The north-west corner to be moved.
NwResize,
/// The south border to be moved.
SResize,
/// The south-east corner to be moved.
SeResize,
/// The south-west corner to be moved.
SwResize,
/// The west border to be moved.
WResize,
/// The east and west borders to be moved.
EwResize,
/// The south and north borders to be moved.
NsResize,
/// The north-east and south-west corners to be moved.
NeswResize,
/// The north-west and south-east corners to be moved.
NwseResize,
/// Indicates that the item/column can be resized horizontally. Often
/// rendered as arrows pointing left and right with a vertical bar
/// separating them.
ColResize,
/// Indicates that the item/row can be resized vertically. Often rendered as
/// arrows pointing up and down with a horizontal bar separating them.
RowResize,
/// Indicates that the something can be scrolled in any direction. Often
/// rendered as arrows pointing up, down, left, and right with a dot in the
/// middle.
AllScroll,
/// Indicates that something can be zoomed in. Often rendered as a
/// magnifying glass with a "+" in the center of the glass.
ZoomIn,
/// Indicates that something can be zoomed in. Often rendered as a
/// magnifying glass with a "-" in the center of the glass.
ZoomOut,
}