# core/pointercursor (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/pointercursor/

Mouse cursor. (PointerCursor of vanilla.js core/pointercursor.js)
- The engine holds one. (Engine::get_pointer_cursor_mut) When on (set_enabled(true)), the engine sets the cursor of the window or canvas at the end of every frame.
  When off (default), it does nothing, same as before.
- Over a UIButton (including while pressing), it is the button's cursor (initially the pointer hand), otherwise the cursor the screen set with set_screen_cursor.
- Names are CSS cursor names. (pointer, crosshair, not-allowed, grab, grabbing, text, move, help, wait, progress, none, default)
  Desktop converts them to winit cursor icons, and the web writes them to the canvas cursor style.
- When an image cursor is added with register_image_cursor, the web uses that image (1x and 2x) as a CSS value, and desktop uses a window cursor made from that image file (relative to the asset folder).
  (on desktop, the CSS cursor given with it while it is being made and if the image cannot be read)
- Not touched on finger devices (pointer: coarse). Passed to the platform only when it changes.
- The original apply writes directly to the global engine's canvas (readEngineCanvas). In the Rust version the engine receives the changed value with take_cursor_change and passes it to the platform. (the cursor is written to the platform window the engine holds)

Example: Turning on the engine cursor and making the screen cursor a crosshair

```rust
use vanilla::{Engine, EngineConfiguration};

let engine = Engine::new(EngineConfiguration::new());
let mut pointer_cursor = engine.get_pointer_cursor_mut();
pointer_cursor.set_enabled(true);
pointer_cursor.set_screen_cursor("crosshair");
drop(pointer_cursor);
assert_eq!(engine.get_pointer_cursor().get_screen_cursor(), "crosshair");
```

## CursorValue (struct) (Clone, PartialEq, Debug)

Cursor to pass to the platform. (the CSS value for the web, the CSS name for desktop, the image URL and hotspot for an image cursor)

Example: Values of an image cursor and a CSS name cursor

```rust
use vanilla::PointerCursor;

let mut pointer_cursor = PointerCursor::new();
pointer_cursor.register_image_cursor("icon", "examples/assets/atlas/icons.png", "", 3.4, 2.6, "pointer");
let image_cursor_value = pointer_cursor.read_cursor_value("icon");
assert_eq!(image_cursor_value.css_value, "url(\"examples/assets/atlas/icons.png\") 3 3, pointer");
assert_eq!(image_cursor_value.fallback_name, "pointer");
assert_eq!((image_cursor_value.hotspot_x, image_cursor_value.hotspot_y), (3.0, 3.0));

let css_cursor_value = pointer_cursor.read_cursor_value("grab");
assert_eq!(css_cursor_value.css_value, "grab");
assert!(css_cursor_value.image_url.is_empty());
assert_eq!(pointer_cursor.read_cursor_value("unknown").css_value, "");
```

- `pub css_value: String` (field, since 0.4.0): CSS value for the web canvas cursor style. (url or -webkit-image-set with hotspot and CSS cursor for an image cursor, empty text for the default arrow)
  - returns String
- `pub fallback_name: String` (field, since 0.4.0): CSS cursor name used when the image cannot be used and by desktop. (empty text for the default arrow)
  - returns String
- `pub image_url: String` (field, since 0.4.0): 1x image URL of the image cursor. (empty text if not an image cursor)
  - returns String
- `pub image_url_2x: String` (field, since 0.4.0): 2x image URL of the image cursor. (empty text if none)
  - returns String
- `pub hotspot_x: f64` (field, since 0.4.0): Hotspot relative to the 1x image. (rounded like the original)
  - returns f64
- `pub hotspot_y: f64` (field, since 0.4.0): y of the hotspot relative to the 1x image. (pixels, rounded like the original, 0 if not an image cursor)
  - returns f64

## PointerCursor (struct) (Default)

Mouse cursor. (when on, picks the cursor over buttons and for the screen and passes it to the platform only when it changes, the engine holds one)

Example: Taking it out only when the screen cursor and the over-button cursor change

```rust
use vanilla::PointerCursor;

let mut pointer_cursor = PointerCursor::new();
assert_eq!(pointer_cursor.take_cursor_change(false), None);
pointer_cursor.set_enabled(true);
pointer_cursor.set_screen_cursor("crosshair");
assert_eq!(pointer_cursor.take_cursor_change(false).unwrap().css_value, "crosshair");
assert_eq!(pointer_cursor.take_cursor_change(false), None);
pointer_cursor.mark_over_button();
assert_eq!(pointer_cursor.take_cursor_change(false).unwrap().css_value, "pointer");
```

- `pub fn new() -> PointerCursor` (constructor, since 0.4.0): Constructor.
  - returns PointerCursor: New cursor, off. (pointer over buttons, default for the default, default for the screen)
