A class that represents a graph. Serves as a container for a group of nodes.

Available since

0.2.0

.

Constructor

@:value({ trackNetworks : false, defaultAutoConnect : false })new(?parent:Object, defaultAutoConnect:Bool = false, trackNetworks:Bool = false)

Creates an nb.Graph instance.

Parameters:

parent

The instance's parent object.

defaultAutoConnect

Whether this instance should use assign a default function to autoConnect. This function connects a newly added node to the previous node that was added.

trackNetworks

Whether this instance should track networks. (See networks.)

Variables

@:value([])read onlyallNodes:Array<Node> = []

All nb.Node instances this graph is using.

@:value([])deadNetworksIds:Array<Int> = []

Contains network ids that were used but anymore.

@:value(new Graphics())read onlydebugG:Graphics = new Graphics()

The nb.Graphics instance to draw this instance's debug visuals.

@:value([])debugInteractives:Array<Interactive> = []

The nb.Graphics instance to draw this instance's interactives debug visuals.

@:value([])read onlylastPath:Array<Node> = []

The last path that the pathfinder made.

@:value([[]])networks:Array<Array<Node>> = [[]]

Contains all networks of this instance. A network is an array of nb.Node instances that are all connected. A connection betweens two nodes in a network means that there exists a path that connects the two.

A network has an associated ID number starting from 1. Doing networks[5] accesses the array of nodes pertaining to a network with 5 as the id.

@:value([])skippedNetworksIds:Array<Int> = []

Contains network ids that were never used. Those ids are always lower than the highest used netword id.

@:value(false)takeDeadGroups:Bool = false

Whether dead networks can be revived to form a new network.

read onlytrackNetworks:Bool

Whether this instance should track networks. Networks are nodes all connected to each other, directly or indirectly.

Methods

@:value({ autoConnect : false })addNode(p:Point, autoConnect:Bool = false):Node

Adds a node to this graph.

Parameters:

p

An h2d.col.Point instance containing the coordinates of the node.

autoConnect

If true this instance's autoConnect function will be called.

Returns:

The nb.Graph.Node instance representing the node.

dynamicautoConnect(allNodes:Array<Node>, newNode:Node):Void

A saved connection logic.

Parameters:

allNodes

All nodes in this instance.

newNode

The node that was just added to this instance.

clear():Void

Empties the graph.

connect(node:Node, toNodes:Array<Node>):Void

Connects a node to other nodes.

Parameters:

node

The node that will be connected.

toNodes

The nodes node will connects to.

convertNetwork(from:Int, to:Int):Void

Moves all the nodes of a network to another network.

All the nodes belonging to the network with id from now belongs to the network with id to. Nothing happens to the nodes that are already in the destination network.

Parameters:

from

The ID of the network to move from.

to

The ID of the network to move to.

@:value({ withInteractive : false, pointRadius : 2 })debugDraw(pointRadius:Float = 2, withInteractive:Bool = false):Void

Draws a visualization of this instance.

POINT: Green: Connected, in path | Grey: Connected, not in path | Red: No connection

SEGMENT: Darker green: In path | Grey: Anything else

Parameters:

pointRadius

The radius of the circle drawn for each node.

withInteractive

Whether the nodes will have their interactives enabled, making them movable.

disconnect(node:Node, fromNodes:Array<Node>):Void

Disconnects a node from other nodes.

Parameters:

node

The node that will be disconnected.

fromNodes

The nodes node will disconnects from.

dynamicgetDistance(n1:Node, n2:Node):Float

Returns a distance between two nodes.

getLastPathInfo():{start:Node, newCalcNodes:Array<Node>, lastPath:Array<Node>, end:Node, currentNode:Node, checkedNodes:Array<Node>, calculatedNodes:Array<Node>}

getNodeAtPoint(p:Point):Null<Node>

