# core/component (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/component/

Component.

## Component extends Object

Component.

Example: A component that spins and draws on its node

```js
import { Engine, EngineConfiguration, Scene, WorldNode, Component, Rect, Colors } from "./build/vanilla.js";

class SpinningBox extends Component {
	tick(timeDelta) {
		const node = this.getNode();
		const rotation = node.getLocalRotation();
		node.setLocalRotation(rotation + 90 * timeDelta);
	}

	draw(graphic) {
		const node = this.getNode();
		const contentSize = node.getContentSize();
		graphic.setFillColor(Colors.white);
		graphic.drawRect(Rect.create(0, 0, contentSize.x, contentSize.y));
	}

	isDrawnInsideContent() {
		return true;
	}
}

class MainScene extends Scene {
	initialize(engine) {
		const root = this.getRoot();
		const node = new WorldNode();
		node.setRect(100, 100, 80, 80);
		node.addComponent(SpinningBox);
		root.addChild(node);
	}
}

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

- `constructor()` (constructor, since 0.0.8): Constructor.
- `tick(timeDelta)` (method, since 0.0.8): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `isTickNeeded()` (method, since 0.4.0): Whether tick has work to do. (if false, the node does not call this component's tick)
  - By default, determined by whether tick is redefined. Components that only draw avoid the cost of being called every frame.
  - If there are times with nothing to do even though tick is redefined, the subclass redefines this.
  - returns boolean: True if the node should call tick.
- `draw(graphic)` (method, since 0.0.8): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `drawGizmos(graphic)` (method, since 0.0.8): Gizmo draw.
  - `graphic` Graphic: Graphic to draw with.
- `isDrawnInsideContent()` (method, since 0.4.0): Returns whether it draws only inside the node's content area (0, 0 ~ contentSize). (default false)
  - If it returns true and Graphic's off-screen culling is on, the engine skips draw when the content area is off screen.
  - Leave false if it can draw even slightly outside the area (world coordinates, text overflow, shadow padding, etc.).
  - returns boolean: True if drawing only inside the content area.
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes into rect the rectangle drawn by a component that draws outside the content area. (origin at the top-left of the node content area, true if known, default false)
  - Asked only of components whose isDrawnInsideContent is false. If it returns true, off-screen culling uses this rectangle.
  - A zero-size rectangle means nothing is drawn.
  - Leave false if unknown. (that node is not culled)
  - The node stores this rectangle in its subtree draw bounds and asks again only when something changed. Call markDrawnRectDirty when a value that changes the rectangle (text, font size and so on) changes.
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if written to rect. (false if unknown)

  Example: Report drawing that spills outside the content area

  ```js
  import { WorldNode, Component, Rect, Vector2 } from "./build/vanilla.js";
  
  class Glow extends Component {
  	calculateDrawnRect(rect) {
  		const node = this.getNode();
  		const contentSize = node.getContentSize();
  		rect.position.set(-10, -10);
  		rect.size.set(contentSize.x + 20, contentSize.y + 20);
  		return true;
  	}
  }
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(100, 50));
  const glow = node.addComponent(Glow);
  const drawnRect = Rect.zero();
  const isKnown = glow.calculateDrawnRect(drawnRect);
  console.log(isKnown, drawnRect.size.x, drawnRect.size.y);
  ```

- `markDrawnRectDirty()` (method, since 0.4.0): Notifies the node that the drawn rectangle (calculateDrawnRect) changed. (makes the node recalculate its stored subtree draw bounds)
  - A component that overrides calculateDrawnRect calls it when a value that changes that rectangle changes. (the same place as Godot's item_rect_changed and Unity's SetVerticesDirty)
  - A component that draws only inside the content area (isDrawnInsideContent) follows the node's content size, so it does not need to call it.
  - Does nothing if not attached to a node.

  Example: Report when the spread width changes

  ```js
  import { WorldNode, Component, Rect, Vector2 } from "./build/vanilla.js";
  
  class Glow extends Component {
  	glowSize = 10;
  	setGlowSize(glowSize) {
  		this.glowSize = glowSize;
  		this.markDrawnRectDirty();
  	}
  	calculateDrawnRect(rect) {
  		const contentSize = this.getNode().getContentSize();
  		rect.position.set(-this.glowSize, -this.glowSize);
  		rect.size.set(contentSize.x + this.glowSize * 2, contentSize.y + this.glowSize * 2);
  		return true;
  	}
  }
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(100, 50));
  const glow = node.addComponent(Glow);
  glow.setGlowSize(20);
  const drawBounds = Rect.zero();
  const isKnown = node.readSubtreeDrawBounds(drawBounds);
  console.log(isKnown, drawBounds.size.x, drawBounds.size.y);
  ```

- `static markAllDrawnRectsDirty()` (static, since 0.4.0): Notifies that the drawn rectangles of all components may have changed. (when they change all at once, as with a font load, nodes recalculate their bounds the next time they draw)

  Example: Make every node recalculate its draw bounds after changing fonts

  ```js
  import { Component } from "./build/vanilla.js";
  
  const generationBefore = Component.getDrawnRectGeneration();
  Component.markAllDrawnRectsDirty();
  console.log(Component.getDrawnRectGeneration() === generationBefore + 1);
  ```

- `static getDrawnRectGeneration()` (static, since 0.4.0): Returns the drawn rectangle generation. (goes up by one on each markAllDrawnRectsDirty call, compared with the bounds stored by nodes)
  - returns number: The drawn rectangle generation.
- `writeGeometryKey(geometryKey)` (method, since 0.4.0): Writes the draw inputs to the geometry cache key. (true if written, false by default)
  - Returning true makes Graphic cache the vertices this component drew; while the values written to the key and the drawing state stay the same, draw is not called and the cache is copied into the batch.
    If only the translation (scrolling etc.) or opacity changes, only the new matrix is applied to the cached local vertices. (does the job of Unity UGUI's SetVerticesDirty and Slate's Invalidate by comparing inputs)
  - Write every value draw reads (color fields, sizes, text, images, the node's content size, etc.) with geometryKey.add. If a missing value changes, it does not show on screen.
  - If it draws something that changes every frame such as time or random numbers, leave it false. (draw is called every frame)
  - `geometryKey` GeometryKey: Key to write the inputs to. (written with add)
  - returns boolean: True if all inputs were written. (false if unknown)

  Example: Reporting the inputs of a component that reads only a color and a size

  ```js
  import { WorldNode, Component, GeometryKey, Color, Rect, Vector2 } from "./build/vanilla.js";
  
  class Box extends Component {
  	color = new Color(0.2, 0.6, 1, 1);
  	draw(graphic) {
  		const contentSize = this.getNode().getContentSize();
  		graphic.setFillColor(this.color);
  		graphic.drawRect(Rect.create(0, 0, contentSize.x, contentSize.y));
  	}
  	writeGeometryKey(geometryKey) {
  		const contentSize = this.getNode().getContentSize();
  		geometryKey.add(this.color.red);
  		geometryKey.add(this.color.green);
  		geometryKey.add(this.color.blue);
  		geometryKey.add(this.color.alpha);
  		geometryKey.add(contentSize.x);
  		geometryKey.add(contentSize.y);
  		return true;
  	}
  }
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(100, 50));
  const box = node.addComponent(Box);
  const geometryKey = new GeometryKey();
  geometryKey.begin();
  console.log(box.writeGeometryKey(geometryKey), geometryKey.end());
  ```

- `require()` (method, since 0.2.1): Returns the list of dependent components. (overridden by child classes)
  - Called by addComponent just before attaching this component, and the returned class list
    is automatically added to the same node (skipped if already present).
  - Same idea as Unity's RequireComponent.
  - returns Function[]: List of component classes to attach to the same node first.
- `attach(node)` (method, since 0.0.11): Attached to a node.
  - `node` ComponentNode: The node attached to.
- `detach(node)` (method, since 0.0.11): Detached from a node.
  - `node` ComponentNode: The node detached from.
- `setNode(node)` (method, since 0.0.8): Sets the owner.
  - `node` ComponentNode: The owner node. (null when detached)
- `getNode()` (method, since 0.0.8): Returns the owner.
  - returns ComponentNode: The owner node. (null if not attached)
- `setGizmoVisible(isVisible)` (method, since 0.0.9): Sets whether gizmos are drawn.
  - `isVisible` boolean: If true, gizmos are drawn.
- `isGizmoVisible()` (method, since 0.0.9): Returns whether gizmos are drawn.
  - returns boolean: True if gizmos are drawn.
- `setEnable(isEnable)` (method, since 0.0.11): Sets whether enabled.
  - `isEnable` boolean: Whether to enable it. (the node does not call draw while disabled)
- `isEnable()` (method, since 0.0.11): Returns whether enabled.
  - returns boolean: True if enabled.
- `setComponentType(componentType)` (method, since 0.2.0): Sets the component type.
  - `componentType` string: Component type name. ("Component" at first; kinds are told apart by names like Mask)
- `getComponentType()` (method, since 0.2.0): Returns the component type.
  - returns string: Component type name.
