# misc/visual (vanilla-engine 0.4.0)

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

Visual component. (Visual of the original misc/visual.js, 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 (vanilla.js tools/visualeditor). ({ version, root, frameSize? }, both scenes and prefabs)
  VisualAsset reads the file and makes a Visual holding the document.
- create_node builds a new node tree from the 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.
- Difference from the original: the original createNode() builds with the global UIDocument, the Rust create_node takes the UIDocument to build with and the node tree.
  (A Rust UIDocument is a value holding the registration tables) Where the original returns null for no document and throws for a malformed one, Rust gives Ok(None) and Err.

## Visual (struct) (Default, Component)

Visual component.
- Holds visual editor document text and a location. create_node builds a new node tree from that document. It does not draw or update. (is_tick_needed is false)

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

```rust
use vanilla::{Component, NodeTree, UIDocument, Vector2, Visual};

let document_text = r#"{"version":1,"root":{"type":"WorldNode","name":"Badge","position":[300,200],"contentSize":[64,64],"components":[],"children":[]}}"#;
let mut visual = Visual::new();
visual.set_document_text(Some(document_text.to_string()));
visual.set_location(Vector2::create(40.0, 20.0));

let mut document = UIDocument::new();
let mut node_tree = NodeTree::new();
let parent_id = node_tree.create_node("parent");
let badge_id = visual.create_node(&mut document, &mut node_tree).expect("document").expect("root");
assert!(node_tree.add_child(parent_id, badge_id));
assert_eq!(node_tree[badge_id].get_name(), "Badge");
assert_eq!(node_tree[badge_id].get_local_position(), Vector2::create(40.0, 20.0));
assert_eq!(node_tree.get_parent_id(badge_id), Some(parent_id));
assert_eq!(visual.get_component_type(), "Visual");
```

- `pub fn new() -> Visual` (constructor, since 0.3.2): Constructor. (position 0)
  - returns Visual: A visual with no document text at location (0, 0).
- `pub fn set_location(&mut self, location: Vector2)` (method, since 0.3.2): Sets the position.
  - `location` Vector2: Local position given to the root node built by create_node. (Relative to the parent, default 0)
- `pub fn get_location(&self) -> Vector2` (method, since 0.3.2): Returns the position.
  - returns Vector2: Local position given to the built root node.
- `pub fn set_document_text(&mut self, document_text: Option<String>)` (method, since 0.4.0): Set the document text.
  - `document_text` Option<String>: UIDocument JSON text saved by the visual editor. (None clears it, it is read at create_node)
- `pub fn get_document_text(&self) -> Option<&str>` (method, since 0.4.0): Get the document text.
  - returns Option<&str>: Held document text. None when there is none.
- `pub fn create_node(&self, document: &mut UIDocument, node_tree: &mut NodeTree) -> Result<Option<NodeId>, String>` (method, since 0.4.0): Build a node tree from the document.
  - `document` &mut UIDocument: UIDocument that builds the nodes and components. (Uses the kinds and image resolver the game registered)
  - `node_tree` &mut NodeTree: Node tree to build in.
  - returns Result<Option<NodeId>, String>: Newly built root node. (Not attached to any parent, its local position is location) Ok(None) when there is no document, Err when the document is malformed.

  Example: No document, a malformed document, and two copies from the same document

  ```rust
  use vanilla::{NodeTree, UIDocument, Vector2, Visual};
  
  let mut document = UIDocument::new();
  let mut node_tree = NodeTree::new();
  let mut visual = Visual::new();
  assert_eq!(visual.create_node(&mut document, &mut node_tree), Ok(None));
  visual.set_document_text(Some(String::from(r#"{"version":1}"#)));
  assert!(visual.create_node(&mut document, &mut node_tree).is_err());
  
  visual.set_document_text(Some(String::from(r#"{"version":1,"root":{"type":"WorldNode","name":"Coin","contentSize":[32,32],"components":[],"children":[]}}"#)));
  let first_id = visual.create_node(&mut document, &mut node_tree).expect("document").expect("root");
  visual.set_location(Vector2::create(48.0, 0.0));
  let second_id = visual.create_node(&mut document, &mut node_tree).expect("document").expect("root");
  assert_ne!(first_id, second_id);
  assert_eq!(node_tree[second_id].get_name(), "Coin");
  assert_eq!(node_tree[first_id].get_local_position().x, 0.0);
  assert_eq!(node_tree[second_id].get_local_position().x, 48.0);
  assert!(node_tree.get_parent_id(second_id).is_none());
  ```

