UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
element.h
Go to the documentation of this file.
1 #ifndef UGDK_ACTION_3D_ELEMENT_H_
2 #define UGDK_ACTION_3D_ELEMENT_H_
3 
4 #include <memory>
5 #include <string>
6 #include <typeindex>
7 #include <unordered_map>
8 
9 namespace Ogre {
10 class SceneNode;
11 }
12 
13 namespace ugdk {
14 namespace action {
15 namespace mode3d {
16 
17 class Scene3D;
18 class Component;
19 
26 class Element : public std::enable_shared_from_this<Element> {
27  public:
28  Element(Scene3D& scene, const std::string& name="");
29  virtual ~Element();
30 
31  // to make this class uncopyable
32  Element& operator=(const Element&) = delete;
33  Element(const Element&) = delete;
34 
35  void AddComponent(const std::shared_ptr<Component> &the_component);
36  template <class T>
37  T* component();
38 
39  void AttachTo(Element& parent);
40  void AttachTo(Scene3D& scene);
41  void Destroy();
42  bool marked_for_removal() const { return marked_for_removal_; }
43 
44  Ogre::SceneNode& node() { return *node_; }
45  Scene3D& scene() { return scene_; }
46  std::string name() const { return name_; }
47 
48  protected:
49  virtual void OnAttach() {}
50  virtual void OnParentDestroyed() {}
51 
52  private:
53  std::unordered_map<std::type_index, std::shared_ptr<Component>> components_;
54  Ogre::SceneNode *node_;
55  Scene3D& scene_;
56  std::string name_;
57 
58  friend Scene3D;
59  bool marked_for_removal_;
60 }; // class Element.
61 
62 template <class T>
64  auto check = components_.find(typeid(T));
65  if (check == components_.end()) {
66  return nullptr;
67  }
68  return dynamic_cast<T*>(check->second.get());
69 }
70 
71 } // namespace 3D
72 } // namespace action
73 } // namespace ugdk
74 
75 #endif /* UGDK_ACTION_3D_ELEMENT_H_ */
bool marked_for_removal() const
Definition: element.h:42
A 3D Element.
Definition: element.h:26
virtual void OnParentDestroyed()
Definition: element.h:50
std::string name() const
Definition: element.h:46
Definition: animation.h:11
T * component()
Definition: element.h:63
Scene3D & scene()
Definition: element.h:45
virtual void OnAttach()
Definition: element.h:49
void AddComponent(const std::shared_ptr< Component > &the_component)
Element(Scene3D &scene, const std::string &name="")
void AttachTo(Element &parent)
Element & operator=(const Element &)=delete
Definition: camera.h:8
Ogre::SceneNode & node()
Definition: element.h:44
A 3D game scene, or screen.
Definition: scene3d.h:32