vanilladocs
vanillapunch.com
API / misc/pointergesture

misc/pointergesture

포인터 제스처 인식기. (노드 없이)

  • 원본 misc/pointergesture.js 를 옮긴 것이다. 탭 / 드래그 / 롱프레스를 임계값으로 나눈다. 노드에 얽매이지 않아 어디서나 쓴다.
  • 사용: press(x, y) → move(x, y) → tick(time_delta) → release() 순으로 넘기고, 콜백(set_tap_event / set_drag_event / set_drag_end_event / set_long_press_event)으로 결과를 받는다.
  • 원본과 다른 점: 이동은 두 엔진 모두 move_pointer 다. (원본 movePointer, 예약어 move 를 이름으로 쓰지 않는다) options 객체는 PointerGestureOptions 구조체다. 콜백은 Vector2 를 받는 FnMut 이다.

Pointer gesture recognizer. (without nodes)

  • A port of the original misc/pointergesture.js. Splits tap / drag / long press by thresholds. It is not tied to nodes, so it can be used anywhere.
  • Usage: pass press(x, y), move(x, y), tick(time_delta), release() in that order, and receive the results through callbacks (set_tap_event / set_drag_event / set_drag_end_event / set_long_press_event).
  • Differences from the original:
Movement is move_pointer in both engines. (original movePointer; the reserved word move is not used as a name)
The options object is a PointerGestureOptions struct. Callbacks are FnMut taking a Vector2.

PointerGestureOptionsstruct traits Clone, Copy, Debug, PartialEq, Default

소스source

제스처 설정. (원본 options = { drag_threshold = 12, long_press_seconds = 0.6 })

Gesture settings. (original options = { drag_threshold = 12, long_press_seconds = 0.6 })

예제Example롱프레스 시간만 바꾸기Change only the long press time
use vanilla::{PointerGesture, PointerGestureOptions};

let pointer_gesture_options = PointerGestureOptions {
	long_press_seconds: 1.0,
	..PointerGestureOptions::default()
};
assert_eq!(pointer_gesture_options.drag_threshold, 12.0);
let pointer_gesture = PointerGesture::new(pointer_gesture_options);
assert!(!pointer_gesture.is_pressed());
필드fieldpub drag_threshold: f64#0.3.2 부터since 0.3.2소스source

누른 곳에서 이만큼 움직이면 드래그로 친다. (픽셀)

Moving this far from the press point counts as a drag. (pixels)

반환Returnsf64

필드fieldpub long_press_seconds: f64#0.3.2 부터since 0.3.2소스source

움직이지 않고 이만큼 누르고 있으면 롱프레스. (초)

Holding this long without moving is a long press. (seconds)

반환Returnsf64

PointerGesturestruct traits Default

짝Counterpart: vanilla.js PointerGesture소스source

포인터 제스처 인식기.

  • press, move_pointer, tick, release 차례로 넘기고 콜백으로 탭, 드래그, 드래그 끝, 롱프레스를 받는다.

Pointer gesture recognizer.

  • Pass press, move_pointer, tick, release in order and receive tap, drag, drag end, and long press through callbacks.
예제Example탭과 드래그 가르기Tell a tap from a drag
use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{PointerGesture, PointerGestureOptions, Vector2};

let event_list = Rc::new(RefCell::new(Vec::new()));
let mut pointer_gesture = PointerGesture::new(PointerGestureOptions::default());
let tap_event_list = event_list.clone();
pointer_gesture.set_tap_event(move |position| {
	tap_event_list.borrow_mut().push(("tap", position));
});
let drag_end_event_list = event_list.clone();
pointer_gesture.set_drag_end_event(move |position, _press_position| {
	drag_end_event_list.borrow_mut().push(("drag_end", position));
});

pointer_gesture.press(10.0, 10.0);
pointer_gesture.move_pointer(14.0, 10.0);
pointer_gesture.tick(0.1);
pointer_gesture.release();

