Skip to content

Node2D ​

Base class for all 2D canvas nodes.

Node2D represents a transformable object in a canvas hierarchy. It manages local and world transforms, parent–child relationships, and hooks for per-frame updates and input events. All 2D drawable objects, such as sprites and text, derive from this class and share the same hierarchical behavior.

The canvas is a tree rooted at a Canvas node: each node can have zero or more children and at most one parent. Transforms propagate downward, changing a parent updates the world-space transforms of all descendants. Opacity is inherited the same way, and hiding a node hides its entire subtree. Nodes are drawn in tree order: parents before children, siblings in insertion order. Input events propagate bottom-up through the hierarchy: children receive the event before their parents, and any node can stop propagation by marking the event as handled.

Construction ​

Constructors ​


Node2D() ​

Constructs a node.

cpp
Node2D::Node2D();

Factories preferred ​


Node2D::Create() std::unique_ptr<Node2D> ​

Creates an instance of Node2D.

cpp
static std::unique_ptr<Node2D> Node2D::Create();

Types ​

Node2D::Type enum ​

Enumerates all node categories.

Used by the engine and renderer to distinguish between drawable nodes, the canvas root, and generic nodes. Custom node types typically reuse one of the existing categories.

ValueDescription
DefaultGeneric node without special behavior.
CanvasRoot of a canvas hierarchy.
RenderableAny node that can be drawn to the canvas.
SpriteTextured quad drawn in canvas space.
TextGlyph quads laid out from a bitmap font.

Properties ​

opacity float ​

Opacity of this node, where 1.0 is fully opaque.

Multiplied by the opacity of every ancestor at draw time so fading a parent fades its whole subtree. see GetWorldOpacity. Does not affect updates or input.

cpp
float opacity {1.0f};

transform Transform2 ​

Local transform (position, rotation, scale, center) of this node.

cpp
Transform2 transform {};

visible bool ​

When true this node and its descendants are drawn.

Hidden nodes are skipped by the renderer along with their entire subtree, but still receive OnUpdate calls.

cpp
bool visible {true};

Functions ​

Add() T* ​

Adds a child node to this node and returns a non-owning reference.

This overload transfers ownership of a node into this node’s children list. The canvas is the sole owner of all nodes. The returned pointer is a non-owning reference that remains valid only while the node is attached to the canvas.

cpp
T* Node2D::Add(std::unique_ptr<T> node);
ParameterDescription
nodeNode to attach. Ownership is transferred.

Detach() std::unique_ptr<T> ​

Detaches a direct child node from this node and returns ownership.

Removes a node from this node’s children list without destroying it and returns the owned subtree as a std::unique_ptr. The detached node’s parent pointer is cleared, its attached state is reset, and its transform is marked dirty.

cpp
std::unique_ptr<T> Node2D::Detach(T* node);
ParameterDescription
nodeDirect child node to detach.

UpdateTransformHierarchy() void ​

Recursively updates world transforms for this node and its descendants.

If the node’s local transform is dirty, or an ancestor was recomputed during this pass, the world transform is recomputed from the parent’s world transform (or from the local transform if this node is a root) and the local transform is marked clean. The method then recurses into each child.

This is the primary mechanism used by the renderer to update transform propagation across the entire hierarchy once per frame.

cpp
void Node2D::UpdateTransformHierarchy();

GetCachedWorldTransform() const Matrix3& ​

Returns the world transform cached by the last hierarchy update.

Reflects the state as of the most recent call to UpdateTransformHierarchy and may be stale if a transform changed since. Intended for the renderer, which reads it after the per-frame update. Use GetWorldTransform for an always-current result.

cpp
const Matrix3& Node2D::GetCachedWorldTransform() const;

GetWorldTransform() Matrix3 ​

Returns the node’s world transform matrix.

Composes the local transforms along the parent chain without modifying any cached state, so the result is current regardless of when the hierarchy was last updated. The cost is proportional to the node’s depth.

