Class Node

  • All Implemented Interfaces:
    TransformProvider
    Direct Known Subclasses:
    AnchorNode, Camera

    public class Node
    extends NodeParent
    implements TransformProvider
    A Node represents a transformation within the scene graph's hierarchy. It can contain a renderable for the rendering engine to render.

    Each node can have an arbitrary number of child nodes and one parent. The parent may be another node, or the scene.

    • Constructor Detail

      • Node

        public Node()
        Creates a node with no parent.
    • Method Detail

      • setName

        public final void setName​(java.lang.String name)
        Sets the name of this node. Nodes can be found using their names. Multiple nodes may have the same name, in which case calling NodeParent.findByName(String) will return the first node with the given name.
        Parameters:
        name - The name of the node.
      • getName

        public final java.lang.String getName()
        Returns the name of the node. The default value is "Node".
      • setParent

        public void setParent​(@Nullable
                              NodeParent parent)
        Changes the parent node of this node. If set to null, this node will be detached from its parent. The local position, rotation, and scale of this node will remain the same. Therefore, the world position, rotation, and scale of this node may be different after the parent changes.

        The parent may be another Node or a Scene. If it is a scene, then this Node is considered top level. getParent() will return null, and getScene() will return the scene.

        Parameters:
        parent - The new parent that this node will be a child of. If null, this node will be detached from its parent.
        See Also:
        getParent(), getScene()
      • getScene

        @Nullable
        public final Scene getScene()
        Returns the scene that this node is part of, null if it isn't part of any scene. A node is part of a scene if its highest level ancestor is a Scene
      • getParent

        @Nullable
        public final Node getParent()
        Returns the parent of this node. If this Node has a parent, and that parent is a Node or Node subclass, then this function returns the parent as a Node. Returns null if the parent is a Scene, use getScene() to retrieve the parent instead.
        Returns:
        the parent as a Node, if the parent is a Node.
      • isTopLevel

        public boolean isTopLevel()
        Returns true if this node is top level. A node is considered top level if it has no parent or if the parent is the scene.
        Returns:
        true if the node is top level
      • isDescendantOf

        public final boolean isDescendantOf​(NodeParent ancestor)
        Checks whether the given node parent is an ancestor of this node recursively.
        Parameters:
        ancestor - the node parent to check
        Returns:
        true if the node is an ancestor of this node
      • setEnabled

        public final void setEnabled​(boolean enabled)
        Sets the enabled state of this node. Note that a Node may be enabled but still inactive if it isn't part of the scene or if its parent is inactive.
        Parameters:
        enabled - the new enabled status of the node
        See Also:
        isActive()
      • isEnabled

        public final boolean isEnabled()
        Gets the enabled state of this node. Note that a Node may be enabled but still inactive if it isn't part of the scene or if its parent is inactive.
        Returns:
        the node's enabled status.
        See Also:
        isActive()
      • isActive

        public final boolean isActive()
        Returns true if the node is active. A node is considered active if it meets ALL of the following conditions:
        • The node is part of a scene.
        • the node's parent is active.
        • The node is enabled.

        An active Node has the following behavior:

        Returns:
        the node's active status
        See Also:
        onActivate(), onDeactivate()
      • isSelectable

        public boolean isSelectable()
        Retrieve if the node can be selected within the when a touch event happened.
        Returns:
        true if the node can be selected
      • setSelectable

        public void setSelectable​(boolean selectable)
        Defines if the node can be selected within the when a touch event happened.
        Parameters:
        selectable - true if the node can be selected
      • setOnTouchListener

        public void setOnTouchListener​(@Nullable
                                       Node.OnTouchListener onTouchListener)
        Registers a callback to be invoked when a touch event is dispatched to this node. The way that touch events are propagated mirrors the way touches are propagated to Android Views. This is only called when the node is active.

        When an ACTION_DOWN event occurs, that represents the start of a gesture. ACTION_UP or ACTION_CANCEL represents when a gesture ends. When a gesture starts, the following is done:

        • Dispatch touch events to the node that was touched as detected by Scene.hitTest(Ray, boolean) .
        • If the node doesn't consume the event, recurse upwards through the node's parents and dispatch the touch event until one of the node's consumes the event.
        • If no nodes consume the event, the gesture is ignored and subsequent events that are part of the gesture will not be passed to any nodes.
        • If one of the node's consumes the event, then that node will consume all future touch events for the gesture.

        When a touch event is dispatched to a node, the event is first passed to the node's Node.OnTouchListener. If the Node.OnTouchListener doesn't handle the event, it is passed to onTouchEvent(HitTestResult, MotionEvent).

        See Also:
        Node.OnTouchListener
      • setOnTapListener

        public void setOnTapListener​(@Nullable
                                     Node.OnTapListener onTapListener)
        Registers a callback to be invoked when this node is tapped. If there is a callback registered, then touch events will not bubble to this node's parent. If the Node.onTouchEvent is overridden and super.onTouchEvent is not called, then the tap will not occur.
        See Also:
        Node.OnTapListener
      • addLifecycleListener

        public void addLifecycleListener​(Node.LifecycleListener lifecycleListener)
        Adds a listener that will be called when node lifecycle events occur. The listeners will be called in the order in which they were added.
      • removeLifecycleListener

        public void removeLifecycleListener​(Node.LifecycleListener lifecycleListener)
        Removes a listener that will be called when node lifecycle events occur.
      • addTransformChangedListener

        public void addTransformChangedListener​(Node.TransformChangedListener transformChangedListener)
        Adds a listener that will be called when the node's transformation changes.
      • removeTransformChangedListener

        public void removeTransformChangedListener​(Node.TransformChangedListener transformChangedListener)
        Removes a listener that will be called when the node's transformation changes.
      • getLocalScale

        public final Vector3 getLocalScale()
        Gets a copy of the nodes scale relative to its parent (local-space). If isTopLevel() is true, then this is the same as getWorldScale().
        Returns:
        a new vector that represents the node's local-space scale
        See Also:
        setLocalScale(Vector3)
      • getWorldPosition

        public final Vector3 getWorldPosition()
        Get a copy of the nodes world-space position.
        Returns:
        a new vector that represents the node's world-space position
        See Also:
        setWorldPosition(Vector3)
      • getWorldRotation

        public final Quaternion getWorldRotation()
        Gets a copy of the nodes world-space rotation.
        Returns:
        a new quaternion that represents the node's world-space rotation
        See Also:
        setWorldRotation(Quaternion)
      • getWorldScale

        public final Vector3 getWorldScale()
        Gets a copy of the nodes world-space scale. Some precision will be lost if the node is skewed.
        Returns:
        a new vector that represents the node's world-space scale
        See Also:
        setWorldScale(Vector3)
      • setWorldPosition

        public void setWorldPosition​(Vector3 position)
        Sets the world-space position of this node.
        Parameters:
        position - The position to apply.
        See Also:
        getWorldPosition()
      • setWorldRotation

        public void setWorldRotation​(Quaternion rotation)
        Sets the world-space rotation of this node.
        Parameters:
        rotation - The rotation to apply.
        See Also:
        getWorldRotation()
      • setWorldScale

        public void setWorldScale​(Vector3 scale)
        Sets the world-space scale of this node.
        Parameters:
        scale - The scale to apply.
        See Also:
        getWorldScale()
      • localToWorldPoint

        public final Vector3 localToWorldPoint​(Vector3 point)
        Converts a point in the local-space of this node to world-space.
        Parameters:
        point - the point in local-space to convert
        Returns:
        a new vector that represents the point in world-space
      • worldToLocalPoint

        public final Vector3 worldToLocalPoint​(Vector3 point)
        Converts a point in world-space to the local-space of this node.
        Parameters:
        point - the point in world-space to convert
        Returns:
        a new vector that represents the point in local-space
      • localToWorldDirection

        public final Vector3 localToWorldDirection​(Vector3 direction)
        Converts a direction from the local-space of this node to world-space. Not impacted by the position or scale of the node.
        Parameters:
        direction - the direction in local-space to convert
        Returns:
        a new vector that represents the direction in world-space
      • worldToLocalDirection

        public final Vector3 worldToLocalDirection​(Vector3 direction)
        Converts a direction from world-space to the local-space of this node. Not impacted by the position or scale of the node.
        Parameters:
        direction - the direction in world-space to convert
        Returns:
        a new vector that represents the direction in local-space
      • getForward

        public final Vector3 getForward()
        Gets the world-space forward vector (-z) of this node.
        Returns:
        a new vector that represents the node's forward direction in world-space
      • getBack

        public final Vector3 getBack()
        Gets the world-space back vector (+z) of this node.
        Returns:
        a new vector that represents the node's back direction in world-space
      • getRight

        public final Vector3 getRight()
        Gets the world-space right vector (+x) of this node.
        Returns:
        a new vector that represents the node's right direction in world-space
      • getLeft

        public final Vector3 getLeft()
        Gets the world-space left vector (-x) of this node.
        Returns:
        a new vector that represents the node's left direction in world-space
      • getUp

        public final Vector3 getUp()
        Gets the world-space up vector (+y) of this node.
        Returns:
        a new vector that represents the node's up direction in world-space
      • getDown

        public final Vector3 getDown()
        Gets the world-space down vector (-y) of this node.
        Returns:
        a new vector that represents the node's down direction in world-space
      • getRenderable

        @Nullable
        public Renderable getRenderable()
        Gets the renderable to display for this node.
        Returns:
        renderable to display for this node
      • setLight

        public void setLight​(@Nullable
                             Light light)
        Sets the Light to display. To use, first create a Light using Light.Builder. Set the parameters you care about and then attach it to the node using this function. A node may have a renderable and a light or just act as a Light.
        Parameters:
        light - Properties of the Light to render, pass null to remove the light.
      • getLight

        @Nullable
        public Light getLight()
        Gets the current light, which is mutable.
      • setLookDirection

        public final void setLookDirection​(Vector3 lookDirection,
                                           Vector3 upDirection)
        Sets the direction that the node is looking at in world-space. After calling this, getForward() will match the look direction passed in. The up direction will determine the orientation of the node around the direction. The look direction and up direction cannot be coincident (parallel) or the orientation will be invalid.
        Parameters:
        lookDirection - a vector representing the desired look direction in world-space
        upDirection - a vector representing a valid up vector to use, such as Vector3.up()
      • setLookDirection

        public final void setLookDirection​(Vector3 lookDirection)
        Sets the direction that the node is looking at in world-space. After calling this, getForward() will match the look direction passed in. World-space up (0, 1, 0) will be used to determine the orientation of the node around the direction.
        Parameters:
        lookDirection - a vector representing the desired look direction in world-space
      • onActivate

        public void onActivate()
        Handles when this node becomes active. A Node is active if it's enabled, part of a scene, and its parent is active.

        Override to perform any setup that needs to occur when the node is activated.

        See Also:
        isActive(), isEnabled()
      • onDeactivate

        public void onDeactivate()
        Handles when this node becomes inactivate. A Node is inactive if it's disabled, not part of a scene, or its parent is inactive.

        Override to perform any setup that needs to occur when the node is deactivated.

        See Also:
        isActive(), isEnabled()
      • onUpdate

        public void onUpdate​(FrameTime frameTime)
        Handles when this node is updated. A node is updated before rendering each frame. This is only called when the node is active.

        Override to perform any updates that need to occur each frame.

        Parameters:
        frameTime - provides time information for the current frame
      • onTouchEvent

        public boolean onTouchEvent​(HitTestResult hitTestResult,
                                    android.view.MotionEvent motionEvent)
        Handles when this node is touched.

        Override to perform any logic that should occur when this node is touched. The way that touch events are propagated mirrors the way touches are propagated to Android Views. This is only called when the node is active.

        When an ACTION_DOWN event occurs, that represents the start of a gesture. ACTION_UP or ACTION_CANCEL represents when a gesture ends. When a gesture starts, the following is done:

        • Dispatch touch events to the node that was touched as detected by Scene.hitTest(MotionEvent, boolean).
        • If the node doesn't consume the event, recurse upwards through the node's parents and dispatch the touch event until one of the node's consumes the event.
        • If no nodes consume the event, the gesture is ignored and subsequent events that are part of the gesture will not be passed to any nodes.
        • If one of the node's consumes the event, then that node will consume all future touch events for the gesture.

        When a touch event is dispatched to a node, the event is first passed to the node's Node.OnTouchListener. If the Node.OnTouchListener doesn't handle the event, it is passed to onTouchEvent(HitTestResult, MotionEvent).

        Parameters:
        hitTestResult - Represents the node that was touched, and information about where it was touched. On ACTION_DOWN events, HitTestResult.getNode() will always be this node or one of its children. On other events, the touch may have moved causing the HitTestResult.getNode() to change (or possibly be null).
        motionEvent - The motion event.
        Returns:
        True if the event was handled, false otherwise.
      • onTransformChange

        public void onTransformChange​(Node originatingNode)
        Handles when this node's transformation is changed.

        The originating node is the most top-level node in the hierarchy that triggered this node to change. It will always be either the same node or one of its' parents. i.e. if node A's position is changed, then that will trigger onTransformChange(Node) to be called for all of it's children with the originatingNode being node A.

        Parameters:
        originatingNode - the node that triggered this node's transformation to change
      • callOnHierarchy

        public void callOnHierarchy​(java.util.function.Consumer<Node> consumer)
        Traverses the hierarchy and call a method on each node (including this node). Traversal is depth first.
        Overrides:
        callOnHierarchy in class NodeParent
        Parameters:
        consumer - the method to call on each node
      • findInHierarchy

        @Nullable
        public Node findInHierarchy​(java.util.function.Predicate<Node> condition)
        Traverses the hierarchy to find the first node (including this node) that meets a condition. Once the predicate is met, the traversal stops. Traversal is depth first.
        Overrides:
        findInHierarchy in class NodeParent
        Parameters:
        condition - predicate the defines the conditions of the node to search for.
        Returns:
        the first node that matches the conditions of the predicate, otherwise null is returned
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object