# ui/uitogglebutton (vanilla.js 0.4.0)

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

Toggle button component.

## UIToggleButton extends UIButton

Toggle button component.

Example: A button that turns on and off with each press

```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): Constructor.
- `updateTintTransition(timeDelta)` (method, since 0.2.0): Updates the tint transition (override: held fixed in the on state).
  - `timeDelta` number: Time since the last frame. (Seconds)
- `setClickedEvent(callback)` (method, since 0.2.0): Sets the click event (override: stores the external callback separately).
  - `callback` function(UIToggleButton): void: Function called after a click changes the on state. (called after the toggled event, not called if null)
- `setIsOn(isOn)` (method, since 0.2.0): Sets whether on.
  - `isOn` boolean: Whether it is on. (the toggled and clicked events are not called)
- `getIsOn()` (method, since 0.2.0): Returns whether on.
  - returns boolean: True if enabled.
- `setOnTintColor(color)` (method, since 0.2.0): Sets the on tint color.
  - `color` Color | string: Tint color applied while on. (the pressed tint color becomes this color too, default new Color(0, 0, 0, 0.3))
- `getOnTintColor()` (method, since 0.2.0): Returns the on tint color.
  - returns Color: On tint color.
- `setToggledEvent(callback)` (method, since 0.2.0): Sets the toggle event.
  - `callback` function(UIToggleButton): void: Function called when a click changes the on state. (the argument is this button, not called if null)
- `getToggledEvent()` (method, since 0.2.0): Returns the toggle event.
  - returns function(UIToggleButton): void: Toggled event function. (null if none)
