# ui/uiview (vanilla-engine 0.4.0)

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

View component. (UIView of the original ui/uiview.js)
- The base of all UI widgets. When attached to a node (host), creates a "content" child node under the host. (top-left pivot, anchor 0,0, position 0,0)
- If the background color's alpha is greater than 0, paints the host's content_size area with that color.
- In the original, widgets like UILabel and UIButton inherit UIView. In the Rust version, widgets embed UIView (ui_view field) and pass through content and the background color.
  So the original usage of finding subclass widgets at once with node.get_component(UIView) does not exist. (look them up by concrete type, like `get_component::<UIButton>()`)
- The content node is created in attach_to_tree. It must be attached with node_tree.add_component(node_id, UIView::new()).
- The original setEngine / get_engine were not ported. The engine is obtained with ComponentContext::get_engine during tick / touch.
- The original setBackgroundColor(null) clears the background. (returns to the initial value, white with alpha 0) The Rust version has no None, so give a color with alpha 0 (the initial value or Color::transparent()).

## UIView (struct) (Default, Component)

View component.
- When attached to a node, creates a content child node under the host and paints the host's content_size area with the background color.

Example: A translucent background panel and its content node

```rust
use vanilla::{Color, NodeTree, UIView, Vector2};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let panel_id = node_tree.create_child(root_id, "panel");
node_tree[panel_id].set_content_size(Vector2::create(200.0, 100.0));
let panel_view = node_tree.add_component(panel_id, UIView::new()).unwrap();
panel_view.set_background_color(Color::create(0.1, 0.1, 0.2, 0.8));
let content_id = panel_view.get_content().unwrap();
assert_eq!(node_tree.get_parent_id(content_id), Some(panel_id));
assert_eq!(node_tree[content_id].get_name(), "content");
```

- `pub fn new() -> UIView` (constructor, since 0.3.2): Constructor. (the background is transparent white)
  - returns UIView: New view. (the content node is created when attached to a tree)
- `pub fn create_content_node(&mut self, node_tree: &mut NodeTree, node_id: NodeId)` (method, since 0.3.2): Creates the content node. (a child of the host, aligned to the top left. left as is if it already exists)
  - `node_tree` &mut NodeTree: Tree that holds the host.
  - `node_id` NodeId: Host node. (the content node becomes a child of this node)
- `pub fn set_backdrop_blur(&mut self, blur_radius: f64)` (method, since 0.4.0): Sets the backdrop blur. (same meaning as CSS backdrop-filter: blur() and Unreal UMG BackgroundBlur, original setBackdropBlur)
  - When on, as this view is drawn it blurs what was drawn before it, only inside this view's content area, then lays the (translucent) background color on top.
  - The argument is the standard deviation, the same as the N of CSS backdrop-filter: blur(Npx). (in node coordinate units, on screen multiplied by the transform scale into device pixels)
  - The blur is ScreenEffect's GaussianBlur (separable Gaussian, linear sampling, up to 3 sigma, edges folded as a mirror). It matches Chrome's backdrop-filter.
  - It works only when drawing to the screen. (it does not blur while drawing into a render target) While on, the geometry cache is not used. (the screen is read on every draw)
  - `blur_radius` f64: Blur standard deviation. (the N of CSS blur(Npx), in node coordinate units, 0 or less turns it off, default 0)

  Example: Blurring behind a translucent panel

  ```rust
  use vanilla::{Color, UIView};
  
  let mut view = UIView::new();
  view.set_background_color(Color::create(1.0, 1.0, 1.0, 0.25));
  view.set_backdrop_blur(6.0);
  assert_eq!(view.get_backdrop_blur(), 6.0);
  ```

