# ui/uiprogressring (vanilla-engine 0.4.0)

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

Progress ring. (UIProgressRing of vanilla.js ui/uiprogressring.js)
- A thin line that runs along the node's shape (a rounded rectangle, a circle when the rounding is half the size).
- From the top center clockwise, the filled fraction is drawn in the fill color and the rest in the background color.
- The whole content size of the attached node is the ring's outer shape, and the center of the line sits inside it by half the thickness.
- The perimeter points are recalculated only when the size, rounding, or thickness changes.

## UIProgressRing (struct) (Default, Component)

Progress ring.
- Uses the attached node's content size as the outer shape and draws the fraction filled clockwise from the top center.

Example: A 48 x 48 circular ring

```rust
use vanilla::{NodeTree, UIProgressRing, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let ring_id = node_tree.create_child(root_id, "LoadingRing");
node_tree[ring_id].set_content_size(Vector2::create(48.0, 48.0));
let ring = node_tree.add_component(ring_id, UIProgressRing::new()).expect("ring");
ring.set_round_size(24.0);
ring.set_line_width(4.0);
ring.set_ratio(0.25);
assert_eq!(ring.get_ratio(), 0.25);
ring.set_ratio(1.5);
assert_eq!(ring.get_ratio(), 1.0);
```

- `pub fn new() -> UIProgressRing` (constructor, since 0.4.0): Constructor.
  - returns UIProgressRing: Ring before being attached. (fraction 0, rounding 0, thickness 2, white fill color)
- `pub fn draw_ring(&mut self, transform: &NodeTransform, graphic: &mut Graphic)` (method, since 0.4.0): Draw. (one full background ring, with the filled fraction on top)
  - `transform` &NodeTransform: Node transform. (the content size is the ring's outer shape, not drawn if the width or height minus the thickness is 1 or less)
  - `graphic` &mut Graphic: Graphic to draw with.
- `pub fn read_perimeter_points(left: f64, top: f64, width: f64, height: f64, round_size: f64) -> Vec<Vector2>` (static, since 0.4.0): Points on the rounded rectangle perimeter. (starting from the top center, clockwise, without the closing point)
  - `left` f64: Left of the rectangle.
  - `top` f64: Top of the rectangle.
  - `width` f64: Width of the rectangle.
  - `height` f64: Height of the rectangle.
  - `round_size` f64: Corner radius. (clamped to half the width and height if it exceeds that)
  - returns Vec<Vector2>: 45 perimeter points. (1 at the top center, 11 per corner)

  Example: The first point is the top center

  ```rust
  use vanilla::{UIProgressRing, Vector2};
  
  let points = UIProgressRing::read_perimeter_points(0.0, 0.0, 100.0, 60.0, 0.0);
  assert_eq!(points.len(), 45);
  assert_eq!(points[0], Vector2::create(50.0, 0.0));
  ```

- `pub fn read_partial_points(points: &[Vector2], ratio: f64) -> Vec<Vector2>` (static, since 0.4.0): Points for the leading fraction of the perimeter. (computed by length; the end point is cut on the last edge and added)
  - `points` &[Vector2]: Perimeter points. (without a closing point, the result of read_perimeter_points)
  - `ratio` f64: Fraction to draw from the start. (ratio of the closed perimeter length, 0 ~ 1)
  - returns Vec<Vector2>: Points connected from the first point up to that fraction. (an empty list if points is empty, returns to the first point and closes if 1 or more)

  Example: Half a turn ends at the bottom center

  ```rust
  use vanilla::UIProgressRing;
  
  let points = UIProgressRing::read_perimeter_points(0.0, 0.0, 100.0, 100.0, 0.0);
  let half_points = UIProgressRing::read_partial_points(&points, 0.5);
  let last_point = half_points[half_points.len() - 1];
  assert!((last_point.x - 50.0).abs() < 0.01);
  assert!((last_point.y - 100.0).abs() < 0.01);
  ```

- `pub fn set_ratio(&mut self, ratio: f64)` (method, since 0.4.0): Sets the filled fraction. (clamped to 0 ~ 1)
  - `ratio` f64: Filled fraction. (0 ~ 1, clamped if exceeded)
- `pub fn get_ratio(&self) -> f64` (method, since 0.4.0): Returns the filled fraction.
  - returns f64: Filled fraction. (0 ~ 1, default 0)
- `pub fn set_round_size(&mut self, round_size: f64)` (method, since 0.4.0): Sets the corner roundness. (a circle if half the size or more)
  - `round_size` f64: Corner radius of the outer shape. (the center of the line runs at the radius minus half the thickness)
- `pub fn get_round_size(&self) -> f64` (method, since 0.4.0): Returns the corner roundness.
  - returns f64: Corner radius of the outer shape. (default 0)
- `pub fn set_line_width(&mut self, line_width: f64)` (method, since 0.4.0): Sets the line thickness.
  - `line_width` f64: Line thickness. (default 2)
- `pub fn get_line_width(&self) -> f64` (method, since 0.4.0): Returns the line thickness.
  - returns f64: Line thickness. (default 2)
- `pub fn set_fill_color(&mut self, color: impl Into<Color>)` (method, since 0.4.0): Sets the color of the filled fraction.
  - `color` impl Into<Color>: Color of the filled fraction. (default white)
- `pub fn get_fill_color(&self) -> Color` (method, since 0.4.0): Returns the color of the filled fraction.
  - returns Color: Color of the filled share.
- `pub fn set_track_color(&mut self, color: impl Into<Color>)` (method, since 0.4.0): Sets the color of the rest.
  - `color` impl Into<Color>: Color of the background ring. (default `Color::create(0.2, 0.2, 0.25, 1.0)`)
- `pub fn get_track_color(&self) -> Color` (method, since 0.4.0): Returns the color of the rest.
  - returns Color: Color of the background ring.
- `pub fn get_perimeter_points(&self) -> &[Vector2]` (method, since 0.4.0): The last calculated perimeter points. (valid after drawing, original getPerimeterPoints)
  - returns &[Vector2]: Perimeter points the center of the line passes through. (an empty list if never drawn)
