# ui/uiview (vanilla.js 0.4.0)

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

View component.

## UIView extends Component

View component.
- Base class of all view components such as ScrollView, SnapScrollView.
- Lower view components can be queried at once by type.
  e.g.: node.getComponent(View)
- Since it acts as a container, it inherits Component directly instead of UIControl.
  Instead, it has its own setEngine/getEngine that UIControl had.

Example: Put a label on a panel with a background color

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

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

	initialize(engine) {
		super.initialize(engine);
		const panelNode = new WorldNode();
		panelNode.setPivot(Pivot.topLeft);
		panelNode.setAnchor(Pivot.topLeft);
		panelNode.setLocalPosition(Vector2.create(40, 40));
		panelNode.setContentSize(Vector2.create(240, 160));
		const view = panelNode.addComponent(UIView);
		view.setBackgroundColor(new Color(0.1, 0.12, 0.16, 1));

		const labelNode = new WorldNode();
		labelNode.setPivot(Pivot.topLeft);
		labelNode.setAnchor(Pivot.topLeft);
		labelNode.setLocalPosition(Vector2.create(16, 16));
		labelNode.setContentSize(Vector2.create(208, 32));
		const label = labelNode.addComponent(UILabel);
		label.setText("Panel");
		label.setFontSize(20);
		const content = view.getContent();
		content.addChild(labelNode);

		const root = this.getRoot();
		root.addChild(panelNode);
	}
}

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.0): Constructor.
- `setEngine(engine)` (method, since 0.2.1): Sets the engine. (moved from the former UIComponent)
  - `engine` Engine: Engine.
- `getEngine()` (method, since 0.2.1): Returns the engine. (moved from the former UIComponent)
  - returns Engine | null: The engine given with setEngine. (null if not given)
- `attach(node)` (method, since 0.2.0): Attached to a node.
  - `node` ComponentNode: Node to attach to.
- `detach(node)` (method, since 0.2.0): Detached from a node.
  - `node` ComponentNode: Node being detached from.
- `isDrawnInsideContent()` (method, since 0.4.0): Returns whether it draws only inside the content area. (the background fills only the content-size rectangle; used by off-screen and outside-clip culling)
  - Subclasses that override draw return false because where they draw is unknown. (such a class overrides this function too to report it)
  - returns boolean: True if drawing only inside the content area.
- `writeGeometryKey(geometryKey)` (method, since 0.4.0): Writes the draw inputs to the geometry cache key. (background color, content size)
  - A subclass that overrides draw returns false because what it reads is unknown. (that class overrides this function too to report them)
  - `geometryKey` GeometryKey: Key to write the inputs to.
  - returns boolean: True if the inputs were written.
- `draw(graphic)` (method, since 0.2.0): Draw. (draws the background in the scroll view area if a background color is set)
  - `graphic` Graphic: Graphics.
- `setBackdropBlur(blurRadius)` (method, since 0.4.0): Sets the backdrop blur. (same meaning as CSS backdrop-filter: blur() and Unreal UMG BackgroundBlur)
  - 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)
  - `blurRadius` number: Blur standard deviation. (the N of CSS blur(Npx), in node coordinate units, 0 or less turns it off, initially 0)

  Example: Blurring behind a translucent panel

  ```js
  import { UIView, Color } from "./build/vanilla.js";
  
  const view = new UIView();
  view.setBackgroundColor(new Color(1, 1, 1, 0.25));
  view.setBackdropBlur(6);
  console.log(view.getBackdropBlur());
  ```

- `setBackdropCornerRadius(cornerRadius)` (method, since 0.4.0): Sets the rounded corners of the backdrop blur and background color. (same meaning as CSS border-radius)
  - 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)
  - `cornerRadius` number: Corner radius. (in node coordinate units, 0 or less makes a rectangle, initially 0)

  Example: Blurring behind a rounded button shape

  ```js
  import { UIView, Color } from "./build/vanilla.js";
  
  const view = new UIView();
  view.setBackgroundColor(new Color(1, 1, 1, 0.25));
  view.setBackdropBlur(6);
  view.setBackdropCornerRadius(12);
  console.log(view.getBackdropCornerRadius());
  ```

- `getBackdropCornerRadius()` (method, since 0.4.0): Returns the rounded corner radius of the backdrop blur.
  - returns number: Corner radius. (in node coordinate units, 0 makes a rectangle)
- `getBackdropBlur()` (method, since 0.4.0): Returns the backdrop blur radius.
  - returns number: Blur standard deviation. (the N of CSS blur(Npx), 0 means off)
- `touchPress(viewInputPosition)` (method, since 0.2.0): Touch press. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: Press position. (view coordinates)
- `touchMove(viewInputPosition)` (method, since 0.2.0): Touch move. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: Moved position. (view coordinates)
- `touchRelease(viewInputPosition)` (method, since 0.2.0): Touch release. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: Release position. (view coordinates)
- `touchCancel(viewInputPosition)` (method, since 0.2.0): Touch cancel. (TouchRaycaster → WorldNode)
  - `viewInputPosition` Vector2: Position at cancel. (view coordinates)
- `getContent()` (method, since 0.2.0): Returns the content node. (child nodes added to this node become scroll targets)
  - returns WorldNode: Content node. (null before being attached to a node)
- `setBackgroundColor(backgroundColor)` (method, since 0.2.0): Sets the background color. (null clears the background)
  - `backgroundColor` Color | null: Background color. (A copy is kept, nothing is drawn when alpha is 0, null or undefined resets it to the initial transparent white so nothing is drawn)

  Example: Paint a background and clear it

  ```js
  import { UIView, Color } from "./build/vanilla.js";
  
  const view = new UIView();
  view.setBackgroundColor(new Color(0.1, 0.12, 0.16, 1));
  const paintedColor = view.getBackgroundColor();
  view.setBackgroundColor(null);
  const clearedColor = view.getBackgroundColor();
  console.log(paintedColor.alpha, clearedColor.alpha);
  ```

- `getBackgroundColor()` (method, since 0.2.0): Returns the background color.
  - returns Color: Background color. (default white with alpha 0)

## Functions and constants

- `isViewPositionInsideNode(node, viewPosition)` (method, since 0.4.0): Whether a point in view coordinates is inside the node's content area. (Tested with the four corners after all parent transforms, Rust edition ui::uiview::is_view_position_inside_node)
  - `node` WorldNode | null: Node to test. (false if none)
  - `viewPosition` Vector2: A point in view coordinates. (pixels)
  - returns boolean: True if inside the node's contentSize area.

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

  ```js
  import { isViewPositionInsideNode, Vector2, WorldNode } from "./build/vanilla.js";
  
  const rootNode = new WorldNode();
  const buttonNode = new WorldNode();
  rootNode.addChild(buttonNode);
  buttonNode.setLocalPosition(Vector2.create(100, 100));
  buttonNode.setContentSize(Vector2.create(40, 20));
  console.log(isViewPositionInsideNode(buttonNode, Vector2.create(100, 100)));
  console.log(isViewPositionInsideNode(buttonNode, Vector2.create(81, 91)));
  console.log(isViewPositionInsideNode(buttonNode, Vector2.create(79, 100)));
  ```

