# ui/uiscrollview (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uiscrollview/

Scroll view. (UIScrollView, ScrollMode of the original ui/uiscrollview.js)
- Uses the host's content_size as the visible area and moves the content node by dragging or rolling the wheel. Scroll offset = local position of the content node.
- As in the original, attaches a Mask to the host (require) to clip the content, and turns the host interactable. Attach with node_tree.add_component(node_id, UIScrollView::new()).
- Scroll bars (nodes with UIScrollBar attached) are created as children of the host (siblings of content) as in the original code.
- Wheel: the original TouchRaycaster.touchWheel → UIScrollView.wheel path. The engine asks for wheel from the node under the touch toward its parents, and this component consumes it.
- Differences from the original:
  - The original changes nodes right away in set_scroll_offset / set_scroll_content_size / setShows...ScrollBar. Scene code in the Rust version holds only the component, so
    it sets the values and applies them to the content node and scroll bar nodes on the next tick. To move right away in the same frame, use UIScrollView::set_scroll_offset_in_tree.
  - So get_vertical_scroll_bar / get_horizontal_scroll_bar return a node handle only after the first tick following setting them to show.
    To create and turn on the scroll bar node on the spot as in the original, use UIScrollView::set_shows_vertical_scroll_bar_in_tree (horizontal for horizontal). (returns the created node handle)
  - The visible area size (host content_size) is read on attach and on tick / touch / wheel. An offset set from scene code is reclamped to the new size on the next tick.
  - The gesture that hands a drag over to the scroll view above (the original TouchRecognizer) is separate, in ui/touchrecognizer.rs. The scene chooses to use it.
- A quirk kept from the original: a scroll bar node that turned itself off (auto hide) because the content fits in the area does not tick, so it does not turn back on by itself.
  Calling setShows...ScrollBar(true) again turns it on.

## UIScrollView (struct) (Default, Component)

Scroll view.
- The host's content_size is the visible area, and children attached to the content node move by the offset. The offset is between 0 and (visible size - content size).

Example: Hold a vertically long list and scroll to the end

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, 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");
for index in 0..10 {
	let item_id = node_tree.create_child(content_id, "item");
	node_tree[item_id].set_pivot(Pivot::TOP_LEFT);
	node_tree[item_id].set_rect(0.0, index as f64 * 40.0, 200.0, 40.0);
}

