vanilladocs
vanillapunch.com
API / ui/toastmotion

ui/toastmotion

알림이 나오고 사라지는 모션. (vanilla.js ui/toastmotion.js 의 ToastMotion, ToastPhase, readToastSeconds, attachToastMotion)

  • 알림 노드에 붙여 show 로 띄운다. 바깥쪽(위 알림은 위, 아래 알림은 아래)에서 미끄러져 들어오며 보이고 살짝 커졌다 돌아오고, 머무는 시간이 지나면 떠오르며 사라진다.
  • 떠 있는 동안 다른 글이 오면 앞 것이 빨리 비키고 새 글이 들어오며, 같은 글이 또 오면 시간만 다시 채우고 통 튄다.
  • 노드의 자리는 놓인 자리를 기준으로 그 둘레에서만 움직인다. (그 사이 다시 놓으면 그 자리가 새 기준)
  • 원본과 다른 것: show 는 요청만 받아 두고 다음 tick 에서 노드를 움직인다. (장면 코드는 노드 문맥 없이 부르므로) 글은 show 에 준 함수가 나오기 시작할 때 노드에 넣는다. (원본 show(text, seconds, applyText) 와 같다) 숨은 동안 노드는 끄지 않고 보이지 않게만 한다. (tick 이 계속 돌게)
  • UIToast 는 줄 세운 알약 알림을 스스로 만들고, 이것은 게임이 만든 알림 노드를 움직인다.

Motion for a notification appearing and disappearing. (ToastMotion, ToastPhase, readToastSeconds, attachToastMotion of vanilla.js ui/toastmotion.js)

  • Attach it to a notification node and show it with show. It slides in from the outside (upper notifications from above, lower notifications from below), is visible, grows slightly and returns, and when the stay time passes it floats up and disappears.
  • If another text arrives while it is up, the earlier one moves out quickly and the new text comes in; if the same text arrives again, only the time is refilled and it bounces once.
  • The node's position moves only around the placed position as the reference. (if placed again in between, that position becomes the new reference)
  • Differences from the original: show only records the request and moves the node on the next tick. (scene code calls it without a node context) The text is put into the node when the function given to show starts to appear. (same as the original show(text, seconds, applyText)) While hidden, the node is not turned off but only made invisible. (so tick keeps running)
  • UIToast creates stacked pill notifications by itself, and this one moves a notification node made by the game.

ToastApplyTexttype

소스source

노드에 글과 색을 넣는 함수. (원본 show 의 applyText, 나오기 시작할 때 한 번 부른다, 같은 노드의 UILabel 같은 컴포넌트는 문맥의 노드에서 찾는다)