Returns the node with the coordinates of p. (There's a tiny tolerance value.)

dynamiconConnect(node1:Node, node2:Node):Void

Called whenever two nodes connects.

dynamiconCurrentNode(node:Node):Void

Called in pathfinding, when the pathfinder goes on a node.

dynamiconDisconnect(node1:Node, node2:Node):Void

Called whenever two nodes disconnects.

dynamiconJustCalculated(node:Node):Void

Called in pathfinding, when a node's pathfinder values was just assigned.

dynamiconNodeMove(node:Node):Void

Called whenever a node moves.

dynamiconPathStart():Void

Called when pathfinding start.

dynamiconWasCalculated(node:Node):Void

Called in pathfinding, at the start of the loop, when a node had its pathfinder values assigned in the previous loop.

@:value({ updateVars : true, skipEvents : false })path(start:Node, end:Node, ?maxStep:Int, skipEvents:Bool = false, updateVars:Bool = true):Null<Array<Node>>

Returns a path between a node and another node.

Parameters:

start

The node to start the path from.

end

The node to end the path to.

maxStep

The maximum number of steps/loop count.

skipEvents

Whether pathfinder events should be called.

setLastPath

Whether lastPath will be set to the resulting path.

Returns:

An array of nb.Graph.Node instances representing a path.

removeNode(node:Node):Void

Removes a node from this graph.

Inherited Variables

Defined by Object

@:value(true)autoTogglableVisibility:Bool = true

Whether visible can be auto-toggled by this instance.

@:value(true)doOnContentChangedOnAdd:Bool = true

Whether the onContentChanged function should get called in the onAdd function.

@:value({ w : 0, h : 0 })read onlysize:Size = { w : 0, h : 0 }

The size of the object. You shouldn't modify it directly, use the setSize function instead.

This size is independent of the size of the displayed content that you get from functions like getBounds().

By default, this value doesn't change and gets its behavior defined by a children class.

Defined by Object

private@:dox(show)allocated:Bool

A flag that indicates whether the object was allocated or not.

When adding children to allocated objects, onAdd is being called immediately, otherwise it's delayed until the whole tree is added to a currently active Scene.

@:value(1.)alpha:Float = 1.

The amount of transparency of the Object.

@:value(Alpha)blendMode:BlendMode = Alpha

The blending mode of the object.

If there is no Object.filter active, only applies to the current object (not inherited by children). Otherwise tells how the filter is blended with background.

filter:Filter

The post process filter for this object.

When set, Object.alpha value affects both filter and object transparency (use Drawable.color.a to set transparency only for the object).

name:String

The name of the object. Can be used to retrieve an object within a tree by using Object.getObjectByName.

read onlynumChildren:Int

How many immediate children this object has.

@:value(nextId++)finalobjId:Int = nextId++

read onlyparent:Object

The parent object in the scene tree.

private@:dox(show)parentContainer:Object

The parent container of this object. See Object.contentChanged for more details.

private@:dox(show)posChanged:Bool

A flag that indicates that the object transform was modified and absolute position recalculation is required.

Automatically cleared on Object.sync and can be manually synced with the Object.syncPos.

@:value(0)rotation:Float = 0

The rotation angle of this object, in radians.

@:value(1)scaleX:Float = 1

The amount of horizontal scaling of this object.

@:value(1)scaleY:Float = 1

The amount of vertical scaling of this object.

@:value([])read onlytags:Array<{name:String, inherited:Bool, inheritable:Bool}> = []

@:value(true)visible:Bool = true

Is the object and its children are displayed on screen.

@:value(0)x:Float = 0

The x position (in pixels) of the object relative to its parent.

@:value(0)y:Float = 0

The y position (in pixels) of the object relative to its parent.

Inherited Methods

Defined by Object

addChildAt(s:Object, pos:Int):Void

Inserts a child object at the specified position of the children list.

private@:dox(show)contentChanged(o:Object):Void

Called by the children of a container object if they have parentContainer defined in them.

move(dx:Float, dy:Float):Void

Moves this object by the given amount but unlike h2d.Object, it doesn't take rotation into account.

moveTo(x:Float, y:Float):Void

Moves this object to a given position. It is meant to be overriden instead of setPosition.

dynamiconParentResize():Void

Called when a parent object gets resized.

dynamiconWindowResize():Void

Called when the window gets resized and this object is on a scene.

outsideScene():Bool

Checks whether this object's content can be seen.

searchByClass<T>(type:Class<T>):Array<T>

Returns an array of all children of the given type.

searchByName(regex:EReg):Array<Object>

Returns an array of h2d.Objects which name matches the regex.

setSize(w:Float, h:Float):Void

Sets size.

update(dt:Float):Void

The update function called every frame by nb.Manager.

Defined by Object

private@:dox(show)addBounds(relativeTo:Object, out:Bounds, dx:Float, dy:Float, width:Float, height:Float):Void

Adds specified area in local coordinate space to the bounds. Expected to be used within Object.getBoundsRec.

Parameters:

relativeTo

An object relative to which the Object bounds coordinates should be. If not set, coordinates are in absolute coordinate space.

out

An output Bounds instance.

dx

The top-left X offset of the added bounds rectangle.

dy

The top-left Y offset of the added bounds rectangle.

width

The width of the added bounds rectangle.

height

The height of the added bounds rectangle.

addChild(s:Object):Void

Add a child object at the end of the children list.

@:value({ inherited : false, inheritable : false })addTag(tagName:String, inheritable:Bool = false, inherited:Bool = false):Void

private@:dox(show)calcAbsPos():Void

Internal usage Calculates the absolute object position transform. See `Object.syncPos` for a safe position sync method. This method does not ensure that object parents also have up-to-date transform nor does it clear the `Object.posChanged` flag.

private@:value({ scaleY : 1., scaleX : 1. })@:dox(show)clipBounds(ctx:RenderContext, bounds:Bounds, scaleX:Float = 1., scaleY:Float = 1.):Void

Internal usage Clip the local bounds with our global viewport. Used during filter rendering in order to clip out areas that are off-screen and should not be rendered.

h2d.Scene note: clipBounds will always output bounds equivalent to entire window. This is done in order for scene to never clip out cameras as they may render virtually any area of the Scene.

private@:dox(show)constraintSize(maxWidth:Float, maxHeight:Float):Void

Advanced usage This can be called by a parent container to constraint the size of its children. Negative value mean that constraint is to be disabled.

For example, Text constraints it's maximum width, causing word-wrap to occur within constrained area.

See also:

  • FlowProperties.constraint

contains(o:Object):Bool

Tells if the object is contained into this object children, recursively.

private@:dox(show)draw(ctx:RenderContext):Void

Override this method in order to add custom graphics rendering to your Object. draw is invoked before rendering of the object children.

drawTo(t:Texture):Void

Draw the object and all its children into the given Texture.

drawToTextures(texs:Array<Texture>, outputs:Array<Output>):Void

Draw the object and all its children into the given Textures.

private@:dox(show)emitTile(ctx:RenderContext, tile:Tile):Void

Draws single Tile instance with this Object transform.

fOnChildren(?fAll:Object ‑> Void, ?fNb:Object ‑> Void):Void

find<T>(f:Object ‑> Null<T>):Null<T>

Find a single object in the tree by calling f on each and returning the first not-null value returned, or null if not found.

findAll<T>(f:Object ‑> Null<T>, ?arr:Array<T>):Array<T>

Find several objects in the tree by calling f on each and returning all the not-null values returned.

Parameters:

arr

An optional array instance to fill results with. Allocates a new array if not set.

getAbsPos():Matrix

Returns the updated absolute position matrix. See Object.getMatrix for current matrix values.

getBounds(?relativeTo:Object, ?out:Bounds):Bounds

Return the bounds of the object for its whole content, recursively.

Parameters:

relativeTo

An optional object relative to coordinates of which bounds are returned. Returns bounds in the absolute coordinates if not set.

out

An optional bounds instance to fill. Allocates new Bounds instance and returns it if not set.

private@:dox(show)getBoundsRec(relativeTo:Object, out:Bounds, forSize:Bool):Void

Override this method in order to expand the reported bounds of an object. Object.addBounds can be used to add bounds with respect to relativeTo. Do not remove the super call.

Parameters:

relativeTo

An object relative to which the Object bounds coordinates should be.

out

An output Bounds instance.

forSize

Whether it's being called for Object.getSize or Object.getBounds.

getChildAt(n:Int):Object

Return the nth element among the immediate children list of this object, or null if there is no Object at this position.

getChildIndex(o:Object):Int

Return the index of the object o within the immediate children list of this object, or -1 if it is not part of the children list.

private@:dox(show)getMatrix(m:Matrix):Void

Populates Matrix with current absolute object transform values. See Object.getAbsPos for up-to-date values.

getObjectByName(name:String):Object

Search for an object recursively by name, return null if not found.

getObjectsCount():Int

Return the total number of children in the whole tree, recursively.

getScene():Scene

Returns an h2d.Scene down the hierarchy tree or null if object is not added to Scene.

finalgetSize(?out:Bounds):Bounds

Similar to getBounds(parent), but instead of the full content, it will return the size based on the alignment of the object. For instance for a text, Object.getBounds will return the full glyphs size whereas getSize will ignore the pixels under the baseline.

Parameters:

out

An optional bounds instance to fill. Allocates new Bounds instance and returns it if not set.

globalToLocal(pt:Point):Point

Convert an absolute screen position into a local position relative to the object origin, applying all the inherited transforms.

Parameters:

pt

A position to convert and return. Modifies the Point instance as is.

hasTag(tagName:String):Bool

inlineiterator():ArrayIterator_h2d_Object

Return an iterator over this object immediate children

localToGlobal(?pt:Point):Point

Convert a local position (or [0,0] if pt is null) relative to the object origin into an absolute screen position, applying all the inherited transforms.

Parameters:

pt

An optional position to convert and return. Allocates new Point at 0,0 position if not set. Modifies the Point instance as is.

private@:dox(show)onAdd():Void

Sent when object is being added to an allocated scene.

Do not remove the super call when overriding.

private@:dox(show)inlineonContentChanged():Void

Should be called when Object content was changed in order to notify parent container. See Object.contentChanged.

private@:dox(show)onHierarchyMoved(parentChanged:Bool):Void

Sent when object was already allocated and moved within scene object tree hierarchy.

Do not remove the super call when overriding.

Parameters:

parentChanged

Whether Object was moved withing same parent (through Layers.ysort for example) or relocated to a new one.

private@:dox(show)onRemove():Void

Sent when object is removed from the allocated scene.

Do not remove the super call when overriding.

inlineremove():Void

Same as parent.removeChild(this), but does nothing if parent is null.

removeChild(s:Object):Void

Remove the given object from the immediate children list of the object if it's part of it.

removeChildren():Void

Remove all children from the immediate children list.

inlinerotate(v:Float):Void

Rotate the object by the given angle (in radians)

inlinescale(v:Float):Void

Scale uniformly the object by the given factor.

private@:dox(show)setParentContainer(c:Object):Void

Sets the parent container for this Object and it's children. See Object.contentChanged for more details.

inlinesetPosition(x:Float, y:Float):Void

Set the position of the object relative to its parent.

inlinesetScale(v:Float):Void

Set the uniform scale for the object.

private@:dox(show)sync(ctx:RenderContext):Void

Performs a sync of data for rendering (such as absolute position recalculation). While this method can be used as a substitute to an update loop, it's primary purpose it to prepare the Object to be rendered.

Do not remove the super call when overriding.

private@:dox(show)syncPos():Void

Ensures that object has an up-to-date position transform.