vanilladocs
vanillapunch.com
API / ui/uisnapscrollview

ui/uisnapscrollview

스냅 스크롤 뷰. (원본 ui/uisnapscrollview.js 의 UISnapScrollView)

  • 드래그를 놓으면 content 자식(아이템) 하나의 가운데가 보이는 영역 가운데에 오도록 미끄러진다. 아이템 크기가 달라도 된다.
  • 가로 스냅: set_horizontal(true), set_vertical(false) (기본). 세로 스냅: set_horizontal(false), set_vertical(true).
  • 한 번 끌면 최대 한 칸만 움직인다. 놓을 때 드래그 속도가 set_velocity_threshold(기본 200, 오프셋의 초당 픽셀) 이상이면 그 방향으로 한 칸 넘기고, 아니면 끈 거리가 다음 아이템 크기의 set_page_change_threshold 비율(기본 0.35) 이상일 때 넘어간다. 끄는 동안에는 오프셋이 지금 칸 ±1 칸 범위를 넘지 않는다.
  • set_snap_to_nearest(true) 면 끄는 동안 칸 범위를 자르지 않고, 놓으면 스냅 오프셋이 지금 오프셋과 가장 가까운 아이템으로 간다.
  • page_to(node_tree, index, smooth): 지정 칸으로 이동. smooth 면 0.3 초 cubic.out 으로 미끄러지고, 아니면 바로 옮긴다.
  • 원본은 UIScrollView 를 상속한다. 러스트 판은 UIScrollView 를 품고(scroll_view 필드) 터치, 휠, 그리기, 붙기를 넘겨준다. 스크롤 뷰 설정은 get_scroll_view_mut 로 품은 뷰에 한다. 자주 쓰는 것(콘텐트 크기, 오프셋, 축)은 이 위젯에도 있다. node_tree.add_component(node_id, UISnapScrollView::new()) 로 붙인다. (content 노드, Mask, interactable 은 품은 뷰가 만든다)
  • 원본과 다른 것:
  • 칸을 바꾸는 함수(page_to, set_snap_current_index, play_snap_tween 등)는 content 자식을 읽어야 해서 트리를 받는다. 씬 코드에서는 node_tree.with_component_mut::<UISnapScrollView, _>(snap_id, |snap_view, tree| snap_view.page_to(tree, 2, true)) 로 부른다.
  • 스냅 미끄러짐은 원본 Tween 객체 대신 같은 계산(0.3 초, cubic.out, 만든 tick 에 첫 걸음)을 이 위젯 안에서 한다. (원본 트윈의 갱신 콜백이 스크롤 뷰 자신을 부르는데, 러스트 트윈 콜백은 트리만 받아 tick 중에 꺼내진 이 위젯에 닿지 않는다)
  • 미끄러지는 동안 content 노드는 그 tick 안에서 바로 옮긴다. (품은 뷰의 반영은 다음 tick 이라 한 프레임 늦지 않게)
  • 원본은 스냅 뷰가 UIScrollView 이기도 해서 TouchRecognizer, UIScrollBar, UIListView 가 그대로 알아본다. 러스트 판은 TouchRecognizer 와 UIListView 가 find_scroll_view 로 알아본다. UIScrollBar 는 UIScrollView 컴포넌트만 찾아서 스냅 뷰에는 스크롤바를 붙일 수 없다. (setShows...ScrollBar 는 품은 뷰에 값만 들어간다)