cpp
Matrix3 Node2D::GetWorldTransform() const;

GetWorldPosition() Vector2 ​

Returns the node’s world-space position.

Computes the world transform via GetWorldTransform, then extracts the translation column of the matrix.

cpp
Vector2 Node2D::GetWorldPosition() const;

GetWorldOpacity() float ​

Returns the node’s effective opacity.

Multiplies opacity by the opacity of every ancestor up to the root. Like GetWorldTransform, the result is computed on demand and is always current.

cpp
float Node2D::GetWorldOpacity() const;

Remove() void ​

Removes a direct child node from this node and destroys it.

If the node exists in the children list it is detached and destroyed.

cpp
void Node2D::Remove(Node2D* node);
ParameterDescription
nodeDirect child node to remove.

RemoveAllChildren() void ​

Removes all children from this node.

Each child is detached from the canvas, has its parent pointer cleared, its attached state reset, and its transform marked dirty. After all children are processed, the children list is emptied.

cpp
void Node2D::RemoveAllChildren();

GetChildren() std::span<const std::unique_ptr< Node2D> > ​

Returns a view of this node’s direct children.

The returned span exposes read-only access to the owning node's std::unique_ptr<Node2D> objects for each child. Ownership is retained by the canvas.

cpp
std::span<const std::unique_ptr< Node2D> > Node2D::GetChildren() const;

GetChild() Node2D* ​

Returns a direct child node with the given name.

Searches only this node's immediate children for a node whose name matches the specified identifier. If no matching child is found, returns nullptr.

This function does not search descendants. Use GetChildren to implement recursive searches when needed.

cpp
Node2D* Node2D::GetChild(std::string_view name) const;
ParameterDescription
nameName of the child node to retrieve.

IsChild() bool ​

Checks whether the given node exists anywhere in this node’s subtree.

cpp
bool Node2D::IsChild(const Node2D* node) const;
ParameterDescription
nodeNode to test for membership in the subtree.

GetParent() const Node2D* ​

Returns this node’s parent.

cpp
const Node2D* Node2D::GetParent() const;

GetCanvas() const Canvas* ​

Returns the canvas that owns this node.

Returns the canvas this node is currently attached to or nullptr if the node is not attached to any canvas.

cpp
const Canvas* Node2D::GetCanvas() const;

OnUpdate() void virtual ​

Per-frame update callback.

Called once per frame with the elapsed time since the last frame in seconds. Override this to implement node behavior.

cpp
virtual void Node2D::OnUpdate(float delta);
ParameterDescription
deltaTime step in seconds.

OnKeyboardEvent() void virtual ​

Keyboard event handler.

Override this to react to keyboard events. Events can be marked as handled to stop propagation.

cpp
virtual void Node2D::OnKeyboardEvent(KeyboardEvent* event);
ParameterDescription
eventKeyboard event pointer.

OnMouseEvent() void virtual ​

Mouse event handler.

Override this to react to cursor, button, or scroll events. Events can be marked as handled to stop propagation.

cpp
virtual void Node2D::OnMouseEvent(MouseEvent* event);
ParameterDescription
eventMouse event pointer.

OnGamepadEvent() void virtual ​

Gamepad event handler.

Override this to react to gamepad connection, button, or axis events. Events can be marked as handled to stop propagation.

cpp
virtual void Node2D::OnGamepadEvent(GamepadEvent* event);
ParameterDescription
eventGamepad event pointer.

GetNodeType() Node2D::Type virtual ​

Returns the node's type identifier.

Subclasses override this to report their specific type. The default is Node2D::Type::Default.

cpp
virtual Type vglx::Node2D::GetNodeType() const;

IsRenderable() bool virtual ​

Returns whether this node is renderable.

Drawable nodes derive from Renderable2D which overrides this and returns true. The renderer draws any node reporting true through that interface. The base implementation always returns false.

cpp
virtual bool Node2D::IsRenderable() const;

Released under the MIT License.