# ui/uinode (vanilla.js 0.4.0)

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

A sentinel value with the same meaning as UIKit's UIView.noIntrinsicMetric.
- If x or y of getIntrinsicContentSize is this value, that axis has no intrinsic preference.

## UINode extends TransformNode

UI node.
- A UI node on a separate track from the existing WorldNode family.
- The area is defined by 8 solver variables (left / right / top / bottom / width / height
  + derived centerX / centerY), and the user places constraints on the LayoutSolver
  to determine those values.
- This class holds the variables and the essential constraints (right == left + width, bottom == top
  + height, centerX == left + width/2, centerY == top + height/2), and registers these 4
  automatically.
- User constraints are registered with addConstraint(constraint).
- The solver is attached with setSolver(solver). When operating the whole tree together, set it only on the root UINode
  and the caller manages it so that children receive the same instance through setSolver.
- intrinsicContentSize / contentHuggingPriority / contentCompressionResistancePriority
  follow the same idea as UIKit's APIs of the same name. Subclasses override getIntrinsicContentSize.
- layoutMargins / layoutMarginsGuide follow the same idea as UIKit's UIView.layoutMargins /
  layoutMarginsGuide. The guide, in the form of a child UINode, is exposed lazily.

Example: Place a row inside a panel's margins

```js
import { UINode, LayoutSolver } from "./build/vanilla.js";

const solver = new LayoutSolver();
const panelNode = new UINode();
panelNode.setSolver(solver);
const panelLeftAnchor = panelNode.leftAnchor;
const panelTopAnchor = panelNode.topAnchor;
const panelWidthAnchor = panelNode.widthAnchor;
const panelHeightAnchor = panelNode.heightAnchor;
panelNode.addConstraint(panelLeftAnchor.equalTo(20));
panelNode.addConstraint(panelTopAnchor.equalTo(20));
panelNode.addConstraint(panelWidthAnchor.equalTo(400));
panelNode.addConstraint(panelHeightAnchor.equalTo(300));
panelNode.setLayoutMargins({ top: 16, left: 24, bottom: 16, right: 24 });

const marginsGuide = panelNode.getLayoutMarginsGuide();
const rowNode = new UINode();
rowNode.setSolver(solver);
const guideLeftAnchor = marginsGuide.leftAnchor;
const guideRightAnchor = marginsGuide.rightAnchor;
const guideTopAnchor = marginsGuide.topAnchor;
const rowLeftAnchor = rowNode.leftAnchor;
const rowRightAnchor = rowNode.rightAnchor;
const rowTopAnchor = rowNode.topAnchor;
const rowHeightAnchor = rowNode.heightAnchor;
rowNode.addConstraint(rowLeftAnchor.equalTo(guideLeftAnchor));
rowNode.addConstraint(rowRightAnchor.equalTo(guideRightAnchor));
rowNode.addConstraint(rowTopAnchor.equalTo(guideTopAnchor));
rowNode.addConstraint(rowHeightAnchor.equalTo(48));

solver.updateVariables();
console.log(rowNode.getLeft(), rowNode.getTop(), rowNode.getWidth(), rowNode.getHeight());
```

