vanilladocs
vanillapunch.com
API / ui/uidraggable

ui/uidraggable

드래그 가능 컴포넌트. (원본 ui/uidraggable.js 의 UIDraggable)

  • 노드에 붙이면 눌러서 끌 수 있다. 놓으면 끝 이벤트로 알리고, set_snap_back_enabled(true) 면 잡기 전 자리로 미끄러져 돌아간다.
  • 이동량은 뷰 좌표 차이를 그대로 로컬 좌표에 더한다. (조상 노드에 배율이 있으면 감도가 달라진다. 원본과 같음)
  • 원본은 UIControl 을 상속한다. 러스트 판은 UIControl 을 품고(ui_control 필드) content, 배경색을 넘겨준다. attach_to_tree 에서 호스트 노드를 interactable 로 켠다. node_tree.add_component(node_id, UIDraggable::new()) 로 붙인다.
  • 사용:
let draggable = node_tree.add_component(card_id, UIDraggable::new()).expect("draggable");
draggable.set_snap_back_enabled(true);
draggable.set_drag_end_event(Box::new(|draggable, context, view_position| { if 놓을 자리면 { draggable.settle_here(context.get_node_tree_mut()); } }));
  • 원본과 다른 것:
  • 이벤트는 드래그 컴포넌트와 문맥을 받는다. (이동 / 끝은 뷰 좌표도) 원본 콜백은 컴포넌트(와 뷰 좌표)만 받는다.
  • settle_here / snap_home 은 노드 자리를 읽고 써야 해서 트리를 받는다. (끝 이벤트 안에서는 context.get_node_tree_mut())

Draggable component. (UIDraggable of the original ui/uidraggable.js)

  • Attached to a node, it can be pressed and dragged. On release it notifies with the end event, and with set_snap_back_enabled(true) it slides back to where it was before being grabbed.
  • The movement adds the view coordinate difference as is to the local coordinates. (sensitivity differs if an ancestor node has a scale. same as the original)
  • The original inherits UIControl. The Rust version embeds UIControl (ui_control field) and passes through content and the background color. attach_to_tree turns the host node interactable. Attach with node_tree.add_component(node_id, UIDraggable::new()).
  • Usage:
let draggable = node_tree.add_component(card_id, UIDraggable::new()).expect("draggable");
draggable.set_snap_back_enabled(true);
draggable.set_drag_end_event(Box::new(|draggable, context, view_position| { if is_drop_spot { draggable.settle_here(context.get_node_tree_mut()); } }));
  • Differences from the original:
  • Events take the drag component and the context. (move / end also the view coordinates) Original callbacks take only the component (and the view coordinates).
  • settle_here / snap_home need to read and write the node position, so they take the tree. (context.get_node_tree_mut() inside the end event)

UIDraggableEventtype

소스source

드래그 이벤트. (드래그 컴포넌트와 문맥을 받는다. 시작 이벤트)

Drag event. (takes the drag component and the context. start event)

pub type UIDraggableEvent = Box<dyn FnMut(&mut UIDraggable, &mut ComponentContext)>

UIDraggablePositionEventtype

소스source

드래그 자리 이벤트. (드래그 컴포넌트, 문맥, 뷰 좌표. 이동 / 끝 이벤트)

Drag position event. (drag component, context, view coordinates. move / end events)

pub type UIDraggablePositionEvent = Box<dyn FnMut(&mut UIDraggable, &mut ComponentContext, Vector2)>

UIDraggablestruct traits Default, Component

짝Counterpart: vanilla.js UIDraggable소스source

드래그 가능 컴포넌트.

Draggable component.

