vanilladocs
vanillapunch.com
API / ui/touchrecognizer

ui/touchrecognizer

드래그로 전환되었다고 판정할 거리(px).

Distance (px) at which it is judged to have switched to a drag.

TouchRecognizerextends TouchRaycaster

짝Counterpart: vanilla-engine TouchRecognizer

터치 인식기.

  • TouchRaycaster 를 확장해 UIKit 스타일 제스처 인식을 한다.
  • 누른 노드의 부모 체인에서 UIScrollView 를 가진 노드를 찾아두고, 드래그 임계를 넘으면: 1) 누른 노드에 touchCancel 을 보내 클릭을 취소 2) 그 위 ScrollView 에 touchPress(최초 누른 위치) → touchMove(현재) 를 순서대로 보내 스크롤을 인계한다.
  • 임계 이내면 누른 노드에만 touch* 를 보낸다. (기본 버튼처럼 동작)
  • ScrollView 가 setPassesCrossAxisDrag(true) 면, 스크롤하지 않는 축으로 더 많이 끈 손가락은 인계하지 않는다. 보류된 press 를 곧바로 hit 노드에 보내고 그 뒤 move 도 hit 노드에 보낸다. (스크롤 안의 슬라이더를 가로로 끌 때)
  • 부모에 ScrollView 가 있을 때는 hit 노드의 touchPress 전달을 DELAY_PRESS_MS 만큼 지연시킨다. 그 사이 드래그로 판명나면 press 가 아예 전달되지 않으므로 "프레스 트렌지션 도중 취소" 가 발생하지 않는다. 짧은 탭은 release 시점에 보류된 press 를 즉시 발동 후 release 를 이어 보내 보정한다.

Touch recognizer.

  • Extends TouchRaycaster to do UIKit-style gesture recognition.
  • Finds the node with a UIScrollView in the pressed node's parent chain in advance, and when the drag threshold is exceeded: 1) sends touchCancel to the pressed node to cancel the click 2) sends touchPress (initial press position) → touchMove (current) to the ScrollView above, in order, to hand over the scroll.
  • Within the threshold, sends touch* only to the pressed node. (Behaves like a basic button)
  • If the ScrollView has setPassesCrossAxisDrag(true), a finger dragged more along the non-scrolling axis is not handed over. The pending press is sent right away to the hit node, and the following moves are also sent to the hit node. (When dragging a slider inside a scroll horizontally)
  • When a parent has a ScrollView, delivery of touchPress to the hit node is delayed by DELAY_PRESS_MS. If it turns out to be a drag meanwhile, press is never delivered, so "cancel during press transition" does not occur. For a short tap, at release time the pending press is fired immediately and release is sent right after to compensate.
