# ui/uispinner (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uispinner/

Spinning loading indicator. (UISpinner of the original ui/uispinner.js)
- A standard spinner that places several dots around a circle with brightness rotating like a tail. (dot opacity 0.15 ~ 1)
- The original is a node that inherits WorldNode. The Rust version is a component attached to a node (host), and on attach, like the original constructor, it
  sets up the host (center pivot, size = (radius + dot radius) x 2) and creates dot nodes (rounded Paint) as children.
  UISpinner::create(node_tree, parent_id, options) takes the place of the original new UISpinner(options) + add_child.
- Usage:
  ```text
    let spinner_id = UISpinner::create(node_tree, panel_id, UISpinnerOptions { radius: 9.0, dot_count: 10, ..UISpinnerOptions::default() });
    node_tree[spinner_id].set_local_position(Vector2::create(x, y));
  ```
- Differences from the original:
  - set_dot_color sets the color and applies it to the dot nodes on the next tick. (the dot nodes cannot be reached while scene code holds this widget)
  - The phase is wrapped to 0 ~ 1. The tail calculation has a period of 1, so the visible result is the same, and precision does not degrade even after spinning a long time.
  - Dot node names are "SpinnerDot{index}" as in the original. The options object is the UISpinnerOptions struct.

## UISpinnerOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

Spinner settings. (original options = { radius = 9, dot_count = 10, dot_radius = 2, color?, rotation_speed = 1 })

Example: A four-dot spinner

```rust
use vanilla::{NodeTree, UISpinner, UISpinnerOptions, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let options = UISpinnerOptions {
	dot_count: 4,
	..UISpinnerOptions::default()
};
let spinner_id = UISpinner::create(&mut node_tree, root_id, options);
assert_eq!(node_tree[spinner_id].get_content_size(), Vector2::create(22.0, 22.0));
assert_eq!(node_tree[spinner_id].get_component::<UISpinner>().expect("spinner").get_dot_nodes().len(), 4);
```

- `pub radius: f64` (field, since 0.3.2): Radius of the circle the dots sit on.
  - returns f64
- `pub dot_count: usize` (field, since 0.3.2): Dot count. (default 10)
  - returns usize
- `pub dot_radius: f64` (field, since 0.3.2): Radius of one dot. (default 2)
  - returns f64
- `pub color: Color` (field, since 0.3.2): Dot color. (default white)
  - returns Color
- `pub rotation_speed: f64` (field, since 0.3.2): Turns per second.
  - returns f64

## UISpinner (struct) (Default, Component, UIDocumentNode)

Spinning loading indicator.

Example: A spinner shown over a panel

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, UISpinner, UISpinnerOptions, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
let spinner_id = UISpinner::create(&mut node_tree, panel_id, UISpinnerOptions { radius: 9.0, dot_count: 10, ..UISpinnerOptions::default() });
node_tree[spinner_id].set_local_position(Vector2::create(120.0, 80.0));
node_tree[spinner_id].get_component_mut::<UISpinner>().expect("spinner").set_dot_color("#ffcc00");
node_tree.tick(&engine, 0.1);
let dot_ids = node_tree[spinner_id].get_component::<UISpinner>().expect("spinner").get_dot_nodes().to_vec();
assert!((node_tree[dot_ids[1]].get_local_opacity() - 1.0).abs() < 0.0001);
```

- `pub fn new() -> UISpinner` (constructor, since 0.3.2): Constructor. (default settings)
  - returns UISpinner: Spinner before being attached. (UISpinnerOptions::default)
- `pub fn create_with_options(options: UISpinnerOptions) -> UISpinner` (static, since 0.3.2): Creates with settings. (attach to a node with node_tree.add_component)
  - `options` UISpinnerOptions: Spinner settings.
  - returns UISpinner: Spinner before being attached.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UISpinnerOptions) -> NodeId` (static, since 0.3.2): Creates the node. (creates and attaches a "UISpinner" node under parent_id. original new UISpinner(options) + add_child)
  - `node_tree` &mut NodeTree: Node tree.
  - `parent_id` NodeId: Parent node.
  - `options` UISpinnerOptions: Spinner settings.
  - returns NodeId: The created spinner node. (center pivot, size is (radius + dot radius) x 2)
- `pub fn calculate_dot_opacity(&self, dot_index: usize) -> f64` (method, since 0.3.2): Computes a dot's opacity. (the smaller the tail ratio, the dot order ratio minus the phase, the brighter)
  - If there are no dots (dot_count 0), the order ratio is treated as 0.
  - `dot_index` usize: Dot index. (from 0, index 0 is on the right of the circle and higher indices go clockwise)
  - returns f64: Dot opacity. (0.15 ~ 1)

  Example: At phase 0, dot 0 is the brightest

  ```rust
  use vanilla::UISpinner;
  
  let spinner = UISpinner::new();
  assert!((spinner.calculate_dot_opacity(0) - 1.0).abs() < 0.0001);
  assert!((spinner.calculate_dot_opacity(5) - 0.575).abs() < 0.0001);
  ```

- `pub fn set_dot_color(&mut self, dot_color: impl Into<Color>)` (method, since 0.3.2): Sets the dot color. (applied to the dot nodes on the next tick)
  - `dot_color` impl Into<Color>: Dot color. (text like `"#ff0000"` or a Color)
- `pub fn get_dot_color(&self) -> Color` (method, since 0.3.2): Returns the dot color.
  - returns Color: Dot color. (initially the color from the settings)
- `pub fn set_rotation_speed(&mut self, rotation_speed: f64)` (method, since 0.3.2): Sets the rotation speed. (turns per second)
  - `rotation_speed` f64: Turns per second. (spins the other way if negative)
- `pub fn get_rotation_speed(&self) -> f64` (method, since 0.3.2): Returns the rotation speed.
  - returns f64: Turns per second. (initially rotation_speed from the settings, default 1)
- `pub fn get_dot_nodes(&self) -> &[NodeId]` (method, since 0.3.2): List of dot node handles.
  - returns &[NodeId]: Dot nodes. (in index order, an empty list before being attached to a tree)
