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
제스처 설정. (원본 options = { drag_threshold = 12, long_press_seconds = 0.6 })
Gesture settings. (original options = { drag_threshold = 12, long_press_seconds = 0.6 })
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());누른 곳에서 이만큼 움직이면 드래그로 친다. (픽셀)
Moving this far from the press point counts as a drag. (pixels)
반환Returnsf64
움직이지 않고 이만큼 누르고 있으면 롱프레스. (초)
Holding this long without moving is a long press. (seconds)
반환Returnsf64
PointerGesturestruct traits Default
포인터 제스처 인식기.
- 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.
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))]);생성.
Constructor.
- optionsPointerGestureOptions
- 드래그 임계값과 롱프레스 시간.Drag threshold and long press time.
반환ReturnsPointerGesture 눌리지 않고 콜백이 없는 새 인식기.A new recognizer, not pressed and with no callbacks.
짝Counterpart: vanilla.js PointerGesture.constructor
누름.
- 드래그, 롱프레스 상태와 누른 시간을 비우고 누른 위치를 기억한다.
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
이동. (누른 곳에서 임계값을 넘으면 드래그로 정하고, 그 뒤 움직일 때마다 드래그 알림을 부른다)
- 눌려 있지 않으면 아무것도 하지 않는다. 누른 곳에서 거리가 임계값 이상이면 드래그다.
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
갱신. (드래그가 아닌 채 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
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);뗌. (드래그였으면 드래그 끝, 롱프레스였으면 아무것도, 둘 다 아니면 탭으로 알린다)
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
취소. (아무것도 알리지 않고 상태만 되돌린다)
Cancels. (notifies nothing and only resets the state)
짝Counterpart: vanilla.js PointerGesture.cancel
롱프레스 진행 비율. (게이지 표시용, 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
눌려 있는지.
Whether it is pressed.
반환Returnsbool press 뒤 release 나 cancel 전이면 true.true after press and before release or cancel.
짝Counterpart: vanilla.js PointerGesture.isPressed
드래그 중인지.
Whether it is dragging.
반환Returnsbool 이번 누름에서 임계값을 넘게 움직였으면 true.true if it moved past the threshold during this press.
짝Counterpart: vanilla.js PointerGesture.isDragging
탭 알림 설정. (뗀 위치)
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
드래그 알림 설정. (지금 위치, 누른 위치)
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
pub fn set_drag_end_event(&mut self, drag_end_event: impl FnMut(Vector2, Vector2) + 'static)#드래그 끝 알림 설정. (지금 위치, 누른 위치)
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
롱프레스 알림 설정. (누른 위치)
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
드래그 임계값 설정. (픽셀)
Sets the drag threshold. (pixels)
- drag_thresholdf64
- 드래그로 칠 거리. (픽셀)Distance that counts as a drag. (pixels)
짝Counterpart: vanilla.js PointerGesture.setDragThreshold
롱프레스 시간 설정. (초)
Sets the long press time. (seconds)
- long_press_secondsf64
- 롱프레스로 칠 누른 시간. (초)Press time that counts as a long press. (seconds)
짝Counterpart: vanilla.js PointerGesture.setLongPressSeconds