vanilladocs
vanillapunch.com
API / ui/buttonmotion

ui/buttonmotion

단추 반응. (vanilla.js ui/buttonmotion.js 의 ButtonMotion, ButtonMotionStyle, attachButtonMotion, setButtonMotionArea, shakeButton)

  • 단추 노드에 UIButton 다음으로 붙이면, 프레임마다 그 단추의 상태를 읽어 노드를 가운데를 두고 키우고 줄인다. 올리면 커지고, 누르는 동안 줄고, 떼면 통 튀고, 누를 수 없을 때 shake 를 부르면 좌우로 흔들린다.
  • 색은 건드리지 않는다. (글 단추의 바탕과 글자 색은 화면의 코드가 정하고, 그림 단추는 UIButton 의 틴트가 맡는다)
  • 손가락이 주된 기기는 올림을 쓰지 않는다. (뗀 뒤에도 손가락 자리가 남아 올린 채로 보이지 않게)
  • 반응이 없을 때는 노드의 자리와 크기를 건드리지 않아, 다른 곳이 같은 노드를 키우는 연출과 겹치지 않는다.
  • 꼴은 set_style 로 단추마다 정하거나, 비워 두면 위 노드 가운데 ButtonMotionArea 를 건 가장 가까운 것을 따르고, 없으면 Normal 이다.
  • Inset 꼴에서 올린 동안은 단추 바탕 Paint 의 색을 밝히고 안쪽에 흰 테를 그린다. (바탕 Paint 가 없거나 보이지 않는 단추는 밝히지 않는다) 단추에 얹은 꾸밈이 제 모양으로 밝히게 하려면 set_lit_handler 로 함수를 건다. (그때 단추의 눌림 색은 투명으로 둔다) 건 함수는 Rc<RefCell> 에 담는다. get_lit_handler 는 그 Rc 를 돌려주고, 부르는 쪽은 borrow_mut 로 빌려 부른다.
  • 원본과 다른 것: 가운데를 두는 셈이 피봇을 읽는다. (원본은 왼쪽 위 피봇만), 꼴을 거는 표시는 ButtonMotionArea 컴포넌트다. (원본은 WeakMap) 밝힌 바탕과 흰 테를 자식 노드로 두지 않는다. (원본은 맨 밑 자식 둘, 러스트 예제처럼 글이 같은 노드의 컴포넌트면 자식이 글을 덮는다)

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)

ButtonLitHandlertype

소스source

밝힘을 맡는 함수. (문맥, 밝히는지, 누르는 중인지)

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

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

ButtonMotionAreastruct traits Component

소스source

노드 밑의 단추가 따를 꼴을 거는 표시. (창이 제 판에 건다)

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

예제Example판 안의 단추를 Inset 꼴로Make the buttons inside a panel Inset style
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);
생성자constructorpub fn new(style: ButtonMotionStyle) -> ButtonMotionArea#0.4.0 부터since 0.4.0소스source

생성. (노드에 걸 때는 set_button_motion_area 를 쓴다)

Creates one. (use set_button_motion_area to set it on a node)

styleButtonMotionStyle
이 노드 밑의 단추가 따를 꼴.Style for buttons under this node.

반환ReturnsButtonMotionArea 그 꼴을 거는 표시.Marker that sets that style.

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

건 꼴 반환.

Returns the set style.

반환ReturnsButtonMotionStyle 이 노드 밑의 단추가 따를 꼴.Style for buttons under this node.

ButtonMotionstruct traits Default, Component

짝Counterpart: vanilla.js ButtonMotion소스source

단추 반응. (단추 노드에 UIButton 다음으로 붙어, 단추 상태에 따라 노드를 가운데를 두고 키우고 줄인다)

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
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);
생성자constructorpub fn new() -> ButtonMotion#0.4.0 부터since 0.4.0소스source

생성.

Constructor.

반환ReturnsButtonMotion 꼴을 정하지 않은 새 단추 반응. (위 노드에 건 꼴을 따른다, 단추 노드에 붙일 때는 attach_button_motion 을 쓴다)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)

짝Counterpart: vanilla.js ButtonMotion.constructor

메서드methodpub fn set_style(&mut self, style: Option<ButtonMotionStyle>)#0.4.0 부터since 0.4.0소스source

이 단추의 꼴 설정. (None 이면 위 노드에 건 꼴을 따른다)

Sets this button's style. (follows the style set on nodes above if None)

styleOption<ButtonMotionStyle>
이 단추의 꼴. (None 이면 위 노드에 건 가장 가까운 꼴, 그것도 없으면 Normal)This button's style. (if None, the closest style set on nodes above, or Normal if none)

