# ui/uislider (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uislider/

슬라이더 컨트롤.

## UISlider extends UIControl

슬라이더 컨트롤.
- 사용자 입력(드래그/탭)으로 값을 조정한다. → UIControl 을 상속.
- 표시 부분(트랙/채움)은 UIProgressView 와 외관이 비슷하지만, 컨트롤 트리에
  속해야 하므로 UIProgressView 를 상속하지 않고 직접 그린다.
- thumb 까지 추가로 그린다.

예제: 음량 슬라이더 만들기

```js
import { Engine, EngineConfiguration, UIScene, UISlider, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";

class MainScene extends UIScene {
	constructor() {
		super();
		this.setLoadingMinDurationMs(0);
	}

	initialize(engine) {
		super.initialize(engine);
		const sliderNode = new WorldNode();
		sliderNode.setPivot(Pivot.topLeft);
		sliderNode.setAnchor(Pivot.topLeft);
		sliderNode.setLocalPosition(Vector2.create(40, 60));
		sliderNode.setContentSize(Vector2.create(240, 24));
		const slider = sliderNode.addComponent(UISlider);
		slider.setRange(0, 100);
		slider.setValue(50);
		slider.setTrackThickness(6);
		slider.setCornerRadius(3);
		slider.setThumbRadius(12);
		slider.setValueChangedEvent((changedSlider) => {
			console.log("volume", changedSlider.getValue());
		});
		const root = this.getRoot();
		root.addChild(sliderNode);
		console.log(slider.getValue(), slider.getRatio());
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.canvasId = "mainCanvas";
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.2.1): 생성.
- `attach(node)` (method, since 0.2.1): 노드에 붙음. (WorldNode 의 isInteractable 을 자동 활성화)
  - `node` ComponentNode: 붙을 노드.
- `draw(graphic)` (method, since 0.2.1): 출력. (트랙 + 채움 + thumb)
  - `graphic` Graphic: 그래픽.
- `calculateDrawnRect(rect)` (method, since 0.4.0): 그리는 사각형을 rect 에 적기. (오버라이드: 트랙과 채움은 내용 영역 안, 손잡이 원은 값이 움직이는 줄 전체에 반지름만큼 넘친다)
  - 값에 따라 움직이는 손잡이가 어디에 있든 품도록 값과 상관없는 사각형을 알린다. (값을 바꿔도 경계를 다시 셈하지 않는다)
  - 손잡이 노드를 정했으면 원을 그리지 않으므로 내용 영역만. 가장자리 매끄럽게 하기만큼 한 단위씩 넓혀 본다.
  - `rect` Rect: 그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)
  - returns boolean: 노드에 붙어 있으면 참.

  예제: 손잡이 원이 넘치는 만큼 보기

  ```js
  import { WorldNode, UISlider, Rect, Vector2 } from "./build/vanilla.js";
  
  const sliderNode = new WorldNode();
  sliderNode.setContentSize(Vector2.create(200, 8));
  const slider = sliderNode.addComponent(UISlider);
  slider.setThumbRadius(12);
  const drawnRect = Rect.zero();
  slider.calculateDrawnRect(drawnRect);
  console.log(drawnRect.position.x, drawnRect.position.y, drawnRect.size.x, drawnRect.size.y);
  ```

- `touchPress(viewInputPosition)` (method, since 0.2.1): 터치 누름.
  - `viewInputPosition` Vector2: 누른 자리. (뷰 좌표)
- `touchMove(viewInputPosition)` (method, since 0.2.1): 터치 이동.
  - `viewInputPosition` Vector2: 옮긴 자리. (뷰 좌표)
- `touchRelease(viewInputPosition)` (method, since 0.2.1): 터치 뗌.
  - `viewInputPosition` Vector2: 뗀 자리. (뷰 좌표)
- `touchCancel(viewInputPosition)` (method, since 0.2.1): 터치 취소.
  - `viewInputPosition` Vector2: 취소된 때의 자리. (뷰 좌표)
- `getRatio()` (method, since 0.2.1): 값/범위/비율.
  - returns number: 범위 안에서 값의 비율. (0 ~ 1, 범위가 0 이하면 0)
- `setValue(value)` (method, since 0.2.1): 값 설정. (범위 안으로 자른다, 값 바뀜 이벤트는 부르지 않는다)
  - `value` number: 새 값. (minValue ~ maxValue)
- `getValue()` (method, since 0.2.1): 값 반환.
  - returns number: 지금 값. (기본 0)
- `setRange(minValue, maxValue)` (method, since 0.2.1): 범위 설정. (지금 값도 새 범위 안으로 자른다)
  - `minValue` number: 가장 작은 값. (기본 0)
  - `maxValue` number: 가장 큰 값. (기본 1)
- `getMinValue()` (method, since 0.2.1): 가장 작은 값 반환.
  - returns number: 가장 작은 값.
- `getMaxValue()` (method, since 0.2.1): 가장 큰 값 반환.
  - returns number: 가장 큰 값.
- `fireValueChanged()` (method, since 0.2.1): 외부 setValue 후 콜백을 강제 호출하고 싶을 때.
- `setTrackColor(color)` (method, since 0.2.1): 트랙 색 설정.
  - `color` Color | string: 트랙 색. (기본 new Color(0.7, 0.7, 0.7, 1))
- `getTrackColor()` (method, since 0.2.1): 트랙 색 반환.
  - returns Color: 트랙 색.
- `setFillColor(color)` (method, since 0.2.1): 채움 색 설정.
  - `color` Color | string: 값까지 채우는 색. (기본 new Color(0.23, 0.51, 0.96, 1))
- `getFillColor()` (method, since 0.2.1): 채움 색 반환.
  - returns Color: 채움 색.
- `setThumbColor(color)` (method, since 0.2.1): 손잡이 색 설정.
  - `color` Color | string: 손잡이 원의 색. (기본 흰색, 손잡이 노드를 정했으면 쓰지 않는다)
- `getThumbColor()` (method, since 0.2.1): 손잡이 색 반환.
  - returns Color: 손잡이 원의 색.
- `setThumbRadius(radius)` (method, since 0.2.1): 손잡이 반지름 설정.
  - `radius` number: 손잡이 원의 반지름. (기본 16, 손잡이 노드를 정했으면 쓰지 않는다)
- `getThumbRadius()` (method, since 0.2.1): 손잡이 반지름 반환.
  - returns number: 손잡이 원의 반지름.
- `setTrackThickness(thickness)` (method, since 0.3.0): 트랙 두께 설정. (노드 가운데에 이 두께로 그린다)
  - `thickness` number: 트랙 두께. (0 이하면 노드 전체, 기본 0)
- `getTrackThickness()` (method, since 0.3.0): 트랙 두께 반환.
  - returns number: 트랙 두께. (0 이면 노드 전체)
- `setThumbNode(node)` (method, since 0.3.0): 손잡이 노드 설정. (정하면 원을 그리지 않고 이 노드를 손잡이 자리로 옮긴다)
  - `node` WorldNode: 손잡이로 쓸 노드. (슬라이더 노드의 자식, 왼쪽 위 피봇과 앵커로 둔다, null 이면 원을 그린다)

  예제: 그림 노드를 손잡이로 쓰기

  ```js
  import { Engine, EngineConfiguration, UIScene, UISlider, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";
  
  class MainScene extends UIScene {
  	constructor() {
  		super();
  		this.setLoadingMinDurationMs(0);
  	}
  
  	initialize(engine) {
  		super.initialize(engine);
  		const sliderNode = new WorldNode();
  		sliderNode.setPivot(Pivot.topLeft);
  		sliderNode.setAnchor(Pivot.topLeft);
  		sliderNode.setLocalPosition(Vector2.create(40, 60));
  		sliderNode.setContentSize(Vector2.create(240, 24));
  		const slider = sliderNode.addComponent(UISlider);
  		slider.setTrackThickness(4);
  
  		const thumbNode = new WorldNode();
  		thumbNode.setPivot(Pivot.topLeft);
  		thumbNode.setAnchor(Pivot.topLeft);
  		thumbNode.setContentSize(Vector2.create(20, 28));
  		const thumbPaint = thumbNode.addComponent(Paint);
  		thumbPaint.setColor(new Color(1, 0.6, 0.2, 1));
  		thumbPaint.setRoundSize(6);
  		sliderNode.addChild(thumbNode);
  		slider.setThumbNode(thumbNode);
  		slider.setValue(0.3);
  
  		const root = this.getRoot();
  		root.addChild(sliderNode);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  engineConfiguration.canvasId = "mainCanvas";
  engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
  engineConfiguration.autoResizeOnWindowResize = true;
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `getThumbNode()` (method, since 0.3.0): 손잡이 노드 반환.
  - returns WorldNode: 손잡이 노드. (없으면 null)
- `setCornerRadius(radius)` (method, since 0.2.1): 모서리 둥글기 설정.
  - `radius` number: 트랙과 채움의 모서리 반지름. (0 이하면 각진 사각형, 기본 0)
- `getCornerRadius()` (method, since 0.2.1): 모서리 둥글기 반환.
  - returns number: 트랙과 채움의 모서리 반지름.
- `setDirection(direction)` (method, since 0.2.1): 방향 설정.
  - `direction` string: 방향. (horizontal 이면 왼쪽에서 오른쪽, vertical 이면 아래에서 위로 찬다, 기본 horizontal)
- `getDirection()` (method, since 0.2.1): 방향 반환.
  - returns string: 방향. (horizontal 이나 vertical)
- `setValueChangedEvent(callback)` (method, since 0.2.1): 콜백 / 인터랙션.
  - `callback` (slider: UISlider) => void: 누르거나 끌어 값이 바뀌면 부를 함수. (인자는 이 슬라이더, setValue 로 바꿀 때는 부르지 않는다, null 이면 부르지 않는다)
- `getValueChangedEvent()` (method, since 0.2.1): 값 바뀜 이벤트 반환.
  - returns (slider: UISlider) => void: 값 바뀜 이벤트 함수. (없으면 null)
- `setInteractable(isInteractable)` (method, since 0.2.1): 입력 받기 설정.
  - `isInteractable` boolean: 거짓이면 새 누름을 받지 않는다. (기본 참)
- `getInteractable()` (method, since 0.2.1): 입력 받기 여부 반환.
  - returns boolean: 누름을 받으면 참.
- `isDragging()` (method, since 0.2.1): 끄는 중인지.
  - returns boolean: 누른 뒤 떼거나 취소하기 전까지 참.
