nodesnim/nodes/node

Node is the root type of all other nodes.

Types

NodeObj = object of RootObj
  kind*: NodeKind
  visible*: bool
  is_ready*: bool
  can_use_anchor*: bool
  can_use_size_anchor*: bool
  relative_z_index*: bool      ## Uses in the Node2D
  z_index*, z_index_global*: float ## Uses in the Node2D
  pausemode*: PauseMode        ## Pause mode, by default is INHERIT.
  name*: string                ## Node name.
  position*: Vector2Ref        ## Node position, by default is Vector2(0, 0).
  global_position*: Vector2Ref ## Node global position.
  rect_size*: Vector2Ref       ## Node size.
  size_anchor*: Vector2Ref     ## Node size anchor.
  anchor*: AnchorRef           ## Node anchor.
  parent*: NodePtr             ## Node parent.
  children*: seq[NodePtr]      ## Node children.
  on_enter*: proc (self: NodePtr) ## This called when scene changed.
  on_exit*: proc (self: NodePtr) ## This called when exit from the scene.
  on_input*: proc (self: NodePtr; event: InputEvent) ## This called on user input.
  on_ready*: proc (self: NodePtr) ## This called when the scene changed and the `enter` was called.
  on_process*: proc (self: NodePtr) ## This called every frame.
  
NodePtr = ptr NodeObj

Procs

proc Node(name: string; variable: var NodeObj): NodePtr {...}{.raises: [], tags: [].}
Creates a new Node pointer.
proc Node(variable: var NodeObj): NodePtr {...}{.inline, raises: [], tags: [].}

Methods

method addChild(self: NodePtr; child: NodePtr) {...}{.base, raises: [], tags: [].}

Adds new child in current node.

Arguments:

  • child: other node.
method addChilds(self: NodePtr; childs: varargs[NodePtr]) {...}{.base, raises: [], tags: [].}

Adds new child in current node.

Arguments:

  • child: other node.
method calcGlobalPosition(self: NodePtr) {...}{.base, raises: [], tags: [].}
Returns global node position.
method calcPositionAnchor(self: NodePtr) {...}{.base, raises: [], tags: [].}
Calculates node position with anchor. This used in the Window object.
method draw(self: NodePtr; w, h: GLfloat) {...}{.base, raises: [], tags: [].}
Draws node. This used in the Window object.
method draw2stage(self: NodePtr; w, h: GLfloat) {...}{.base, raises: [], tags: [].}
Draws node. This used in the Window object.
method duplicate(self: NodePtr; obj: var NodeObj): NodePtr {...}{.base, raises: [], tags: [].}
Duplicates Node object and create a new Node pointer.
method getChild(self: NodePtr; index: int): NodePtr {...}{.base, raises: [], tags: [].}

Returns child at index position, if available.

Arguments:

  • index: child index.
method getChildCount(self: NodePtr): int {...}{.base, inline, raises: [], tags: [].}
Returns child count.
method getChildIndex(self: NodePtr; name: string): int {...}{.base, raises: [], tags: [].}
Returns child index or -1, if another node is not the child.
method getChildIndex(self: NodePtr; child: NodePtr): int {...}{.base, raises: [], tags: [].}
Returns child index or -1, if another node is not the child.
method getChildIter(self: NodePtr): seq[NodePtr] {...}{.base, raises: [Exception],
    tags: [RootEffect].}
Returns all children iter.
method getNode(self: NodePtr; path: string): NodePtr {...}{.base, raises: [], tags: [].}
Returns child by path
method getPath(self: NodePtr): string {...}{.base, raises: [], tags: [].}
Returns node path.
method getParent(self: NodePtr): NodePtr {...}{.base, raises: [], tags: [].}
Returns node parent.
method getPauseMode(self: NodePtr): PauseMode {...}{.base, raises: [], tags: [].}
Calculates pause mode
method getRootNode(self: NodePtr): NodePtr {...}{.base, raises: [], tags: [].}
Gets root node.
method isCollide(self: NodePtr; x, y: float): bool {...}{.base, raises: [], tags: [].}
method isCollide(self: NodePtr; vec2: Vector2Ref): bool {...}{.base, raises: [], tags: [].}
method isCollide(self, other: NodePtr): bool {...}{.base, raises: [], tags: [].}
method isParentOf(self, other: NodePtr): bool {...}{.base, inline, raises: [], tags: [].}
method handle(self: NodePtr; event: InputEvent; mouse_on: var NodePtr) {...}{.base,
    raises: [], tags: [].}
Handles user input. This used in the Window object.
method hasNode(self: NodePtr; name: string): bool {...}{.base, raises: [], tags: [].}

Returns true, if a node with name name in children.

Arguments:

  • name: node name.
method hasNode(self: NodePtr; other: NodePtr): bool {...}{.base, raises: [], tags: [].}

Returns true, if other in self children.

Arguments:

  • other: other node.
method hasParent(self: NodePtr): bool {...}{.base, inline, raises: [], tags: [].}
Returns true, when node has parent.
method hide(self: NodePtr) {...}{.base, raises: [], tags: [].}
method move(self: NodePtr; x, y: float) {...}{.base, inline, raises: [], tags: [].}

Adds x and` y` to the node position.

Arguments:

  • x: how much to add to the position on the X axis.
  • y: how much to add to the position on the Y axis.
method move(self: NodePtr; vec2: Vector2Ref) {...}{.base, inline, raises: [], tags: [].}

Adds vec2 to the node position.

Arguments:

  • vec2: how much to add to the position on the X,Y axes.
method removeChild(self: NodePtr; index: int) {...}{.base, raises: [], tags: [].}

Removes node child at a specific position.

Arguments:

  • index: child index.
method removeChild(self: NodePtr; other: NodePtr) {...}{.base, raises: [], tags: [].}

Removes another node from self, if other in self children.

Arguments:

  • other: other node.
method setAnchor(self: NodePtr; anchor: AnchorRef) {...}{.base, raises: [], tags: [].}

Changes node anchor.

Arguments:

  • anchor - AnchorRef object.
method setAnchor(self: NodePtr; x1, y1, x2, y2: float) {...}{.base, raises: [], tags: [].}

Changes node anchor.

Arguments:

  • x1 and y1 - anchor relative to the parent node.
  • x2 and y2 - anchor relative to this node.
method setSizeAnchor(self: NodePtr; anchor: Vector2Ref) {...}{.base, raises: [], tags: [].}
method setSizeAnchor(self: NodePtr; x, y: float) {...}{.base, raises: [], tags: [].}
method show(self: NodePtr) {...}{.base, raises: [], tags: [].}
method delete(self: NodePtr) {...}{.base, raises: [], tags: [].}
Deletes current node.

Macros

macro `@`(node: NodePtr; event_name, code: untyped): untyped

It provides a convenient wrapper for the event handler.

Arguments:

  • node is an any node pointer.
  • event_name is an event name, e.g.: process.
  • code is the proc code.

Examples

var
  smth_node = Node("Simple node")

smth_node@ready:
  echo "node is ready!"

smth_node@input(event):
  if event.isInputEventMouseButton():
    echo event

Templates

template nodepattern(nodetype: untyped): untyped
This used in childs of the NodeObj.