vanilladocs
vanillapunch.com
API / ui/popupmotion

ui/popupmotion

팝업 등장 / 퇴장 전환기. (원본 ui/popupmotion.js 의 PopupMotion, PopupMotionState)

  • closed → opening → open → closing → closed 네 상태를 오가며 배율(등장: 0.45 → 1.18 되튕김 → 1, 퇴장: 1 → 1.08 → 0.08)과 어둡게 가리는 비율(dim_ratio)을 산출한다. 노드도 컴포넌트도 아닌 계산기라서 원본 그대로 옮겼다.
  • 매 프레임 tick(time_delta) 뒤 get_scale() / get_dim_ratio() 를 읽어 노드 배율 / 막 알파에 건다. set_instant_enabled(true) 면 연출 없이 바로 바뀐다.
  • 원본과 다른 것:
  • 상태는 문자열 대신 PopupMotionState 열거형이다.
  • options 객체는 PopupMotionOptions 구조체다. 열림 / 닫힘 알림은 인자 없는 Box<dyn FnMut()> 다. (노드를 바꿔야 하는 UIDialog 는 알림 대신 tick 앞뒤의 상태를 비교해 처리한다)

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)

PopupMotionOptionsstruct traits Clone, Copy, Debug, PartialEq, Default

소스source

전환기 설정. (원본 options = { open_duration = 0.32, close_duration = 0.22 })

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

예제Example등장 시간만 줄이기Shorten only the appear time
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);
필드fieldpub open_duration: f64#0.3.2 부터since 0.3.2소스source

등장 시간. (초)

Appear time. (seconds)

반환Returnsf64

필드fieldpub close_duration: f64#0.3.2 부터since 0.3.2소스source

퇴장 시간. (초)

Disappear time. (seconds)

반환Returnsf64

PopupMotionEventtype

소스source

알림. (인자 없음)

Notification. (no arguments)

pub type PopupMotionEvent = Box<dyn FnMut()>

PopupMotionstruct traits Default

짝Counterpart: vanilla.js PopupMotion소스source

팝업 전환기.

Popup transitioner.

예제Example판의 배율과 막의 알파에 걸기Applying it to the panel scale and the dim alpha
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);
생성자constructorpub fn new() -> PopupMotion#0.3.2 부터since 0.3.2소스source

생성. (기본 시간)

Creates one. (default times)

반환ReturnsPopupMotion 닫힌 새 전환기. (등장 0.32 초, 퇴장 0.22 초)New closed transitioner. (appear 0.32 seconds, disappear 0.22 seconds)

짝Counterpart: vanilla.js PopupMotion.constructor

정적staticpub fn create_with_options(options: PopupMotionOptions) -> PopupMotion#0.3.2 부터since 0.3.2소스source

설정으로 생성.

Creates one with settings.

optionsPopupMotionOptions
등장, 퇴장 시간.Appear and disappear times.

반환ReturnsPopupMotion 닫힌 새 전환기.New closed transitioner.

메서드methodpub fn open(&mut self)#0.3.2 부터since 0.3.2소스source

열기. (이미 열렸거나 열리는 중이면 그대로)

Opens. (left as is if already open or opening)

짝Counterpart: vanilla.js PopupMotion.open

메서드methodpub fn close(&mut self)#0.3.2 부터since 0.3.2소스source

닫기. (이미 닫혔거나 닫히는 중이면 그대로)

Closes. (left as is if already closed or closing)

짝Counterpart: vanilla.js PopupMotion.close

메서드methodpub fn tick(&mut self, time_delta: f64)#0.3.2 부터since 0.3.2소스source

갱신. (등장 / 퇴장 시간이 다 차면 다음 상태로 넘기고 알린다)

Update. (moves to the next state and notifies once the appear / disappear time is up)

time_deltaf64
지난 시간. (초, 여는 중이나 닫는 중일 때만 쌓는다)Elapsed time. (seconds, accumulated only while opening or closing)

짝Counterpart: vanilla.js PopupMotion.tick

메서드methodpub fn get_scale(&self) -> f64#0.3.2 부터since 0.3.2소스source

지금 배율 반환.

Returns the current scale.

반환Returnsf64 판에 걸 배율. (닫힘 0, 열림 1, 여는 중 0.45 에서 1.18 을 거쳐 1, 닫는 중 1 에서 1.08 을 거쳐 0.08)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)

