# misc/visual (vanilla.js 0.4.0)

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

Visual object. (A component that builds a visual editor document into a node tree at a set location)

## Visual extends Component

Visual object. (A component that builds a visual editor document into a node tree at a set location)
- The document is the UIDocument JSON text saved by the visual editor (tools/visualeditor). ({ version, root, frameSize? }, both scenes and prefabs)
  VisualAsset reads the file and makes a Visual holding the document.
- createNode(uiDocument) builds a new node tree with the given document and sets the root's local position to location. (The caller attaches it to the parent it wants, a new tree on every call)
- It does not draw or update by itself.

Example: Build a node tree from a visual holding a document

```js
import { Visual, UIDocument, WorldNode, Vector2 } from "./build/vanilla.js";

const documentData = {
	version: 1,
	root: { type: "WorldNode", name: "Badge", position: [300, 200], contentSize: [64, 64], components: [], children: [] },
};
const visual = new Visual();
visual.setDocumentText(JSON.stringify(documentData));
visual.setLocation(Vector2.create(40, 20));
const parentNode = new WorldNode();
const badgeNode = visual.createNode(new UIDocument());
parentNode.addChild(badgeNode);
const localPosition = badgeNode.getLocalPosition();
console.log(badgeNode.getName(), localPosition.x, localPosition.y, visual.getComponentType());
```

- `constructor()` (constructor, since 0.0.9): Constructor.
- `setLocation(location)` (method, since 0.0.9): Sets the position.
  - `location` Vector2: Local position given to the root node built by createNode. (Relative to the parent, default 0)

  Example: Set and read the location

  ```js
  import { Visual, Vector2 } from "./build/vanilla.js";
  
  const visual = new Visual();
  visual.setLocation(Vector2.create(120, 80));
  const location = visual.getLocation();
  console.log(location.x, location.y);
  ```

- `getLocation()` (method, since 0.0.9): Returns the position.
  - returns Vector2: Local position given to the built root node. (Not a copy)
- `setDocumentText(documentText)` (method, since 0.4.0): Set the document text.
  - `documentText` string | null: UIDocument JSON text saved by the visual editor. (null clears it, it is read at createNode)
- `getDocumentText()` (method, since 0.4.0): Get the document text.
  - returns string | null: Held document text. (null when there is none)
- `createNode(uiDocument)` (method, since 0.4.0): Builds a node tree from the document. (builds with the given UIDocument, using the types and image resolver the game registered)
  - `uiDocument` UIDocument: The document that builds the nodes and components.
  - returns WorldNode | null: Newly built root node. (Not attached to any parent, its local position is location. null when there is no document, throws when the document is malformed)

  Example: Build two copies from the same document

  ```js
  import { Visual, UIDocument, Vector2 } from "./build/vanilla.js";
  
  const uiDocument = new UIDocument();
  const documentData = {
  	version: 1,
  	root: { type: "WorldNode", name: "Coin", contentSize: [32, 32], components: [], children: [] },
  };
  const visual = new Visual();
  console.log(visual.createNode(uiDocument));
  visual.setDocumentText(JSON.stringify(documentData));
  const firstNode = visual.createNode(uiDocument);
  visual.setLocation(Vector2.create(48, 0));
  const secondNode = visual.createNode(uiDocument);
  const secondPosition = secondNode.getLocalPosition();
  console.log(firstNode !== secondNode, secondNode.getName(), secondPosition.x);
  ```

