class Object
package h2d
extended by Console, Drawable, Flow, Interactive, Layers, Mask, ObjectFollower, Loader
A base 2D class that all scene tree elements inherit from.
Serves as a virtual container that does not display anything but can contain other objects so the various transforms are inherited to its children.
Private events Object.onAdd
, Object.onRemove
and Object.onHierarchyMoved
can be used
to capture when Object is added/removed from the currently active scene as well as being moved withing the object tree.
Object exposes a number of properties to control position, scale and rotation of the Object relative to its parent,
but they are used indirectly during rendering. Instead, they are being used to calculate the absolute matrix transform
relative to the Scene. As optimization, it's not recalculated as soon as properties are modified and delayed until
Object.sync
. Absolute object position can be accessed through private variables Object.matA
, Object.matB
,
Object.matC
, Object.matD
, Object.absX
and Object.absY
.
But it should be noted that in order to ensure up-to-date values, it's advised to call Object.syncPos
before accessing them.
Constructor
new(?parent:Object)
Create a new empty object.
Parameters:
parent | An optional parent |
---|
Variables
privateallocated: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
.
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
.
privateparentContainer:Object
The parent container of this object.
See Object.contentChanged
for more details.
privateposChanged: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
.
Methods
privateaddBounds(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. |
addChildAt(s:Object, pos:Int):Void
Insert a child object at the specified position of the children list.
privatecalcAbsPos():Void
privateclipBounds(ctx:RenderContext, bounds:Bounds, scaleX:Float = 1., scaleY:Float = 1.):Void
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.
privateconstraintSize(maxWidth:Float, maxHeight:Float):Void
For example, Text
constraints it's maximum width, causing word-wrap to occur within constrained area.
See also:
privatecontentChanged(s:Object):Void
privatedraw(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.
drawToTextures(texs:Array<Texture>, outputs:Array<Output>):Void
Draw the object and all its children into the given Textures.
privateemitTile(ctx:RenderContext, tile:Tile):Void
Draws single Tile instance with this Object transform.
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. |
---|
inlinegetAbsPos():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. |
privategetBoundsRec(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 |
getChildAt(n:Int):Object
Return the n
th 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.
privategetMatrix(m:Matrix):Void
Populates m
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.
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. |
---|
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. |
---|
move(dx:Float, dy:Float):Void
Move the object by the specified amount along its current direction (Object.rotation
angle).
privateonAdd():Void
Sent when object is being added to an allocated scene.
Do not remove the super call when overriding.
privateinlineonContentChanged():Void
Should be called when Object content was changed in order to notify parent container. See Object.contentChanged
.
privateonHierarchyMoved(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 |
---|
privateonRemove():Void
Sent when object is removed from the allocated scene.
Do not remove the super call when overriding.
removeChild(s:Object):Void
Remove the given object from the immediate children list of the object if it's part of it.
privatesetParentContainer(c:Object):Void
Sets the parent container for this Object and it's children.
See Object.contentChanged
for more details.
privatesync(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.