예제Example놓을 자리에 놓으면 눌러앉고, 아니면 제자리로 돌아가는 카드A card that settles when dropped on a drop spot and returns to its place otherwise
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIDraggable, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let card_id = node_tree.create_child(root_id, "Card");
node_tree[card_id].set_local_position(Vector2::create(100.0, 100.0));
node_tree[card_id].set_content_size(Vector2::create(50.0, 50.0));
let draggable = node_tree.add_component(card_id, UIDraggable::new()).expect("draggable");
draggable.set_snap_back_enabled(true);
draggable.set_drag_end_event(Box::new(|draggable: &mut UIDraggable, context: &mut ComponentContext, view_position: Vector2| {
	if view_position.x > 300.0 {
		draggable.settle_here(context.get_node_tree_mut());
	}
}));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Press, Vector2::create(100.0, 100.0));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Moved, Vector2::create(400.0, 100.0));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Release, Vector2::create(400.0, 100.0));
node_tree.tick(&engine, 0.5);
assert_eq!(node_tree[card_id].get_local_position(), Vector2::create(400.0, 100.0));
assert_eq!(node_tree[card_id].get_component::<UIDraggable>().expect("draggable").get_home_position(), Some(Vector2::create(400.0, 100.0)));
생성자constructorpub fn new() -> UIDraggable#0.3.2 부터since 0.3.2소스source

생성. (복귀 꺼짐, 복귀 속도 14)

Constructor. (snap back off, snap back speed 14)

반환ReturnsUIDraggable 붙기 전의 드래그 컴포넌트.Drag component before being attached.

짝Counterpart: vanilla.js UIDraggable.constructor

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

품은 컨트롤 반환.

Returns the embedded control.

반환Returns&UIControl 품은 UIControl.The embedded UIControl.

메서드methodpub fn get_uicontrol_mut(&mut self) -> &mut UIControl#0.3.2 부터since 0.3.2소스source

품은 컨트롤 반환. (변경용)

Returns the embedded control. (mutable)

반환Returns&mut UIControl 품은 UIControl.The embedded UIControl.

메서드methodpub fn get_content(&self) -> Option<NodeId>#0.3.2 부터since 0.3.2소스source

컨텐트 노드 핸들.

Content node handle.

반환ReturnsOption<NodeId> 컨텐트 노드. (트리에 붙기 전이면 None)Content node. (None before being attached to a tree)

짝Counterpart: vanilla.js UIView.getContent

메서드methodpub fn set_background_color(&mut self, background_color: impl Into<Color>)#0.3.2 부터since 0.3.2소스source

배경색 설정.

Sets the background color.

background_colorimpl Into<Color>
배경색. (품은 UIControl 이 그린다)Background color. (drawn by the embedded UIControl)

짝Counterpart: vanilla.js UIView.setBackgroundColor

메서드methodpub fn settle_here(&mut self, node_tree: &mut NodeTree)#0.3.2 부터since 0.3.2소스source

지금 자리에 눌러앉기. (끝 이벤트에서 놓기에 성공했을 때 부른다. 복귀를 멈춘다)

Settles at the current position. (called when the drop succeeds in the end event. stops the snap back)

node_tree&mut NodeTree
드래그 노드가 든 노드 트리. (지금 로컬 자리를 제자리로 삼는다, 끝 이벤트 안에서는 context.get_node_tree_mut())Node tree that holds the drag node. (makes the current local position the home position, context.get_node_tree_mut() inside the end event)

짝Counterpart: vanilla.js UIDraggable.settleHere

메서드methodpub fn snap_home(&mut self, node_tree: &mut NodeTree)#0.3.2 부터since 0.3.2소스source

제자리로 바로 되돌리기.

Returns to the home position immediately.

node_tree&mut NodeTree
드래그 노드가 든 노드 트리. (잡은 적이 없으면 자리는 그대로이고 복귀만 멈춘다)Node tree that holds the drag node. (if never grabbed, the position stays and only the snap back stops)

짝Counterpart: vanilla.js UIDraggable.snapHome

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

놓으면 제자리로 돌아갈지 설정.

Sets whether to return to the home position on release.

is_snap_back_enabledbool
놓으면 제자리로 미끄러져 돌아가면 true. (기본 false)true to slide back to the home position on release. (default false)

짝Counterpart: vanilla.js UIDraggable.setSnapBackEnabled

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

복귀 속도 설정. (초당 남은 거리를 줄이는 비율 계수)

Sets the snap back speed. (rate factor that reduces the remaining distance per second)

