9 #ifndef TurtleBrains_Node_hpp
10 #define TurtleBrains_Node_hpp
12 #include <turtle_brains/core/tb_configuration.hpp>
13 #include <turtle_brains/core/tb_noncopyable.hpp>
14 #include <turtle_brains/core/tb_uuid.hpp>
15 #include <turtle_brains/core/tb_typed_integer.hpp>
16 #include <turtle_brains/core/tb_typed_range.hpp>
17 #include <turtle_brains/core/tb_component.hpp>
22 #include <turtle_brains/math/tb_vector.hpp>
23 #include <turtle_brains/math/tb_matrix.hpp>
33 enum class Space { Local, World };
34 enum class Recursive { No, Yes };
36 enum class NodeKeyType { };
37 using NodeKey = TypedUUID<NodeKeyType>;
40 using NodePointer = std::unique_ptr<Node>;
42 namespace NodeImplementation
46 using ContainerType = std::vector<std::reference_wrapper<NodeType>>;
48 ContainerType mRecursedNodes;
53 RecurseTree(mRecursedNodes, node, recursive);
56 static void RecurseTree(ContainerType& recursedNodes, NodeType& node,
const Recursive recursive = Recursive::Yes);
58 typename ContainerType::const_iterator begin(
void)
const {
return mRecursedNodes.begin(); }
59 typename ContainerType::const_iterator end(
void)
const {
return mRecursedNodes.end(); }
60 typename ContainerType::iterator begin(
void) {
return mRecursedNodes.begin(); }
61 typename ContainerType::iterator end(
void) {
return mRecursedNodes.end(); }
65 const NodePointer& GetRootNode(
void);
66 NodePointer& GetMutableRootNode(
void);
67 void SetRootNode(NodePointer&& rootNode);
72 enum class ChildIndexType :
uint16 { };
75 static constexpr
ChildIndex InvalidChild(
void) {
return ChildIndex::Integer(~0); }
77 explicit Node(
const NodeKey& nodeKey = NodeKey::Invalid());
84 inline bool HasParent(
void)
const {
return nullptr != mParent; }
85 inline Node& GetParent(
void)
const {
return *mParent; }
87 void ClearChildren(
void);
89 ChildIndex AddChild(
const NodeKey& nodeKey = NodeKey::Invalid(),
const String& name =
"");
90 ChildIndex AddChild(std::unique_ptr<Node>&& childNode);
96 inline const ChildIndex GetNumberOfChildren(
void)
const {
return tbCore::RangedCast<ChildIndex::Integer>(mChildren.size()); }
97 inline const Node& GetChild(
const ChildIndex childIndex)
const {
return *mChildren[childIndex]; }
98 inline Node& GetChild(
const ChildIndex childIndex) {
return *mChildren[childIndex]; }
100 using ConstChildContainerAccessor = OtherTypedRange<ChildIndex, const Node&>;
101 using ChildContainerAccessor = OtherTypedRange<ChildIndex, Node&>;
102 inline ConstChildContainerAccessor AllChildren(
void)
const {
103 return ConstChildContainerAccessor(std::bind(&Node::GetNumberOfChildren,
this),
104 std::bind(
static_cast<const Node& (Node::*)(
const ChildIndex) const
>(&Node::GetChild),
this, std::placeholders::_1));
107 inline ChildContainerAccessor AllChildren(
void) {
108 return ChildContainerAccessor(std::bind(&Node::GetNumberOfChildren,
this),
109 std::bind(
static_cast<Node& (Node::*)(
const ChildIndex)
>(&Node::GetChild),
this, std::placeholders::_1));
112 inline NodeImplementation::NodeHierarchyAccessor<const Node> RecurseTree(
const NodeKey& nodeKey, Recursive recursive = Recursive::Yes)
const
114 const Node* startNode = (nodeKey == GetKey()) ?
this : FindChildByKey(nodeKey, recursive);
115 tb_error_if(
nullptr == startNode,
"Expected to find the startNode for RecurseTree().");
116 return NodeImplementation::NodeHierarchyAccessor<const Node>(*startNode, recursive);
119 inline NodeImplementation::NodeHierarchyAccessor<const Node> RecurseTree(Recursive recursive = Recursive::Yes)
const
121 return NodeImplementation::NodeHierarchyAccessor<const Node>(*
this, recursive);
124 inline NodeImplementation::NodeHierarchyAccessor<Node> RecurseTree(
const NodeKey& nodeKey, Recursive recursive = Recursive::Yes)
131 Node* startNode = (nodeKey == GetKey()) ?
this : FindChildByKey(nodeKey, Recursive::Yes);
132 tb_error_if(
nullptr == startNode,
"Expected to find the startNode for RecurseTree().");
133 return NodeImplementation::NodeHierarchyAccessor<Node>(*
this, recursive);
136 inline NodeImplementation::NodeHierarchyAccessor<Node> RecurseTree(Recursive recursive = Recursive::Yes)
138 return NodeImplementation::NodeHierarchyAccessor<Node>(*
this, recursive);
144 const Node* FindChildByName(
const String& childName, Recursive recursive = Recursive::No)
const;
145 Node* FindChildByName(
const String& childName, Recursive recursive = Recursive::No);
146 const Node* FindChildByKey(
const NodeKey& childNodeKey, Recursive recursive = Recursive::No)
const;
147 Node* FindChildByKey(
const NodeKey& childNodeKey, Recursive recursive = Recursive::No);
151 ChildIndex FindChildIndexByName(
const String& childName)
const;
154 ChildIndex FindChildIndexByKey(
const NodeKey& childNodeKey)
const;
161 Component* AddComponent(ComponentPointer&& component);
163 template<
typename ComponentType> ComponentType* AddComponent(
void)
165 ComponentPointer componentPointer = ComponentCreator::CreateComponent<ComponentType>(*
this);
166 if (
nullptr == componentPointer)
171 return dynamic_cast<ComponentType*
>(AddComponent(std::move(componentPointer)));
174 const Component* GetComponent(
const ComponentKey& componentKey)
const;
175 Component* GetComponent(
const ComponentKey& componentKey);
176 const Component* GetComponentByType(
const ComponentTypeKey& componentTypeKey)
const;
177 Component* GetComponentByType(
const ComponentTypeKey& componentTypeKey);
179 template<
typename ComponentType>
const ComponentType* GetComponent(
void)
const
181 return dynamic_cast<const ComponentType*
>(GetComponentByType(ComponentType::ComponentTypeKey()));
184 template<
typename ComponentType> ComponentType* GetComponent(
void)
186 return dynamic_cast<ComponentType*
>(GetComponentByType(ComponentType::ComponentTypeKey()));
189 template<
typename ComponentType>
const std::vector<const ComponentType*> GetComponents(
void)
const
191 std::vector<const ComponentType*> components;
192 components.reserve(mComponents.size());
194 for (
const ComponentPointer& component : mComponents)
196 if (
nullptr == components.emplace_back(
dynamic_cast<const ComponentType*
>(component.get())))
198 components.pop_back();
205 template<
typename ComponentType> std::vector<ComponentType*> GetComponents(
void)
207 std::vector<ComponentType*> components;
208 components.reserve(mComponents.size());
210 for (ComponentPointer& component : mComponents)
212 if (
nullptr == components.emplace_back(
dynamic_cast<ComponentType*
>(component.get())))
214 components.pop_back();
223 inline size_t GetNumberOfComponents(
void)
const {
return mComponents.size(); }
224 const Component& GetComponent(
const size_t componentIndex)
const {
return *mComponents[componentIndex]; }
225 Component& GetMutableComponent(
const size_t componentIndex) {
return *mComponents[componentIndex]; }
228 bool IsActive(
void)
const;
229 inline bool IsActiveSelf(
void)
const {
return mIsActive; }
241 inline const String& GetName(
void)
const {
return mName; }
242 inline void SetName(
const String& name) { mName = name; }
244 inline const NodeKey& GetKey(
void)
const {
return mNodeKey; }
261 inline tbMath::Matrix3 GetOrientation(
const Space space = Space::Local)
const
266 void Rotate(
const tbMath::Vector3& eulerAngles,
const Space space = Space::Local);
267 void Translate(
const tbMath::Vector3& amount,
const Space space = Space::Local);
270 void Update(
const float deltaTime);
271 void Render(
void)
const;
274 virtual void OnAwake(
void);
275 virtual void OnDestroy(
void);
277 virtual void OnActivate(
void);
278 virtual void OnDeactivate(
void);
280 virtual void OnSimulate(
void);
281 virtual void OnUpdate(
const float deltaTime);
282 virtual void OnRender(
void)
const;
286 void Deactivate(
void);
288 void RecomputeTransforms(
void);
290 const NodeKey mNodeKey;
295 std::vector<Node*> mChildren;
296 std::vector<NodePointer> mManagedChildren;
297 std::vector<ComponentPointer> mComponents;
303 template<
typename NodeType>
void NodeImplementation::NodeHierarchyAccessor<NodeType>::RecurseTree(
304 ContainerType& recursedNodes, NodeType& node,
const Recursive recursive)
306 recursedNodes.push_back(node);
308 if (Recursive::Yes == recursive)
310 for (NodeType& child : node.AllChildren())
312 RecurseTree(recursedNodes, child, recursive);
Definition: tb_component.hpp:95
Definition: tb_node.hpp:70
ChildIndex AddChild(Node &childNode)
void RemoveAllComponents(void)
void SetActive(const bool active)
bool HasParent(void) const
Definition: tb_node.hpp:84
Definition: tb_noncopyable.hpp:21
Definition: tb_uuid.hpp:33
Definition: tb_matrix.hpp:156
Definition: tb_matrix.hpp:596
TypedVector3< Type > GetBasis(const size_t &basisIndex) const
Definition: tb_matrix.hpp:948
TypedMatrix3< Type > GetOrientation(void) const
Definition: tb_matrix.hpp:1009
TypedVector3< Type > GetPosition(void) const
Definition: tb_matrix.hpp:978
Definition: tb_vector.hpp:472
#define tb_error_if(errorTest, message,...)
Definition: tb_error.hpp:42
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:125
std::uint16_t uint16
Unsigned integer with a size of 16 bits. Supports values from 0 to 65535.
Definition: tb_types.hpp:25
std::string String
Definition: tb_string.hpp:302
Definition: tb_node.hpp:45
Definition: tb_typed_integer.hpp:49