# ui/uislider (vanilla.js 0.4.0)

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

Slider control.

## UISlider extends UIControl

Slider control.
- Adjusts the value by user input (drag/tap). → inherits UIControl.
- The display part (track/fill) looks similar to UIProgressView, but since it must belong to the control
  tree it does not inherit UIProgressView and draws itself.
- Also draws the thumb.

Example: Make a volume slider

```js
import { Engine, EngineConfiguration, UIScene, UISlider, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";

class MainScene extends UIScene {
	constructor() {
		super();
		this.setLoadingMinDurationMs(0);
	}

	initialize(engine) {
		super.initialize(engine);
		const sliderNode = new WorldNode();
		sliderNode.setPivot(Pivot.topLeft);
		sliderNode.setAnchor(Pivot.topLeft);
		sliderNode.setLocalPosition(Vector2.create(40, 60));
		sliderNode.setContentSize(Vector2.create(240, 24));
		const slider = sliderNode.addComponent(UISlider);
		slider.setRange(0, 100);
		slider.setValue(50);
		slider.setTrackThickness(6);
		slider.setCornerRadius(3);
		slider.setThumbRadius(12);
		slider.setValueChangedEvent((changedSlider) => {
			console.log("volume", changedSlider.getValue());
		});
		const root = this.getRoot();
		root.addChild(sliderNode);
		console.log(slider.getValue(), slider.getRatio());
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.canvasId = "mainCanvas";
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.2.1): Constructor.
- `attach(node)` (method, since 0.2.1): Attached to a node. (automatically enables the WorldNode's isInteractable)
  - `node` ComponentNode: Node to attach to.
- `draw(graphic)` (method, since 0.2.1): Render. (track + fill + thumb)
  - `graphic` Graphic: Graphics.
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes the drawn rectangle to rect. (Override: the track and fill stay inside the content area, the thumb circle spills by its radius along the whole range it moves over)
  - Reports a value-independent rectangle that covers the thumb wherever the value moves it. (Changing the value does not recalculate bounds)
  - With a thumb node set, no circle is drawn, so only the content area. Grown by one unit for anti-aliasing.
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if attached to a node.

  Example: Seeing how far the thumb circle spills

  ```js
  import { WorldNode, UISlider, Rect, Vector2 } from "./build/vanilla.js";
  
  const sliderNode = new WorldNode();
  sliderNode.setContentSize(Vector2.create(200, 8));
  const slider = sliderNode.addComponent(UISlider);
  slider.setThumbRadius(12);
  const drawnRect = Rect.zero();
  slider.calculateDrawnRect(drawnRect);
  console.log(drawnRect.position.x, drawnRect.position.y, drawnRect.size.x, drawnRect.size.y);
  ```

- `touchPress(viewInputPosition)` (method, since 0.2.1): Touch press.
  - `viewInputPosition` Vector2: Press position. (view coordinates)
- `touchMove(viewInputPosition)` (method, since 0.2.1): Touch move.
  - `viewInputPosition` Vector2: Moved position. (view coordinates)
- `touchRelease(viewInputPosition)` (method, since 0.2.1): Touch release.
  - `viewInputPosition` Vector2: Release position. (view coordinates)
- `touchCancel(viewInputPosition)` (method, since 0.2.1): Touch cancel.
  - `viewInputPosition` Vector2: Position at cancel. (view coordinates)
- `getRatio()` (method, since 0.2.1): Value/range/ratio.
  - returns number: Ratio of the value within the range. (0 ~ 1, 0 if the range is 0 or less)
- `setValue(value)` (method, since 0.2.1): Sets the value. (clamped to the range, does not call the value changed event)
  - `value` number: New value. (minValue ~ maxValue)
- `getValue()` (method, since 0.2.1): Returns the value.
  - returns number: The current value. (default 0)
- `setRange(minValue, maxValue)` (method, since 0.2.1): Sets the range. (the current value is also clamped to the new range)
  - `minValue` number: Minimum value. (default 0)
  - `maxValue` number: Maximum value. (default 1)
- `getMinValue()` (method, since 0.2.1): Returns the minimum value.
  - returns number: Minimum value.
- `getMaxValue()` (method, since 0.2.1): Returns the maximum value.
  - returns number: Maximum value.
- `fireValueChanged()` (method, since 0.2.1): For when you want to force the callback after an external setValue.
- `setTrackColor(color)` (method, since 0.2.1): Sets the track color.
  - `color` Color | string: Track color. (default new Color(0.7, 0.7, 0.7, 1))
- `getTrackColor()` (method, since 0.2.1): Returns the track color.
  - returns Color: Track color.
- `setFillColor(color)` (method, since 0.2.1): Sets the fill color.
  - `color` Color | string: Color filled up to the value. (default new Color(0.23, 0.51, 0.96, 1))
- `getFillColor()` (method, since 0.2.1): Returns the fill color.
  - returns Color: Fill color.
- `setThumbColor(color)` (method, since 0.2.1): Sets the thumb color.
  - `color` Color | string: Color of the thumb circle. (default white, unused if a thumb node is set)
- `getThumbColor()` (method, since 0.2.1): Returns the thumb color.
  - returns Color: Color of the thumb circle.
- `setThumbRadius(radius)` (method, since 0.2.1): Set the thumb radius.
  - `radius` number: Radius of the thumb circle. (default 16, unused if a thumb node is set)
- `getThumbRadius()` (method, since 0.2.1): Returns the thumb radius.
  - returns number: Radius of the thumb circle.
- `setTrackThickness(thickness)` (method, since 0.3.0): Sets the track thickness. (drawn at this thickness in the middle of the node)
  - `thickness` number: Track thickness. (the whole node if 0 or less, default 0)
- `getTrackThickness()` (method, since 0.3.0): Returns the track thickness.
  - returns number: Track thickness. (0 means the whole node)
- `setThumbNode(node)` (method, since 0.3.0): Sets the thumb node. (when set, moves this node to the thumb position instead of drawing a circle)
  - `node` WorldNode: Node to use as the thumb. (a child of the slider node with top-left pivot and anchor, a circle is drawn if null)

  Example: Use a drawn node as the thumb

  ```js
  import { Engine, EngineConfiguration, UIScene, UISlider, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";
  
  class MainScene extends UIScene {
  	constructor() {
  		super();
  		this.setLoadingMinDurationMs(0);
  	}
  
  	initialize(engine) {
  		super.initialize(engine);
  		const sliderNode = new WorldNode();
  		sliderNode.setPivot(Pivot.topLeft);
  		sliderNode.setAnchor(Pivot.topLeft);
  		sliderNode.setLocalPosition(Vector2.create(40, 60));
  		sliderNode.setContentSize(Vector2.create(240, 24));
  		const slider = sliderNode.addComponent(UISlider);
  		slider.setTrackThickness(4);
  
  		const thumbNode = new WorldNode();
  		thumbNode.setPivot(Pivot.topLeft);
  		thumbNode.setAnchor(Pivot.topLeft);
  		thumbNode.setContentSize(Vector2.create(20, 28));
  		const thumbPaint = thumbNode.addComponent(Paint);
  		thumbPaint.setColor(new Color(1, 0.6, 0.2, 1));
  		thumbPaint.setRoundSize(6);
  		sliderNode.addChild(thumbNode);
  		slider.setThumbNode(thumbNode);
  		slider.setValue(0.3);
  
  		const root = this.getRoot();
  		root.addChild(sliderNode);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  engineConfiguration.canvasId = "mainCanvas";
  engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
  engineConfiguration.autoResizeOnWindowResize = true;
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `getThumbNode()` (method, since 0.3.0): Returns the thumb node.
  - returns WorldNode: Thumb node. (null if none)
- `setCornerRadius(radius)` (method, since 0.2.1): Sets the corner roundness.
  - `radius` number: Corner radius of the track and fill. (square corners if 0 or less, default 0)
- `getCornerRadius()` (method, since 0.2.1): Returns the corner roundness.
  - returns number: Corner radius of the track and fill.
- `setDirection(direction)` (method, since 0.2.1): Sets the direction.
  - `direction` string: Direction. (horizontal fills left to right, vertical fills bottom to top, default horizontal)
- `getDirection()` (method, since 0.2.1): Returns the direction.
  - returns string: Direction. (horizontal or vertical)
- `setValueChangedEvent(callback)` (method, since 0.2.1): Callback / interaction.
  - `callback` (slider: UISlider) => void: Function called when pressing or dragging changes the value. (the argument is this slider, not called when changed with setValue, not called if null)
- `getValueChangedEvent()` (method, since 0.2.1): Returns the value changed event.
  - returns (slider: UISlider) => void: Value changed event function. (null if none)
- `setInteractable(isInteractable)` (method, since 0.2.1): Sets whether it accepts input.
  - `isInteractable` boolean: If false, new presses are ignored. (default true)
- `getInteractable()` (method, since 0.2.1): Returns whether it accepts input.
  - returns boolean: True if it accepts presses.
- `isDragging()` (method, since 0.2.1): Whether it is being dragged.
  - returns boolean: True from press until release or cancel.
