vanilladocs
vanillapunch.com
API / ui/uidraggable

ui/uidraggable

드래그 가능 컴포넌트.

Draggable component.

UIDraggableextends UIControl

짝Counterpart: vanilla-engine UIDraggable

드래그 가능 컴포넌트.

  • 노드에 붙이면 눌러서 끌 수 있다. 놓으면 dragEndEvent 로 알리고, setSnapBackEnabled(true) 면 잡기 전 자리로 미끄러져 돌아간다.
  • 이동량은 뷰 좌표 차분을 그대로 로컬 좌표에 더한다. (조상 노드에 배율이 있으면 감도가 달라질 수 있다)
  • 사용:
const draggable = cardNode.addComponent(UIDraggable);
draggable.setSnapBackEnabled(true);
draggable.setDragEndEvent((self, viewPosition) => { ... });

Draggable component.

  • Attached to a node, it can be pressed and dragged. On release it notifies via dragEndEvent, and if setSnapBackEnabled(true), it slides back to where it was before being grabbed.
  • The movement adds the view coordinate delta directly to the local position. (sensitivity may differ if an ancestor node has a scale)
  • Usage:
const draggable = cardNode.addComponent(UIDraggable);
draggable.setSnapBackEnabled(true);
draggable.setDragEndEvent((self, viewPosition) => { ... });
예제Example칸에 놓으면 머물고 밖에 놓으면 돌아가는 카드A card that stays when dropped in the slot and returns when dropped outside
import { Engine, EngineConfiguration, UIScene, UIDraggable, WorldNode, Paint, Color, Pivot, Vector2 } from "./build/vanilla.js";

