java.lang.Object
com.marginallyclever.ro3.node.Node
- Direct Known Subclasses:
Behavior,BehaviorTreeRunner,Brain,Crab,DHParameter,Environment,HingeJoint,LegLimbic,LimbPlanner,LimbSolver,LinearJoint,MarlinRobot,Material,Motor,NetworkSession,Neuron,Pose,RandomHemisphereTest,Synapse
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:
NodeAttachListener: called when a node is attached to a parent.NodeDetachListener: called when a node is detached from a parent.NodeReadyListener: called when a node is attached and all children are ready.NodeRenameListener: called when a node is renamed.
Nodes can be serialized to and from JSON.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttachListener(NodeAttachListener listener) voidAdd a child to this node at the given index.voidAppend a child to this node.voidaddDetachListener(NodeDetachListener listener) voidaddReadyListener(NodeReadyListener listener) voidaddRenameListener(NodeRenameListener listener) findByPath(String path) Find the node in the tree with the given path.Find the first child of this node with the given name.Find the first child of this node with the given name.<T extends Node>
TfindFirstChild(Class<T> type) Find the first child of the given type.<T extends Node>
TfindFirstSibling(Class<T> type) Find the first sibling of the given type.<T extends Node>
TfindNodeByID(String nodeID, Class<T> type) Depth-first search for a node with a matching ID and type.<T extends Node>
TfindParent(Class<T> type) Find the first parent of the given type.findParent(String name) Find the first parent with the given name.voidfromJSON(org.json.JSONObject from) Deserialize this node and its children from a JSON object and its children.voidgetComponents(List<JPanel> list) Build a Swing Component that represents this Node.getIcon()Set a custom icon for this node.getName()booleanbooleanisNameUsedBySibling(String newName) voidremoveAttachListener(NodeAttachListener listener) voidremoveChild(Node child) voidremoveDetachListener(NodeDetachListener listener) voidremoveReadyListener(NodeReadyListener listener) voidremoveRenameListener(NodeRenameListener listener) voidorg.json.JSONObjecttoJSON()Serialize this node and its children to a JSON object and its children.voidupdate(double dt) Called every frame.voidEverybody in this tree gets a new unique ID.
-
Constructor Details
-
Node
public Node() -
Node
-
-
Method Details
-
addChild
-
addChild
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
-
getName
-
getParent
-
getUniqueID
- Returns:
- the unique ID of this node.
-
setName
- Parameters:
name- the new name of this node.- Throws:
IllegalArgumentException- if the new name is already used by a sibling.
-
getChildren
-
findParent
-
findParent
-
findChild
-
findChild
-
getRootNode
-
getAbsolutePath
- Returns:
- the absolute path to this node.
-
addAttachListener
-
removeAttachListener
-
addDetachListener
-
removeDetachListener
-
addReadyListener
-
removeReadyListener
-
addRenameListener
-
removeRenameListener
-
update
public void update(double dt) Called every frame.- Parameters:
dt- the time since the last frame.
-
getComponents
-
isNameUsedBySibling
- Parameters:
newName- the new name to check- Returns:
- true if the new name is already used by a sibling
-
findFirstChild
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
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
- 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
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 fortype- the type of node to search for- Returns:
- the first node found with a matching ID and type, or null if none found.
-
findByPath
-
getIcon
-