Snap scroll view. (UISnapScrollView of the original ui/uisnapscrollview.js)

  • On drag release, slides so that the center of one content child (item) lands at the center of the visible area. Items may differ in size.
  • Horizontal snap: set_horizontal(true), set_vertical(false) (default). Vertical snap: set_horizontal(false), set_vertical(true).
  • One drag moves at most one slot. On release, if the drag velocity is at least set_velocity_threshold (default 200, offset pixels per second), it moves one slot in that direction, otherwise it moves when the dragged distance is at least the set_page_change_threshold ratio (default 0.35) of the next item's size. While dragging, the offset does not go beyond ±1 slot from the current slot.
  • With set_snap_to_nearest(true), the slot range is not clamped while dragging, and on release it goes to the item whose snap offset is closest to the current offset.
  • page_to(node_tree, index, smooth): moves to the given slot. If smooth, slides with 0.3 seconds cubic.out, otherwise moves right away.
  • The original inherits UIScrollView. The Rust version embeds UIScrollView (scroll_view field) and passes through touch, wheel, drawing, and attaching. Scroll view settings are made on the embedded view with get_scroll_view_mut. Commonly used ones (content size, offset, axes) are also on this widget. Attach with node_tree.add_component(node_id, UISnapScrollView::new()). (the embedded view creates the content node, Mask, and interactable)
  • Differences from the original:
  • Functions that change the slot (page_to, set_snap_current_index, play_snap_tween, etc.) need to read content children, so they take the tree. Scene code calls them with node_tree.with_component_mut::<UISnapScrollView, _>(snap_id, |snap_view, tree| snap_view.page_to(tree, 2, true)).
  • Instead of the original Tween object, the snap slide does the same calculation (0.3 seconds, cubic.out, first step on the tick it is made) inside this widget. (the original tween's update callback calls the scroll view itself, but a Rust tween callback takes only the tree and cannot reach this widget, which is taken out during tick)
  • While sliding, the content node is moved right away within that tick. (so it does not lag a frame, since the embedded view applies on the next tick)
  • In the original, the snap view is also a UIScrollView, so TouchRecognizer, UIScrollBar, UIListView recognize it as is. In the Rust version, TouchRecognizer and UIListView recognize it through find_scroll_view. UIScrollBar looks only for the UIScrollView component, so a scroll bar cannot be attached to a snap view. (setShows...ScrollBar only puts the value into the embedded view)

UISnapScrollViewstruct traits Default, Component

짝Counterpart: vanilla.js UISnapScrollView소스source

스냅 스크롤 뷰.

  • content 자식 하나하나가 한 칸이다. 놓으면 가장 가까운 칸(끈 방향으로 최대 한 칸)의 가운데가 보이는 영역 가운데로 미끄러진다.
  • 오프셋은 품은 스크롤 뷰가 콘텐트 크기로 자르므로 set_scroll_content_size 로 칸 전체 길이를 정해 둔다.

Snap scroll view.

  • Each content child is one slot. On release, the center of the nearest slot (at most one slot in the drag direction) slides to the center of the visible area.
  • The embedded scroll view clamps the offset to the content size, so set the full length of all slots with set_scroll_content_size.
예제Example다섯 칸 가로 페이지를 끌어 다음 칸으로 넘기기Drag a five-slot horizontal page over to the next slot
use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, TouchKind, UISnapScrollView, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let snap_id = node_tree.create_child(root_id, "snap");
node_tree[snap_id].set_pivot(Pivot::TOP_LEFT);
node_tree[snap_id].set_content_size(Vector2::create(200.0, 100.0));
let snap_view = node_tree.add_component(snap_id, UISnapScrollView::new()).expect("snap view");
snap_view.set_scroll_content_size(Vector2::create(1000.0, 100.0));
let content_id = snap_view.get_content().expect("content");
for page_index in 0..5 {
	let page_id = node_tree.create_child(content_id, "page");
	node_tree[page_id].set_pivot(Pivot::TOP_LEFT);
	node_tree[page_id].set_rect(page_index as f64 * 200.0, 0.0, 200.0, 100.0);
}

node_tree.dispatch_touch(snap_id, &engine, TouchKind::Press, Vector2::create(150.0, 50.0));
node_tree.dispatch_touch(snap_id, &engine, TouchKind::Moved, Vector2::create(50.0, 50.0));
node_tree.tick(&engine, 0.016);
node_tree.dispatch_touch(snap_id, &engine, TouchKind::Release, Vector2::create(50.0, 50.0));
for _ in 0..30 {
	node_tree.tick(&engine, 0.016);
}

let snap_view = node_tree[snap_id].get_component::<UISnapScrollView>().expect("snap view");
assert_eq!(snap_view.get_snap_current_index(), 1);
assert_eq!(snap_view.get_scroll_offset(), Vector2::create(-200.0, 0.0));
assert_eq!(node_tree[content_id].get_local_position(), Vector2::create(-200.0, 0.0));
생성자constructorpub fn new() -> UISnapScrollView#0.3.2 부터since 0.3.2소스source

생성. (clamp 모드, 가로만 스크롤)

  • 노드에 붙일 때는 node_tree.add_component 를 쓴다. (품은 뷰가 content 노드와 Mask 를 만든다)

Constructor. (clamp mode, horizontal scrolling only)

  • Use node_tree.add_component to attach it to a node. (the embedded view creates the content node and the Mask)

반환ReturnsUISnapScrollView 지금 칸 0, 칸 넘김 비율 0.35, 방향성 스냅 최소 속도 200 의 새 스냅 뷰.New snap view with current slot 0, slot change ratio 0.35, and directional snap minimum velocity 200.

짝Counterpart: vanilla.js UISnapScrollView.constructor

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

품은 스크롤 뷰 반환.

Returns the embedded scroll view.

반환Returns&UIScrollView 품은 스크롤 뷰.The embedded scroll view.

메서드methodpub fn get_scroll_view_mut(&mut self) -> &mut UIScrollView#0.3.2 부터since 0.3.2소스source

품은 스크롤 뷰 반환. (변경용)

Returns the embedded scroll view. (mutable)

반환Returns&mut UIScrollView 품은 스크롤 뷰의 가변 참조. (드래그 배율, 휠 배율 같은 스크롤 뷰 설정을 여기에 한다)Mutable reference to the embedded scroll view. (scroll view settings such as drag scale and wheel scale go here)

메서드methodpub fn get_content(&self) -> Option<NodeId>#0.3.2 부터since 0.3.2소스source

컨텐트 노드 핸들. (아이템 노드를 여기에 붙인다)

Content node handle. (attach item nodes here)

반환ReturnsOption<NodeId> content 노드. 트리에 붙기(node_tree.add_component) 전에는 None.content node. None before being attached to a tree (node_tree.add_component).

짝Counterpart: vanilla.js UIView.getContent

메서드methodpub fn set_background_color(&mut self, background_color: impl Into<Color>)#0.3.2 부터since 0.3.2소스source

배경색 설정.

Sets the background color.

background_colorimpl Into<Color>
보이는 영역의 바탕색. (기본 투명, 투명이면 그리지 않는다)Background color of the visible area. (default transparent, not drawn if transparent)

짝Counterpart: vanilla.js UIView.setBackgroundColor

메서드methodpub fn set_scroll_content_size(&mut self, scroll_content_size: Vector2)#0.3.2 부터since 0.3.2소스source

스크롤 콘텐츠 크기 설정.

Sets the scroll content size.

scroll_content_sizeVector2
칸 전체를 담는 콘텐트 크기. (픽셀, 오프셋을 이 크기로 자르므로 칸 전체 길이 이상으로 둔다)Content size that holds all slots. (pixels, the offset is clamped to this size, so set it to at least the full length of all slots)

짝Counterpart: vanilla.js UIScrollView.setScrollContentSize

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

스크롤 콘텐츠 크기 반환.

Returns the scroll content size.

반환ReturnsVector2 콘텐트 전체 크기. (픽셀)Total content size. (pixels)

짝Counterpart: vanilla.js UIScrollView.getScrollContentSize

메서드methodpub fn set_scroll_offset(&mut self, offset: Vector2)#0.3.2 부터since 0.3.2소스source

오프셋 설정. (범위로 자른다)

Sets the offset. (clamped to the range)

offsetVector2
새 오프셋. (픽셀, content 노드의 로컬 위치. 칸을 바꾸지 않고 content 는 다음 tick 에 옮겨진다)New offset. (pixels, the local position of the content node. does not change the slot, and the content is moved on the next tick)

짝Counterpart: vanilla.js UIScrollView.setScrollOffset

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

오프셋 반환.

Returns the offset.

반환ReturnsVector2 지금 오프셋. (픽셀, 0 이하)Current offset. (pixels, 0 or less)

짝Counterpart: vanilla.js UIScrollView.getScrollOffset

메서드methodpub fn set_horizontal(&mut self, is_horizontal: bool)#0.3.2 부터since 0.3.2소스source

가로 스크롤 켜기 설정.

Sets whether horizontal scrolling is on.

is_horizontalbool
true 면 가로로 스냅한다. (기본 true, 세로 스냅은 false 로 두고 set_vertical(true))If true, snaps horizontally. (default true, for vertical snap set it to false and set_vertical(true))

짝Counterpart: vanilla.js UIScrollView.setHorizontal

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

가로 스크롤 켜짐 여부.

Whether horizontal scrolling is on.

반환Returnsbool 가로 스크롤이 켜져 있으면 true. (true 면 진행 축이 x 다)true if horizontal scrolling is on. (if true, the progress axis is x)

짝Counterpart: vanilla.js UIScrollView.isHorizontal

메서드methodpub fn set_vertical(&mut self, is_vertical: bool)#0.3.2 부터since 0.3.2소스source

세로 스크롤 켜기 설정.

Sets whether vertical scrolling is on.

is_verticalbool
true 면 세로로 스크롤한다. (기본 false)If true, scrolls vertically. (default false)

짝Counterpart: vanilla.js UIScrollView.setVertical

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

세로 스크롤 켜짐 여부.

Whether vertical scrolling is on.

반환Returnsbool 세로 스크롤이 켜져 있으면 true.true if vertical scrolling is on.

짝Counterpart: vanilla.js UIScrollView.isVertical

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

드래그 중인지.

Whether it is dragging.

반환Returnsbool 눌러 끄는 중이면 true.true while pressing and dragging.

짝Counterpart: vanilla.js UIScrollView.isDragging

메서드methodpub fn set_snap_to_nearest(&mut self, is_snap_to_nearest: bool)#0.3.2 부터since 0.3.2소스source

스냅 모드 설정. (true 면 끄는 동안 칸 범위를 자르지 않고, 놓으면 가장 가까운 아이템으로 간다)

Sets the snap mode. (if true, the slot range is not clamped while dragging, and on release it goes to the nearest item)

is_snap_to_nearestbool
가장 가까운 칸으로 스냅할지. (기본 false)Whether to snap to the nearest slot. (default false)

짝Counterpart: vanilla.js UISnapScrollView.setSnapToNearest

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

스냅 모드 반환.

Returns the snap mode.

반환Returnsbool set_snap_to_nearest 로 정한 값.The value set with set_snap_to_nearest.

짝Counterpart: vanilla.js UISnapScrollView.getSnapToNearest

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

방향성 스냅 최소 속도 설정. (오프셋의 초당 픽셀, 한 칸 모드에서 놓을 때 드래그 속도가 이 이상이면 끈 거리와 상관없이 그 방향으로 한 칸 넘긴다)

Sets the minimum velocity for directional snapping. (offset pixels per second, in one-slot mode, if the drag velocity on release is at least this, it moves one slot in that direction regardless of the dragged distance)

velocity_thresholdf64
방향성 스냅 최소 속도. (픽셀/초, 기본 200)Minimum velocity for directional snapping. (pixels/second, default 200)

짝Counterpart: vanilla.js UISnapScrollView.setVelocityThreshold

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

방향성 스냅 최소 속도 반환.

Returns the directional snap minimum velocity.

반환Returnsf64 방향성 스냅 최소 속도. (픽셀/초)Directional snap minimum velocity. (pixels/second)

짝Counterpart: vanilla.js UISnapScrollView.getVelocityThreshold

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

칸을 넘길 끈 거리 비율 설정. (다음 아이템 크기 대비, 기본 0.35)

Sets the drag distance ratio for changing slots. (relative to the next item's size, default 0.35)

page_change_thresholdf64
끈 거리가 끈 쪽 다음 칸 크기의 이 비율 이상이면 그 칸으로 넘긴다. (0 ~ 1)If the drag distance is at least this ratio of the next slot's size in the drag direction, moves to that slot. (0 ~ 1)

짝Counterpart: vanilla.js UISnapScrollView.setPageChangeThreshold

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

칸을 넘길 끈 거리 비율 반환.

Returns the drag distance ratio for changing slots.

반환Returnsf64 칸 넘김 비율.Slot change ratio.

짝Counterpart: vanilla.js UISnapScrollView.getPageChangeThreshold

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

드래그 속도 반환. (끄는 동안 진행 축 오프셋이 움직인 초당 픽셀, 다음 칸 쪽이 음수. 마지막으로 끈 tick 의 값이고 끌기를 시작하면 0)

Returns the drag velocity. (pixels per second the offset on the progress axis moved while dragging, negative toward the next slot. The value of the last dragged tick, 0 when a drag starts)

반환Returnsf64 끄는 동안 스크롤 축 오프셋이 움직인 속도. (초당 픽셀, 다음 칸 쪽이 음수, 마지막으로 끈 tick 의 값이고 끌기를 시작하면 0)Velocity of the scroll axis offset while dragging. (Pixels per second, negative toward the next item, the value of the last dragging tick, 0 when a drag starts)

짝Counterpart: vanilla.js UISnapScrollView.getDragVelocity

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

지금 스냅 칸 반환.

Returns the current snap slot.

반환Returnsusize 지금 칸 번호. (0 부터, content 자식 순서)Current slot index. (from 0, in content child order)

짝Counterpart: vanilla.js UISnapScrollView.getSnapCurrentIndex

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

미끄러지는 중인지.

Whether it is sliding.

반환Returnsbool 스냅 미끄러짐이 진행 중이면 true.true if a snap slide is in progress.

메서드methodpub fn compute_snap_offsets(&self, node_tree: &NodeTree) -> Vec<f64>#0.3.2 부터since 0.3.2소스source

content 자식마다 스냅 오프셋 계산. (각 아이템 가운데가 보이는 영역 가운데에 오는 오프셋, 크기가 달라도 맞다)

Calculates the snap offset for each content child. (the offset that puts each item's center at the center of the visible area, correct even with different sizes)

node_tree&NodeTree
이 스냅 뷰가 붙은 노드 트리.Node tree this snap view is attached to.

반환ReturnsVec<f64> content 자식 순서대로 진행 축의 스냅 오프셋. (픽셀, 범위로 자르지 않은 값) 트리에 붙기 전이거나 content 가 없으면 빈 목록.Snap offsets along the progress axis in content child order. (pixels, values not clamped to the range) An empty list before being attached to a tree or if there is no content.

짝Counterpart: vanilla.js UISnapScrollView.computeSnapOffsets

예제Example200 픽셀 칸 셋의 스냅 오프셋Snap offsets of three 200 pixel slots
use vanilla::{NodeTree, Pivot, UISnapScrollView, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let snap_id = node_tree.create_child(root_id, "snap");
node_tree[snap_id].set_pivot(Pivot::TOP_LEFT);
node_tree[snap_id].set_content_size(Vector2::create(200.0, 100.0));
let snap_view = node_tree.add_component(snap_id, UISnapScrollView::new()).expect("snap view");
let content_id = snap_view.get_content().expect("content");
for page_index in 0..3 {
	let page_id = node_tree.create_child(content_id, "page");
	node_tree[page_id].set_pivot(Pivot::TOP_LEFT);
	node_tree[page_id].set_rect(page_index as f64 * 200.0, 0.0, 200.0, 100.0);
}

let snap_view = node_tree[snap_id].get_component::<UISnapScrollView>().expect("snap view");
assert_eq!(snap_view.compute_snap_offsets(&node_tree), vec![0.0, -200.0, -400.0]);
assert!(UISnapScrollView::new().compute_snap_offsets(&node_tree).is_empty());
메서드methodpub fn clamp_scroll_offset_to_adjacent_pages(&mut self, node_tree: &mut NodeTree)#0.3.2 부터since 0.3.2소스source

끄는 동안 오프셋을 지금 칸의 앞뒤 칸 범위로 자른다.

  • tick 이 끄는 중에 부른다. 칸이 없으면 아무것도 하지 않는다.

While dragging, clamps the offset to the range of slots before and after the current slot.

  • Called by tick while dragging. Does nothing if there are no slots.
node_tree&mut NodeTree
이 스냅 뷰가 붙은 노드 트리. (content 자식을 읽는다)Node tree this snap view is attached to. (reads the content children)

짝Counterpart: vanilla.js UISnapScrollView.clampScrollOffsetToAdjacentPages

메서드methodpub fn play_snap_tween(&mut self, node_tree: &mut NodeTree)#0.3.2 부터since 0.3.2소스source

지금 오프셋으로 목표 칸을 정하고 미끄러짐을 시작한다.

  • 가장 가까운 아이템 모드: 스냅 오프셋이 지금 오프셋과 가장 가까운 칸. (같으면 앞 칸)
  • 한 칸 모드: 드래그 속도가 최소 속도 이상이면 속도 방향으로 한 칸, 아니면 끈 방향으로 최대 한 칸. (다음 칸이 비율 이상 드러나면 넘김)

Decides the target slot from the current offset and starts sliding.

  • Nearest item mode: the slot whose snap offset is closest to the current offset. (the earlier slot on a tie)
  • One-slot mode: one slot in the velocity direction if the drag velocity is at least the minimum velocity, otherwise at most one slot in the drag direction. (moves if the next slot is revealed by at least the ratio)
node_tree&mut NodeTree
이 스냅 뷰가 붙은 노드 트리. (content 자식을 읽는다)Node tree this snap view is attached to. (reads the content children)

짝Counterpart: vanilla.js UISnapScrollView.playSnapTween

메서드methodpub fn page_to(&mut self, node_tree: &mut NodeTree, index: usize, smooth: bool)#0.3.2 부터since 0.3.2소스source

지정 칸으로 이동. (smooth 면 미끄러지고, 아니면 바로 옮긴다)

  • 이 컴포넌트를 트리에서 꺼내 불러야 해서 씬 코드에서는 node_tree.with_component_mut 안에서 부른다.

Moves to the given slot. (slides if smooth, otherwise moves right away)

  • This component must be taken out of the tree to call it, so scene code calls it inside node_tree.with_component_mut.
node_tree&mut NodeTree
이 스냅 뷰가 붙은 노드 트리.Node tree this snap view is attached to.
indexusize
옮길 칸 번호. (0 부터, 칸 수를 넘으면 마지막 칸) 칸이 없으면 아무것도 하지 않는다.Slot index to move to. (from 0, the last slot if it exceeds the slot count) Does nothing if there are no slots.
smoothbool
true 면 0.3 초 cubic.out 으로 미끄러지고(다음 tick 부터), false 면 오프셋과 content 노드를 바로 옮긴다.If true, slides with 0.3 seconds cubic.out (from the next tick), if false, moves the offset and the content node right away.

짝Counterpart: vanilla.js UISnapScrollView.pageTo

예제Example바로 옮기기와 미끄러지기Moving right away and sliding
use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, UISnapScrollView, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let snap_id = node_tree.create_child(root_id, "snap");
node_tree[snap_id].set_pivot(Pivot::TOP_LEFT);
node_tree[snap_id].set_content_size(Vector2::create(200.0, 100.0));
let snap_view = node_tree.add_component(snap_id, UISnapScrollView::new()).expect("snap view");
snap_view.set_scroll_content_size(Vector2::create(1000.0, 100.0));
let content_id = snap_view.get_content().expect("content");
for page_index in 0..5 {
	let page_id = node_tree.create_child(content_id, "page");
	node_tree[page_id].set_pivot(Pivot::TOP_LEFT);
	node_tree[page_id].set_rect(page_index as f64 * 200.0, 0.0, 200.0, 100.0);
}

node_tree.with_component_mut::<UISnapScrollView, _>(snap_id, |snap_view, tree| {
	snap_view.page_to(tree, 9, false);
});
assert_eq!(node_tree[content_id].get_local_position(), Vector2::create(-800.0, 0.0));

node_tree.with_component_mut::<UISnapScrollView, _>(snap_id, |snap_view, tree| {
	snap_view.page_to(tree, 2, true);
});
for _ in 0..20 {
	node_tree.tick(&engine, 0.016);
}
let snap_view = node_tree[snap_id].get_component::<UISnapScrollView>().expect("snap view");
assert_eq!(snap_view.get_snap_current_index(), 2);
assert_eq!(node_tree[content_id].get_local_position(), Vector2::create(-400.0, 0.0));
메서드methodpub fn set_snap_current_index(&mut self, node_tree: &mut NodeTree, index: usize)#0.3.2 부터since 0.3.2소스source

지정 칸으로 미끄러짐 시작. (지금 오프셋에서 그 칸의 스냅 오프셋까지)

  • 미끄러짐은 다음 tick 부터 0.3 초 cubic.out 으로 간다. (page_to 의 smooth 와 같다)

Starts sliding to the given slot. (from the current offset to that slot's snap offset)

  • The slide runs with 0.3 seconds cubic.out from the next tick. (same as smooth in page_to)
node_tree&mut NodeTree
이 스냅 뷰가 붙은 노드 트리.Node tree this snap view is attached to.
indexusize
목표 칸 번호. (0 부터, 칸 수를 넘으면 마지막 칸) 칸이 없으면 아무것도 하지 않는다.Target slot index. (from 0, the last slot if it exceeds the slot count) Does nothing if there are no slots.

짝Counterpart: vanilla.js UISnapScrollView.setSnapCurrentIndex

함수Functions

함수functionpub fn find_scroll_view(node: &WorldNode) -> Option<&UIScrollView>#0.3.2 부터since 0.3.2소스source

노드의 스크롤 뷰 찾기. (UIScrollView, 없으면 UISnapScrollView 가 품은 뷰. 원본 getComponents(UIScrollView) 가 하위 클래스까지 잡는 자리)

Finds the node's scroll view. (UIScrollView, or else the view embedded in UISnapScrollView. where the original getComponents(UIScrollView) also catches subclasses)

node&WorldNode
찾을 노드.Node to find.

반환ReturnsOption<&UIScrollView> 노드의 UIScrollView, 없으면 UISnapScrollView 가 품은 스크롤 뷰. 둘 다 없거나 그 컴포넌트가 tick 중이라 꺼내져 있으면 None.The node's UIScrollView, or else the scroll view embedded in UISnapScrollView. None if neither exists or that component is taken out because it is in tick.

예제Example스냅 뷰 노드에서 품은 스크롤 뷰 찾기Find the embedded scroll view from a snap view node
use vanilla::{NodeTree, UISnapScrollView};
use vanilla::ui::uisnapscrollview::find_scroll_view;

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let snap_id = node_tree.create_child(root_id, "snap");
node_tree.add_component(snap_id, UISnapScrollView::new());

let scroll_view = find_scroll_view(&node_tree[snap_id]).expect("scroll view");
assert!(scroll_view.is_horizontal());
assert!(!scroll_view.is_vertical());
assert!(find_scroll_view(&node_tree[root_id]).is_none());
함수functionpub fn find_scroll_view_mut(node: &mut WorldNode) -> Option<&mut UIScrollView>#0.3.2 부터since 0.3.2소스source

노드의 스크롤 뷰 찾기. (변경용)

Finds the node's scroll view. (mutable)

node&mut WorldNode
찾을 노드.Node to find.

반환ReturnsOption<&mut UIScrollView> 노드의 UIScrollView, 없으면 UISnapScrollView 가 품은 스크롤 뷰의 가변 참조. 둘 다 없으면 None.Mutable reference to the node's UIScrollView, or else the scroll view embedded in UISnapScrollView. None if neither exists.