core/component
컴포넌트.
Component.
Componentextends Object
컴포넌트.
Component.
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());모든 컴포넌트의 그리는 사각형이 바뀌었을 수 있음을 알리기. (글꼴 적재처럼 한꺼번에 바뀌는 때, 노드들은 다음에 그릴 때 경계를 다시 셈한다)
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)
짝Counterpart: vanilla-engine ComponentBase.mark_all_drawn_rects_dirty
import { Component } from "./build/vanilla.js";
const generationBefore = Component.getDrawnRectGeneration();
Component.markAllDrawnRectsDirty();
console.log(Component.getDrawnRectGeneration() === generationBefore + 1);그리는 사각형 세대 반환. (markAllDrawnRectsDirty 를 부를 때마다 하나씩 오른다, 노드가 셈해 둔 경계와 견준다)
Returns the drawn rectangle generation. (goes up by one on each markAllDrawnRectsDirty call, compared with the bounds stored by nodes)
반환Returnsnumber 그리는 사각형 세대.The drawn rectangle generation.
짝Counterpart: vanilla-engine ComponentBase.get_drawn_rect_generation
갱신.
Update.
- timeDeltanumber
- 지난 프레임부터의 시간. (초)Time since the last frame. (Seconds)
짝Counterpart: vanilla-engine Component.tick
tick 이 할 일이 있는지. (거짓이면 노드가 이 컴포넌트의 tick 을 부르지 않는다)
- 기본은 tick 을 다시 정의했는지로 본다. 그리기만 하는 컴포넌트는 매 프레임 부르는 비용이 없어진다.
- tick 을 다시 정의했어도 할 일이 없는 때가 있으면 하위 클래스가 다시 정의한다.
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.
반환Returnsboolean 노드가 tick 을 불러야 하면 참.True if the node should call tick.
짝Counterpart: vanilla-engine Component.is_tick_needed
기즈모 출력.
Gizmo draw.
- graphicGraphic
- 그릴 그래픽.Graphic to draw with.
짝Counterpart: vanilla-engine Component.draw_gizmos
노드의 내용 영역(0, 0 ~ contentSize) 안에서만 그리는지 반환. (기본 false)
- true 를 돌려주면 Graphic 의 화면 밖 거르기가 켜져 있을 때, 내용 영역이 화면 밖이면 엔진이 draw 를 건너뛴다.
- 영역 밖(월드 좌표, 글자 넘침, 그림자 여백 등)에 조금이라도 그릴 수 있으면 false 로 둔다.
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.).
반환Returnsboolean 내용 영역 안에서만 그리면 참.True if drawing only inside the content area.
짝Counterpart: vanilla-engine Component.is_drawn_inside_content
내용 영역 밖까지 그리는 컴포넌트가 그리는 사각형을 rect 에 적기. (노드 내용 영역의 왼쪽 위가 원점, 알면 true, 기본 false)
- isDrawnInsideContent 가 false 인 컴포넌트만 묻는다. true 를 돌려주면 화면 밖 거르기가 이 사각형으로 본다.
- 크기가 0 인 사각형은 아무것도 그리지 않는다는 뜻이다.
- 모르면 false 로 둔다. (그 노드는 거르지 않는다)
- 노드는 이 사각형을 하위 트리 그리기 경계에 셈해 두고 바뀐 때만 다시 묻는다. 사각형을 바꾸는 값(글, 글꼴 크기 등)이 바뀌면 markDrawnRectDirty 를 부른다.
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.
- rectRect
- 그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)Where to write the drawn rectangle. (origin at the top-left of the node's content area)
반환Returnsboolean rect 에 적었으면 참. (모르면 거짓)True if written to rect. (false if unknown)
짝Counterpart: vanilla-engine Component.calculate_drawn_rect
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);그리는 사각형(calculateDrawnRect)이 바뀌었음을 노드에 알리기. (노드가 셈해 둔 하위 트리 그리기 경계를 다시 셈하게 한다)
- calculateDrawnRect 를 덮어쓴 컴포넌트는 그 사각형을 바꾸는 값이 바뀔 때 부른다. (Godot 의 item_rect_changed, 유니티의 SetVerticesDirty 와 같은 자리)
- 내용 영역 안에서만 그리는 컴포넌트(isDrawnInsideContent)는 노드의 내용 크기를 보므로 부르지 않아도 된다.
- 노드에 붙지 않았으면 아무것도 하지 않는다.
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.
짝Counterpart: vanilla-engine ComponentBase.mark_drawn_rect_dirty
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);그리기 입력을 기하 캐시 열쇠에 적기. (적었으면 true, 기본 false)
- true 를 돌려주면 Graphic 이 이 컴포넌트가 그린 정점을 캐시해 두고, 열쇠에 적은 값과 그리는 상태가 그대로면 draw 를 부르지 않고 캐시를 묶음에 옮긴다. 이동(스크롤 등)이나 투명도만 바뀌면 캐시한 지역 정점에 새 행렬만 곱한다. (유니티 UGUI 의 SetVerticesDirty, Slate 의 Invalidate 와 같은 일을 입력 견주기로 한다)
- draw 가 읽는 값(색의 칸, 크기, 글, 그림, 노드의 내용 크기 등)을 모두 geometryKey.add 로 적는다. 빠진 값이 바뀌면 화면에 나오지 않는다.
- 시각이나 난수처럼 프레임마다 달라지는 것을 그리면 false 로 둔다. (프레임마다 draw 를 부른다)
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)
- geometryKeyGeometryKey
- 입력을 적을 열쇠. (add 로 적는다)Key to write the inputs to. (written with add)
반환Returnsboolean 입력을 모두 적었으면 참. (모르면 거짓)True if all inputs were written. (false if unknown)
짝Counterpart: vanilla-engine Component.write_geometry_key
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());의존 컴포넌트 목록 반환. (자식 클래스가 오버라이드)
- addComponent 가 본 컴포넌트를 부착하기 직전에 호출하여, 반환된 클래스 목록을 같은 노드에 자동으로 추가한다 (이미 있으면 건너뜀).
- Unity 의 RequireComponent 와 같은 사상.
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.
반환ReturnsFunction[] 같은 노드에 먼저 붙일 컴포넌트 클래스 목록.List of component classes to attach to the same node first.
짝Counterpart: vanilla-engine Component.require
노드에 붙음.
Attached to a node.
- nodeComponentNode
- 붙은 노드.The node attached to.
짝Counterpart: vanilla-engine Component.attach
노드에서 떨어짐.
Detached from a node.
- nodeComponentNode
- 떨어진 노드.The node detached from.
짝Counterpart: vanilla-engine Component.detach
소유권자 설정.
Sets the owner.
- nodeComponentNode
- 소유권자 노드. (떨어지면 null)The owner node. (null when detached)
소유권자 반환.
Returns the owner.
반환ReturnsComponentNode 소유권자 노드. (붙지 않았으면 null)The owner node. (null if not attached)
기즈모 그리기 여부 설정.
Sets whether gizmos are drawn.
- isVisibleboolean
- 참이면 기즈모를 그린다.If true, gizmos are drawn.
짝Counterpart: vanilla-engine ComponentBase.set_gizmo_visible
기즈모 그리기 여부 반환.
Returns whether gizmos are drawn.
반환Returnsboolean 기즈모를 그리면 참.True if gizmos are drawn.
짝Counterpart: vanilla-engine ComponentBase.is_gizmo_visible
활성화 여부 설정.
Sets whether enabled.
- isEnableboolean
- 켤지. (꺼지면 노드가 draw 를 부르지 않는다)Whether to enable it. (the node does not call draw while disabled)
짝Counterpart: vanilla-engine ComponentBase.set_enable
활성화 여부 반환.
Returns whether enabled.
반환Returnsboolean 켜져 있으면 참.True if enabled.
짝Counterpart: vanilla-engine ComponentBase.is_enable
컴포넌트 타입 설정.
Sets the component type.
- componentTypestring
- 컴포넌트 타입 이름. (처음은 "Component", Mask 같은 이름으로 종류를 가른다)Component type name. ("Component" at first; kinds are told apart by names like Mask)
컴포넌트 타입 반환.
Returns the component type.
반환Returnsstring 컴포넌트 타입 이름.Component type name.
짝Counterpart: vanilla-engine Component.get_component_type