# misc/nodelayout (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/nodelayout/

Node layout.

## NodeLayout extends Object

Node layout.
- A fluent builder that declaratively composes the node hierarchy without local variables.
- A structure similar to Unreal Slate / Flutter widgets.
- Specifies all positions/sizes with number pairs (x, y) without Vector2.
- Specifies the node type with create(nodeClass). (default: WorldNode)

Example:
  const panel = NodeLayout.create(WorldNode)
      .pivot(Pivot.middleCenter)
      .contentSize(680, 900)
      .component(Paint, (c) => { c.setColor(new Color(0.1, 0.1, 0.1, 1)); })
      .children(
          NodeLayout.create(WorldNode)
              .contentSize(120, 70)
              .component(Text, (c) => { c.setText("OK"); })
      )
      .build();

Example: Declare a panel and a button at once and attach them to the root

```js
import { Engine, EngineConfiguration, Scene, NodeLayout, WorldNode, Paint, Pivot, Color } from "./build/vanilla.js";

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		const root = this.getRoot();
		const panelLayout = NodeLayout.create(WorldNode)
			.name("panel")
			.pivot(Pivot.middleCenter)
			.localPosition(320, 180)
			.contentSize(300, 160)
			.component(Paint, (paint) => {
				paint.setColor(new Color(0.1, 0.1, 0.1, 1));
			})
			.children(
				NodeLayout.create(WorldNode)
					.name("button")
					.pivot(Pivot.middleCenter)
					.localPosition(150, 110)
					.contentSize(120, 40)
					.component(Paint, (paint) => {
						paint.setColor(new Color(0.2, 0.6, 1, 1));
					})
			);
		const panelNode = panelLayout.build(root);
		console.log(panelNode.getName(), panelNode.getChildCount());
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor(nodeClass)` (constructor, since 0.2.0): Constructor.
  - `nodeClass` Function: Class of the node to create. (WorldNode if omitted)
- `static arrangeFan(nodeList, options)` (static, since 0.3.0): Fan layout. (static, spreads nodes over an arc like cards in a hand)
  - Spreads them symmetrically left and right so the middle item faces the baseAngleRadian direction (default up).
  - If rotateNodes is true, also aligns each node's rotation to the arc's tangent. (Node rotation is in degrees, so radians are converted to degrees)
  - `nodeList` WorldNode[]: List of nodes to fan out.
  - `options` object: { centerX, centerY, radius, angleStepRadian = 0.12, baseAngleRadian = -PI/2, rotateNodes = true }

  Example: Fan five hand cards out on an arc

  ```js
  import { NodeLayout, WorldNode } from "./build/vanilla.js";
  
  const cardList = [];
  for (let index = 0; index < 5; ++index) {
  	cardList.push(new WorldNode());
  }
  NodeLayout.arrangeFan(cardList, { centerX: 320, centerY: 600, radius: 400, angleStepRadian: 0.12 });
  for (const card of cardList) {
  	const position = card.getLocalPosition();
  	const rotation = card.getLocalRotation();
  	console.log(position.x.toFixed(1), position.y.toFixed(1), rotation.toFixed(1));
  }
  ```

- `static create(nodeClass)` (static, since 0.2.0): Creates a layout instance.
  - `nodeClass` Function: Class of the node to create. (WorldNode if omitted)
  - returns NodeLayout: New layout.
- `apply(callback)` (method, since 0.2.0): Direct node access. (Used to set properties not in the builder)
  - `callback` Function: Function that receives the node and modifies it directly. (node) => void
  - returns NodeLayout: This layout. (for chaining)
- `active(active)` (method, since 0.2.0): Sets active.
  - `active` boolean: If true, activates the node.
  - returns NodeLayout: This layout. (for chaining)
- `name(name)` (method, since 0.2.0): Sets the name.
  - `name` string: Node name.
  - returns NodeLayout: This layout. (for chaining)
