Component.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_DCOMPONENT_INCLUDED
34 #define H_DCOMPONENT_INCLUDED
35 
36 #include <Dynacoe/Entity.h>
37 
38 
39 namespace Dynacoe {
40 class Entity;
41 
66 class Component {
67  public:
68 
79  #define DynacoeEvent(_Name_) bool _Name_(void * functionData, Dynacoe::Component * component, Dynacoe::Entity::ID self, Dynacoe::Entity::ID source, const std::vector<std::string> & args)
80 
81 
82 
83 
84  void Step();
85  void Draw();
86 
87  bool draw;
88  bool step;
89 
90  //virtual Component * Clone() {return nullptr;}
91 
95  std::string GetTag() { return tag; };
96 
100  Entity * GetHost() const {return host; };
101 
104  template<typename T>
105  T * GetHostAs() const {return dynamic_cast<T*>(GetHost());}
106 
109  Entity::ID GetHostID() const {return host->GetID();}
110 
114  virtual std::string GetInfo(){return "";}
115 
116 
117 
118 
127 
132  using EventHandler = bool (*)(void *, Component * component, Entity::ID self, Entity::ID source, const std::vector<std::string> & args);
133 
134 
135 
145  bool EmitEvent(const std::string & eventName, Entity::ID source = Entity::ID(), const std::vector<std::string> & args = {});
146 
149  bool CanHandleEvent(const std::string & eventName);
150 
151 
157  void InstallHook(const std::string & eventName, EventHandler, void * data = nullptr);
158 
161  void UninstallHook(const std::string & eventName, EventHandler);
162 
168  void InstallHandler(const std::string & eventName, EventHandler, void * data = nullptr);
169 
172  void UninstallHandler(const std::string & eventName, EventHandler);
173 
174 
177  std::vector<std::string> GetKnownEvents() const;
179  virtual ~Component(){}
180  protected:
181  Component(const std::string &);
182 
183 
187  virtual void OnStep(){};
188 
189 
193  virtual void OnDraw(){};
194 
198  virtual void OnAttach(){};
199 
200 
205  void InstallEvent(const std::string & eventName, EventHandler mainHandler = nullptr, void * data = nullptr);
206 
209  void UninstallEvent(const std::string &);
210 
211  Component();
212  void * operator new(std::size_t);
213  void * operator new[](std::size_t);
214  void operator delete(void * ptr);
215  void operator delete[](void * ptr);
216 
217  private:
218  void SetHost(Entity *);
219 
220  friend class Entity;
221  std::string tag;
222  Entity * host;
223 
224  struct EventSet {
225  std::vector<std::pair<EventHandler, void*>> hooks;
226  std::vector<std::pair<EventHandler, void*>> handlers;
227  };
228  std::unordered_map<std::string, EventSet> handlers;
229 };
230 }
231 
232 
233 #endif
Basic interactive object.
Definition: Entity.h:62
bool CanHandleEvent(const std::string &eventName)
Returns whether there exists at least one handler for the given event.
void InstallHook(const std::string &eventName, EventHandler, void *data=nullptr)
Adds a hook to the event.
virtual void OnAttach()
Function that is called upon a host entity's Attach() function calling. It is run right after the hos...
Definition: Component.h:198
bool EmitEvent(const std::string &eventName, Entity::ID source=Entity::ID(), const std::vector< std::string > &args={})
Triggers the specified event for this EventSystem.
bool(*)(void *, Component *component, Entity::ID self, Entity::ID source, const std::vector< std::string > &args) EventHandler
A handler for an event.
Definition: Component.h:132
ID GetID()
Returns the Entity's unique ID.
T * GetHostAs() const
Convenience function. Equivalent to dynamic_cast(GetHost())
Definition: Component.h:105
void UninstallHook(const std::string &eventName, EventHandler)
Removes a hook added with InstallHook()
void UninstallEvent(const std::string &)
removes a handler of an event
void InstallEvent(const std::string &eventName, EventHandler mainHandler=nullptr, void *data=nullptr)
void InstallHandler(const std::string &eventName, EventHandler, void *data=nullptr)
virtual std::string GetInfo()
Returns a string containing human-readable information on the state of the component.
Definition: Component.h:114
Definition: AssetID.h:37
Entity::ID GetHostID() const
Returns a ID of the host.
Definition: Component.h:109
std::vector< std::string > GetKnownEvents() const
virtual void OnStep()
Function that is called upon each Run iteration. Component Run()s are run before the entity's run fun...
Definition: Component.h:187
std::string GetTag()
Returns a string identifier belonging to the component. Each component implementation should have a u...
Definition: Component.h:95
virtual void OnDraw()
Function that is called upon each Draw iteration. Component Draw()s are run before the entity's draw ...
Definition: Component.h:193
void UninstallHandler(const std::string &eventName, EventHandler)
Removes a handler added with InstallHandler()
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66
Uniquely identifies an Entity.
Definition: Entity.h:67
Entity * GetHost() const
Returns the set host of the component. If no host is set, nullptr is returned.
Definition: Component.h:100