# ui/buttonmotion (vanilla-engine 0.4.0)

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

Button motion. (ButtonMotion, ButtonMotionStyle, attachButtonMotion, setButtonMotionArea, shakeButton of vanilla.js ui/buttonmotion.js)
- Attached to a button node after UIButton, it reads that button's state every frame and scales the node up and down around its center.
  It grows on hover, shrinks while pressed, bounces on release, and shakes left and right when shake is called while it cannot be pressed.
- Colors are not touched. (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 whose primary input is a finger do not use hover. (so it does not look hovered from the finger position left after release)
- When there is no motion, the node's position and size are not touched, so it does not clash with other effects that scale the same node.
- The style is set per button with set_style, or if left empty follows the closest node above with a ButtonMotionArea, otherwise Normal.
- In the Inset style, while hovered, brightens the color of the button's background Paint and draws a white rim inside. (buttons without a background Paint or that are invisible are not brightened)
  To have the decoration on a button brighten in its own way, hook a function with set_lit_handler. (the button's pressed color is then left transparent)
  The hooked function is kept in an `Rc<RefCell>`. get_lit_handler returns that Rc, and the caller borrows it with borrow_mut to call it.
- Differences from the original: the centering math reads the pivot (the original only handles a top-left pivot), and the marker that sets the style is the ButtonMotionArea component (the original uses a WeakMap).
  The brightened background and white rim are not child nodes. (the original uses the two bottommost children, and if the text is a component of the same node as in the Rust example, children would cover the text)

## ButtonLitHandler (type)

Function that handles brightening. (context, whether lit, whether pressed)

```rust
pub type ButtonLitHandler = Box<dyn FnMut(&mut ComponentContext, bool, bool)>
```

## ButtonMotionArea (struct) (Component)

Marker that sets the style for buttons under a node. (a window sets it on its own panel)

Example: Make the buttons inside a panel Inset style

```rust
use vanilla::{attach_button_motion, set_button_motion_area, ButtonMotionArea, ButtonMotionStyle, Engine, EngineConfiguration, NodeTree, TouchKind, UIButton, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "Panel");
set_button_motion_area(&mut node_tree, panel_id, ButtonMotionStyle::Inset);
let button_id = node_tree.create_child(panel_id, "Button");
node_tree[button_id].set_content_size(Vector2::create(120.0, 40.0));
node_tree.add_component(button_id, UIButton::new());
attach_button_motion(&mut node_tree, button_id).expect("button motion");

node_tree.dispatch_touch(button_id, &engine, TouchKind::Press, Vector2::zero());
node_tree.tick(&engine, 0.1);
let pressed_scale = node_tree[button_id].get_local_scale();
assert!((pressed_scale.x - 0.96).abs() < 0.001);
let area = node_tree[panel_id].get_component::<ButtonMotionArea>().expect("area");
assert_eq!(area.get_style(), ButtonMotionStyle::Inset);
```

- `pub fn new(style: ButtonMotionStyle) -> ButtonMotionArea` (constructor, since 0.4.0): Creates one. (use set_button_motion_area to set it on a node)
  - `style` ButtonMotionStyle: Style for buttons under this node.
  - returns ButtonMotionArea: Marker that sets that style.
- `pub fn get_style(&self) -> ButtonMotionStyle` (method, since 0.4.0): Returns the set style.
  - returns ButtonMotionStyle: Style for buttons under this node.

## ButtonMotion (struct) (Default, Component)

Button motion. (attached to a button node after UIButton, scales the node up and down around its center according to the button state)

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

```rust
use vanilla::{attach_button_motion, shake_button, Engine, EngineConfiguration, NodeTree, Paint, TouchKind, UIButton, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let button_id = node_tree.create_child(root_id, "PlayButton");
node_tree[button_id].set_local_position(Vector2::create(200.0, 100.0));
node_tree[button_id].set_content_size(Vector2::create(160.0, 48.0));
node_tree[button_id].add_component(Paint::create_with_color("#3b82f6"));
node_tree.add_component(button_id, UIButton::new());
attach_button_motion(&mut node_tree, button_id).expect("button motion");

node_tree.dispatch_touch(button_id, &engine, TouchKind::Press, Vector2::create(200.0, 100.0));
node_tree.tick(&engine, 0.05);
let pressed_scale = node_tree[button_id].get_local_scale();
assert!((pressed_scale.x - 0.92).abs() < 0.001);

node_tree.dispatch_touch(button_id, &engine, TouchKind::Cancel, Vector2::create(200.0, 100.0));
for _frame_index in 0..30 {
	node_tree.tick(&engine, 0.05);
}
assert_eq!(node_tree[button_id].get_local_scale(), Vector2::create(1.0, 1.0));
assert_eq!(node_tree[button_id].get_local_position(), Vector2::create(200.0, 100.0));

node_tree[button_id].get_component_mut::<UIButton>().expect("button").set_interactable(false);
shake_button(&mut node_tree, button_id);
node_tree.tick(&engine, 0.05);
assert_ne!(node_tree[button_id].get_local_position().x, 200.0);
```

- `pub fn new() -> ButtonMotion` (constructor, since 0.4.0): Constructor.
  - returns ButtonMotion: New button motion with no style set. (follows the style set on nodes above, use attach_button_motion to attach it to a button node)
- `pub fn set_style(&mut self, style: Option<ButtonMotionStyle>)` (method, since 0.4.0): Sets this button's style. (follows the style set on nodes above if None)
  - `style` Option<ButtonMotionStyle>: This button's style. (if None, the closest style set on nodes above, or Normal if none)
- `pub fn shake(&mut self)` (method, since 0.4.0): Shakes when not pressable. (Called by the pressing side)
- `pub fn set_lit_handler(&mut self, lit_handler: Option<ButtonLitHandler>)` (method, since 0.4.0): Sets the function that handles highlighting. (if None, this part brightens the background Paint color and draw draws an inner white rim)
  - `lit_handler` Option<ButtonLitHandler>: Function to handle brightening. (takes context, whether lit, whether pressed and is called every tick, removes the function if None)

  Example: Brighten with a light node placed on top

  ```rust
  use vanilla::{attach_button_motion, ButtonMotionStyle, ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIButton, Vector2};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let button_id = node_tree.create_child(root_id, "Button");
  let glow_id = node_tree.create_child(button_id, "Glow");
  node_tree[glow_id].set_visible(false);
  node_tree.add_component(button_id, UIButton::new());
  let button_motion = attach_button_motion(&mut node_tree, button_id).expect("button motion");
  button_motion.set_style(Some(ButtonMotionStyle::Inset));
  button_motion.set_lit_handler(Some(Box::new(move |context: &mut ComponentContext, is_lit: bool, _is_pressed: bool| {
  	context.get_node_tree_mut()[glow_id].set_visible(is_lit);
  })));
  
  node_tree.dispatch_touch(button_id, &engine, TouchKind::Press, Vector2::zero());
  node_tree.tick(&engine, 0.016);
  assert_eq!(node_tree[glow_id].is_visible(), !engine.is_coarse_pointer());
  ```

- `pub fn get_lit_handler(&self) -> Option<Rc<RefCell<ButtonLitHandler>>>` (method, since 0.4.0): Returns the function that handles brightening. (borrow it with borrow_mut to call it)
  - returns Option<Rc<RefCell<ButtonLitHandler>>>: The hooked brightening function. None if none is hooked.

## Functions and constants

- `pub fn set_button_motion_area(node_tree: &mut NodeTree, node_id: NodeId, style: ButtonMotionStyle)` (method, since 0.4.0): Sets the style for buttons under a node. (if a window sets Inset on its own panel, the buttons inside do not grow, the style a button set with set_style comes first)
  - `node_tree` &mut NodeTree: Node tree.
  - `node_id` NodeId: Node to set the style on. (such as a window's panel, if a ButtonMotionArea is already attached only the style changes, and nothing happens if the node is missing)
  - `style` ButtonMotionStyle: Style for buttons under that node. (a button walks up and follows the closest one)
- `pub fn attach_button_motion(node_tree: &mut NodeTree, node_id: NodeId) -> Option<&mut ButtonMotion>` (method, since 0.4.0): Attaches motion to a button node. (a node with UIButton already attached, returns the attached ButtonMotion. the original attachButtonMotion)
  - `node_tree` &mut NodeTree: Node tree.
  - `node_id` NodeId: Button node with UIButton already attached. (the motion is attached as the component after UIButton)
  - returns Option<&mut ButtonMotion>: The attached ButtonMotion. None if the node is missing.
- `pub fn shake_button(node_tree: &mut NodeTree, node_id: NodeId)` (method, since 0.4.0): Shakes a button. (finds the node's ButtonMotion and shakes it, nothing happens if missing)
  - Shakes left and right for 0.25 seconds.
  - `node_tree` &mut NodeTree: Node tree.
  - `node_id` NodeId: Button node with a ButtonMotion attached.

## ButtonMotionStyle (enum) (Clone, Copy, PartialEq, Eq, Debug)

Style of button motion.

- `Normal`: Grows on hover, shrinks while pressed and pops on release.
- `Inset`: Button inside a window. It does not grow beyond 1x, so it does not go past the window's inner padding. On hover the background brightens and a white rim appears inside, and on press it shrinks a little and comes back.