- `constructor()` (constructor, since 0.0.8): Constructor.
- `pushTransform(graphic)` (method, since 0.2.1): Begins the draw state.
  - Solver variables (left / top) are treated as absolute positions in the coordinate system of the UI tree root.
    (the same idea as UIKit's anchors being based on the window coordinate system)
  - If the parent is also a UINode, the parent has already applied a (parentLeft, parentTop) transform, so
    the child only translates additionally by (left - parentLeft, top - parentTop).
  - Rotation / scale / alpha use the existing TransformNode members as is.
    (the UINode's area itself is determined by solver variables, so setLocalPosition is not used)
  - `graphic` Graphic: Graphics. (does nothing if null)
- `transformDrawBoundsToParent(rect)` (method, since 0.4.0): Moves subtree draw bounds into the parent's content coordinates. (Override: like pushTransform, moves to the solver's top-left with rotation and scale, no pivot)
  - Measured from the parent's top-left if the parent is a UINode, otherwise the solver's coordinates as is.
  - A node that overrides pushTransform or the position, size, rotation or scale getters has unknown placement and returns false.
  - When the solver's position or size changes (LayoutSolver.updateVariables), this node's bounds are recalculated.
  - `rect` Rect: The bounds to move. (In this node's content coordinates; the bounding box in the parent's content coordinates is written back)
  - returns boolean: True if where the node is placed is known.

  Example: Moving a solver-placed row's bounds into parent coordinates

  ```js
  import { UINode, LayoutSolver, Rect } from "./build/vanilla.js";
  
  const solver = new LayoutSolver();
  const rowNode = new UINode();
  rowNode.setSolver(solver);
  rowNode.addConstraint(rowNode.leftAnchor.equalTo(20));
  rowNode.addConstraint(rowNode.topAnchor.equalTo(40));
  rowNode.addConstraint(rowNode.widthAnchor.equalTo(100));
  rowNode.addConstraint(rowNode.heightAnchor.equalTo(30));
  solver.updateVariables();
  const drawBounds = Rect.create(0, 0, 100, 30);
  rowNode.transformDrawBoundsToParent(drawBounds);
  console.log(drawBounds.position.x, drawBounds.position.y);
  ```

- `getContentSize()` (method, since 0.2.1): Returns the content size. (width / height solved by the solver)
  - Same signature as WorldNode's getContentSize. Components such as Mask can use it as is.
  - returns Vector2: Width and height solved by the solver.
- `buildIntrinsicConstraints()` (method, since 0.2.1): Creates the 4 essential constraints.
  - right  == left + width
  - bottom == top + height
  - centerX == left + width/2
  - centerY == top + height/2
  - returns LayoutConstraint[]: The 4 intrinsic constraints. (returned without adding them to the solver)
- `buildIntrinsicSizeConstraints()` (method, since 0.2.1): Creates the 4 constraints based on the intrinsic content size (hug / compression resistance).
  - On each axis, if the intrinsic value is NO_INTRINSIC_METRIC (-1), that axis's constraints are not created.
  - hug:        widthAnchor.lessThanOrEqualTo(intrinsicWidth) @ huggingPriority
  - compression: widthAnchor.greaterThanOrEqualTo(intrinsicWidth) @ compressionResistancePriority
  - returns LayoutConstraint[]: List of intrinsic size constraints. (two per axis, axes without an intrinsic size are left out, returned without adding them to the solver)
- `getIntrinsicContentSize()` (method, since 0.2.1): Returns this node's intrinsic content size. Subclasses override it.
  - If x or y is NO_INTRINSIC_METRIC (-1), that axis has no intrinsic preference.
  - The default implementation takes the attached components that have getIntrinsicContentSize,
    combines their values with max and returns it. (components such as Text contribute automatically)
  - returns Vector2: Intrinsic size. (the largest component value per axis, -1 for axes without one)
- `invalidateIntrinsicContentSize()` (method, since 0.2.1): Invalidates the intrinsic content size. Same as UIKit's invalidateIntrinsicContentSize.
  - Removes the existing intrinsicSize constraints from the solver and registers them again with the new values.
  - Call when content such as text / image changes and the intrinsic size differs.
- `getContentHuggingPriority(axis)` (method, since 0.2.1): Returns the content hugging priority. (UIKit's contentHuggingPriority(for:))
  - `axis` string: Axis. (a LayoutConstraintAxis value, throws otherwise)
  - returns number: Content hugging priority of that axis. (default LayoutPriority.defaultLow)
- `setContentHuggingPriority(priority, axis)` (method, since 0.2.1): Sets the content hugging priority. (UIKit's setContentHuggingPriority(_:for:))
  - Updates the intrinsicSize constraints immediately.
  - `priority` number: Priority. (1 ~ 1000, LayoutPriority values can be used)
  - `axis` string: Axis. (a LayoutConstraintAxis value, throws otherwise)
- `getContentCompressionResistancePriority(axis)` (method, since 0.2.1): Returns the content compression resistance priority.
  (UIKit's contentCompressionResistancePriority(for:))
  - `axis` string: Axis. (a LayoutConstraintAxis value, throws otherwise)
  - returns number: Compression resistance priority of that axis. (default LayoutPriority.defaultHigh)
- `setContentCompressionResistancePriority(priority, axis)` (method, since 0.2.1): Sets the content compression resistance priority.
  (UIKit's setContentCompressionResistancePriority(_:for:))
  - Updates the intrinsicSize constraints immediately.
  - `priority` number: Priority. (1 ~ 1000, LayoutPriority values can be used)
  - `axis` string: Axis. (a LayoutConstraintAxis value, throws otherwise)
- `getLayoutMargins()` (method, since 0.2.1): Returns layoutMargins. (UIKit's UIView.layoutMargins)
  - returns { top: number, left: number, bottom: number, right: number }: A copy of the layout margins. (default 8 on every side)
- `setLayoutMargins(layoutMargins)` (method, since 0.2.1): Sets layoutMargins. (UIKit's UIView.layoutMargins setter)
  - If layoutMarginsGuide has already been created, updates the guide constraints immediately.
  - `layoutMargins` { top: number, left: number, bottom: number, right: number }: Top, left, bottom and right margins. (the values are copied)
- `getLayoutMarginsGuide()` (method, since 0.2.1): Returns layoutMarginsGuide. (UIKit's UIView.layoutMarginsGuide)
  - On the first call, lazily creates the guide UINode and registers 4 margin constraints with the solver.
  - The guide is not added to the tree so it is not rendered; only its anchors are used.
  - returns UINode: Guide UINode inset by the margins.
- `rebuildLayoutMarginsGuideConstraints()` (method, since 0.2.1): Rebuilds the layoutMarginsGuide margin constraints (4).
  - Removes the existing margin constraints from the solver, rebuilds them with the current #layoutMargins value and registers them again.
- `setSolver(solver)` (method, since 0.2.1): Sets / changes / clears the solver.
  - Removes all of this node's constraints from the existing solver, then registers them again with the new solver.
  - If layoutMarginsGuide already exists, that guide and its constraints are also moved from the old / to the new solver.
  - If solver is null, only clears everything.
  - `solver` LayoutSolver | null: Solver to attach. (if null, only detaches from the current solver)
- `getSolver()` (method, since 0.2.1): Returns the solver.
  - returns LayoutSolver | null: The attached solver. (null if none)
- `addConstraint(constraint)` (method, since 0.2.1): Add a user constraint. Registers it with the solver right away.
  - If the solver does not accept it (a constraint already added, a contradictory constraint), throws and does not keep it in the list.
  - `constraint` LayoutConstraint: Constraint to add. (throws if the solver does not accept it)

  Example: Combine a weak constraint with an upper limit

  ```js
  import { UINode, LayoutSolver, LayoutStrength } from "./build/vanilla.js";
  
  const solver = new LayoutSolver();
  const node = new UINode();
  node.setSolver(solver);
  const leftAnchor = node.leftAnchor;
  const topAnchor = node.topAnchor;
  const widthAnchor = node.widthAnchor;
  const heightAnchor = node.heightAnchor;
  node.addConstraint(leftAnchor.equalTo(0));
  node.addConstraint(topAnchor.equalTo(0));
  node.addConstraint(heightAnchor.equalTo(40));
  const preferredWidthConstraint = widthAnchor.equalTo(300).withStrength(LayoutStrength.weak);
  const maximumWidthConstraint = widthAnchor.lessThanOrEqualTo(200);
  node.addConstraint(preferredWidthConstraint);
  node.addConstraint(maximumWidthConstraint);
  solver.updateVariables();
  console.log(node.getWidth());
  
  node.removeConstraint(maximumWidthConstraint);
  solver.updateVariables();
  console.log(node.getWidth());
  ```

- `removeConstraint(constraint)` (method, since 0.2.1): Removes a user constraint. Also removed from the solver.
  - `constraint` LayoutConstraint: Constraint to remove. (does nothing if it was not added)
- `getConstraints()` (method, since 0.2.1): Returns the list of registered user constraints. (copy)
  - returns LayoutConstraint[]: A copy of the user constraint list.
- `getLeftVariable()` (method, since 0.2.1): Returns the left variable. (when a raw LayoutVariable is needed for purposes other than expressions)
  - returns LayoutVariable: Left variable.
- `getRightVariable()` (method, since 0.2.1): Returns the right variable.
  - returns LayoutVariable: Right variable.
- `getTopVariable()` (method, since 0.2.1): Returns the top variable.
  - returns LayoutVariable: Top variable.
- `getBottomVariable()` (method, since 0.2.1): Returns the bottom variable.
  - returns LayoutVariable: Bottom variable.
- `getWidthVariable()` (method, since 0.2.1): Returns the width variable.
  - returns LayoutVariable: Width variable.
- `getHeightVariable()` (method, since 0.2.1): Returns the height variable.
  - returns LayoutVariable: Height variable.
- `getCenterXVariable()` (method, since 0.2.1): Returns the horizontal center variable.
  - returns LayoutVariable: Horizontal center variable.
- `getCenterYVariable()` (method, since 0.2.1): Returns the vertical center variable.
  - returns LayoutVariable: Vertical center variable.
- `get leftAnchor` (property, since 0.2.1): Returns the left anchor expression. (same idea as UIKit's leftAnchor)
  - Called when writing user constraints, returns a new LayoutExpression instance on every call.
    add / subtract / multiply / divide are immutable, so creating a new one each time is safe.
  - returns LayoutExpression: A new expression of the left variable alone.
- `get rightAnchor` (property, since 0.2.1): Returns the right anchor expression. (same idea as UIKit's rightAnchor)
  - returns LayoutExpression: A new expression of the right variable alone.
- `get topAnchor` (property, since 0.2.1): Returns the top anchor expression. (same idea as UIKit's topAnchor)
  - returns LayoutExpression: A new expression of the top variable alone.
- `get bottomAnchor` (property, since 0.2.1): Returns the bottom anchor expression. (same idea as UIKit's bottomAnchor)
  - returns LayoutExpression: A new expression of the bottom variable alone.
- `get widthAnchor` (property, since 0.2.1): Returns the width anchor expression. (same idea as UIKit's widthAnchor)
  - returns LayoutExpression: A new expression of the width variable alone.
- `get heightAnchor` (property, since 0.2.1): Returns the height anchor expression. (same idea as UIKit's heightAnchor)
  - returns LayoutExpression: A new expression of the height variable alone.
- `get centerXAnchor` (property, since 0.2.1): Returns the horizontal center anchor expression. (same idea as UIKit's centerXAnchor)
  - returns LayoutExpression: A new expression of the horizontal center variable alone.
- `get centerYAnchor` (property, since 0.2.1): Returns the vertical center anchor expression. (same idea as UIKit's centerYAnchor)
  - returns LayoutExpression: A new expression of the vertical center variable alone.
- `get leadingAnchor` (property, since 0.2.1): Returns the leading anchor expression. (same idea as UIKit's leadingAnchor)
  - Same as leftAnchor in LTR environments. RTL is not supported, so it always returns leftAnchor.
  - returns LayoutExpression: Same expression as leftAnchor.
- `get trailingAnchor` (property, since 0.2.1): Returns the trailing anchor expression. (same idea as UIKit's trailingAnchor)
  - Same as rightAnchor in LTR environments. RTL is not supported, so it always returns rightAnchor.
  - returns LayoutExpression: Same expression as rightAnchor.
- `get firstBaselineAnchor` (property, since 0.2.1): Returns the first baseline anchor expression. (same idea as UIKit's firstBaselineAnchor)
  - For a plain UINode without text content, same as topAnchor.
  - Text nodes such as UILabel are overridden by subclasses to adjust for ascent.
  - returns LayoutExpression: Same expression as topAnchor. (subclasses may override it)
- `get lastBaselineAnchor` (property, since 0.2.1): Returns the last baseline anchor expression. (same idea as UIKit's lastBaselineAnchor)
  - For a plain UINode without text content, same as bottomAnchor.
  - Text nodes such as UILabel are overridden by subclasses to adjust for descent.
  - returns LayoutExpression: Same expression as bottomAnchor. (subclasses may override it)
- `getLeft()` (method, since 0.2.1): Returns the left position. (valid after LayoutSolver.updateVariables is called)
  - returns number: Left position solved by the solver. (coordinate system of the UI tree root)
- `getRight()` (method, since 0.2.1): Returns the right position.
  - returns number: Right position solved by the solver. (coordinate system of the UI tree root)
- `getTop()` (method, since 0.2.1): Returns the top position.
  - returns number: Top position solved by the solver. (coordinate system of the UI tree root)
- `getBottom()` (method, since 0.2.1): Returns the bottom position.
  - returns number: Bottom position solved by the solver. (coordinate system of the UI tree root)
- `getWidth()` (method, since 0.2.1): Returns the width.
  - returns number: Width solved by the solver.
- `getHeight()` (method, since 0.2.1): Returns the height.
  - returns number: Height solved by the solver.
- `getCenterX()` (method, since 0.2.1): Returns the horizontal center position.
  - returns number: Horizontal center position solved by the solver. (coordinate system of the UI tree root)
- `getCenterY()` (method, since 0.2.1): Returns the vertical center position.
  - returns number: Vertical center position solved by the solver. (coordinate system of the UI tree root)
- `setInteractable(isInteractable)` (method, since 0.1.0): Set touch interaction. (same as WorldNode)
  - `isInteractable` boolean: If true, TouchRaycaster hits this node. (turned on when a UIButton is attached)
- `isInteractable()` (method, since 0.1.0): Returns whether touch interaction is enabled.
  - returns boolean: True if interactable.
- `convertToViewPosition(localPosition)` (method, since 0.4.0): Local position on this node to view coordinates.
  - Applies the translation that pushTransform applies (the solver's left and top, minus the parent's left and top if the parent is a UINode), rotation and scale, walking up through the parents.
  - A parent that is not a UINode is treated as that node's child coordinate system. (for a WorldNode with a pivot, the top left of the content area is the origin)
  - `localPosition` Vector2: Local position on this node. (the top left of the content area is the origin)
  - returns Vector2: Position in view coordinates.
- `getWorldCorners()` (method, since 0.4.0): Return the area actually drawn on screen. (OBB, the same form as WorldNode)
  - returns Vector2[]: The four corners. (top-left, top-right, bottom-right, bottom-left, in view coordinates)
- `contains(viewPosition)` (method, since 0.4.0): Whether a position is inside the node. (measured with getWorldCorners, same as WorldNode)
  - `viewPosition` Vector2: View coordinates to test.
  - returns boolean: True if inside the four corners. (false if null)

  Example: Press a button placed by the solver

  ```js
  import { UINode, UIButton, LayoutSolver, Vector2 } from "./build/vanilla.js";
  
  const solver = new LayoutSolver();
  const buttonNode = new UINode();
  buttonNode.setSolver(solver);
  buttonNode.addConstraint(buttonNode.leftAnchor.equalTo(40));
  buttonNode.addConstraint(buttonNode.topAnchor.equalTo(20));
  buttonNode.addConstraint(buttonNode.widthAnchor.equalTo(120));
  buttonNode.addConstraint(buttonNode.heightAnchor.equalTo(48));
  solver.updateVariables();
  const button = buttonNode.addComponent(UIButton);
  let clickCount = 0;
  button.setClickEvent(() => {
  	++clickCount;
  	return true;
  });
  buttonNode.touchPress(Vector2.create(60, 40));
  buttonNode.touchRelease(Vector2.create(60, 40));
  console.log(buttonNode.isInteractable(), buttonNode.contains(Vector2.create(60, 40)), buttonNode.contains(Vector2.create(10, 10)), clickCount);
  ```

- `touchPress(viewInputPosition)` (method, since 0.1.0): Touch press. (called by TouchRaycaster, passed to the components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchMove(viewInputPosition)` (method, since 0.1.0): Touch move. (called by TouchRaycaster, passed to the components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchRelease(viewInputPosition)` (method, since 0.1.0): Touch release. (called by TouchRaycaster, passed to the components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchCancel(viewInputPosition)` (method, since 0.1.0): Touch cancel. (called by TouchRaycaster, passed to the components)
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `static noIntrinsicMetric` (static, since 0.2.1): Exposes the noIntrinsicMetric constant. (UIKit's UIView.noIntrinsicMetric)