A function that puts text and color into the node. (applyText of the original show; called once when it starts to appear; components like a UILabel on the same node are found from the context's node)

pub type ToastApplyText = Box<dyn FnOnce(&mut ComponentContext)>

ToastMotionstruct traits Component

짝Counterpart: vanilla.js ToastMotion소스source

알림 모션. (게임이 만든 알림 노드에 붙어, show 로 띄우면 나오고 머물다 사라지는 움직임을 맡는다)

Toast motion. (attached to a toast node made by the game, handles the movement that appears on show, stays, and disappears)

예제Example게임이 만든 알림 노드 띄우기Showing a notification node made by the game
use vanilla::{attach_toast_motion, ComponentContext, Engine, EngineConfiguration, NodeTree, Text, ToastMotion, ToastPhase, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let toast_id = node_tree.create_child(root_id, "Toast");
node_tree[toast_id].set_local_position(Vector2::create(640.0, 80.0));
node_tree[toast_id].set_content_size(Vector2::create(320.0, 48.0));
node_tree[toast_id].add_component(Text::create_with_text("", 24.0));
node_tree[toast_id].set_visible(false);
let toast_motion = attach_toast_motion(&mut node_tree, toast_id, -1.0).expect("toast motion");
toast_motion.show("Saved", 1.0, |context: &mut ComponentContext| {
	if let Some(text_component) = context.get_node_mut().get_component_mut::<Text>() {
		text_component.set_text("Saved");
	}
});
assert!(toast_motion.is_showing());

node_tree.tick(&engine, 0.016);
assert!(node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_component::<Text>().expect("text").get_text(), "Saved");
let toast_motion = node_tree[toast_id].get_component::<ToastMotion>().expect("toast motion");
assert_eq!(toast_motion.get_phase(), ToastPhase::Enter);

for _frame_index in 0..120 {
	node_tree.tick(&engine, 1.0 / 60.0);
}
assert!(!node_tree[toast_id].is_visible());
assert_eq!(node_tree[toast_id].get_local_position(), Vector2::create(640.0, 80.0));
생성자constructorpub fn new() -> ToastMotion#0.4.0 부터since 0.4.0소스source

생성. (원본 new ToastMotion(), 위에서 들어온다, 들어오는 쪽은 set_enter_side 로 바꾼다)

Creates. (the original new ToastMotion(); it comes in from above, and the entering side is changed with set_enter_side)

반환ReturnsToastMotion 숨은 새 모션. (노드에 붙일 때는 attach_toast_motion 을 쓴다)New hidden motion. (use attach_toast_motion to attach it to a node)

짝Counterpart: vanilla.js ToastMotion.constructor

메서드methodpub fn show(&mut self, text: &str, seconds: f64, apply_text: impl FnOnce(&mut ComponentContext) + 'static)#0.4.0 부터since 0.4.0소스source

띄우기 요청. (다음 tick 에서 띄운다, 떠 있는 다른 글은 빨리 비킨 뒤 띄운다)

Requests showing. (shown on the next tick, any other text showing quickly gets out of the way first)

text&str
띄울 글. (떠 있는 글과 같고 사라지는 중이 아니면 머무는 시간만 다시 채우고 통 튄다)Text to show. (if it is the same as the text showing and not disappearing, only refills the stay time and gives a little bounce)
secondsf64
머무는 시간. (초, 나오는 시간과 사라지는 시간은 빼고, read_toast_seconds 로 글 길이에 맞출 수 있다)Stay time. (seconds, excluding the appear and disappear times, read_toast_seconds can fit it to the text length)
apply_textimpl FnOnce(&mut ComponentContext) + 'static
노드에 글과 색을 넣는 함수. (나오기 시작할 때 문맥을 받아 한 번 부른다, 원본 applyText)A function that puts text and color into the node. (receives the context and is called once when it starts to appear; the original applyText)

짝Counterpart: vanilla.js ToastMotion.show

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

곧바로 숨기기 요청. (판이 바뀔 때)

Requests immediate hiding. (when the stage changes)

짝Counterpart: vanilla.js ToastMotion.hideNow

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

떠 있는지. (나오는 중, 머무는 중, 사라지는 중, 띄우기 요청이 있음)

Whether it is showing. (appearing, staying, disappearing, or a show request pending)

반환Returnsbool 숨음 단계가 아니거나 띄우기 요청이 있으면 true.true if not in the hidden phase or a show request is pending.

짝Counterpart: vanilla.js ToastMotion.isShowing

메서드methodpub fn get_phase(&self) -> ToastPhase#0.4.0 부터since 0.4.0소스source

단계 반환.

Returns the phase.

반환ReturnsToastPhase 지금 단계.Current phase.

짝Counterpart: vanilla.js ToastMotion.getPhase

메서드methodpub fn set_enter_side(&mut self, enter_side: f64)#0.4.0 부터since 0.4.0소스source

들어오는 쪽 설정.

Sets the entry side.

enter_sidef64
들어오는 쪽. (-1 이면 위에서, 1 이면 아래에서)Entry side. (-1 from the top, 1 from the bottom)

짝Counterpart: vanilla.js ToastMotion.setEnterSide

함수Functions

함수functionpub fn read_toast_seconds(text: &str, base_seconds: f64) -> f64#0.4.0 부터since 0.4.0소스source

알림 머무는 시간. (긴 글이면 20 자마다 0.5 초를 더하고 4 초까지)

Toast stay time. (for long text, adds 0.5 seconds per 20 characters, up to 4 seconds)

text&str
알림 글. (글자 수는 char 로 센다)Toast text. (characters are counted by char)
base_secondsf64
기본 머무는 시간. (초)Base stay time. (seconds)

반환Returnsf64 머무는 시간. (초, 20 자를 넘은 글자를 20 자마다 올림해 0.5 초씩 더하고, 4 초를 넘지 않는다. base_seconds 가 4 초보다 길어도 4)Stay time. (seconds, adds 0.5 seconds for every 20 characters beyond 20, rounded up, and never exceeds 4 seconds. 4 even if base_seconds is longer than 4 seconds)

짝Counterpart: vanilla.js readToastSeconds

예제Example글 길이에 따른 머무는 초Stay seconds by text length
use vanilla::read_toast_seconds;

assert_eq!(read_toast_seconds("Saved", 2.0), 2.0);
assert_eq!(read_toast_seconds(&"a".repeat(45), 2.0), 3.0);
assert_eq!(read_toast_seconds(&"a".repeat(400), 2.0), 4.0);
함수functionpub fn attach_toast_motion(node_tree: &mut NodeTree, node_id: NodeId, enter_side: f64) -> Option<&mut ToastMotion>#0.4.0 부터since 0.4.0소스source

알림 노드에 모션 붙이기. (들어오는 쪽을 정하고 붙인 ToastMotion 을 돌려준다, 원본 attachToastMotion)

Attaches a motion to a toast node. (sets the entry side and returns the attached ToastMotion, original attachToastMotion)

node_tree&mut NodeTree
노드 트리.Node tree.
node_idNodeId
알림 노드.Notification node.
enter_sidef64
들어오는 쪽. (-1 이면 위에서, 1 이면 아래에서)Entry side. (-1 from the top, 1 from the bottom)

반환ReturnsOption<&mut ToastMotion> 붙인 ToastMotion. 노드가 없으면 None.The attached ToastMotion. None if there is no node.

짝Counterpart: vanilla.js attachToastMotion

ToastPhaseenum traits Clone, Copy, PartialEq, Eq, Debug

짝Counterpart: vanilla.js ToastPhase소스source

알림의 단계.

Notification phases.

이름Name설명Description
Hidden숨음. (노드는 끄지 않고 보이지 않게만 둔다)Hidden. (the node is not turned off, only made invisible)
Enter나오는 중. (미끄러져 들어오며 보이고 살짝 커졌다 돌아온다)Entering. (slides in while appearing, grows slightly and settles)
Stay머무는 중. (머무는 초를 센다)Staying. (counts down the stay seconds)
Exit사라지는 중. (떠오르며 투명해진다)Exiting. (rises while fading out)