# ui/uislider (vanilla-engine 0.4.0)

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

Slider. (UISlider of the original ui/uislider.js)
- Picks a value by dragging or pressing. Draws the track + fill + thumb (circle) directly. The value is only clamped to the range. (the original has no tick snapping either)
- When a thumb node (set_thumb_node) is set, it does not draw the circle and moves that node to the thumb position.
- Differences from the original:
  - The original moves the thumb node inside draw. The Rust version cannot change the tree while drawing, so it moves it in tick. (the thumb node is kept as a child of the host)
  - Input coordinates are relative to the host's top-left corner as in the original, but converted with the inverse of the node matrix so they follow scale and rotation too. (the original only subtracts the corner position)
  - The value changed event takes the slider and the context. fire_value_changed called from scene code fires the event on the next tick.
    Received events are stored in `Rc<RefCell>`. get_value_changed_event returns that Rc, and the caller borrows it with borrow_mut and calls it.
- attach_to_tree turns the host node interactable. Attach with node_tree.add_component(node_id, UISlider::new()).

## UISliderEvent (type)

Slider event. (takes the slider and the context)

```rust
pub type UISliderEvent = Box<dyn FnMut(&mut UISlider, &mut ComponentContext)>
```

## UISlider (struct) (Default, Component)

Slider.

Example: Make a volume slider

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, Pivot, TouchKind, UISlider, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let slider_id = node_tree.create_child(root_id, "VolumeSlider");
node_tree[slider_id].set_pivot(Pivot::TOP_LEFT);
node_tree[slider_id].set_local_position(Vector2::create(100.0, 50.0));
node_tree[slider_id].set_content_size(Vector2::create(200.0, 20.0));
let volume = Rc::new(Cell::new(0.0));
let volume_for_event = volume.clone();
let slider = node_tree.add_component(slider_id, UISlider::new()).expect("slider");
slider.set_range(0.0, 100.0);
slider.set_track_thickness(6.0);
slider.set_thumb_radius(10.0);
slider.set_value_changed_event(Box::new(move |slider: &mut UISlider, _context: &mut ComponentContext| {
	volume_for_event.set(slider.get_value());
}));

node_tree.dispatch_touch(slider_id, &engine, TouchKind::Press, Vector2::create(150.0, 60.0));
assert_eq!(volume.get(), 25.0);
node_tree.dispatch_touch(slider_id, &engine, TouchKind::Moved, Vector2::create(500.0, 60.0));
assert_eq!(volume.get(), 100.0);
node_tree.dispatch_touch(slider_id, &engine, TouchKind::Release, Vector2::create(500.0, 60.0));
assert!(!node_tree[slider_id].get_component::<UISlider>().expect("slider").is_dragging());
```

- `pub fn new() -> UISlider` (constructor, since 0.3.2): Constructor. (value 0, range 0 ~ 1, gray track, blue fill, white circle thumb radius 16)
  - To attach to a tree, use node_tree.add_component(node_id, UISlider::new()).
  - returns UISlider: New horizontal slider. (track thickness 0, rounded corners 0, pressable)
- `pub fn get_uicontrol(&self) -> &UIControl` (method, since 0.3.2): Returns the embedded control.
  - returns &UIControl: The embedded UIControl.
- `pub fn get_uicontrol_mut(&mut self) -> &mut UIControl` (method, since 0.3.2): Returns the embedded control. (mutable)
  - returns &mut UIControl: The embedded UIControl.
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle.
  - returns Option<NodeId>: Content node created under the host node. None before being attached to a tree.
- `pub fn get_ratio(&self) -> f64` (method, since 0.3.2): Returns the 0 ~ 1 ratio.
  - returns f64: Ratio of the value within the range. (0 ~ 1, 0 if the maximum is not greater than the minimum)
- `pub fn set_value(&mut self, value: f64)` (method, since 0.3.2): Sets the value. (clamped to the range. does not fire the event)
  - `value` f64: New value. (the minimum if less than the minimum, the maximum if greater than the maximum)
- `pub fn get_value(&self) -> f64` (method, since 0.3.2): Returns the value.
  - returns f64: Current value. (minimum ~ maximum)
- `pub fn set_range(&mut self, min_value: f64, max_value: f64)` (method, since 0.3.2): Sets the range. (reclamps the current value to the new range)
  - `min_value` f64: Minimum value.
  - `max_value` f64: Maximum value. (must be greater than the minimum, the ratio is always 0 if equal or less)

  Example: Changing the range reclamps the value too

  ```rust
  use vanilla::UISlider;
  
  let mut slider = UISlider::new();
  slider.set_range(10.0, 20.0);
  assert_eq!(slider.get_value(), 10.0);
  slider.set_value(17.5);
  assert_eq!(slider.get_ratio(), 0.75);
  slider.set_value(30.0);
  assert_eq!(slider.get_value(), 20.0);
  slider.set_range(0.0, 5.0);
  assert_eq!(slider.get_value(), 5.0);
  ```

- `pub fn get_min_value(&self) -> f64` (method, since 0.3.2): Returns the minimum value.
  - returns f64: Minimum value. (default 0)
- `pub fn get_max_value(&self) -> f64` (method, since 0.3.2): Returns the maximum value.
  - returns f64: Maximum value. (default 1)
- `pub fn fire_value_changed(&mut self)` (method, since 0.3.2): Forces the value changed event to fire. (after set_value from scene code. fired on the next tick)

  Example: Change the value in code and notify with the event

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, UISlider};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let slider_id = node_tree.create_child(root_id, "Slider");
  let changed_count = Rc::new(Cell::new(0));
  let changed_count_for_event = changed_count.clone();
  let slider = node_tree.add_component(slider_id, UISlider::new()).expect("slider");
  slider.set_value_changed_event(Box::new(move |_slider: &mut UISlider, _context: &mut ComponentContext| {
  	changed_count_for_event.set(changed_count_for_event.get() + 1);
  }));
  slider.set_value(0.5);
  slider.fire_value_changed();
  assert_eq!(changed_count.get(), 0);
  
  node_tree.tick(&engine, 0.016);
  assert_eq!(changed_count.get(), 1);
  ```

