# ui/popupmotion (vanilla-engine 0.4.0)

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

Popup appear / disappear transitioner. (PopupMotion, PopupMotionState of the original ui/popupmotion.js)
- Moves through the four states closed → opening → open → closing → closed and computes the scale (appear: 0.45 → 1.18 overshoot → 1, disappear: 1 → 1.08 → 0.08) and
  the dimming ratio (dim_ratio). It is a calculator, neither a node nor a component, so it is ported from the original as is.
- Every frame after tick(time_delta), read get_scale() / get_dim_ratio() and apply them to the node scale / overlay alpha. If set_instant_enabled(true), it changes right away without the effect.
- Differences from the original:
  - The state is the PopupMotionState enum instead of a string.
  - The options object is the PopupMotionOptions struct. The open / close notifications are argument-less `Box<dyn FnMut()>`.
    (UIDialog, which has to change nodes, handles it by comparing the state before and after tick instead of using notifications)

## PopupMotionOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

Transitioner settings. (the original options = { open_duration = 0.32, close_duration = 0.22 })

Example: Shorten only the appear time

```rust
use vanilla::{PopupMotion, PopupMotionOptions, PopupMotionState};

let options = PopupMotionOptions {
	open_duration: 0.2,
	..PopupMotionOptions::default()
};
assert_eq!(options.close_duration, 0.22);
let mut popup_motion = PopupMotion::create_with_options(options);
popup_motion.open();
popup_motion.tick(0.2);
assert_eq!(popup_motion.get_state(), PopupMotionState::Open);
```

- `pub open_duration: f64` (field, since 0.3.2): Appear time. (seconds)
  - returns f64
- `pub close_duration: f64` (field, since 0.3.2): Disappear time. (seconds)
  - returns f64

## PopupMotionEvent (type)

Notification. (no arguments)

```rust
pub type PopupMotionEvent = Box<dyn FnMut()>
```

## PopupMotion (struct) (Default)

Popup transitioner.

Example: Applying it to the panel scale and the dim alpha

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{Color, NodeTree, Paint, PopupMotion, PopupMotionState, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let dim_id = node_tree.create_child(root_id, "Dim");
node_tree[dim_id].add_component(Paint::create_with_color(Color::create(0.0, 0.0, 0.0, 0.0)));
let panel_id = node_tree.create_child(root_id, "Panel");
let opened_count = Rc::new(Cell::new(0));
let opened_count_for_event = opened_count.clone();
let mut popup_motion = PopupMotion::new();
popup_motion.set_opened_event(Box::new(move || {
	opened_count_for_event.set(opened_count_for_event.get() + 1);
}));

popup_motion.open();
for _frame_index in 0..30 {
	popup_motion.tick(1.0 / 60.0);
	let scale = popup_motion.get_scale();
	node_tree[panel_id].set_local_scale(Vector2::create(scale, scale));
	let dim_paint = node_tree[dim_id].get_component_mut::<Paint>().expect("paint");
	dim_paint.set_color(Color::create(0.0, 0.0, 0.0, 0.5 * popup_motion.get_dim_ratio()));
}
assert_eq!(popup_motion.get_state(), PopupMotionState::Open);
assert_eq!(opened_count.get(), 1);
assert!(popup_motion.is_interactive());
assert_eq!(node_tree[panel_id].get_local_scale(), Vector2::create(1.0, 1.0));

popup_motion.close();
popup_motion.tick(0.3);
assert!(!popup_motion.is_visible());
assert_eq!(popup_motion.get_scale(), 0.0);
```

- `pub fn new() -> PopupMotion` (constructor, since 0.3.2): Creates one. (default times)
  - returns PopupMotion: New closed transitioner. (appear 0.32 seconds, disappear 0.22 seconds)
- `pub fn create_with_options(options: PopupMotionOptions) -> PopupMotion` (static, since 0.3.2): Creates one with settings.
  - `options` PopupMotionOptions: Appear and disappear times.
  - returns PopupMotion: New closed transitioner.
- `pub fn open(&mut self)` (method, since 0.3.2): Opens. (left as is if already open or opening)
- `pub fn close(&mut self)` (method, since 0.3.2): Closes. (left as is if already closed or closing)
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update. (moves to the next state and notifies once the appear / disappear time is up)
  - `time_delta` f64: Elapsed time. (seconds, accumulated only while opening or closing)
- `pub fn get_scale(&self) -> f64` (method, since 0.3.2): Returns the current scale.
  - returns f64: Scale to apply to the panel. (closed 0, open 1, opening from 0.45 through 1.18 to 1, closing from 1 through 1.08 to 0.08)
- `pub fn get_dim_ratio(&self) -> f64` (method, since 0.3.2): Returns the dimming ratio. (0 ~ 1)
  - returns f64: Ratio to multiply the overlay's alpha by. (0 ~ 1, evenly from 0 to 1 while opening and from 1 to 0 while closing)
- `pub fn is_interactive(&self) -> bool` (method, since 0.3.2): Whether interactable. (true only after fully open)
  - returns bool: true if in the open state.
- `pub fn get_state(&self) -> PopupMotionState` (method, since 0.3.2): Returns the state.
  - returns PopupMotionState: The current state.
- `pub fn is_visible(&self) -> bool` (method, since 0.3.2): Whether it is showing. (true unless closed)
  - returns bool: true unless in the closed state.
- `pub fn set_instant_enabled(&mut self, is_instant_enabled: bool)` (method, since 0.3.2): Sets instant transitions. (UI tone without effects)
  - `is_instant_enabled` bool: true to change right away in open, close without the effect. (default false, a transition already opening or closing continues with tick)
- `pub fn is_instant_enabled(&self) -> bool` (method, since 0.3.2): Whether transitions are instant.
  - returns bool: true if transitions are instant.
- `pub fn set_opened_event(&mut self, opened_event: PopupMotionEvent)` (method, since 0.3.2): Sets the open notification.
  - `opened_event` PopupMotionEvent: Function to call on entering the open state. (called right inside open if transitions are instant)
- `pub fn set_closed_event(&mut self, closed_event: PopupMotionEvent)` (method, since 0.3.2): Sets the close notification.
  - `closed_event` PopupMotionEvent: Function to call on entering the closed state. (called right inside close if transitions are instant)

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

Transition state.

- `Closed`: Closed. (scale 0, dim 0)
- `Opening`: Opening. (scale swells from 0.45 to 1.18 and settles at 1)
- `Open`: Open. (scale 1, dim 1, the only state that accepts interaction)
- `Closing`: Closing. (scale swells to 1.08 and shrinks to 0.08)
