# ui/uiscrollview (vanilla.js 0.4.0)

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

Default scrollbar thickness / margin (UIKit indicator style).

## UIScrollView extends UIView

Scroll view component.
- Uses the owner node's getContentSize() as the visible area.
- Creates a separate content node inside, which can be scrolled by dragging.
- Declares Mask as a dependency with require(), so on the attached node (WorldNode family)
  Mask is added automatically and child clipping works.
- Supports nested ScrollView. (works with TouchRaycaster)

Example: Vertically dragged list with a scroll bar

```js
import { Engine, EngineConfiguration, UIScene, UIScrollView, UILabel, WorldNode, Paint, Color, Pivot, Vector2 } from "./build/vanilla.js";

class ScrollScene extends UIScene {
	create() {
		super.create();
		const viewportNode = new WorldNode();
		viewportNode.setPivot(Pivot.topLeft.clone());
		viewportNode.setAnchor(Pivot.topLeft.clone());
		viewportNode.setContentSize(Vector2.create(320, 240));
		viewportNode.setLocalPosition(Vector2.create(160, 60));
		const scrollView = viewportNode.addComponent(UIScrollView);
		scrollView.setBackgroundColor(new Color(0.1, 0.12, 0.16, 1));
		scrollView.setHorizontal(false);
		scrollView.setScrollMode("elastic");
		scrollView.setShowsVerticalScrollBar(true);

		const rowCount = 20;
		const rowHeight = 40;
		const content = scrollView.getContent();
		for (let rowIndex = 0; rowIndex < rowCount; ++rowIndex) {
			const rowNode = new WorldNode();
			rowNode.setPivot(Pivot.topLeft.clone());
			rowNode.setAnchor(Pivot.topLeft.clone());
			rowNode.setContentSize(Vector2.create(280, rowHeight - 4));
			rowNode.setLocalPosition(Vector2.create(8, rowIndex * rowHeight + 2));
			const rowPaint = rowNode.addComponent(Paint);
			rowPaint.setColor(new Color(1, 1, 1, 0.06));
			const rowLabel = rowNode.addComponent(UILabel);
			rowLabel.setText("Row " + rowIndex);
			content.addChild(rowNode);
		}
		scrollView.setScrollContentSize(Vector2.create(320, rowCount * rowHeight));
		scrollView.setScrollOffset(Vector2.create(0, -120));
		const root = this.getRoot();
		root.addChild(viewportNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new ScrollScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.2.0): Constructor.
- `require()` (method, since 0.2.1): Dependent component; Mask is needed for child clipping.
  - returns Function[]: Components to attach to the same node. ([Mask])
- `attach(node)` (method, since 0.2.0): Attached to a node. (automatically enables isInteractable of the WorldNode family)
  - `node` ComponentNode: The node to attach to. (its content size becomes the visible area)
- `touchPress(viewInputPosition)` (method, since 0.2.0): Touch press. (TouchRaycaster → WorldNode → ScrollView)
  - `viewInputPosition` Vector2: Input position. (view coordinates)
- `touchMove(viewInputPosition)` (method, since 0.2.0): Touch move. (TouchRaycaster → WorldNode → ScrollView)
  - `viewInputPosition` Vector2: Input position. (view coordinates, stored only while dragging and applied in tick)
- `touchRelease(viewInputPosition)` (method, since 0.2.0): Touch release. (TouchRaycaster → WorldNode → ScrollView)
  - `viewInputPosition` Vector2: Input position. (view coordinates, unused)
- `touchCancel(viewInputPosition)` (method, since 0.2.0): Touch cancel. (TouchRaycaster → WorldNode → ScrollView)
  - `viewInputPosition` Vector2: Input position. (view coordinates, unused)
- `tick(timeDelta)` (method, since 0.2.0): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `applyScrollOffset(offset)` (method, since 0.2.0): Applies the scroll offset. (including clamping)
  - `offset` Vector2: Scroll offset. (position of the content node, 0 is the top left and it goes negative as you scroll down, only draggable axes are clamped to the range and other axes are 0)
- `setScrollOffset(offset)` (method, since 0.2.0): Sets the scroll offset.
  - `offset` Vector2: Scroll offset. (0 is the top left and it goes negative as you scroll down, clamped to the range)

  Example: Offset sign and range clamping

  ```js
  import { UIScrollView, WorldNode, Vector2 } from "./build/vanilla.js";
  
  const viewportNode = new WorldNode();
  viewportNode.setContentSize(Vector2.create(200, 100));
  const scrollView = viewportNode.addComponent(UIScrollView);
  scrollView.setScrollContentSize(Vector2.create(200, 500));
  scrollView.setScrollOffset(Vector2.create(0, -1000));
  const clampedOffset = scrollView.getScrollOffset();
  console.log(clampedOffset.y);
  scrollView.wheel(Vector2.create(0, -150));
  const wheeledOffset = scrollView.getScrollOffset();
  console.log(wheeledOffset.y);
  ```

- `getScrollOffset()` (method, since 0.2.0): Returns the scroll offset.
  - returns Vector2: Scroll offset. (the original, not a copy, 0 is the top left and it goes negative as you scroll down)
- `setHorizontal(horizontal)` (method, since 0.2.0): Sets horizontal scroll enabled. (false = horizontal scroll disabled)
  - `horizontal` boolean: If true, it can be dragged horizontally.
- `isHorizontal()` (method, since 0.2.0): Returns horizontal scroll enabled.
  - returns boolean: True if it can be dragged horizontally.
- `setVertical(vertical)` (method, since 0.2.0): Sets vertical scroll enabled. (false = vertical scroll disabled)
  - `vertical` boolean: If true, it can be dragged vertically.
- `isVertical()` (method, since 0.2.0): Returns vertical scroll enabled.
  - returns boolean: True if it can be dragged vertically.
- `setPassesCrossAxisDrag(passes)` (method, since 0.3.2): Sets whether to leave a finger dragging along the non-scrolling axis to the pressed node. (default false)
  - If false, as before, the scroll view takes it once the drag threshold is exceeded in any direction.
  - If true, in a vertical-only scroll view a finger dragged more horizontally (vertically, for horizontal-only) is not taken,
    and the pressed node (slider, etc.) receives the press right away and drags to the end. If both axes scroll, it is always taken.
  - `passes` boolean: If true, drags along the axis that does not scroll are left to the pressed node.

  Example: Leaving horizontal drags inside a vertical list

  ```js
  import { UIScrollView, WorldNode, Vector2 } from "./build/vanilla.js";
  
  const viewportNode = new WorldNode();
  viewportNode.setContentSize(Vector2.create(200, 100));
  const scrollView = viewportNode.addComponent(UIScrollView);
  scrollView.setHorizontal(false);
  scrollView.setPassesCrossAxisDrag(true);
  const isSidewaysTaken = scrollView.isScrollDrag(30, 5);
  const isDownwardTaken = scrollView.isScrollDrag(5, 30);
  console.log(isSidewaysTaken, isDownwardTaken);
  ```

- `isPassingCrossAxisDrag()` (method, since 0.3.2): Returns whether a finger dragging along the non-scrolling axis is left to the pressed node.
  - returns boolean: True if drags along the axis that does not scroll are left to the pressed node.
- `isScrollDrag(deltaX, deltaY)` (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)
  - `deltaX` number: Horizontal distance dragged from the pressed point. (pixels, the sign is ignored)
  - `deltaY` number: Vertical distance dragged from the pressed point. (pixels, the sign is ignored)
  - returns boolean: True if this view takes it. (always true if the option is off or both axes scroll, false if no axis can be dragged)
- `setDragSensitivity(dragSensitivity)` (method, since 0.2.0): Sets the drag amount multiplier. (1.0 = default, 2.0 = twice as fast)
  - `dragSensitivity` number: Drag sensitivity multiplier. (multiplied by the dragged distance)
- `getDragSensitivity()` (method, since 0.2.0): Returns the drag amount multiplier.
  - returns number: Drag sensitivity multiplier. (default 1)
- `setScrollMode(scrollMode)` (method, since 0.2.0): Sets the scroll mode.
  - `scrollMode` string: "clamp" stops at the bounds, "elastic" resists past the bounds and springs back on release.
- `getScrollMode()` (method, since 0.2.0): Returns the scroll mode.
  - returns string: "clamp" or "elastic". (default "clamp")
- `setScrollContentSize(scrollContentSize)` (method, since 0.2.0): Sets the scroll content size. (must be set larger than the visible area to scroll)
  - `scrollContentSize` Vector2: Size of the content to scroll. (pixels, cloned and also set as the content node's content size)
- `getScrollContentSize()` (method, since 0.2.0): Returns the scroll content size.
  - returns Vector2: Size of the content to scroll. (pixels)
- `isDragging()` (method, since 0.2.0): Returns whether dragging.
  - returns boolean: True while pressed.
- `getScrollVelocity()` (method, since 0.2.0): Returns the current scroll velocity.
  - returns Vector2: Scroll velocity. (pixels per second, computed only in elastic mode)
- `setShowsVerticalScrollBar(shows)` (method, since 0.2.1): Sets whether to show the vertical scrollbar. If true, creates it automatically and adds it as a child of the host node.
  - `shows` boolean: If true, it is shown. (the scroll bar cannot be created before attaching to a node)
- `setShowsHorizontalScrollBar(shows)` (method, since 0.2.1): Sets whether to show the horizontal scrollbar.
  - `shows` boolean: If true, it is shown. (created and added as a child of the node when first turned on, cannot be created before attaching to a node)
- `getVerticalScrollBar()` (method, since 0.2.1): Returns the vertical / horizontal scrollbar instance. (for customization)
  - returns UIScrollBar | null: Vertical scroll bar. (null if never turned on)
- `getHorizontalScrollBar()` (method, since 0.2.1): Returns the horizontal scroll bar. (for customization)
  - returns UIScrollBar | null: Horizontal scroll bar. (null if never turned on)
- `getVerticalScrollBarReservedWidth()` (method, since 0.2.1): Thickness of the area reserved by the scrollbar. (only while the scrollbar is active/shown)
  - returns number: Width taken by the vertical scroll bar. (pixels, 24 when shown, otherwise 0)
- `getHorizontalScrollBarReservedHeight()` (method, since 0.2.1): Thickness of the area taken by the horizontal scroll bar. (only while the scroll bar is shown)
  - returns number: Height taken by the horizontal scroll bar. (pixels, 24 when shown, otherwise 0)
- `getInnerContentSize()` (method, since 0.2.1): Visible area size where the actual content can be drawn, excluding scrollbars.
  - If there is a horizontal scrollbar the height shrinks by that much, and if there is a vertical scrollbar the width shrinks.
  - returns Vector2: Visible area size minus the scroll bars. (pixels, 0 before attaching to a node)
- `layoutScrollBars()` (method, since 0.2.1): Updates the scrollbar position / size every tick.
- `wheel(delta)` (method, since 0.2.1): Handles wheel input. (TouchRaycaster → UIScrollView)
  - delta: accumulated mouse wheel value (same sign as DOM WheelEvent's deltaX/deltaY. down = +y)
  - `delta` Vector2: Accumulated wheel delta. (multiplied by the wheel scroll scale and subtracted from the offset, does nothing if missing)
- `setWheelScrollScale(scale)` (method, since 0.2.1): Sets the wheel scroll multiplier.
  - `scale` number: Multiplier applied to the accumulated wheel delta. (default 1)

## ScrollMode (enum)

Scroll mode.
- clamp: the default mode that does not go past the bounds.
- elastic: resists past the bounds and springs back when released.

- `clamp` = `clamp`: Clamps at the bounds.
- `elastic` = `elastic`: Resists past the bounds and springs back on release.
