25#include <unordered_map>
50 template <CLASS_INHERITS( NodeT, ga::Node )>
51 static std::shared_ptr<NodeT>
create();
52 static std::shared_ptr<Node>
create() {
return Node::create<Node>(); }
57 template <CLASS_INHERITS( NodeT, ga::Node )>
58 std::shared_ptr<NodeT>
addChild( std::shared_ptr<NodeT> child =
nullptr );
61 template <CLASS_INHERITS( NodeT, ga::Node )>
62 std::shared_ptr<NodeT>
insertChild(
size_t index, std::shared_ptr<NodeT> child =
nullptr );
71 void sortChildren( std::function<
bool(
const std::shared_ptr<Node>& a,
const std::shared_ptr<Node>& b )> comparisonFn );
73 bool isParentOf(
const std::shared_ptr<Node>& child )
const;
74 bool isChildOf(
const std::shared_ptr<Node>& parent )
const;
76 bool hasDescendant(
const std::shared_ptr<Node>& node )
const;
79 const std::vector<std::shared_ptr<Node>>&
getChildren()
const;
86 std::shared_ptr<Scene>
getScene()
const;
145 template <CLASS_INHERITS( ComponentT, Component )>
148 template <CLASS_INHERITS( ComponentT, Component )>
151 template <CLASS_INHERITS( ComponentT, Component )>
154 template <CLASS_INHERITS( ComponentT, Component )>
157 template <CLASS_INHERITS( ComponentT, Component )>
172 virtual void setup();
182 void setScene( std::shared_ptr<Scene> scene );
183 void setParent( std::shared_ptr<Node> parent );
189 void walkTree( std::function<
void( std::shared_ptr<Node> )> fn );
196 std::unordered_map<std::type_index, std::shared_ptr<Component>>
m_components;
215template <
class NodeT,
typename>
218 auto node = std::shared_ptr<NodeT>(
new NodeT() );
224template <
class NodeT,
typename>
228 child = create<NodeT>();
233 child->setParent( shared_from_this() );
234 child->setScene(
m_scene.lock() );
240template <
class NodeT,
typename>
244 child = create<NodeT>();
250 child->setParent( shared_from_this() );
251 child->setScene(
m_scene.lock() );
257template <
class ComponentT,
typename>
260 return addComponent( std::shared_ptr<ComponentT>(
new ComponentT() ) );
264template <
class ComponentT,
typename>
268 componentPtr = std::shared_ptr<ComponentT>(
new ComponentT() );
269 auto p = std::make_pair( std::type_index(
typeid( ComponentT ) ), componentPtr );
272 componentPtr->setNode( shared_from_this() );
274 return r.second ? componentPtr :
nullptr;
278template <
class ComponentT,
typename>
281 auto it =
m_components.find( std::type_index(
typeid( ComponentT ) ) );
283 return std::dynamic_pointer_cast<ComponentT>( it->second );
291template <
class ComponentT,
typename>
294 auto it =
m_components.find( std::type_index(
typeid( ComponentT ) ) );
298 component = std::dynamic_pointer_cast<ComponentT>( it->second );
301 removeComponent<ComponentT>();
305 component = createComponent<ComponentT>();
307 throw std::runtime_error(
"Error creating Component of type: " + std::string(
typeid( ComponentT ).name() ) );
314template <
class ComponentT,
typename>
Node represents a basic "node" (or view) in the scenegraph.
Definition: node.h:46
virtual void update()
Definition: node.cpp:173
void detach()
Definition: node.cpp:17
virtual ~Node()
Definition: node.h:168
bool hasChild(const std::shared_ptr< Node > &child) const
Definition: node.h:75
void enableUpdate()
Definition: node.cpp:138
void disableDraw()
Definition: node.cpp:123
std::shared_ptr< ComponentT > createComponent()
Definition: node.h:258
vec3 scenePosToLocal(const vec3 &pos)
Definition: node.h:123
ga::Signal onDidUpdateChildren
Definition: node.h:164
const ga::Uuid & getUuid()
Definition: node.h:97
void updateTree()
Definition: node.cpp:193
bool hasChildren() const
Definition: node.h:81
std::shared_ptr< Node > addChild()
Definition: node.cpp:7
void drawTree()
Definition: node.cpp:219
bool removeComponent()
Definition: node.h:315
std::shared_ptr< Scene > getScene() const
Definition: node.cpp:87
ga::Transform getSceneTransform()
Definition: node.cpp:114
std::shared_ptr< ComponentT > addComponent(std::shared_ptr< ComponentT > component)
Definition: node.h:265
void walkTree(std::function< void(std::shared_ptr< Node >)> fn)
Definition: node.cpp:256
std::unordered_map< std::type_index, std::shared_ptr< Component > > m_components
Definition: node.h:196
ga::Signal onDidDraw
Definition: node.h:165
void setName(const std::string &name)
Definition: node.h:93
void setScene(std::shared_ptr< Scene > scene)
Definition: node.cpp:265
bool isParentOf(const std::shared_ptr< Node > &child) const
Definition: node.cpp:66
std::shared_ptr< NodeT > insertChild(size_t index, std::shared_ptr< NodeT > child=nullptr)
Definition: node.h:241
std::string m_name
Definition: node.h:199
size_t getSceneHierarchyLevel() const
Definition: node.cpp:92
virtual void setup()
Definition: node.cpp:169
void setUuid(const ga::Uuid &uuid)
Definition: node.h:96
void disableUpdate()
Definition: node.cpp:133
size_t getSceneDrawIndex() const
Definition: node.cpp:103
std::shared_ptr< Node > getParent() const
Definition: node.cpp:143
bool isUpdateEnabled() const
Definition: node.h:130
bool hasScene() const
Definition: node.h:85
ComponentT & component()
Definition: node.h:292
bool m_isDrawEnabled
Definition: node.h:207
void resetUpdateFn()
Definition: node.h:110
std::function< void()> m_drawFn
Definition: node.h:203
bool isRoot() const
Definition: node.h:83
void setDrawFn(std::function< void()> drawFn)
Definition: node.h:103
size_t m_drawIndex
Definition: node.h:201
virtual void draw()
Definition: node.cpp:177
ga::Signal onWillDraw
Definition: node.h:165
bool removeChild(std::shared_ptr< Node > child)
Definition: node.cpp:24
virtual void onDrawIndexChange()
Definition: node.cpp:181
bool hasDescendant(const std::shared_ptr< Node > &node) const
Definition: node.cpp:76
void clearChildren()
Definition: node.cpp:52
ga::Uuid m_uuid
Definition: node.h:200
bool isChildOf(const std::shared_ptr< Node > &parent) const
Definition: node.cpp:71
bool m_isUpdateEnabled
Definition: node.h:208
vec3 localPosToScene(const vec3 &pos)
Definition: node.h:121
std::weak_ptr< Node > m_parent
Definition: node.h:192
bool removeDescendant(std::shared_ptr< Node > node)
Definition: node.cpp:41
void setDrawIndex(size_t index)
Definition: node.cpp:185
ga::Signal onDidUpdate
Definition: node.h:163
const std::vector< std::shared_ptr< Node > > & getChildren() const
Definition: node.cpp:148
const std::string & getName() const
Definition: node.h:94
void resetDrawFn()
Definition: node.h:104
std::vector< std::shared_ptr< Node > > m_children
Definition: node.h:193
ga::Signal onWillDrawChildren
Definition: node.h:166
static std::shared_ptr< Node > create()
Definition: node.h:52
void setParent(std::shared_ptr< Node > parent)
Definition: node.cpp:163
void enableDraw()
Definition: node.cpp:128
bool isDrawEnabled() const
Definition: node.h:135
std::weak_ptr< Scene > m_scene
Definition: node.h:191
Node()
Definition: node.cpp:155
bool hasParent() const
Definition: node.h:82
void setUpdateFn(std::function< void()> updateFn)
Definition: node.h:109
ga::Signal onWillUpdate
Definition: node.h:163
std::function< void()> m_updateFn
Definition: node.h:202
ga::Transform & getTransform()
Definition: node.h:99
ga::Signal onWillUpdateChildren
Definition: node.h:164
void sortChildren(std::function< bool(const std::shared_ptr< Node > &a, const std::shared_ptr< Node > &b)> comparisonFn)
Definition: node.cpp:61
static std::shared_ptr< NodeT > create()
ga::Signal onDidDrawChildren
Definition: node.h:166
std::shared_ptr< ComponentT > getComponent()
Definition: node.h:279
Scene is a view controller.
Definition: scene.h:20
Definition: sigslot.hpp:1134
xg::Guid Uuid
Definition: uuid.h:10