let scroll_view = node_tree[scroll_id].get_component_mut::<UIScrollView>().expect("scroll view");
scroll_view.set_scroll_offset(Vector2::create(0.0, -1000.0));
assert_eq!(scroll_view.get_scroll_offset(), Vector2::create(0.0, -300.0));
node_tree.tick(&engine, 0.016);
assert_eq!(node_tree[content_id].get_local_position(), Vector2::create(0.0, -300.0));
assert_eq!(node_tree[content_id].get_content_size(), Vector2::create(200.0, 400.0));
```

- `pub fn new() -> UIScrollView` (constructor, since 0.3.2): Constructor.
  - Use node_tree.add_component to attach it to a node. (on attach, creates the content node and the Mask and turns interactable on)
  - returns UIScrollView: New scroll view with both horizontal and vertical on, offset 0, clamp mode. (no scroll bars, transparent background)
- `pub fn get_uiview(&self) -> &UIView` (method, since 0.3.2): Returns the embedded view.
  - returns &UIView: Embedded view holding the background color and the content node.
- `pub fn get_uiview_mut(&mut self) -> &mut UIView` (method, since 0.3.2): Returns the embedded view. (mutable)
  - returns &mut UIView: Mutable reference to the embedded view.
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle. (attach child nodes to scroll here)
  - returns Option<NodeId>: content node. None before being attached to a tree (node_tree.add_component).
- `pub fn set_background_color(&mut self, background_color: impl Into<Color>)` (method, since 0.3.2): Sets the background color.
  - `background_color` impl Into<Color>: Background color of the visible area. (default transparent (1, 1, 1, 0), not drawn if transparent)
- `pub fn get_background_color(&self) -> Color` (method, since 0.3.2): Returns the background color.
  - returns Color: Background color.
- `pub fn set_scroll_offset_in_tree(node_tree: &mut NodeTree, scroll_node_id: NodeId, offset: Vector2) -> bool` (static, since 0.3.2): Sets the offset within the tree and moves the content node right away. (false if there is no scroll view or it is currently taken out)
  - Used when a scroll bar is dragged, and when scene code wants to move it right away in the same frame.
  - `node_tree` &mut NodeTree: Node tree that holds the scroll view.
  - `scroll_node_id` NodeId: Node with a UIScrollView attached.
  - `offset` Vector2: New offset. (pixels, rereads the visible area from that node's current content_size and clamps to the range)
  - returns bool: true if set. false if the node does not exist or has no UIScrollView (also when that component is taken out, as during its tick).

  Example: Move the content node on the spot

  ```rust
  use vanilla::{NodeTree, Pivot, UIScrollView, Vector2};
  
  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");
  
  assert!(UIScrollView::set_scroll_offset_in_tree(&mut node_tree, scroll_id, Vector2::create(0.0, -80.0)));
  assert_eq!(node_tree[content_id].get_local_position(), Vector2::create(0.0, -80.0));
  assert!(!UIScrollView::set_scroll_offset_in_tree(&mut node_tree, root_id, Vector2::zero()));
  ```

- `pub fn apply_scroll_offset(&mut self, offset: Vector2)` (method, since 0.3.2): Applies the offset. (clamped to the range. the content node is moved on the next apply)
  - Unlike set_scroll_offset, it is not reclamped on the next tick. (clamped only with the currently read visible area size)
  - `offset` Vector2: New offset. (pixels, the local position of the content node. x is clamped to (visible width - content width) ~ 0, y likewise. 0 on a disabled axis)
- `pub fn set_scroll_offset(&mut self, offset: Vector2)` (method, since 0.3.2): Sets the offset. (for scene code. on the next tick, reclamped to the area size at that time and the content is moved)
  - `offset` Vector2: New offset. (pixels, y is negative when scrolled down. clamped once right away to the current area size)

  Example: The set offset is clamped right away and the content is moved on the next tick

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, 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");
  
  scroll_view.set_scroll_offset(Vector2::create(-50.0, -120.0));
  assert_eq!(scroll_view.get_scroll_offset(), Vector2::create(0.0, -120.0));
  assert_eq!(node_tree[content_id].get_local_position(), Vector2::zero());
  node_tree.tick(&engine, 0.016);
  assert_eq!(node_tree[content_id].get_local_position(), Vector2::create(0.0, -120.0));
  ```

- `pub fn get_scroll_offset(&self) -> Vector2` (method, since 0.3.2): Returns the offset. (local position of the content node, y is negative when scrolled down)
  - returns Vector2: Current offset. (pixels, 0 or less)
- `pub fn set_horizontal(&mut self, is_horizontal: bool)` (method, since 0.3.2): Sets whether horizontal scrolling is on.
  - `is_horizontal` bool: If true, it can be dragged and rolled horizontally. (default true) If false, offset x is clamped to 0.
- `pub fn is_horizontal(&self) -> bool` (method, since 0.3.2): Whether horizontal scrolling is on.
  - returns bool: true if horizontal scrolling is on.
- `pub fn set_vertical(&mut self, is_vertical: bool)` (method, since 0.3.2): Sets whether vertical scrolling is on.
  - `is_vertical` bool: If true, it can be dragged and rolled vertically. (default true) If false, offset y is clamped to 0.
- `pub fn is_vertical(&self) -> bool` (method, since 0.3.2): Whether vertical scrolling is on.
  - returns bool: true if vertical scrolling is on.
- `pub fn set_passes_cross_axis_drag(&mut self, passes_cross_axis_drag: bool)` (method, since 0.3.2): Sets whether a finger dragging along the non-scrolling axis is left to the pressed node. (default false, read by TouchRecognizer)
  - `passes_cross_axis_drag` bool: If true, a scroll view with only one axis on does not take drags along the other axis. (is_scroll_drag)
- `pub fn is_passing_cross_axis_drag(&self) -> bool` (method, since 0.3.2): Whether a finger dragging along the non-scrolling axis is left to the pressed node.
  - returns bool: true if the leave option is on.