snap_back_speedf64
복귀 속도. (tick 마다 남은 거리의 속도 x 초 몫만큼 다가가고, 그 몫이 1 을 넘으면 바로 닿는다. 기본 14)Snap back speed. (each tick moves closer by speed x seconds of the remaining distance, and arrives immediately if that fraction exceeds 1. default 14)

짝Counterpart: vanilla.js UIDraggable.setSnapBackSpeed

예제Example속도 10 이면 0.05 초에 남은 거리의 반을 간다At speed 10, covers half the remaining distance in 0.05 seconds
use vanilla::{Engine, EngineConfiguration, NodeTree, TouchKind, UIDraggable, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let card_id = node_tree.create_child(root_id, "Card");
node_tree[card_id].set_local_position(Vector2::create(100.0, 100.0));
if let Some(draggable) = node_tree.add_component(card_id, UIDraggable::new()) {
	draggable.set_snap_back_enabled(true);
	draggable.set_snap_back_speed(10.0);
}
node_tree.dispatch_touch(card_id, &engine, TouchKind::Press, Vector2::create(100.0, 100.0));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Moved, Vector2::create(200.0, 100.0));
node_tree.dispatch_touch(card_id, &engine, TouchKind::Release, Vector2::create(200.0, 100.0));
node_tree.tick(&engine, 0.05);
assert!((node_tree[card_id].get_local_position().x - 150.0).abs() < 0.001);
메서드methodpub fn set_drag_start_event(&mut self, drag_start_event: UIDraggableEvent)#0.3.2 부터since 0.3.2소스source

시작 이벤트 설정.

Sets the start event.

drag_start_eventUIDraggableEvent
눌러 잡을 때 부르는 이벤트. (잡기 전 자리를 기억한 뒤)Event called when pressed and grabbed. (after remembering the position before the grab)

짝Counterpart: vanilla.js UIDraggable.setDragStartEvent

메서드methodpub fn set_drag_move_event(&mut self, drag_move_event: UIDraggablePositionEvent)#0.3.2 부터since 0.3.2소스source

이동 이벤트 설정.

Sets the move event.

drag_move_eventUIDraggablePositionEvent
끄는 동안 움직일 때마다 부르는 이벤트. (노드를 옮긴 뒤, 터치의 뷰 좌표를 받는다)Event called on every move while dragging. (after moving the node, takes the touch's view coordinates)

짝Counterpart: vanilla.js UIDraggable.setDragMoveEvent

메서드methodpub fn set_drag_end_event(&mut self, drag_end_event: UIDraggablePositionEvent)#0.3.2 부터since 0.3.2소스source

끝 이벤트 설정.

Sets the end event.

drag_end_eventUIDraggablePositionEvent
놓거나 터치가 취소되면 부르는 이벤트. (터치의 뷰 좌표를 받는다, 복귀를 켰으면 돌아가기 시작한 뒤라 settle_here 로 멈춘다)Event called on release or when the touch is canceled. (takes the touch's view coordinates, if snap back is on it has already started returning, so stop it with settle_here)

짝Counterpart: vanilla.js UIDraggable.setDragEndEvent

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

끄는 중인지.

Whether it is being dragged.

반환Returnsbool 눌러 잡은 뒤 놓기 전이면 true.true if after pressing and grabbing and before release.

짝Counterpart: vanilla.js UIDraggable.isDragging

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

돌아가는 중인지.

Whether it is returning.

반환Returnsbool 제자리로 미끄러지는 중이면 true. (0.5 픽셀 안에 들면 false)true if sliding back to the home position. (false once within 0.5 pixels)

메서드methodpub fn get_home_position(&self) -> Option<Vector2>#0.3.2 부터since 0.3.2소스source

잡기 전 로컬 자리. (한 번도 잡지 않았으면 None)

Local position before the grab. (None if never grabbed)

반환ReturnsOption<Vector2> 제자리. (마지막으로 잡기 전 로컬 자리, settle_here 뒤에는 눌러앉은 자리, 잡은 적이 없으면 None)Home position. (the local position before the last grab, the settled position after settle_here, None if never grabbed)

짝Counterpart: vanilla.js UIDraggable.getHomePosition