- `component(componentType, callback)` (method, since 0.2.0): Adds and configures a component.
  - If there is no callback, only adds the component.
  - `componentType` Function: Component class to add.
  - `callback` Function: Function that receives the added component to set it up. (component) => void, only adds if omitted
  - returns NodeLayout: This layout. (for chaining)
- `children(...layouts)` (method, since 0.2.0): Adds child layouts.
  - Several child layouts can be added at once as variadic arguments.
  - `...layouts` ...NodeLayout: Layouts to attach as children. (built together on build)
  - returns NodeLayout: This layout. (for chaining)
- `localPosition(x, y)` (method, since 0.2.0): Sets the local position.
  - `x` number: Local X coordinate.
  - `y` number: Local Y coordinate. (An anchoredPosition called earlier is dropped, the later call wins)
  - returns NodeLayout: This layout. (for chaining)
- `pivot(pivot)` (method, since 0.2.0): Sets the pivot.
  - `pivot` Vector2: Pivot. (0 ~ 1, Pivot constants)
  - returns NodeLayout: This layout. (for chaining)
- `contentSize(x, y)` (method, since 0.2.0): Sets the content size.
  - `x` number: Content width. (pixels)
  - `y` number: Content height. (pixels)
  - returns NodeLayout: This layout. (for chaining)
- `localScale(x, y)` (method, since 0.2.0): Sets the local scale.
  - `x` number: Horizontal scale.
  - `y` number: Vertical scale.
  - returns NodeLayout: This layout. (for chaining)
- `parent(parentNode)` (method, since 0.2.0): Sets the parent node. (When build() is called without arguments, it is added as a child of this parent.)
  - `parentNode` *: Parent node that build attaches the node to.
  - returns NodeLayout: This layout. (for chaining)
- `anchor(x, y)` (method, since 0.2.0): Sets a single anchor. (WorldNode.setAnchor)
  - `x` number: Anchor X. (Ratio of the parent size, clamped to 0 ~ 1)
  - `y` number: Anchor Y. (Ratio of the parent size, clamped to 0 ~ 1)
  - returns NodeLayout: This layout. (for chaining)
- `anchoredPosition(x, y)` (method, since 0.2.0): Sets the position offset relative to the anchor.
  - Applied in build after the node is attached to its parent, relative to the anchor point of the parent contentSize. (While building, the node has no parent yet)
  - Uses the anchor at the time this method is called. (Set the anchor first, a later localPosition call wins)
  - `x` number: X relative to the anchor. (Pixels, used as the local X when build has no parent to attach to)
  - `y` number: Y relative to the anchor. (Pixels, used as the local Y when build has no parent to attach to)
  - returns NodeLayout: This layout. (for chaining)

  Example: Place it offset from the parent's bottom right corner

  ```js
  import { NodeLayout, WorldNode } from "./build/vanilla.js";
  
  const panelNode = NodeLayout.create(WorldNode)
  	.contentSize(680, 900)
  	.children(
  		NodeLayout.create(WorldNode)
  			.name("corner")
  			.anchor(1, 1)
  			.anchoredPosition(-10, -20)
  	)
  	.build();
  const cornerNode = panelNode.findChildByName("corner");
  const localPosition = cornerNode.getLocalPosition();
  console.log(localPosition.x, localPosition.y);
  ```

- `maskEnabled(enabled)` (method, since 0.2.0): Sets whether the mask is enabled.
  - `enabled` boolean: If true, adds a Mask component; if false, removes it.
  - returns NodeLayout: This layout. (for chaining)
- `build(parent)` (method, since 0.2.0): Build.
  - Recursively builds all child layouts.
  - If the parent argument is given, adds it as a child of that parent.
  - If there is no parent argument and a parent was set by the parent() method, adds it as a child of that parent.
  - Returns the finished node.
  - `parent` *: Parent to attach the built node to. (The parent set with parent() if omitted, not attached if neither)
  - returns *: The built node.
