ui/touchrecognizer
터치 인식기. (원본 ui/touchrecognizer.js 의 TouchRecognizer)
- 누른 노드의 부모 쪽에 UIScrollView(또는 UISnapScrollView)가 있으면, 끈 거리가 임계(10px)를 넘을 때 누른 노드의 누름을 취소하고 스크롤 뷰에 처음 누른 자리부터 누름 → 이동을 다시 보내 스크롤을 넘긴다. (스크롤 뷰 안의 버튼을 끌어도 스크롤된다)
- 부모에 스크롤 뷰가 있으면 누른 노드에 가는 누름을 150ms 미룬다. 그 사이 드래그가 되면 누름 자체를 보내지 않는다. 짧게 톡 친 것은 뗄 때 미룬 누름을 바로 보낸 뒤 뗌을 잇는다. (UIKit 의 delaysContentTouches)
- 스크롤 뷰가 set_passes_cross_axis_drag(true) 면 스크롤하지 않는 축으로 더 많이 끈 손가락은 넘기지 않는다.
- 원본과 다른 것:
- 원본은 GameScene 이 늘 쓴다. 러스트 판의 SceneBase::tick_touch 는 원본 TouchRaycaster(맨 위 노드에 그대로 보냄)다.
이 손짓이 필요한 씬은 TouchRecognizer 를 들고, SceneBase::tick 대신 노드 갱신과 이것을 부른다:
self.scene_base.get_node_tree_mut().tick(engine, time_delta);
self.touch_recognizer.tick_touch(self.scene_base.get_node_tree_mut(), engine);
그때 SceneBase 의 첫 터치 콜백과 get_touch_target 은 쓰지 않는다. (이쪽 get_touch_target 을 쓴다)- 원본 setTimeout(150ms) 은 프레임 간격(배율을 곱하지 않은 시간)을 쌓아 센다.
- 스크롤 뷰에 넘기는 터치는 그 노드의 컴포넌트 모두에게 간다. (NodeTree::dispatch_touch)
- tick_touch 는 휠(tick_wheel)과 누름(tick_pointer)으로 나뉜다. 휠을 다른 길(창 층)로 넘긴 씬은 tick_wheel 대신 mark_wheel_handled 를 부른다. (GameSceneBase)
Touch recognizer. (TouchRecognizer of the original ui/touchrecognizer.js)
- If a UIScrollView (or UISnapScrollView) is up the pressed node's parent chain, once the drag distance exceeds the threshold (10px), cancels the press on the pressed node and hands the scroll over by resending press → move to the scroll view from the initial press point. (dragging a button inside a scroll view still scrolls)
- If there is a scroll view among the parents, the press to the pressed node is delayed by 150ms. If it becomes a drag in the meantime, the press itself is never sent. A short tap sends the delayed press right away on release, followed by the release. (UIKit's delaysContentTouches)
- If the scroll view has set_passes_cross_axis_drag(true), a finger dragged more along the non-scrolling axis is not handed over.
- Differences from the original:
- The original GameScene always uses it. The Rust version's SceneBase::tick_touch is the original TouchRaycaster (sends straight to the topmost node).
A scene that needs this gesture holds a TouchRecognizer and, instead of SceneBase::tick, calls the node update and this:
self.scene_base.get_node_tree_mut().tick(engine, time_delta);
self.touch_recognizer.tick_touch(self.scene_base.get_node_tree_mut(), engine);
In that case SceneBase's first touch callback and get_touch_target are not used. (use this one's get_touch_target)- The original setTimeout(150ms) is counted by accumulating frame intervals (time not multiplied by the scale).
- Touches handed to a scroll view go to all components of that node. (NodeTree::dispatch_touch)
- tick_touch is split into wheel (tick_wheel) and press (tick_pointer). A scene that routes the wheel elsewhere (a window layer) calls mark_wheel_handled instead of tick_wheel. (GameSceneBase)
TouchRecognizerstruct traits Default
터치 인식기.
Touch recognizer.
use vanilla::{Engine, EngineConfiguration, Scene, SceneBase, TouchRecognizer};
struct MainScene {
scene_base: SceneBase,
touch_recognizer: TouchRecognizer,
}
impl Scene for MainScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn tick(&mut self, time_delta: f64) {
let engine = Engine::get_engine().unwrap();
self.scene_base.get_node_tree_mut().tick(engine, time_delta);
self.touch_recognizer.tick_touch(self.scene_base.get_node_tree_mut(), engine);
self.scene_base.tick_tweens(time_delta);
}
}
fn main() {
let engine = Engine::new(EngineConfiguration::new());
let main_scene = MainScene {
scene_base: SceneBase::new(),
touch_recognizer: TouchRecognizer::new(),
};
if let Err(error) = engine.run(Box::new(main_scene)) {
eprintln!("{}", error);
}
}생성.
Constructor.
반환ReturnsTouchRecognizer 아무것도 누르지 않은 새 인식기.New recognizer with nothing pressed.
짝Counterpart: vanilla.js TouchRecognizer.constructor
이번 프레임의 휠을 받아 쓴 컴포넌트(스크롤 칸 등)가 있었는지. (tick_touch 뒤에 읽는다)
Whether a component (scroll area, etc.) consumed this frame's wheel. (read after tick_touch)
반환Returnsbool 받아 쓴 컴포넌트가 있었거나 mark_wheel_handled 를 불렀으면 true.true if a component consumed it or mark_wheel_handled was called.
지금 누름을 받는 노드. (드래그를 넘겼으면 스크롤 뷰 노드)
Node currently receiving the press. (the scroll view node if the drag was handed over)
반환ReturnsOption<NodeId> 누름을 받는 노드. 누르고 있지 않거나 누른 자리에 누를 수 있는 노드가 없으면 None.Node receiving the press. None if not pressing or there is no pressable node at the press point.
짝Counterpart: vanilla.js TouchRecognizer.getTouchTarget
누르고 있는 중인지. (누름부터 뗌이나 취소까지)
Whether it is being held down. (From press until release or cancel)
반환Returnsbool 누르고 있으면 true. (누른 자리에 노드가 없어도 true)true if pressing. (true even if there is no node at the press point)
짝Counterpart: vanilla.js TouchRecognizer.isTouching
터치 갱신. (SceneBase::tick_touch 자리. 휠 → 미룬 누름 → 누름 / 이동 / 뗌 / 취소)
Touch update. (in place of SceneBase::tick_touch. wheel → delayed press → press / move / release / cancel)
- node_tree&mut NodeTree
- 터치를 보낼 노드 트리.Node tree to send touches to.
- engine&Engine
- 입력과 시간을 읽고 컴포넌트에 넘길 엔진.Engine that reads input and time and is passed to components.
휠 갱신. (누름과 상관없이 그 자리의 가장 앞 스크롤 칸이나 누를 수 있는 노드부터 부모 쪽으로, NodeTree::dispatch_wheel_at, 받아 썼으면 true)
Wheel update. (regardless of pressing, from the frontmost scroll area or pressable node at that point toward its parents, NodeTree::dispatch_wheel_at, true if consumed)
- node_tree&mut NodeTree
- 휠을 보낼 노드 트리.Node tree to send the wheel to.
- engine&Engine
- 입력 자리와 휠 양을 읽을 엔진.Engine to read the input position and wheel amount from.
반환Returnsbool 그 자리의 컴포넌트가 휠을 받아 썼으면 true. (이번 프레임에 휠이 없으면 false)true if a component at that point consumed the wheel. (false if there is no wheel this frame)
이번 프레임의 휠을 다른 길(창 층 같은)이 받아 썼다고 적기. (tick_wheel 대신 부른다)
Marks this frame's wheel as consumed by another route (like a window layer). (called instead of tick_wheel)
누름 갱신. (미룬 누름 → 누름 / 이동 / 뗌 / 취소)
Press update. (delayed press → press / move / release / cancel)
- node_tree&mut NodeTree
- 터치를 보낼 노드 트리.Node tree to send touches to.
- engine&Engine
- 입력과 시간을 읽고 컴포넌트에 넘길 엔진. (미룬 누름은 배율을 곱하지 않은 프레임 간격으로 센다)Engine that reads input and time and is passed to components. (the delayed press is counted with frame intervals not multiplied by the scale)
pub fn update_press_delay(&mut self, node_tree: &mut NodeTree, engine: &Engine, elapsed_seconds: f64)#미룬 누름의 남은 시간을 줄이고, 다 되면 누른 노드에 누름을 보낸다. (원본 setTimeout 자리)
- 그 사이 드래그로 넘어갔으면 보내지 않는다. 미룬 누름이 없으면 아무 일도 없다.
Reduces the remaining time of the delayed press, and when it runs out sends the press to the pressed node. (in place of the original setTimeout)
- Not sent if it turned into a drag in the meantime. Does nothing if there is no delayed press.
- node_tree&mut NodeTree
- 누름을 보낼 노드 트리.Node tree to send the press to.
- engine&Engine
- 컴포넌트에 넘길 엔진.Engine passed to components.
- elapsed_secondsf64
- 지난 시간. (초, 미루는 시간은 0.15 초)Elapsed time. (seconds, the delay is 0.15 seconds)
pub fn touch_press(&mut self, node_tree: &mut NodeTree, engine: &Engine, view_input_position: Vector2)#터치 누름. (맞은 노드와 그 부모 쪽 스크롤 뷰를 찾아 두고, 스크롤 뷰가 있으면 누름을 미룬다)
- 스크롤 뷰가 없으면 맞은 노드에 바로 누름을 보낸다.
Touch press. (finds the hit node and the scroll view among its parents, and delays the press if there is a scroll view)
- Without a scroll view, sends the press to the hit node right away.
- node_tree&mut NodeTree
- 맞은 노드를 찾고 누름을 보낼 노드 트리.Node tree to find the hit node in and send the press to.
- engine&Engine
- 컴포넌트에 넘길 엔진.Engine passed to components.
- view_input_positionVector2
- 누른 자리. (뷰 좌표)Press position. (view coordinates)
짝Counterpart: vanilla.js TouchRecognizer.touchPress
pub fn touch_move(&mut self, node_tree: &mut NodeTree, engine: &Engine, view_input_position: Vector2)#터치 이동.
- 드래그를 넘겼으면 스크롤 뷰에만 보낸다.
- 스크롤 뷰가 있고 임계를 넘으면 넘긴다: 미룬 누름은 버리고(이미 보냈으면 누른 노드에 취소), 스크롤 뷰에 누름(처음 자리) → 이동.
- 그 밖에는 누른 노드에 이동. (누름을 아직 보내지 않았으면 이동도 보내지 않는다)
- set_passes_cross_axis_drag(true) 인 스크롤 뷰에서 스크롤하지 않는 축으로 더 끈 손가락은 넘기지 않고, 미룬 누름이 있으면 바로 보낸다.
Touch move.
- If the drag was handed over, sends only to the scroll view.
- If there is a scroll view and the threshold is exceeded, hands over: discards the delayed press (cancels the pressed node if already sent), then sends press (initial point) → move to the scroll view.
- Otherwise, sends the move to the pressed node. (if the press has not been sent yet, the move is not sent either)
- In a scroll view with set_passes_cross_axis_drag(true), a finger dragged more along the non-scrolling axis is not handed over, and a delayed press, if any, is sent right away.
- node_tree&mut NodeTree
- 이동을 보낼 노드 트리.Node tree to send the move to.
- engine&Engine
- 컴포넌트에 넘길 엔진.Engine passed to components.
- view_input_positionVector2
- 손가락 자리. (뷰 좌표, 처음 누른 자리에서 가로나 세로로 10 을 넘게 움직이면 드래그로 본다)Finger position. (view coordinates, treated as a drag once it moves more than 10 horizontally or vertically from the initial press point)
짝Counterpart: vanilla.js TouchRecognizer.touchMove
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, Paint, Pivot, TouchRecognizer, UIButton, UIScrollView, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let scroll_id = node_tree.create_child(root_id, "Scroll");
node_tree[scroll_id].set_pivot(Pivot::TOP_LEFT);
node_tree[scroll_id].set_content_size(Vector2::create(200.0, 100.0));
let scroll_view = node_tree.add_component(scroll_id, UIScrollView::new()).expect("scroll view");
scroll_view.set_scroll_content_size(Vector2::create(200.0, 400.0));
let content_id = scroll_view.get_content().expect("content");
let button_id = node_tree.create_child(content_id, "Row");
node_tree[button_id].set_pivot(Pivot::TOP_LEFT);
node_tree[button_id].set_content_size(Vector2::create(200.0, 40.0));
node_tree[button_id].add_component(Paint::new());
let click_count = Rc::new(Cell::new(0));
let click_count_for_event = click_count.clone();
let button = node_tree.add_component(button_id, UIButton::new()).expect("button");
button.set_click_event(Box::new(move |_button: &mut UIButton, _context: &mut ComponentContext| {
click_count_for_event.set(click_count_for_event.get() + 1);
}));
let mut touch_recognizer = TouchRecognizer::new();
touch_recognizer.touch_press(&mut node_tree, &engine, Vector2::create(50.0, 30.0));
touch_recognizer.update_press_delay(&mut node_tree, &engine, 0.2);
touch_recognizer.touch_move(&mut node_tree, &engine, Vector2::create(50.0, 0.0));
assert_eq!(touch_recognizer.get_touch_target(), Some(scroll_id));
node_tree.tick(&engine, 0.016);
let scroll_offset = node_tree[scroll_id].get_component::<UIScrollView>().expect("scroll view").get_scroll_offset();
assert_eq!(scroll_offset, Vector2::create(0.0, -30.0));
touch_recognizer.touch_release(&mut node_tree, &engine, Vector2::create(50.0, 0.0));
assert_eq!(click_count.get(), 0);pub fn touch_release(&mut self, node_tree: &mut NodeTree, engine: &Engine, view_input_position: Vector2)#터치 뗌. (드래그면 스크롤 뷰에, 아니면 누른 노드에. 미룬 누름이 남았으면 그것을 먼저 보낸다)
- 보낸 뒤 추적 상태를 비운다.
Touch release. (to the scroll view if dragging, otherwise to the pressed node. If a delayed press remains, sends it first)
- Clears the tracking state after sending.
- node_tree&mut NodeTree
- 뗌을 보낼 노드 트리.Node tree to send the release to.
- engine&Engine
- 컴포넌트에 넘길 엔진.Engine passed to components.
- view_input_positionVector2
- 뗀 자리. (뷰 좌표)Release position. (view coordinates)
짝Counterpart: vanilla.js TouchRecognizer.touchRelease
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, Paint, Pivot, TouchRecognizer, UIButton, UIScrollView, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let scroll_id = node_tree.create_child(root_id, "Scroll");
node_tree[scroll_id].set_pivot(Pivot::TOP_LEFT);
node_tree[scroll_id].set_content_size(Vector2::create(200.0, 100.0));
let scroll_view = node_tree.add_component(scroll_id, UIScrollView::new()).expect("scroll view");
scroll_view.set_scroll_content_size(Vector2::create(200.0, 400.0));
let content_id = scroll_view.get_content().expect("content");
let button_id = node_tree.create_child(content_id, "Row");
node_tree[button_id].set_pivot(Pivot::TOP_LEFT);
node_tree[button_id].set_content_size(Vector2::create(200.0, 40.0));
node_tree[button_id].add_component(Paint::new());
let click_count = Rc::new(Cell::new(0));
let click_count_for_event = click_count.clone();
let button = node_tree.add_component(button_id, UIButton::new()).expect("button");
button.set_click_event(Box::new(move |_button: &mut UIButton, _context: &mut ComponentContext| {
click_count_for_event.set(click_count_for_event.get() + 1);
}));
let mut touch_recognizer = TouchRecognizer::new();
let tap_position = Vector2::create(50.0, 20.0);
touch_recognizer.touch_press(&mut node_tree, &engine, tap_position);
assert_eq!(touch_recognizer.find_ancestor_scroll_view(&node_tree, Some(button_id)), Some(scroll_id));
assert_eq!(touch_recognizer.get_touch_target(), Some(button_id));
touch_recognizer.touch_release(&mut node_tree, &engine, tap_position);
assert_eq!(click_count.get(), 1);
assert!(!touch_recognizer.is_touching());pub fn touch_cancel(&mut self, node_tree: &mut NodeTree, engine: &Engine, view_input_position: Vector2)#터치 취소. (미룬 누름이 남았으면 누른 노드에는 취소를 보내지 않는다)
- 보낸 뒤 추적 상태를 비운다.
Touch cancel. (if a delayed press remains, no cancel is sent to the pressed node)
- Clears the tracking state after sending.
- node_tree&mut NodeTree
- 취소를 보낼 노드 트리.Node tree to send the cancel to.
- engine&Engine
- 컴포넌트에 넘길 엔진.Engine passed to components.
- view_input_positionVector2
- 취소된 자리. (뷰 좌표)Canceled position. (view coordinates)
짝Counterpart: vanilla.js TouchRecognizer.touchCancel
추적 상태 비우기.
Clears the tracking state.
짝Counterpart: vanilla.js TouchRecognizer.reset
pub fn find_ancestor_scroll_view(&self, node_tree: &NodeTree, target_id: Option<NodeId>) -> Option<NodeId>#노드의 부모 쪽에서 UIScrollView(또는 UISnapScrollView)가 붙은 가장 가까운 노드. (없으면 None)
- 누른 노드가 스크롤바면 자기가 다루는 스크롤 뷰는 빼고 찾는다. (스크롤바 드래그가 그 스크롤 뷰로 넘어가면 안 된다)
The nearest node up the node's parent chain with a UIScrollView (or UISnapScrollView) attached. (None if none)
- If the pressed node is a scroll bar, the scroll view it controls is skipped. (a scroll bar drag must not be handed to that scroll view)
- node_tree&NodeTree
- 찾을 노드 트리.Node tree to search.
- target_idOption<NodeId>
- 누른 노드. (자신은 보지 않고 부모부터 본다)Pressed node. (starts from the parent, not itself)
반환ReturnsOption<NodeId> 가장 가까운 스크롤 뷰 노드. target_id 가 None 이거나 찾지 못하면 None.Nearest scroll view node. None if target_id is None or nothing is found.
짝Counterpart: vanilla.js TouchRecognizer.findAncestorScrollView