# ui/uispinner (vanilla-engine 0.4.0)

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

회전 로딩 표시. (원본 ui/uispinner.js 의 UISpinner)
- 원 둘레에 점 여러 개를 놓고, 밝기가 꼬리처럼 돌아가는 표준 스피너. (점 투명도 0.15 ~ 1)
- 원본은 WorldNode 를 상속한 노드다. 러스트 판은 노드(호스트)에 붙는 컴포넌트이고, 붙을 때 원본 생성자처럼
  호스트를 꾸미고(가운데 피봇, 크기 = (반지름 + 점 반지름) x 2) 점 노드(둥근 Paint)를 자식으로 만든다.
  UISpinner::create(node_tree, parent_id, options) 가 원본 new UISpinner(options) + add_child 자리다.
- 사용:
  ```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));
  ```
- 원본과 다른 것:
  - set_dot_color 는 색을 정해 두고 다음 tick 에 점 노드에 건다. (점 노드는 씬 코드가 이 위젯을 쥔 동안 닿을 수 없다)
  - 위상(phase)은 0 ~ 1 로 접어 둔다. 꼬리 계산이 1 주기라 보이는 결과는 같고, 오래 돌아도 정밀도가 떨어지지 않는다.
  - 점 노드 이름은 원본처럼 "SpinnerDot{번호}" 다. options 객체는 UISpinnerOptions 구조체다.

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

스피너 설정. (원본 options = { radius = 9, dot_count = 10, dot_radius = 2, color?, rotation_speed = 1 })

예제: 점 넷짜리 스피너

```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): 점이 놓이는 원의 반지름.
  - returns f64
- `pub dot_count: usize` (field, since 0.3.2): 점 수. (기본 10)
  - returns usize
- `pub dot_radius: f64` (field, since 0.3.2): 점 하나의 반지름. (기본 2)
  - returns f64
- `pub color: Color` (field, since 0.3.2): 점 색. (기본 흰색)
  - returns Color
- `pub rotation_speed: f64` (field, since 0.3.2): 초당 바퀴 수.
  - returns f64

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

회전 로딩 표시.

예제: 판 위에 띄운 스피너

```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): 생성. (기본 설정)
  - returns UISpinner: 붙기 전의 스피너. (UISpinnerOptions::default)
- `pub fn create_with_options(options: UISpinnerOptions) -> UISpinner` (static, since 0.3.2): 설정으로 생성. (노드에는 node_tree.add_component 로 붙인다)
  - `options` UISpinnerOptions: 스피너 설정.
  - returns UISpinner: 붙기 전의 스피너.
- `pub fn create(node_tree: &mut NodeTree, parent_id: NodeId, options: UISpinnerOptions) -> NodeId` (static, since 0.3.2): 노드 만들기. (parent_id 밑에 "UISpinner" 노드를 만들어 붙인다. 원본 new UISpinner(options) + add_child)
  - `node_tree` &mut NodeTree: 노드 트리.
  - `parent_id` NodeId: 부모 노드.
  - `options` UISpinnerOptions: 스피너 설정.
  - returns NodeId: 만든 스피너 노드. (가운데 피봇, 크기는 (반지름 + 점 반지름) x 2)
- `pub fn calculate_dot_opacity(&self, dot_index: usize) -> f64` (method, since 0.3.2): 점 투명도 계산. (점 순서 비율에서 위상을 뺀 꼬리 비율이 작을수록 밝다)
  - 점이 없으면(dot_count 0) 순서 비율을 0 으로 본다.
  - `dot_index` usize: 점 번호. (0 부터, 0 번은 원의 오른쪽이고 번호가 늘수록 시계 방향)
  - returns f64: 점 투명도. (0.15 ~ 1)

  예제: 위상 0 에서는 0 번 점이 가장 밝다

  ```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): 점 색 설정. (다음 tick 에 점 노드에 건다)
  - `dot_color` impl Into<Color>: 점 색. (`"#ff0000"` 같은 글이나 Color)
- `pub fn get_dot_color(&self) -> Color` (method, since 0.3.2): 점 색 반환.
  - returns Color: 점 색. (처음은 설정의 color)
- `pub fn set_rotation_speed(&mut self, rotation_speed: f64)` (method, since 0.3.2): 회전 속도 설정. (초당 바퀴 수)
  - `rotation_speed` f64: 초당 바퀴 수. (음수면 반대로 돈다)
- `pub fn get_rotation_speed(&self) -> f64` (method, since 0.3.2): 회전 속도 반환.
  - returns f64: 초당 바퀴 수. (처음은 설정의 rotation_speed, 기본 1)
- `pub fn get_dot_nodes(&self) -> &[NodeId]` (method, since 0.3.2): 점 노드 핸들 목록.
  - returns &[NodeId]: 점 노드. (번호 차례, 트리에 붙기 전이면 빈 목록)
