java.lang.Object
com.marginallyclever.ro3.node.Node
- Direct Known Subclasses:
Behavior
,DHParameter
,HingeJoint
,LimbPlanner
,LimbSolver
,MarlinRobotArm
,Material
,Motor
,Pose
,RigidBody3D
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addAttachListener
(NodeAttachListener listener) void
Add a child to this node at the given index.void
Append a child to this node.void
addDetachListener
(NodeDetachListener listener) void
addReadyListener
(NodeReadyListener listener) void
addRenameListener
(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.void
fromJSON
(org.json.JSONObject from) Deserialize this node and its children from a JSON object and its children.void
getComponents
(List<JPanel> list) Build a Swing Component that represents this Node.getName()
boolean
boolean
isNameUsedBySibling
(String newName) void
removeAttachListener
(NodeAttachListener listener) void
removeChild
(Node child) void
removeDetachListener
(NodeDetachListener listener) void
removeReadyListener
(NodeReadyListener listener) void
removeRenameListener
(NodeRenameListener listener) void
org.json.JSONObject
toJSON()
Serialize this node and its children to a JSON object and its children.void
update
(double dt) Called every frame.void
Everybody in this tree gets a new unique ID.
-
Constructor Details
-
Node
public Node() -
Node
-
-
Method Details
-
addChild
Append a child to this node.- Parameters:
child
- the child to add.
-
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
- Returns:
- an iterator so that calling class cannot modify the list.
-
findParent
Find the first parent with the given name.- Parameters:
name
- the name to match.- Returns:
- the first parent, or null if none found.
-
findParent
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
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
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
-
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
Build a Swing Component that represents this Node.- Parameters:
list
- the list to add components to.
-
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
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.
-