짝Counterpart: vanilla.js PopupMotion.getScale

메서드methodpub fn get_dim_ratio(&self) -> f64#0.3.2 부터since 0.3.2소스source

어둡게 가리는 비율 반환. (0 ~ 1)

Returns the dimming ratio. (0 ~ 1)

반환Returnsf64 막의 알파에 곱할 비율. (0 ~ 1, 여는 동안 0 에서 1 로, 닫는 동안 1 에서 0 으로 고르게)Ratio to multiply the overlay's alpha by. (0 ~ 1, evenly from 0 to 1 while opening and from 1 to 0 while closing)

짝Counterpart: vanilla.js PopupMotion.getDimRatio

메서드methodpub fn is_interactive(&self) -> bool#0.3.2 부터since 0.3.2소스source

조작 가능 여부. (완전히 열린 뒤에만 참)

Whether interactable. (true only after fully open)

반환Returnsbool 열림 상태면 true.true if in the open state.

짝Counterpart: vanilla.js PopupMotion.isInteractive

메서드methodpub fn get_state(&self) -> PopupMotionState#0.3.2 부터since 0.3.2소스source

상태 반환.

Returns the state.

반환ReturnsPopupMotionState 지금 상태.The current state.

짝Counterpart: vanilla.js PopupMotion.getState

메서드methodpub fn is_visible(&self) -> bool#0.3.2 부터since 0.3.2소스source

보이는 중인지. (닫힘이 아니면 참)

Whether it is showing. (true unless closed)

반환Returnsbool 닫힘 상태가 아니면 true.true unless in the closed state.

짝Counterpart: vanilla.js PopupMotion.isVisible

메서드methodpub fn set_instant_enabled(&mut self, is_instant_enabled: bool)#0.3.2 부터since 0.3.2소스source

즉시 전환 설정. (연출 없는 UI 톤)

Sets instant transitions. (UI tone without effects)

is_instant_enabledbool
연출 없이 open, close 에서 바로 바꾸려면 true. (기본 false, 이미 여는 중이거나 닫는 중인 전환은 tick 으로 이어진다)true to change right away in open, close without the effect. (default false, a transition already opening or closing continues with tick)

짝Counterpart: vanilla.js PopupMotion.setInstantEnabled

메서드methodpub fn is_instant_enabled(&self) -> bool#0.3.2 부터since 0.3.2소스source

즉시 전환 여부.

Whether transitions are instant.

반환Returnsbool 즉시 전환이면 true.true if transitions are instant.

짝Counterpart: vanilla.js PopupMotion.isInstantEnabled

메서드methodpub fn set_opened_event(&mut self, opened_event: PopupMotionEvent)#0.3.2 부터since 0.3.2소스source

열림 알림 설정.

Sets the open notification.

opened_eventPopupMotionEvent
열림 상태가 될 때 부를 함수. (즉시 전환이면 open 안에서 바로 불린다)Function to call on entering the open state. (called right inside open if transitions are instant)

짝Counterpart: vanilla.js PopupMotion.setOpenedEvent

메서드methodpub fn set_closed_event(&mut self, closed_event: PopupMotionEvent)#0.3.2 부터since 0.3.2소스source

닫힘 알림 설정.

Sets the close notification.

closed_eventPopupMotionEvent
닫힘 상태가 될 때 부를 함수. (즉시 전환이면 close 안에서 바로 불린다)Function to call on entering the closed state. (called right inside close if transitions are instant)

짝Counterpart: vanilla.js PopupMotion.setClosedEvent

PopupMotionStateenum traits Clone, Copy, Debug, PartialEq, Eq

짝Counterpart: vanilla.js PopupMotionState소스source

전환 상태.

Transition state.

이름Name설명Description
Closed닫힘. (배율 0, 가림 0)Closed. (scale 0, dim 0)
Opening여는 중. (배율이 0.45 에서 1.18 로 부풀었다가 1 로)Opening. (scale swells from 0.45 to 1.18 and settles at 1)
Open열림. (배율 1, 가림 1, 이때만 조작할 수 있다)Open. (scale 1, dim 1, the only state that accepts interaction)
Closing닫는 중. (배율이 1.08 로 부풀었다가 0.08 로)Closing. (scale swells to 1.08 and shrinks to 0.08)