class DragScene extends UIScene {
	create() {
		super.create();
		const root = this.getRoot();

		const slotNode = new WorldNode();
		slotNode.setPivot(Pivot.topLeft.clone());
		slotNode.setAnchor(Pivot.topLeft.clone());
		slotNode.setContentSize(Vector2.create(140, 100));
		slotNode.setLocalPosition(Vector2.create(420, 130));
		const slotPaint = slotNode.addComponent(Paint);
		slotPaint.setColor(new Color(1, 1, 1, 0.1));
		root.addChild(slotNode);

		const cardNode = new WorldNode();
		cardNode.setPivot(Pivot.topLeft.clone());
		cardNode.setAnchor(Pivot.topLeft.clone());
		cardNode.setContentSize(Vector2.create(120, 80));
		cardNode.setLocalPosition(Vector2.create(80, 140));
		const cardPaint = cardNode.addComponent(Paint);
		cardPaint.setColor(new Color(0.36, 0.42, 1, 1));
		cardPaint.setRoundSize(8);
		const draggable = cardNode.addComponent(UIDraggable);
		draggable.setSnapBackEnabled(true);
		draggable.setDragEndEvent((self, viewPosition) => {
			if (slotNode.contains(viewPosition)) {
				self.settleHere();
			}
		});
		root.addChild(cardNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new DragScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
생성자constructorconstructor()#0.3.0 부터since 0.3.0소스source

생성.

Constructor.

짝Counterpart: vanilla-engine UIDraggable.new

메서드methodattach(node)#0.3.0 부터since 0.3.0소스source

노드에 붙음. (터치를 받도록 인터랙션 활성화)

Attached to a node. (enables interaction to receive touches)

nodeimport("../core/node/componentnode.js").ComponentNode
붙을 노드. (WorldNode 면 setInteractable(true))The node to attach to. (setInteractable(true) if it is a WorldNode)
메서드methodtouchPress(viewInputPosition)#0.3.0 부터since 0.3.0소스source

터치 눌림.

Touch pressed.

viewInputPositionVector2
입력 위치. (뷰 좌표)Input position. (view coordinates)
메서드methodtouchMove(viewInputPosition)#0.3.0 부터since 0.3.0소스source

터치 이동.

Touch move.

viewInputPositionVector2
입력 위치. (뷰 좌표)Input position. (view coordinates)
메서드methodtouchRelease(viewInputPosition)#0.3.0 부터since 0.3.0소스source

터치 뗌.

Touch release.

viewInputPositionVector2
입력 위치. (뷰 좌표)Input position. (view coordinates)
메서드methodtouchCancel(viewInputPosition)#0.3.0 부터since 0.3.0소스source

터치 취소.

Touch cancel.

viewInputPositionVector2
입력 위치. (뷰 좌표)Input position. (view coordinates)
메서드methodtick(timeDelta)#0.3.0 부터since 0.3.0소스source

갱신. (제자리 복귀 미끄러짐)

Update. (snap-back slide)

timeDeltanumber
지난 프레임부터의 시간. (초)Time since the last frame. (Seconds)
메서드methodsettleHere()#0.3.0 부터since 0.3.0소스source

지금 자리에 눌러앉기. (dragEnd 처리기에서 드롭 성공 시 호출, 복귀를 멈춘다)

Settles at the current position. (call on a successful drop in the dragEnd handler, stops the snap-back)

짝Counterpart: vanilla-engine UIDraggable.settle_here

메서드methodsnapHome()#0.3.0 부터since 0.3.0소스source

제자리로 즉시 되돌리기.

Returns to the original position immediately.

짝Counterpart: vanilla-engine UIDraggable.snap_home

메서드methodsetSnapBackEnabled(isSnapBackEnabled)#0.3.0 부터since 0.3.0소스source

놓았을 때 잡기 전 자리로 돌아갈지 설정.

Sets whether it returns to where it was before being grabbed on release.

isSnapBackEnabledboolean
참이면 놓은 뒤 잡기 전 자리로 미끄러져 돌아간다.If true, it slides back to where it was before being grabbed after release.

짝Counterpart: vanilla-engine UIDraggable.set_snap_back_enabled

메서드methodsetSnapBackSpeed(snapBackSpeed)#0.3.0 부터since 0.3.0소스source

되돌아가는 빠르기 설정. (기본 14)

Sets the return speed. (default 14)

snapBackSpeednumber
초당 되돌아가는 비율 계수. (프레임마다 남은 거리의 이 값 곱하기 프레임 초 몫만큼 다가간다, 몫은 1 을 넘지 않는다)Return rate coefficient per second. (each frame it closes in by this value times the frame seconds share of the remaining distance, the share does not exceed 1)

짝Counterpart: vanilla-engine UIDraggable.set_snap_back_speed

메서드methodsetDragStartEvent(dragStartEvent)#0.3.0 부터since 0.3.0소스source

끌기 시작 처리기 설정. (누른 순간 한 번)

Sets the drag start handler. (once, at the moment of pressing)

dragStartEventFunction
(self)

짝Counterpart: vanilla-engine UIDraggable.set_drag_start_event

메서드methodsetDragMoveEvent(dragMoveEvent)#0.3.0 부터since 0.3.0소스source

끄는 중 처리기 설정. (노드를 옮긴 뒤 이동마다)

Sets the dragging handler. (on each move, after moving the node)

dragMoveEventFunction
(self, viewPosition)

짝Counterpart: vanilla-engine UIDraggable.set_drag_move_event

메서드methodsetDragEndEvent(dragEndEvent)#0.3.0 부터since 0.3.0소스source

끌기 끝 처리기 설정. (뗐거나 취소됐을 때, 되돌아가기를 켠 뒤에 부른다)

  • 처리기 안에서 settleHere 를 부르면 그 자리에 머문다.

Sets the drag end handler. (called when released or cancelled, after the return is turned on)

  • Calling settleHere inside the handler makes it stay in place.
dragEndEventFunction
(self, viewPosition)

짝Counterpart: vanilla-engine UIDraggable.set_drag_end_event

예제Example오른쪽 절반에 놓을 때만 머물기Staying only when dropped on the right half
import { UIDraggable, WorldNode, Vector2 } from "./build/vanilla.js";

const cardNode = new WorldNode();
cardNode.setLocalPosition(Vector2.create(100, 100));
const draggable = cardNode.addComponent(UIDraggable);
draggable.setSnapBackEnabled(true);
draggable.setDragEndEvent((self, viewPosition) => {
	if (viewPosition.x > 320) {
		self.settleHere();
	}
});
draggable.touchPress(Vector2.create(110, 110));
draggable.touchMove(Vector2.create(160, 120));
draggable.touchRelease(Vector2.create(160, 120));
draggable.tick(1);
const localPosition = cardNode.getLocalPosition();
console.log(localPosition.x, localPosition.y);
메서드methodisDragging()#0.3.0 부터since 0.3.0소스source

끄는 중인지 반환.

Returns whether it is being dragged.

반환Returnsboolean 눌러서 끄는 중이면 참. (되돌아가는 중은 거짓)True while being dragged. (false while returning)

짝Counterpart: vanilla-engine UIDraggable.is_dragging

메서드methodgetHomePosition()#0.3.0 부터since 0.3.0소스source

잡기 전 자리 반환. (settleHere 를 불렀으면 그 자리)

Returns the position before being grabbed. (that position if settleHere was called)

반환ReturnsVector2 | null 로컬 자리의 복사본. (한 번도 누르지 않았으면 null)Copy of the local position. (null if never pressed)

짝Counterpart: vanilla-engine UIDraggable.get_home_position