짝Counterpart: vanilla.js ButtonMotion.setStyle

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

누를 수 없을 때 흔들기. (누른 쪽에서 부른다)

Shakes when not pressable. (Called by the pressing side)

짝Counterpart: vanilla.js ButtonMotion.shake

메서드methodpub fn set_lit_handler(&mut self, lit_handler: Option<ButtonLitHandler>)#0.4.0 부터since 0.4.0소스source

밝힘을 맡는 함수 설정. (None 이면 이 부품이 바탕 Paint 의 색을 밝히고 draw 가 안쪽 흰 테를 그린다)

Sets the function that handles highlighting. (if None, this part brightens the background Paint color and draw draws an inner white rim)

lit_handlerOption<ButtonLitHandler>
밝힘을 맡을 함수. (문맥, 밝히는지, 누르는 중인지를 받아 tick 마다 불린다, None 이면 함수를 뗀다)Function to handle brightening. (takes context, whether lit, whether pressed and is called every tick, removes the function if None)

짝Counterpart: vanilla.js ButtonMotion.setLitHandler

예제Example얹은 빛 노드로 밝히기Brighten with a light node placed on top
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());
메서드methodpub fn get_lit_handler(&self) -> Option<Rc<RefCell<ButtonLitHandler>>>#0.4.0 부터since 0.4.0소스source

밝힘을 맡는 함수 반환. (부를 때는 borrow_mut 로 빌린다)

Returns the function that handles brightening. (borrow it with borrow_mut to call it)

반환ReturnsOption<Rc<RefCell<ButtonLitHandler>>> 건 밝힘 함수. 걸지 않았으면 None.The hooked brightening function. None if none is hooked.

짝Counterpart: vanilla.js ButtonMotion.getLitHandler

함수Functions

함수functionpub fn set_button_motion_area(node_tree: &mut NodeTree, node_id: NodeId, style: ButtonMotionStyle)#0.4.0 부터since 0.4.0소스source

노드 밑의 단추가 따를 꼴 걸기. (창이 제 판에 Inset 을 걸면 그 안의 단추는 커지지 않는다, 단추가 set_style 로 정한 꼴이 먼저)

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_idNodeId
꼴을 걸 노드. (창의 판 같은, 이미 ButtonMotionArea 가 붙어 있으면 꼴만 바꾸고 노드가 없으면 아무 일도 없다)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)
styleButtonMotionStyle
그 노드 밑의 단추가 따를 꼴. (단추에서 위로 거슬러 올라 가장 가까운 것을 따른다)Style for buttons under that node. (a button walks up and follows the closest one)

짝Counterpart: vanilla.js setButtonMotionArea

함수functionpub fn attach_button_motion(node_tree: &mut NodeTree, node_id: NodeId) -> Option<&mut ButtonMotion>#0.4.0 부터since 0.4.0소스source

단추 노드에 반응 붙이기. (UIButton 이 이미 붙은 노드, 붙인 ButtonMotion 을 돌려준다. 원본 attachButtonMotion)

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_idNodeId
UIButton 이 이미 붙은 단추 노드. (반응은 UIButton 다음 컴포넌트로 붙는다)Button node with UIButton already attached. (the motion is attached as the component after UIButton)

반환ReturnsOption<&mut ButtonMotion> 붙인 ButtonMotion. 노드가 없으면 None.The attached ButtonMotion. None if the node is missing.

짝Counterpart: vanilla.js attachButtonMotion

함수functionpub fn shake_button(node_tree: &mut NodeTree, node_id: NodeId)#0.4.0 부터since 0.4.0소스source

단추 흔들기. (노드의 ButtonMotion 을 찾아 흔든다, 없으면 아무 일도 없다)

  • 0.25 초 동안 좌우로 흔들린다.

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_idNodeId
ButtonMotion 이 붙은 단추 노드.Button node with a ButtonMotion attached.

짝Counterpart: vanilla.js shakeButton

ButtonMotionStyleenum traits Clone, Copy, PartialEq, Eq, Debug

짝Counterpart: vanilla.js ButtonMotionStyle소스source

단추 반응의 꼴.

Style of button motion.

이름Name설명Description
Normal올리면 커지고, 누르는 동안 줄고, 떼면 통 튄다.Grows on hover, shrinks while pressed and pops on release.
Inset창 안의 단추. 1 배를 넘게 커지지 않아 창 안쪽 여백을 넘지 않는다. 올리면 바탕이 밝아지고 안쪽에 흰 테가 서며, 누르면 조금 줄었다 돌아온다.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.