# ui/buttonmotion (vanilla.js 0.4.0)

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

Button motion. (Grows on hover, shrinks while pressed, bounces on release, shakes when not pressable, does not grow inside windows, section 8.1)

## ButtonMotion extends Component

Button motion. (Grows on hover, shrinks while pressed, bounces on release, shakes when not pressable, does not grow inside windows, section 8.1)

When attached to a button node after UIButton, reads the button's state every frame and scales the node up and down around its center.
Does not touch colors. (The background and text colors of text buttons are set by the screen's code, and image buttons are handled by UIButton's tint)
Devices where touch is primary do not use hover. (So the button does not look hovered because the finger position remains after release)
When there is no motion, the node's position and size are left untouched, so it does not conflict with other effects that scale the same node (the bounce on appearance).
Even when the node grows, the enlarged area fully covers the original area, so hover detection does not flicker at the edge.
The style is set per button with setStyle, or if left empty, follows the nearest ancestor node whose style was set with setButtonMotionArea, otherwise normal. (inset inside windows)
In the inset style, while hovered, a white border panel and a highlighted background panel are placed as the bottommost children of the button node. (Buttons with no visible background are not highlighted, and only for those two does this component set the color)

Example: A pressable button and shaking when it cannot be pressed

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

class ButtonMotionScene extends UIScene {
	create() {
		super.create();
		const buttonNode = new WorldNode();
		buttonNode.setName("BuyButton");
		buttonNode.setPivot(Pivot.topLeft.clone());
		buttonNode.setAnchor(Pivot.topLeft.clone());
		buttonNode.setContentSize(Vector2.create(160, 48));
		buttonNode.setLocalPosition(Vector2.create(240, 156));
		const buttonPaint = buttonNode.addComponent(Paint);
		buttonPaint.setColor(new Color(0.36, 0.42, 1, 1));
		buttonPaint.setRoundSize(10);
		const button = buttonNode.addComponent(UIButton);
		const buttonLabel = buttonNode.addComponent(UILabel);
		buttonLabel.setText("Buy");
		buttonLabel.setFontSize(18);
		attachButtonMotion(buttonNode);
		let coinCount = 40;
		button.setClickedEvent(() => {
			if (coinCount < 100) {
				shakeButton(buttonNode);
				return;
			}
			coinCount -= 100;
		});
		const root = this.getRoot();
		root.addChild(buttonNode);
		shakeButton(buttonNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new ButtonMotionScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `setStyle(style)` (method, since 0.4.0): Sets this button's style. (ButtonMotionStyle, an empty string follows the style set on an ancestor node)
  - `style` string: Style. (a ButtonMotionStyle value, an empty string follows the style set on an ancestor node)
- `readMotionStyle(node)` (method, since 0.4.0): The style this button follows. (The one set, otherwise the nearest style set on an ancestor node, otherwise normal)
  - `node` WorldNode: Button node. (walks up from the parent to find a style set with setButtonMotionArea)
  - returns string: Style to follow. (a ButtonMotionStyle value)
- `attach(node)` (method, since 0.4.0): Attached. (Finds and keeps the button on the same node)
  - `node` WorldNode: The node to attach to. (no reaction if that node has no UIButton)
- `shake()` (method, since 0.4.0): Shakes when not pressable. (Called by the pressing side)
- `tick(timeDelta)` (method, since 0.4.0): Update. (Adjusts the size by the button state and places the node around its center)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `mutePressTint(isMuted)` (method, since 0.4.0): Switches the button's pressed color to transparent or restores it. (Transparent while the glossy shell draws the press, the set color when the shell is turned off)
  - `isMuted` boolean: If true, sets the pressed color's alpha to 0, if false, restores the pressed color read the first time.
- `readScrollPressed(node, buttonState)` (method, since 0.4.0): Whether it is being pressed inside a scroll view. (The engine does not give the pressed state to buttons inside a scroll view while pressing, so it is drawn as pressed if the press started on this button, the pointer is still over the button and the scroll view has not scrolled, and released once scrolling starts)
  - `node` WorldNode: Button node.
  - `buttonState` string: Button state. (a ButtonState value, false if disabled)
  - returns boolean: True if it should be drawn as pressed. (false if there is no scroll view or engine (System.vanillaEngine))
- `readScrollView(node)` (method, since 0.4.0): The nearest scroll view containing the button. (Found once by walking up the ancestor nodes and kept, null if none)
  - `node` WorldNode: Button node.
  - returns UIScrollView | null: Scroll view of the closest ancestor. (null if none)
- `placeLitParts(node, isLit, isPressed)` (method, since 0.4.0): Places the highlight panels. (The white border panel is the button size, the highlighted background panel is inset by the border thickness, both are the bottommost children of the button so they are below text and images)
  - `node` WorldNode: Button node.
  - `isLit` boolean: If true, it lights up. (does not light up if the alpha of the button's background Paint is 0)
  - `isPressed` boolean: Whether it is being pressed. (passed only to the light handler)
- `createLitParts(node)` (method, since 0.4.0): Creates the highlight panels. (Two bottommost children of the button node, they do not receive the button's pressed color)
  - `node` WorldNode: Button node. (adds two bottom-most children)
  - returns object: { ringNode, ringPaint, fillNode, fillPaint, fillColor }
- `isInputOver(node)` (method, since 0.4.0): Whether the pointer (mouse or finger) is over the node.
  - `node` WorldNode: Node to look at.
  - returns boolean: True if the input position is inside the node. (false if there is no engine (System.vanillaEngine))
- `placeAround(node, scale, shakeOffset)` (method, since 0.4.0): Places the node at that size around its center. (The node is anchored at the top left, so it moves by the amount it grew)
    - If the screen has repositioned the node in the meantime (the position differs from the one this component set), that position becomes the new reference.
  - `node` WorldNode: Button node.
  - `scale` number: Scale. (1 is the normal size)
  - `shakeOffset` number: Horizontal push distance. (pixels)
- `setLitHandler(litHandler)` (method, since 0.4.0): Sets the function that handles highlighting. (For when the decoration on the button highlights in its own shape, null means this component places the white border panel and the highlighted background panel)
  - `litHandler` function(WorldNode, boolean, boolean): void | null: (button node, whether highlighted, whether being pressed)
- `getLitHandler()` (method, since 0.4.0): Returns the function that handles highlighting.
  - returns function(WorldNode, boolean, boolean): void | null: Function that handles lighting. (null if none)

## Functions and constants

- `attachButtonMotion(node)` (method, since 0.4.0): Attaches motion to a button node. (A node with UIButton already attached, returns the ButtonMotion)
  - `node` WorldNode: Button node with a UIButton attached.
  - returns ButtonMotion: The attached motion component.
- `setButtonMotionArea(node, style)` (method, since 0.4.0): Sets the style that buttons under the node follow. (When a window sets inset on its panel, buttons inside it do not grow, a style set on the button with setStyle takes precedence)
  - `node` WorldNode: The node to set the style on. (all buttons below it follow)
  - `style` string: ButtonMotionStyle

  Example: Buttons inside a panel in the inset style

  ```js
  import { WorldNode, UIButton, ButtonMotionStyle, attachButtonMotion, setButtonMotionArea } from "./build/vanilla.js";
  
  const panelNode = new WorldNode();
  setButtonMotionArea(panelNode, ButtonMotionStyle.inset);
  const buttonNode = new WorldNode();
  buttonNode.addComponent(UIButton);
  panelNode.addChild(buttonNode);
  const buttonMotion = attachButtonMotion(buttonNode);
  const areaStyle = buttonMotion.readMotionStyle(buttonNode);
  console.log(areaStyle);
  buttonMotion.setStyle(ButtonMotionStyle.normal);
  const ownStyle = buttonMotion.readMotionStyle(buttonNode);
  console.log(ownStyle);
  ```

- `shakeButton(node)` (method, since 0.4.0): Shakes a button that cannot be pressed. (Only buttons with motion attached, nothing happens otherwise)
  - `node` WorldNode: Button node with a ButtonMotion attached.

## ButtonMotionStyle (enum)

Button motion style.
  - normal: grows on hover, shrinks while pressed, bounces on release.
  - inset: a button inside a window. Does not grow beyond 1x so it does not exceed the window's inner padding. On hover the background brightens and a white border appears inside, and on press it shrinks slightly and returns.

- `normal` = `normal`: Grows on hover, shrinks while pressed and pops on release.
- `inset` = `inset`: A button inside a window. (does not grow, on hover the background brightens and a white inner ring appears, and when pressed it shrinks slightly and comes back)
