# ui/uitogglebutton (vanilla.js 0.4.0)

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

토글 버튼 컴포넌트.

## UIToggleButton extends UIButton

토글 버튼 컴포넌트.

예제: 누를 때마다 켜고 끄는 단추

```js
import { Engine, EngineConfiguration, UIScene, UIToggleButton, UILabel, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";

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

	initialize(engine) {
		super.initialize(engine);
		const toggleNode = new WorldNode();
		toggleNode.setPivot(Pivot.topLeft);
		toggleNode.setAnchor(Pivot.topLeft);
		toggleNode.setLocalPosition(Vector2.create(40, 40));
		toggleNode.setContentSize(Vector2.create(180, 56));
		const paint = toggleNode.addComponent(Paint);
		paint.setColor(new Color(0.25, 0.6, 0.35, 1));
		paint.setRoundSize(12);
		const label = toggleNode.addComponent(UILabel);
		label.setText("Sound");
		label.setFontSize(20);
		const toggleButton = toggleNode.addComponent(UIToggleButton);
		toggleButton.setOnTintColor(new Color(0, 0, 0, 0.4));
		toggleButton.setToggledEvent((button) => {
			console.log("sound on", button.getIsOn());
		});
		const root = this.getRoot();
		root.addChild(toggleNode);
		toggleButton.setIsOn(true);
	}
}

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.0): 생성.
- `updateTintTransition(timeDelta)` (method, since 0.2.0): 틴트 트랜지션 갱신 (오버라이드: 켜짐 상태 고정).
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `setClickedEvent(callback)` (method, since 0.2.0): 클릭 이벤트 설정 (오버라이드: 외부 콜백 별도 저장).
  - `callback` function(UIToggleButton): void: 클릭으로 켜짐이 바뀐 뒤 부를 함수. (토글 이벤트 다음에 부른다, null 이면 부르지 않는다)
- `setIsOn(isOn)` (method, since 0.2.0): 켜짐 여부 설정.
  - `isOn` boolean: 켜짐 여부. (토글 이벤트와 클릭 이벤트는 부르지 않는다)
- `getIsOn()` (method, since 0.2.0): 켜짐 여부 반환.
  - returns boolean: 켜져 있으면 참.
- `setOnTintColor(color)` (method, since 0.2.0): 켜짐 틴트 색상 설정.
  - `color` Color | string: 켜져 있을 때 덮을 틴트 색. (눌림 틴트 색도 이 색이 된다, 기본 new Color(0, 0, 0, 0.3))
- `getOnTintColor()` (method, since 0.2.0): 켜짐 틴트 색상 반환.
  - returns Color: 켜짐 틴트 색.
- `setToggledEvent(callback)` (method, since 0.2.0): 토글 이벤트 설정.
  - `callback` function(UIToggleButton): void: 클릭으로 켜짐이 바뀌면 부를 함수. (인자는 이 버튼, null 이면 부르지 않는다)
- `getToggledEvent()` (method, since 0.2.0): 토글 이벤트 반환.
  - returns function(UIToggleButton): void: 토글 이벤트 함수. (없으면 null)