- `pub fn set_track_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the track color.
  - `color` impl Into<Color>: Track color. (default gray 0.7)
- `pub fn get_track_color(&self) -> Color` (method, since 0.3.2): Returns the track color.
  - returns Color: Track color.
- `pub fn set_fill_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the fill color.
  - `color` impl Into<Color>: Color that fills from the minimum end up to the current value. (default blue)
- `pub fn get_fill_color(&self) -> Color` (method, since 0.3.2): Returns the fill color.
  - returns Color: Fill color.
- `pub fn set_thumb_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the thumb color.
  - `color` impl Into<Color>: Color of the thumb circle. (default white, the circle is not drawn when a thumb node is set)
- `pub fn get_thumb_color(&self) -> Color` (method, since 0.3.2): Returns the thumb color.
  - returns Color: Thumb color.
- `pub fn set_thumb_radius(&mut self, thumb_radius: f64)` (method, since 0.3.2): Sets the thumb radius.
  - `thumb_radius` f64: Radius of the thumb circle. (same units as the host content_size, default 16)
- `pub fn get_thumb_radius(&self) -> f64` (method, since 0.3.2): Returns the thumb radius.
  - returns f64: Radius of the thumb circle.
- `pub fn set_track_thickness(&mut self, track_thickness: f64)` (method, since 0.3.2): Sets the track thickness. (0 for the whole host area, otherwise that thickness in the middle)
  - `track_thickness` f64: Track thickness. (same units as the host content_size, default 0)
- `pub fn get_track_thickness(&self) -> f64` (method, since 0.3.2): Returns the track thickness.
  - returns f64: Track thickness. (0 for the whole host area)
- `pub fn set_thumb_node(&mut self, thumb_node_id: Option<NodeId>)` (method, since 0.3.2): Sets the thumb node. (a child of the host. when set, moves this node to the thumb position instead of the circle)
  - In tick, places the thumb node's local position at (thumb center - half the node size). (with a top-left pivot, its center lands on the thumb position)
  - `thumb_node_id` Option<NodeId>: Node to use as the thumb. (None draws the circle)

  Example: Use a drawn node as the thumb

  ```rust
  use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, UISlider, Vector2};
  
  let engine = Engine::new(EngineConfiguration::new());
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let slider_id = node_tree.create_child(root_id, "Slider");
  node_tree[slider_id].set_pivot(Pivot::TOP_LEFT);
  node_tree[slider_id].set_content_size(Vector2::create(200.0, 20.0));
  node_tree.add_component(slider_id, UISlider::new());
  let thumb_id = node_tree.create_child(slider_id, "Thumb");
  node_tree[thumb_id].set_pivot(Pivot::TOP_LEFT);
  node_tree[thumb_id].set_content_size(Vector2::create(10.0, 10.0));
  let slider = node_tree[slider_id].get_component_mut::<UISlider>().expect("slider");
  slider.set_thumb_node(Some(thumb_id));
  slider.set_value(0.5);
  
  node_tree.tick(&engine, 0.016);
  assert_eq!(node_tree[thumb_id].get_local_position(), Vector2::create(95.0, 5.0));
  ```

- `pub fn get_thumb_node(&self) -> Option<NodeId>` (method, since 0.3.2): Returns the thumb node.
  - returns Option<NodeId>: Thumb node. None if not set.
- `pub fn set_corner_radius(&mut self, corner_radius: f64)` (method, since 0.3.2): Sets the rounded corner radius.
  - `corner_radius` f64: Rounded corner radius of the track and fill rectangles. (0 for square corners, default 0)
- `pub fn get_corner_radius(&self) -> f64` (method, since 0.3.2): Returns the rounded corner radius.
  - returns f64: Rounded corner radius.
- `pub fn set_direction(&mut self, direction: ProgressDirection)` (method, since 0.3.2): Sets the direction.
  - `direction` ProgressDirection: Direction. (Horizontal has the minimum on the left, Vertical has the minimum at the bottom, default Horizontal)
- `pub fn get_direction(&self) -> ProgressDirection` (method, since 0.3.2): Returns the direction.
  - returns ProgressDirection: Current direction.
- `pub fn set_value_changed_event(&mut self, value_changed_event: UISliderEvent)` (method, since 0.3.2): Sets the value changed event.
  - `value_changed_event` UISliderEvent: Function called when the value changes by pressing or dragging. (takes the slider and the context, not called for the same value nor when changed with set_value)
- `pub fn get_value_changed_event(&self) -> Option<Rc<RefCell<UISliderEvent>>>` (method, since 0.4.0): Returns the value changed event. (borrow it with borrow_mut to call it)
  - returns Option<Rc<RefCell<UISliderEvent>>>: The set value changed event. None if not set.
- `pub fn set_interactable(&mut self, is_interactable: bool)` (method, since 0.3.2): Sets whether it is enabled. (when off, presses are not taken)
  - `is_interactable` bool: true to take presses. (default true, even when turned off, a finger already dragging is followed to the end)
- `pub fn get_interactable(&self) -> bool` (method, since 0.3.2): Whether it is enabled.
  - returns bool: true if it takes presses.
- `pub fn is_dragging(&self) -> bool` (method, since 0.3.2): Whether it is being dragged.
  - returns bool: true from press until release or cancel.