pointer_gesture.press(10.0, 10.0);
pointer_gesture.move_pointer(40.0, 10.0);
assert!(pointer_gesture.is_dragging());
pointer_gesture.release();

assert_eq!(*event_list.borrow(), vec![("tap", Vector2::create(14.0, 10.0)), ("drag_end", Vector2::create(40.0, 10.0))]);
생성자constructorpub fn new(options: PointerGestureOptions) -> PointerGesture#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

optionsPointerGestureOptions
드래그 임계값과 롱프레스 시간.Drag threshold and long press time.

반환ReturnsPointerGesture 눌리지 않고 콜백이 없는 새 인식기.A new recognizer, not pressed and with no callbacks.

짝Counterpart: vanilla.js PointerGesture.constructor

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

누름.

  • 드래그, 롱프레스 상태와 누른 시간을 비우고 누른 위치를 기억한다.

Press.

  • Clears the drag and long press states and the press time, and remembers the press position.
xf64
누른 위치 x. (픽셀, 좌표계는 넘기는 쪽이 정한다)Press position x. (pixels, the caller decides the coordinate system)
yf64
누른 위치 y. (픽셀)Press position y. (pixels)

짝Counterpart: vanilla.js PointerGesture.press

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

이동. (누른 곳에서 임계값을 넘으면 드래그로 정하고, 그 뒤 움직일 때마다 드래그 알림을 부른다)

  • 눌려 있지 않으면 아무것도 하지 않는다. 누른 곳에서 거리가 임계값 이상이면 드래그다.

Move. (becomes a drag once it passes the threshold from the press point, and calls the drag notification on every move after that)

  • Does nothing if not pressed. It is a drag if the distance from the press point is at least the threshold.
xf64
지금 위치 x. (픽셀, press 와 같은 좌표계)Current position x. (pixels, same coordinate system as press)
yf64
지금 위치 y. (픽셀)Current position y. (pixels)

짝Counterpart: vanilla.js PointerGesture.movePointer

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

갱신. (드래그가 아닌 채 long_press_seconds 를 넘기면 롱프레스를 한 번 알린다)

Update. (notifies a long press once when long_press_seconds passes without being a drag)

time_deltaf64
지난 시간. (초, 누른 시간에 더한다)Elapsed time. (seconds, added to the press time)

짝Counterpart: vanilla.js PointerGesture.tick

예제Example0.6 초 누르고 있으면 롱프레스, 뗄 때 탭은 없다Holding for 0.6 seconds is a long press, and there is no tap on release
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{PointerGesture, PointerGestureOptions};

let long_press_count = Rc::new(Cell::new(0));
let tap_count = Rc::new(Cell::new(0));
let mut pointer_gesture = PointerGesture::new(PointerGestureOptions::default());
let long_press_count_for_event = long_press_count.clone();
pointer_gesture.set_long_press_event(move |_position| {
	long_press_count_for_event.set(long_press_count_for_event.get() + 1);
});
let tap_count_for_event = tap_count.clone();
pointer_gesture.set_tap_event(move |_position| {
	tap_count_for_event.set(tap_count_for_event.get() + 1);
});

pointer_gesture.press(50.0, 50.0);
pointer_gesture.tick(0.3);
assert!((pointer_gesture.get_long_press_ratio() - 0.5).abs() < 0.0001);
pointer_gesture.tick(0.3);
pointer_gesture.tick(0.3);
pointer_gesture.release();
assert_eq!(long_press_count.get(), 1);
assert_eq!(tap_count.get(), 0);
메서드methodpub fn release(&mut self)#0.3.2 부터since 0.3.2소스source

뗌. (드래그였으면 드래그 끝, 롱프레스였으면 아무것도, 둘 다 아니면 탭으로 알린다)

Release. (notifies a drag end if it was a drag, nothing if it was a long press, otherwise a tap)

짝Counterpart: vanilla.js PointerGesture.release

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

취소. (아무것도 알리지 않고 상태만 되돌린다)