- `pub fn is_scroll_drag(&self, delta_x: f64, delta_y: f64) -> bool` (method, since 0.3.2): Whether the drag direction is a drag this view should take. (only distinguishes when the option to leave drags on the non-scrolling axis is on)
  - `delta_x` f64: Drag distance x. (pixels, the sign is ignored)
  - `delta_y` f64: Drag distance y. (pixels, the sign is ignored)
  - returns bool: true if it is a drag to take. Always true if the option is off or both axes are on. With only vertical on, true when |y| >= |x|, with only horizontal on, true when |x| >= |y|. false if both axes are off.

  Example: A vertical list leaving horizontal drags alone

  ```rust
  use vanilla::UIScrollView;
  
  let mut scroll_view = UIScrollView::new();
  scroll_view.set_horizontal(false);
  assert!(scroll_view.is_scroll_drag(30.0, 2.0));
  
  scroll_view.set_passes_cross_axis_drag(true);
  assert!(!scroll_view.is_scroll_drag(30.0, 2.0));
  assert!(scroll_view.is_scroll_drag(2.0, -30.0));
  ```

- `pub fn set_drag_sensitivity(&mut self, drag_sensitivity: f64)` (method, since 0.3.2): Sets the drag amount scale. (1 = default, 2 = twice as fast)
  - `drag_sensitivity` f64: Scale to multiply the drag distance by. (default 1)
- `pub fn get_drag_sensitivity(&self) -> f64` (method, since 0.3.2): Returns the drag amount multiplier.
  - returns f64: Drag scale.
- `pub fn set_scroll_mode(&mut self, scroll_mode: ScrollMode)` (method, since 0.3.2): Sets the scroll mode.
  - `scroll_mode` ScrollMode: Boundary handling mode. (default ScrollMode::Clamp)
- `pub fn get_scroll_mode(&self) -> ScrollMode` (method, since 0.3.2): Returns the scroll mode.
  - returns ScrollMode: Current scroll mode.
- `pub fn set_scroll_content_size(&mut self, scroll_content_size: Vector2)` (method, since 0.3.2): Sets the scroll content size. (must be larger than the visible area to scroll. the content node size is fitted on the next tick)
  - `scroll_content_size` Vector2: Total size of the content to scroll. (pixels, default (0, 0))
- `pub fn get_scroll_content_size(&self) -> Vector2` (method, since 0.3.2): Returns the scroll content size.
  - returns Vector2: Total content size. (pixels)
- `pub fn is_dragging(&self) -> bool` (method, since 0.3.2): Whether it is dragging.
  - returns bool: true while pressing and dragging. (false after release or cancel)
- `pub fn get_scroll_velocity(&self) -> Vector2` (method, since 0.3.2): Returns the current scroll velocity.
  - returns Vector2: Scroll velocity. (pixels/second) Calculated only in elastic mode, and in clamp mode it is reset to 0 on press and stays there.
