# ui/uibutton (vanilla.js 0.4.0)

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

Button.

## UIButton extends UIControl

Button.

Example: Make a button with text

```js
import { Engine, EngineConfiguration, UIScene, UIButton, 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 buttonNode = new WorldNode();
		buttonNode.setPivot(Pivot.topLeft);
		buttonNode.setAnchor(Pivot.topLeft);
		buttonNode.setLocalPosition(Vector2.create(40, 40));
		buttonNode.setContentSize(Vector2.create(200, 60));
		const paint = buttonNode.addComponent(Paint);
		paint.setColor(new Color(0.2, 0.45, 0.85, 1));
		paint.setRoundSize(12);
		const label = buttonNode.addComponent(UILabel);
		label.setText("Start");
		label.setFontSize(24);
		const button = buttonNode.addComponent(UIButton);
		button.setPressedTintColor(new Color(0, 0, 0, 0.35));
		button.setTransitionDuration(0.15);
		button.setClickedEvent((clickedButton) => {
			console.log("start", clickedButton.getButtonState());
		});
		const root = this.getRoot();
		root.addChild(buttonNode);
	}
}

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.
- `excludeNodeFromTint(node)` (method, since 0.2.1): Excludes a node from transition targets. The components of the node itself and all its descendant nodes are
  excluded from color collection.
  - `node` ComponentNode: Node to exclude from the tint. (its descendants are excluded too)

  Example: Keep a badge inside the button untinted

  ```js
  import { Engine, EngineConfiguration, UIScene, UIButton, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";
  
  class MainScene extends UIScene {
  	constructor() {
  		super();
  		this.setLoadingMinDurationMs(0);
  	}
  
  	initialize(engine) {
  		super.initialize(engine);
  		const buttonNode = new WorldNode();
  		buttonNode.setPivot(Pivot.topLeft);
  		buttonNode.setAnchor(Pivot.topLeft);
  		buttonNode.setLocalPosition(Vector2.create(40, 40));
  		buttonNode.setContentSize(Vector2.create(200, 60));
  		const paint = buttonNode.addComponent(Paint);
  		paint.setColor(new Color(0.2, 0.45, 0.85, 1));
  
  		const badgeNode = new WorldNode();
  		badgeNode.setPivot(Pivot.topLeft);
  		badgeNode.setAnchor(Pivot.topLeft);
  		badgeNode.setLocalPosition(Vector2.create(176, -8));
  		badgeNode.setContentSize(Vector2.create(24, 24));
  		const badgePaint = badgeNode.addComponent(Paint);
  		badgePaint.setColor(new Color(0.9, 0.2, 0.2, 1));
  		badgePaint.setRoundSize(12);
  		buttonNode.addChild(badgeNode);
  
  		const button = buttonNode.addComponent(UIButton);
  		button.excludeNodeFromTint(badgeNode);
  		const root = this.getRoot();
  		root.addChild(buttonNode);
  	}
  }
  
  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());
  ```

- `excludeComponentFromTint(component)` (method, since 0.2.1): Excludes only a specific component from transition targets.
  - `component` Component: Component to exclude from the tint.
- `clearTintExclusions()` (method, since 0.2.1): Clears the exclusion list.
- `attach(node)` (method, since 0.2.0): Attached to a node. (turns on the node's isInteractable automatically, WorldNode and UINode)
  - `node` ComponentNode: Node to attach to.
- `tick(timeDelta)` (method, since 0.2.0): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `updateHoverState()` (method, since 0.3.0): Updates the mouse-over state.
  - Does not touch it while pressed or when unusable.
  - Gets the engine instance via the global accessor so as not to create circular references.
- `draw(graphic)` (method, since 0.2.0): Draw.
  - `graphic` Graphic: Graphics.
- `isDrawnInsideContent()` (method, since 0.4.0): Returns whether it draws only inside the content area. (the button draws nothing itself; used by off-screen and outside-clip culling)
  - Subclasses that override draw return false because where they draw is unknown. (such a class overrides this function too to report it)
  - returns boolean: True if drawing only inside the content area.
- `touchPress(viewInputPosition)` (method, since 0.2.0): Touch press. (TouchRaycaster → WorldNode → Button)
  - `viewInputPosition` Vector2: Press position. (view coordinates)
- `touchRelease(viewInputPosition)` (method, since 0.2.0): Touch release. (TouchRaycaster → WorldNode → Button)
  - `viewInputPosition` Vector2: Release position. (view coordinates)
- `performRelease(viewInputPosition)` (method, since 0.3.0): Release handling body.
  - `viewInputPosition` Vector2: Release position. (view coordinates, counts as a click if inside the node)
- `fireClickEvents()` (method, since 0.4.0): Calls only the click notifications. (clicked, then click, as if a click happened without a press; used when UIHitArea passes on a click on its widened area)
- `touchCancel(viewInputPosition)` (method, since 0.2.0): Touch cancel. (TouchRaycaster → WorldNode → Button)
  - `viewInputPosition` Vector2: Position at cancel. (view coordinates)
- `applyTintProgress(progress)` (method, since 0.2.0): Applies the tint.
  - `progress` number: Tint amount. (0 is the original color, 1 applies the full tint color)
- `collectColorTargets()` (method, since 0.2.0): Collects color targets.
- `collectFromNode(node)` (method, since 0.2.0): Recursively collects color targets from a node.
  - Nodes registered with excludeNodeFromTint are skipped entirely, along with their descendants.
  - Components registered with excludeComponentFromTint are excluded individually.
  - `node` ComponentNode: Node to walk. (its children are walked in turn)
- `setButtonState(buttonState)` (method, since 0.2.0): Changes the button state.
  - `buttonState` ButtonState: Button state. (a ButtonState value)
- `getButtonState()` (method, since 0.2.0): Returns the button state.
  - returns ButtonState: The current button state. (a ButtonState value)
- `setLongPressedEvent(longPressedEvent)` (method, since 0.3.0): Set the long press notification. (notifies once when the threshold time passes and swallows the click of that press)
  - `longPressedEvent` function(UIButton): void: Function called once on a long press. (the argument is this button, long presses are not detected if null)

  Example: Delete on a long press, open on a short press

  ```js
  import { Engine, EngineConfiguration, UIScene, UIButton, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";
  
  class MainScene extends UIScene {
  	constructor() {
  		super();
  		this.setLoadingMinDurationMs(0);
  		this.button = null;
  	}
  
  	initialize(engine) {
  		super.initialize(engine);
  		const itemNode = new WorldNode();
  		itemNode.setPivot(Pivot.topLeft);
  		itemNode.setAnchor(Pivot.topLeft);
  		itemNode.setLocalPosition(Vector2.create(40, 40));
  		itemNode.setContentSize(Vector2.create(96, 96));
  		const paint = itemNode.addComponent(Paint);
  		paint.setColor(new Color(0.3, 0.3, 0.35, 1));
  		const button = itemNode.addComponent(UIButton);
  		button.setLongPressSeconds(0.8);
  		button.setLongPressedEvent(() => {
  			console.log("delete item");
  		});
  		button.setClickedEvent(() => {
  			console.log("open item");
  		});
  		this.button = button;
  		const root = this.getRoot();
  		root.addChild(itemNode);
  	}
  
  	tick(timeDelta) {
  		super.tick(timeDelta);
  		if (this.button !== null) {
  			const pressedSeconds = this.button.getPressedElapsedSeconds();
  			const longPressSeconds = this.button.getLongPressSeconds();
  			const gaugeRatio = Math.min(1, pressedSeconds / longPressSeconds);
  			if (gaugeRatio > 0) {
  				console.log("hold", gaugeRatio);
  			}
  		}
  	}
  }
  
  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());
  ```

- `setLongPressSeconds(longPressSeconds)` (method, since 0.3.0): Sets the long press threshold time. (seconds)
  - `longPressSeconds` number: Time that counts as a long press. (seconds, default 0.5)
- `getPressedElapsedSeconds()` (method, since 0.3.0): Returns how long the current press has lasted. (for gauge effects)
  - returns number: How long the current press has lasted. (seconds, 0 if not pressed)
- `getLongPressSeconds()` (method, since 0.3.0): Returns the long press threshold time.
  - returns number: Long press threshold time. (seconds)
- `setMinimumPressedSeconds(minimumPressedSeconds)` (method, since 0.3.0): Sets the minimum pressed display time. (even a quick tap shows the pressed look for this long before calling the click)
  - `minimumPressedSeconds` number: Minimum time to show the pressed look. (seconds, 0 if below 0, default 0 means immediately)
- `getMinimumPressedSeconds()` (method, since 0.3.0): Returns the minimum pressed display time.
  - returns number: Minimum pressed display time. (seconds)
- `setClickEvent(callback)` (method, since 0.2.0): Sets the click event.
  - `callback` function(UIButton): boolean: Function called after the clicked event when a click happens. (the argument is this button, the return value is not used, not called if null)
- `getClickEvent()` (method, since 0.2.0): Returns the click event.
  - returns function(UIButton): boolean: Click event function. (null if none)
- `setPressedEvent(callback)` (method, since 0.2.0): Sets the press event.
  - `callback` function(UIButton): void: Function called on press. (the argument is this button, not called when not interactable)
- `getPressedEvent()` (method, since 0.2.0): Returns the press event.
  - returns function(UIButton): void: Press event function. (null if none)
- `setReleasedEvent(callback)` (method, since 0.2.0): Sets the release event.
  - `callback` function(UIButton): void: Function called on release after a press. (the argument is this button, called even when released outside the node)
- `getReleasedEvent()` (method, since 0.2.0): Returns the release event.
  - returns function(UIButton): void: Release event function. (null if none)
- `setClickedEvent(callback)` (method, since 0.2.0): Sets the clicked event.
  - `callback` function(UIButton): void: Function called when released inside the node after a press. (the argument is this button, not called if a long press was notified, not called if null)
- `getClickedEvent()` (method, since 0.2.0): Returns the clicked event.
  - returns function(UIButton): void: Clicked event function. (null if none)
- `setHoverTintColor(color)` (method, since 0.3.0): Sets the pressed tint color.
  - `color` Color | string: Tint color applied while the mouse is over it. (a copy is stored, default new Color(1, 1, 1, 0.12))
- `getHoverTintColor()` (method, since 0.3.0): Returns the over tint color.
  - returns Color: Hover tint color.
- `setHoverEvent(callback)` (method, since 0.3.0): Sets the over enter event.
  - `callback` function(UIButton): void: Function called when the mouse enters the node. (the argument is this button)
- `getHoverEvent()` (method, since 0.3.0): Returns the over enter event.
  - returns function(UIButton): void: Hover enter event function. (null if none)
- `setPressedTintColor(color)` (method, since 0.2.0): Sets the pressed tint color.
  - `color` Color | string: Tint color applied while pressed. (default new Color(0, 0, 0, 0.3))
- `getPressedTintColor()` (method, since 0.2.0): Returns the pressed tint color.
  - returns Color: Pressed tint color.
- `setTransitionDuration(duration)` (method, since 0.2.0): Sets the transition duration.
  - `duration` number: Time for the tint to fully apply or clear. (seconds, default 0.3)
- `getTransitionDuration()` (method, since 0.2.0): Returns the transition duration.
  - returns number: Transition duration. (seconds)
- `setInteractable(isInteractable)` (method, since 0.2.0): Sets whether enabled.
  - `isInteractable` boolean: If false, ignores presses and covers sprites with the disabled tint. (default true)
- `getInteractable()` (method, since 0.2.0): Returns whether enabled.
  - returns boolean: True if it accepts presses.

## ButtonState (enum)

Button state.

- `normal` = `Enum.begin()`: Normal state.
- `hover` = `Enum.auto()`: Pointer is over the button.
- `pressed` = `Enum.auto()`: Pressed state.
- `released` = `Enum.auto()`: Just released state.
- `disabled` = `Enum.auto()`: Disabled state, ignores input.
