java.lang.Object
com.marginallyclever.ro3.node.Node
Direct Known Subclasses:
Behavior, DHParameter, HingeJoint, LimbPlanner, LimbSolver, MarlinRobotArm, Material, Motor, Pose, RigidBody3D

public class Node extends Object

Node is the base class for all nodes in the scene tree.

Each Node can have a parent and multiple children, forming a tree-like structure. This class provides several functionalities:

  • Nodes have a unique IDs and a name.
  • Nodes can be attached or detached from their parents.
  • Nodes can be renamed.
  • Nodes can be serialized to and from JSON format.
  • Nodes can be updated every frame, which is useful for animations or game logic.
  • Nodes can be searched by their name or type.
  • Nodes can be found by their absolute path in the tree.
  • Nodes can be checked if their name is used by a sibling.
  • Nodes can be found by their unique ID.
  • Nodes can be found by their path, which can be relative or absolute.

This class also provides several events:

Nodes can be serialized to and from JSON.

  • Constructor Details

    • Node

      public Node()
    • Node

      public Node(String name)
  • Method Details

    • addChild

      public void addChild(Node child)
      Append a child to this node.
      Parameters:
      child - the child to add.
    • addChild

      public void addChild(int index, Node child)
      Add a child to this node at the given index.
      Parameters:
      index - the index to add the child at.
      child - the child to add.
    • removeChild

      public void removeChild(Node child)
    • getName

      public String getName()
    • getParent

      public Node getParent()
    • getUniqueID

      public String getUniqueID()
      Returns:
      the unique ID of this node.
    • setName

      public void setName(String name)
      Parameters:
      name - the new name of this node.
      Throws:
      IllegalArgumentException - if the new name is already used by a sibling.
    • getChildren

      public List<Node> getChildren()
      Returns:
      an iterator so that calling class cannot modify the list.
    • findParent

      public Node findParent(String name)
      Find the first parent with the given name.
      Parameters:
      name - the name to match.
      Returns:
      the first parent, or null if none found.
    • findParent

      public <T extends Node> T findParent(Class<T> type)
      Find the first parent of the given type.
      Parameters:
      type - the type of node to find
      Returns:
      the first parent of the given type, or null if none found.
    • findChild

      public Node findChild(String name)
      Find the first child of this node with the given name.
      Parameters:
      name - the name to match.
      Returns:
      the child, or null if none found.
    • findChild

      public Node findChild(String name, int maxDepth)
      Find the first child of this node with the given name.
      Parameters:
      name - the name to match.
      maxDepth - the maximum depth to search.
      Returns:
      the child, or null if none found.
    • getRootNode

      public Node getRootNode()
    • getAbsolutePath

      public String getAbsolutePath()
      Returns:
      the absolute path to this node.
    • addAttachListener

      public void addAttachListener(NodeAttachListener listener)
    • removeAttachListener

      public void removeAttachListener(NodeAttachListener listener)
    • addDetachListener

      public void addDetachListener(NodeDetachListener listener)
    • removeDetachListener

      public void removeDetachListener(NodeDetachListener listener)
    • addReadyListener

      public void addReadyListener(NodeReadyListener listener)
    • removeReadyListener

      public void removeReadyListener(NodeReadyListener listener)
    • addRenameListener

      public void addRenameListener(NodeRenameListener listener)
    • removeRenameListener

      public void removeRenameListener(NodeRenameListener listener)
    • update

      public void update(double dt)
      Called every frame.
      Parameters:
      dt - the time since the last frame.
    • getComponents

      public void getComponents(List<JPanel> list)
      Build a Swing Component that represents this Node.
      Parameters:
      list - the list to add components to.
    • isNameUsedBySibling

      public boolean isNameUsedBySibling(String newName)
      Parameters:
      newName - the new name to check
      Returns:
      true if the new name is already used by a sibling
    • findFirstChild

      public <T extends Node> T findFirstChild(Class<T> type)
      Find the first child of the given type. The type must be an exact match - it will not match subclasses.
      Type Parameters:
      T - the type of node to find
      Parameters:
      type - the type of node to find
      Returns:
      the first sibling of the given type, or null if none found.
    • findFirstSibling

      public <T extends Node> T findFirstSibling(Class<T> type)
      Find the first sibling of the given type. The type must be an exact match - it will not match subclasses.
      Type Parameters:
      T - the type of node to find
      Parameters:
      type - the type of node to find
      Returns:
      the first sibling of the given type, or null if none found.
    • toJSON

      public org.json.JSONObject toJSON()
      Serialize this node and its children to a JSON object and its children. Classes that override this method should call super.toJSON() first, then add to the object returned.
      Returns:
      the JSON object.
    • fromJSON

      public void fromJSON(org.json.JSONObject from)
      Deserialize this node and its children from a JSON object and its children. Classes that override this method should call super.fromJSON(). When they do it will trigger the creation of child nodes. The child nodes will then call their own fromJSON() methods.
      Parameters:
      from - the JSON object to read from.
    • hasParent

      public boolean hasParent(Node subject)
      Parameters:
      subject - the node to search for
      Returns:
      true if the given node is a parent of this node.
    • witnessProtection

      public void witnessProtection()
      Everybody in this tree gets a new unique ID.
    • findNodeByID

      public <T extends Node> T findNodeByID(String nodeID, Class<T> type)
      Depth-first search for a node with a matching ID and type. Type match can be any subclass.
      Type Parameters:
      T - the type of node to search for
      Parameters:
      nodeID - the ID to search for
      type - the type of node to search for
      Returns:
      the first node found with a matching ID and type, or null if none found.
    • findByPath

      public Node findByPath(String path)
      Find the node in the tree with the given path.
      Parameters:
      path - the path to the node. can be relative or absolute. understands ".." to go up one level.
      Returns:
      the node, or null if none found.