- `pub fn request_element_cursor(&self, cursor_name: &str)` (method, since 0.4.0): Requests a cursor for a document element. (this frame only. In the original the browser shows the CSS cursor of the DOM element. Shown regardless of the enabled setting)
  - `cursor_name` &str: CSS cursor name to show. (such as col-resize, passed to the platform as is)
- `pub fn set_enabled(&mut self, is_enabled: bool)` (method, since 0.4.0): Enabled setting. (when turned off, returns to the default cursor next frame)
  - `is_enabled` bool: true makes the engine set the cursor at the end of every frame. (default false)
- `pub fn is_enabled(&self) -> bool` (method, since 0.4.0): Whether enabled.
  - returns bool: true after set_enabled(true).
- `pub fn mark_over_button(&self)` (method, since 0.4.0): Reports that the mouse is over a button. (the hovered button calls it every frame, &self because components only borrow the engine to read)
- `pub fn set_screen_cursor(&mut self, cursor_name: &str)` (method, since 0.4.0): Sets the screen cursor. (The cursor shown when not over a button)
  - `cursor_name` &str: A CSS cursor name or a name added with register_image_cursor. (the default cursor if default)
- `pub fn get_screen_cursor(&self) -> &str` (method, since 0.4.0): Returns the screen cursor.
  - returns &str: Screen cursor name. (default is default)
- `pub fn set_cursor_names(&mut self, button_cursor_name: &str, default_cursor_name: &str)` (method, since 0.4.0): Sets the over-button and default cursors.
  - `button_cursor_name` &str: Cursor name to show over buttons (including while pressing). (default pointer)
  - `default_cursor_name` &str: Cursor name to show instead when the screen cursor is default. (default default)
- `pub fn get_button_cursor_name(&self) -> &str` (method, since 0.4.0): Returns the button-hover cursor.
  - returns &str: Over-button cursor name. (default pointer)
- `pub fn get_default_cursor_name(&self) -> &str` (method, since 0.4.0): Returns the default cursor.
  - returns &str: Default cursor name. (default default)
- `pub fn is_coarse_pointer() -> bool` (static, since 0.4.0): Whether the primary pointer is a finger. (the original static isCoarsePointer, CSS pointer: coarse. On desktop, true in Windows tablet mode or on touch devices without a mouse and on Linux devices with only a touchscreen, false on macOS)
  - returns bool: true if the primary pointer is a finger. (returns the platform's is_coarse_pointer as is)
- `pub fn register_image_cursor(&mut self, name: &str, url: &str, url_2x: &str, hotspot_x: f64, hotspot_y: f64, fallback_cursor_name: &str)` (method, since 0.4.0): Adds an image cursor. (1x and 2x image URLs, hotspot relative to the 1x image, CSS cursor for when the image cannot be used and for desktop)
  - Adding again with the same name replaces it.
  - `name` &str: Cursor name. (used for set_screen_cursor, set_cursor_names)
  - `url` &str: 1x image URL. (CSS url on the web, an image file relative to the asset folder on desktop)
  - `url_2x` &str: 2x image URL. Only the 1x image is used if empty.
  - `hotspot_x` f64: Hotspot x relative to the 1x image. (pixels, rounded)
  - `hotspot_y` f64: Hotspot y relative to the 1x image. (pixels, rounded)
  - `fallback_cursor_name` &str: CSS cursor name to show when the image cannot be used and while desktop is making the image.

  Example: Adding an image cursor and using it as the screen cursor

  ```rust
  use vanilla::PointerCursor;
  
  let mut pointer_cursor = PointerCursor::new();
  pointer_cursor.register_image_cursor("icon", "examples/assets/atlas/icons.png", "", 0.0, 0.0, "default");
  pointer_cursor.set_enabled(true);
  pointer_cursor.set_screen_cursor("icon");
  let cursor_value = pointer_cursor.take_cursor_change(false).unwrap();
  assert_eq!(cursor_value.image_url, "examples/assets/atlas/icons.png");
  ```

- `pub fn read_cursor_value(&self, cursor_name: &str) -> CursorValue` (method, since 0.4.0): Turns a cursor name into the value to pass to the platform. (that value for an image cursor, as is for a usable CSS name, otherwise the default arrow)
  - `cursor_name` &str: Cursor name. (an image cursor name or a CSS cursor name)
  - returns CursorValue: Value to pass to the platform. (css_value and fallback_name are empty text for the default arrow)
- `pub fn take_cursor_change(&mut self, is_coarse_pointer: bool) -> Option<CursorValue>` (method, since 0.4.0): Takes out this frame's cursor if it changed. (called by the engine at the end of every frame, clears the button mark)
  - The cursor requested by a document element comes first, then the over-button cursor if enabled, otherwise the screen cursor.
  - `is_coarse_pointer` bool: Whether the primary pointer is a finger. (true changes nothing)
  - returns Option<CursorValue>: The new value if it differs from the value passed last time. None if the same, on a finger device, or off with no document element cursor either.