- `pub fn set_shows_vertical_scroll_bar(&mut self, shows: bool)` (method, since 0.3.2): Sets showing the vertical scroll bar. (if true, creates it as a child of the host and turns it on at the next tick)
  - `shows` bool: If true, shows it, if false, turns off the already created scroll bar node. (default false)

  Example: A vertical scroll bar that appears after the first tick

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, 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));
  scroll_view.set_shows_vertical_scroll_bar(true);
  assert!(scroll_view.get_vertical_scroll_bar().is_none());
  
  node_tree.tick(&engine, 0.016);
  let scroll_view = node_tree[scroll_id].get_component::<UIScrollView>().expect("scroll view");
  let scroll_bar_id = scroll_view.get_vertical_scroll_bar().expect("scroll bar");
  assert_eq!(scroll_view.get_inner_content_size(), Vector2::create(176.0, 100.0));
  assert_eq!(node_tree[scroll_bar_id].get_content_size(), Vector2::create(12.0, 88.0));
  ```

- `pub fn set_shows_horizontal_scroll_bar(&mut self, shows: bool)` (method, since 0.3.2): Sets showing the horizontal scroll bar.
  - Same form as vertical. (if true, creates it as a child of the host and turns it on at the next tick)
  - `shows` bool: If true, shows it, if false, turns off the already created scroll bar node. (default false)
- `pub fn set_shows_vertical_scroll_bar_in_tree(node_tree: &mut NodeTree, scroll_node_id: NodeId, shows: bool) -> Option<NodeId>` (static, since 0.4.0): Applies the vertical scroll bar show setting to the node on the spot. (like the original setShowsVerticalScrollBar, creates it as a child of the host if missing and turns it on or off, scroll bar node handle)
  - `node_tree` &mut NodeTree: Node tree that holds the scroll view.
  - `scroll_node_id` NodeId: Node with a UIScrollView attached.
  - `shows` bool: If true, creates and turns it on, if false, turns off the existing node.
  - returns Option<NodeId>: Vertical scroll bar node. None if there is no node or UIScrollView. Also None when turning off and no node has been created yet.

  Example: Create and turn off right away without a tick

  ```rust
  use vanilla::{NodeTree, Pivot, UIScrollBar, UIScrollView, Vector2};
  
  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));
  node_tree.add_component(scroll_id, UIScrollView::new());
  
  let scroll_bar_id = UIScrollView::set_shows_vertical_scroll_bar_in_tree(&mut node_tree, scroll_id, true).expect("scroll bar");
  assert_eq!(node_tree.get_parent_id(scroll_bar_id), Some(scroll_id));
  assert!(node_tree[scroll_bar_id].get_component::<UIScrollBar>().is_some());
  assert!(node_tree[scroll_bar_id].is_active());
  
  assert_eq!(UIScrollView::set_shows_vertical_scroll_bar_in_tree(&mut node_tree, scroll_id, false), Some(scroll_bar_id));
  assert!(!node_tree[scroll_bar_id].is_active());
  assert!(UIScrollView::set_shows_horizontal_scroll_bar_in_tree(&mut node_tree, scroll_id, false).is_none());
  ```

- `pub fn set_shows_horizontal_scroll_bar_in_tree(node_tree: &mut NodeTree, scroll_node_id: NodeId, shows: bool) -> Option<NodeId>` (static, since 0.4.0): Applies the horizontal scroll bar show setting to the node on the spot. (original setShowsHorizontalScrollBar)
  - Same form as vertical. (creates it as a child of the host if missing and turns it on or off)
  - `node_tree` &mut NodeTree: Node tree that holds the scroll view.
  - `scroll_node_id` NodeId: Node with a UIScrollView attached.
  - `shows` bool: If true, creates and turns it on, if false, turns off the existing node.
  - returns Option<NodeId>: Horizontal scroll bar node. None if there is no node or UIScrollView. Also None when turning off and no node has been created yet.
- `pub fn get_vertical_scroll_bar(&self) -> Option<NodeId>` (method, since 0.3.2): Vertical scroll bar node handle. (`node_tree[id].get_component_mut::<UIScrollBar>()` to customize)
  - returns Option<NodeId>: Vertical scroll bar node. None if not created yet. (appears after the first tick following set_shows_vertical_scroll_bar)
- `pub fn get_horizontal_scroll_bar(&self) -> Option<NodeId>` (method, since 0.3.2): Horizontal scroll bar node handle.
  - returns Option<NodeId>: Horizontal scroll bar node. None if not created yet.
- `pub fn get_vertical_scroll_bar_reserved_width(&self) -> f64` (method, since 0.3.2): Width taken by the vertical scroll bar. (only while shown)
  - returns f64: 24 if set to show and the node exists (twice thickness 12 + margin 6, pixels), otherwise 0.
- `pub fn get_horizontal_scroll_bar_reserved_height(&self) -> f64` (method, since 0.3.2): Height taken by the horizontal scroll bar. (only while shown)
  - returns f64: 24 if set to show and the node exists (twice thickness 12 + margin 6, pixels), otherwise 0.
- `pub fn get_inner_content_size(&self) -> Vector2` (method, since 0.3.2): Visible area size where content can be drawn, excluding the scroll bars.
  - returns Vector2: Size of the visible area minus the scroll bar space. (pixels, never below 0. the visible area is the value read on attach and on tick, touch, wheel)
- `pub fn set_wheel_scroll_scale(&mut self, wheel_scroll_scale: f64)` (method, since 0.3.2): Sets the wheel scroll multiplier.
  - `wheel_scroll_scale` f64: Scale to multiply the wheel value by. (default 1)

## ScrollMode (enum) (Clone, Copy, Debug, PartialEq, Eq)

Scroll mode.

- `Clamp`: Default mode that does not go past the bounds.
- `Elastic`: Going past the bounds adds resistance, and on release it bounces back like a spring.