- `pub fn set_backdrop_corner_radius(&mut self, corner_radius: f64)` (method, since 0.4.0): Sets the rounded corners of the backdrop blur and background color. (same meaning as CSS border-radius, original setBackdropCornerRadius)
  - While the blur is on, the blur area and the background color are laid only inside a rounded rectangle of this radius. (outside the corners the screen behind stays as it is)
  - `corner_radius` f64: Corner radius. (in node coordinate units, 0 or less makes a rectangle, default 0)

  Example: Blurring behind a rounded button shape

  ```rust
  use vanilla::{Color, UIView};
  
  let mut view = UIView::new();
  view.set_background_color(Color::create(1.0, 1.0, 1.0, 0.25));
  view.set_backdrop_blur(6.0);
  view.set_backdrop_corner_radius(12.0);
  assert_eq!(view.get_backdrop_corner_radius(), 12.0);
  ```

- `pub fn get_backdrop_corner_radius(&self) -> f64` (method, since 0.4.0): Returns the rounded corner radius of the backdrop blur.
  - returns f64: Corner radius. (in node coordinate units, 0 makes a rectangle)
- `pub fn get_backdrop_blur(&self) -> f64` (method, since 0.4.0): Returns the backdrop blur radius.
  - returns f64: Blur standard deviation. (the N of CSS blur(Npx), 0 means off)
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle. (child nodes attached here become scroll targets. None before being attached to a tree)
  - returns Option<NodeId>: content node. None before being attached to a tree.
- `pub fn set_background_color(&mut self, background_color: impl Into<Color>)` (method, since 0.3.2): Sets the background color. (not drawn if alpha is 0. To clear the background like the original null, give a color with alpha 0)
  - `background_color` impl Into<Color>: Background color. (a Color or text like "#rrggbb", not painted if alpha is 0)
- `pub fn get_background_color(&self) -> Color` (method, since 0.3.2): Returns the background color.
  - returns Color: Background color. (default is white with alpha 0)
- `pub fn get_engine(&self) -> Result<&'static crate::core::engine::Engine, crate::core::globalengine::EngineAccessError>` (method, since 0.4.0): Returns the engine. (the original getEngine, in place of the engine given with the original setEngine. The Rust version has one engine, so it gives the global engine)
  - The same as Engine::get_engine. It is taken anywhere right after the engine is created.
  - returns Result<&'static crate::core::engine::Engine, crate::core::globalengine::EngineAccessError>: The global engine, or the reason it could not be taken.
- `pub fn draw_background(&self, transform: &NodeTransform, graphic: &mut Graphic)` (method, since 0.3.2): Draws the background. (a transparent background is not drawn, so invisible draws do not break batching)
  - `transform` &NodeTransform: Transform of the host node. (paints the content_size area (0, 0) ~ (width, height))
  - `graphic` &mut Graphic: Graphic to draw with.

## Functions and constants

- `pub fn is_view_position_inside_node(node_tree: &NodeTree, node_id: NodeId, view_position: Vector2) -> bool` (method, since 0.3.2): Whether a point in view coordinates is inside the node's content_size area. (original WorldNode.contains)
  - `node_tree` &NodeTree: Tree holding the node.
  - `node_id` NodeId: Node to look at.
  - `view_position` Vector2: Point in view coordinates. (pixels)
  - returns bool: true if inside the node's content_size area as seen through the world matrix accumulated down to the node. false if there is no node.

  Example: Inside and outside of a 40 x 20 node with a center pivot (default)

  ```rust
  use vanilla::ui::uiview::is_view_position_inside_node;
  use vanilla::{NodeTree, Vector2};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let button_id = node_tree.create_child(root_id, "button");
  node_tree[button_id].set_local_position(Vector2::create(100.0, 100.0));
  node_tree[button_id].set_content_size(Vector2::create(40.0, 20.0));
  assert!(is_view_position_inside_node(&node_tree, button_id, Vector2::create(100.0, 100.0)));
  assert!(is_view_position_inside_node(&node_tree, button_id, Vector2::create(81.0, 91.0)));
  assert!(!is_view_position_inside_node(&node_tree, button_id, Vector2::create(79.0, 100.0)));
  ```