Cancels. (notifies nothing and only resets the state)

짝Counterpart: vanilla.js PointerGesture.cancel

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

롱프레스 진행 비율. (게이지 표시용, 0 ~ 1)

Long press progress ratio. (for showing a gauge, 0 ~ 1)

반환Returnsf64 누른 시간 / 롱프레스 시간. (1 에서 자른다) 눌려 있지 않거나, 드래그 중이거나, 롱프레스 시간이 0 이하면 0.Press time / long press time. (clamped at 1) 0 if not pressed, dragging, or the long press time is 0 or less.

짝Counterpart: vanilla.js PointerGesture.getLongPressRatio

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

눌려 있는지.

Whether it is pressed.

반환Returnsbool press 뒤 release 나 cancel 전이면 true.true after press and before release or cancel.

짝Counterpart: vanilla.js PointerGesture.isPressed

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

드래그 중인지.

Whether it is dragging.

반환Returnsbool 이번 누름에서 임계값을 넘게 움직였으면 true.true if it moved past the threshold during this press.

짝Counterpart: vanilla.js PointerGesture.isDragging

메서드methodpub fn set_tap_event(&mut self, tap_event: impl FnMut(Vector2) + 'static)#0.3.2 부터since 0.3.2소스source

탭 알림 설정. (뗀 위치)

Sets the tap notification. (release position)

tap_eventimpl FnMut(Vector2) + 'static
탭일 때 부를 함수. 마지막 위치를 받는다. (앞의 알림은 바꾼다)Function to call on a tap. Receives the last position. (replaces the previous notification)

짝Counterpart: vanilla.js PointerGesture.setTapEvent

메서드methodpub fn set_drag_event(&mut self, drag_event: impl FnMut(Vector2, Vector2) + 'static)#0.3.2 부터since 0.3.2소스source

드래그 알림 설정. (지금 위치, 누른 위치)

Sets the drag notification. (current position, press position)

drag_eventimpl FnMut(Vector2, Vector2) + 'static
드래그 중 움직일 때마다 부를 함수. 지금 위치와 누른 위치를 받는다.Function to call on every move while dragging. Receives the current position and the press position.

짝Counterpart: vanilla.js PointerGesture.setDragEvent

메서드methodpub fn set_drag_end_event(&mut self, drag_end_event: impl FnMut(Vector2, Vector2) + 'static)#0.3.2 부터since 0.3.2소스source

드래그 끝 알림 설정. (지금 위치, 누른 위치)

Sets the drag end notification. (current position, press position)

drag_end_eventimpl FnMut(Vector2, Vector2) + 'static
드래그하다 뗄 때 부를 함수. 마지막 위치와 누른 위치를 받는다.Function to call on release while dragging. Receives the last position and the press position.

짝Counterpart: vanilla.js PointerGesture.setDragEndEvent

메서드methodpub fn set_long_press_event(&mut self, long_press_event: impl FnMut(Vector2) + 'static)#0.3.2 부터since 0.3.2소스source

롱프레스 알림 설정. (누른 위치)

Sets the long press notification. (press position)

long_press_eventimpl FnMut(Vector2) + 'static
롱프레스가 되는 순간 한 번 부를 함수. 누른 위치를 받는다.Function to call once at the moment it becomes a long press. Receives the press position.

짝Counterpart: vanilla.js PointerGesture.setLongPressEvent

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

드래그 임계값 설정. (픽셀)

Sets the drag threshold. (pixels)

drag_thresholdf64
드래그로 칠 거리. (픽셀)Distance that counts as a drag. (pixels)

짝Counterpart: vanilla.js PointerGesture.setDragThreshold

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

롱프레스 시간 설정. (초)

Sets the long press time. (seconds)

long_press_secondsf64
롱프레스로 칠 누른 시간. (초)Press time that counts as a long press. (seconds)

짝Counterpart: vanilla.js PointerGesture.setLongPressSeconds