예제Example일반 씬에서 위젯에 터치 넘기기Pass touches to widgets in a plain scene
import { Engine, EngineConfiguration, Scene, TouchRecognizer, UIButton, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		this.setTouchRaycaster(new TouchRecognizer());

		const root = this.getRoot();
		const buttonNode = new WorldNode();
		buttonNode.setPivot(Pivot.topLeft);
		buttonNode.setAnchor(Pivot.topLeft);
		buttonNode.setLocalPosition(Vector2.create(40, 40));
		buttonNode.setContentSize(Vector2.create(160, 56));
		const paint = buttonNode.addComponent(Paint);
		paint.setColor(new Color(0.2, 0.45, 0.85, 1));
		const button = buttonNode.addComponent(UIButton);
		button.setClickedEvent(() => {
			console.log("clicked");
		});
		root.addChild(buttonNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.canvasId = "mainCanvas";
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
생성자constructorconstructor()#0.2.1 부터since 0.2.1소스source

생성.

Constructor.

짝Counterpart: vanilla-engine TouchRecognizer.new

메서드methodtouchPress(viewInputPosition)#0.2.1 부터since 0.2.1소스source

터치 누름.

  • hit 노드와, 그 부모 체인에서 ScrollView 를 보유한 노드를 찾아둔다.
  • 부모에 ScrollView 가 없으면 hit 노드에 즉시 touchPress 를 전달한다.
  • 부모에 ScrollView 가 있으면 DELAY_PRESS_MS 후에 전달하도록 예약한다. 그 사이 드래그가 시작되면 예약된 press 는 취소된다.

Touch press.

  • Finds the hit node and the node holding a ScrollView in its parent chain in advance.
  • If no parent has a ScrollView, delivers touchPress to the hit node immediately.
  • If a parent has a ScrollView, schedules delivery after DELAY_PRESS_MS. If a drag starts meanwhile, the scheduled press is cancelled.
viewInputPositionVector2
누른 자리. (뷰 좌표)Press position. (view coordinates)

짝Counterpart: vanilla-engine TouchRecognizer.touch_press

메서드methodtouchMove(viewInputPosition)#0.2.1 부터since 0.2.1소스source

터치 이동.

  • 드래그 모드면 ScrollView 에만 전달.
  • 드래그 모드가 아니고, ScrollView 가 있고, 임계를 넘었다면 모드 전환:
(1) 지연 press 가 보류 중이면 취소(전달 자체를 안 함),
    이미 press 가 전달되었으면 hit 노드에 touchCancel,
(2) ScrollView 에 touchPress(최초 위치) → touchMove(현재 위치).
  • 그 외에는 hit 노드에 touchMove. 단 press 가 아직 전달되지 않았다면 touchMove 도 전달하지 않는다.

Touch move.

  • In drag mode, delivers only to the ScrollView.
  • If not in drag mode, there is a ScrollView, and the threshold is exceeded, switches modes:
(1) if a delayed press is pending, cancels it (not delivered at all),
    if press was already delivered, touchCancel to the hit node,
(2) touchPress (initial position) → touchMove (current position) to the ScrollView.
  • Otherwise touchMove to the hit node. However, if press has not been delivered yet, touchMove is not delivered either.
viewInputPositionVector2
옮긴 자리. (뷰 좌표)Moved position. (view coordinates)

짝Counterpart: vanilla-engine TouchRecognizer.touch_move

메서드methodtouchRelease(viewInputPosition)#0.2.1 부터since 0.2.1소스source

터치 뗌.

  • 드래그 모드면 ScrollView 에 release.
  • 아니면 hit 노드에 release. 단, 짧은 탭이라 지연 press 가 아직 발동되지 않았다면 보류된 press 를 즉시 발동한 뒤 release 를 이어 보내, 시각효과가 한 프레임이라도 들어가게 한다.

Touch release.

  • In drag mode, release to the ScrollView.
  • Otherwise release to the hit node. However, if it is a short tap and the delayed press has not fired yet, fires the pending press immediately and then sends release right after, so the visual effect shows for at least one frame.
viewInputPositionVector2
뗀 자리. (뷰 좌표)Release position. (view coordinates)

짝Counterpart: vanilla-engine TouchRecognizer.touch_release

메서드methodtouchCancel(viewInputPosition)#0.2.1 부터since 0.2.1소스source

터치 취소.

  • 지연 press 가 보류 중이면 hit 노드에 cancel 을 보내지 않는다 (press 자체가 전달되지 않았으므로).

Touch cancel.

  • If a delayed press is pending, does not send cancel to the hit node (since press itself was not delivered).
viewInputPositionVector2
취소된 때의 자리. (뷰 좌표)Position at cancel. (view coordinates)

짝Counterpart: vanilla-engine TouchRecognizer.touch_cancel

메서드methodgetTouchTarget()#0.4.0 부터since 0.4.0소스source

지금 누름을 받는 노드. (드래그를 넘겼으면 스크롤 뷰 노드, 없으면 null)

The node currently receiving the press. (The scroll view node if the drag was handed over, null if none)

반환Returns* 지금 누름을 받는 노드. (없으면 null)The node currently receiving the press. (null if none)

짝Counterpart: vanilla-engine TouchRecognizer.get_touch_target

메서드methodisTouching()#0.4.0 부터since 0.4.0소스source

누르고 있는 중인지. (누름부터 뗌이나 취소까지)

Whether it is being held down. (From press until release or cancel)

반환Returnsboolean 누름부터 뗌이나 취소까지 참.True from press until release or cancel.

짝Counterpart: vanilla-engine TouchRecognizer.is_touching

메서드methodreset()#0.2.1 부터since 0.2.1소스source

트래킹 상태 초기화.

Resets the tracking state.

짝Counterpart: vanilla-engine TouchRecognizer.reset

메서드methodfindAncestorScrollView(target)#0.2.1 부터since 0.2.1소스source

노드의 부모 체인에서 UIScrollView 컴포넌트를 가진 가장 가까운 노드의 ScrollView 컴포넌트를 반환한다. 없으면 null.

  • 단, target 이 자신이 컨트롤하는 ScrollView 의 위젯(getScrollView 보유)이면 그 ScrollView 는 검색에서 제외한다. (스크롤바 자신은 자기 부모 ScrollView 의 드래그 핸들러로 인계되면 안 되기 때문.)

In the node's parent chain, finds the nearest node with a UIScrollView component and returns its ScrollView component. null if none.

  • However, if target is a widget (has getScrollView) of the ScrollView it controls, that ScrollView is excluded from the search. (Because the scrollbar itself must not be handed over to the drag handler of its own parent ScrollView.)
target*
누른 노드. (없으면 null 을 돌려준다)The pressed node. (returns null if none)

반환ReturnsUIScrollView | null 가장 가까운 조상의 UIScrollView. (없으면 null)The nearest ancestor UIScrollView. (null if none)

짝Counterpart: vanilla-engine TouchRecognizer.find_ancestor_scroll_view