MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eventmanager.h
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _eventmanager_h
41 #define _eventmanager_h
42 ///////////////////////////////////////////////////////////////////////////////
43 //This will capture all event and store them for easy dispatching.
44 //There will be an instance of this class in the world.
45 ///////////////////////////////////////
46 
47 #include "managerbase.h"
48 #include "managerfactory.h"
49 #include "Input/metacode.h"
50 #include "eventbase.h"
51 #include "singleton.h"
52 #include "vector2.h"
53 
54 #ifndef SWIG
55  #include "XML/xml.h"
56 #endif
57 #include "Threading/workunit.h"
58 
59 ///////////////////////////////////////////////////////////////////////////////
60 // Class External << Operators for streaming or assignment
61 namespace Mezzanine
62 {
63  class EventManager; //forward declaration so we can use this in our << and >> operators
64 }
65 
66 /// @brief Serializes the passed Mezzanine::EventManager to XML
67 /// @param stream The ostream to send the xml to.
68 /// @param Mgr the Mezzanine::EventManager to be serialized
69 /// @return this returns the ostream, now with the serialized data
70 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::EventManager& Mgr);
71 
72 /// @brief Deserialize a Mezzanine::EventManager
73 /// @param stream The istream to get the xml from to (re)make the Mezzanine::EventManager.
74 /// @param Mgr the Mezzanine::EventManager to be deserialized.
75 /// @return this returns the ostream, advanced past the Mezzanine::EventManager that was recreated onto Ev.
76 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::EventManager& Mgr);
77 
78 /// @brief Set all values of a Mezzanine::EventManager from parsed xml.
79 /// @param OneNode The istream to get the xml from to (re)make the Mezzanine::EventManager.
80 /// @param Mgr the Mezzanine::EventManager to be reset.
81 /// @return This returns theXML::Node that was passed in.
82 void MEZZ_LIB operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::EventManager& Mgr);
83 
84 namespace Mezzanine
85 {
86  /// Forward Declarations
87  class Entresol;
88  class EventGameWindow;
89  class EventManager;
90  class EventUserInput;
91  class EventQuit;
92 
93  namespace Internal {
94  class EventManagerInternalData;
95  }
96 
97  // Used by the scripting language binder to help create bindgings for this class. SWIG does know to creation template instances
98  #ifdef SWIG
99  %template(SingletonEventManager) Singleton<EventManager>;
100  #endif
101 
102  ///////////////////////////////////////////////////////////////////////////////
103  /// @brief Every frame the OS must be queried for changes to the state, this does that querying on behalf of an eventmanager.
104  /// @details
105  ///////////////////////////////////////
107  {
108  protected:
109  /// @internal
110  /// @brief The Manager this does the work for.
112  /// @internal
113  /// @brief Protected copy constructor. THIS IS NOT ALLOWED.
114  /// @param Other The other work unit being copied from. WHICH WILL NEVER HAPPEN.
115  EventPumpWorkUnit(const EventPumpWorkUnit& Other);
116  /// @internal
117  /// @brief Protected assignment operator. THIS IS NOT ALLOWED.
118  /// @param Other The other work unit being copied from. WHICH WILL NEVER HAPPEN.
119  EventPumpWorkUnit& operator=(const EventPumpWorkUnit& Other);
120  public:
121  /// @brief Create the WorkUnit for getting updates from the OS.
122  /// @param Target The Manager to save this data in.
124  /// @brief Virtual Deconstructor
125  virtual ~EventPumpWorkUnit();
126 
127  ///////////////////////////////////////////////////////////////////////////////
128  // Utility
129 
130  /// @brief This does the actual work of the getting the data form the OS.
131  /// @param CurrentThreadStorage Only really used for the logger.
132  virtual void DoWork(Threading::DefaultThreadSpecificStorage::Type& CurrentThreadStorage);
133  };//EventPumpWorkUnit
134 
135  ///////////////////////////////////////////////////////////////////////////////
136  /// @class EventManager
137  /// @headerfile eventmanager.h
138  /// @brief This is a container for Events and facilitates the transfer of data.
139  /// @details The Event Manager Exists to passed important information about
140  /// Gamestate from where it is generated to where it is needed. It is the Game
141  /// Developers option whether they want to grab events directly using the get
142  /// functions that have filters, or if they want to get all the events at once
143  /// from a central location and dispatch form there. \n
144  /// Since all User input comes in the form of events, this is also where user
145  /// input Polling and optional input sources like Joysticks are controlled
146  /// from. \n
147  /// All of these event are stored in an internal Queue and order is preserved.
148  /// So the First item In will be the First Out (FIFO). This is not strictly a
149  /// FIFO buffer, there are a number of functions for getting of managing
150  /// specific kinds of events. Generally these 'Filtered' management functions
151  /// Still return the first of those kinds of event.
152  /// @warning Delete pointers you get from this. Anything can create events and
153  /// Put them here, and anything can get them out, This means the simple way to
154  /// not cause memory leaks is to have the routines extracting the events delete
155  /// the events.
156  /// @warning Currently this is not thread safe, even though it should be.
157  ///////////////////////////////////////////////////////////////////////////////
159  {
160  private:
161  /// @internal
162  /// @brief All the internal data for this EventManager
164 
165  /// @internal
166  /// @brief Checks for quit messages and adds them to the queue
167  void UpdateQuitEvents();
168 
169  /// @brief Private copy semantics, because copying this will cause problems.
170  /// @param Dummy A dummy argument
171  /// @details Since copying, or having more than one event manager doesn't seem to make sense
172  /// I just made it non-copyable.
173  EventManager(const EventManager& Dummy)
174  {}
175 
176  friend class EventWorkUnit;
177  friend std::ostream& MEZZ_LIB ::operator << (std::ostream& stream, const Mezzanine::EventManager& Mgr);
178  friend std::istream& MEZZ_LIB ::operator >> (std::istream& stream, Mezzanine::EventManager& Mgr);
179  friend void MEZZ_LIB ::operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::EventManager& Mgr);
180 
181  public:
182  /// @brief Default constructor.
183  EventManager();
184  /// @brief XML constructor.
185  /// @param XMLNode The node of the xml document to construct from.
186  EventManager(XML::Node& XMLNode);
187  /// @brief Default Deconstructor.
188  virtual ~EventManager();
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  // Management functions - Work with all events
192 
193  /// @brief Gets a count of events
194  /// @details This returns a total count of all events stored in this EventManager.
195  /// @return This returns an unsigned integer with the amount of of total events
196  size_t GetRemainingEventCount();
197 
198  /// @brief Return a pointer to the Next event
199  /// @details This returns a pointer to the next Event. It is advisable to use this for performance reasons
200  /// because it runs in constant time. However it does not return a specific kind of event, and must be cast
201  /// in order to use the true content. This returns a pointer to 0 if there are no events in the queue.
202  /// @return A pointer to a Event, that still needs to be removed from the event manager and deleted.
203  EventBase* GetNextEvent();
204 
205  /// @brief Return a pointer to the Next event, and removes the Event from storage
206  /// @details This functions just like GetNextEvent , except that it also removes the item from the internal storage
207  /// of the EventManager. This returns a pointer to 0 if there are no events in the que.
208  /// @return A pointer to a Event, that will need to be deleted once it has been used.
209  EventBase* PopNextEvent();
210 
211  /// @brief Removes an Event From the que without looking at it.
212  /// @details This together with GetNextEvent() are the same as call PopNextEvent().
213  /// @warning If you did not call GetNextEvent() and haven't deleted or stored, or somehow dealt with this pointer, then this is a memory leak.
214  /// Don't use this unless you are certain you have taken care of the pointer appropriately
215  /// @exception This can throw any STL exception a que could. Any with likely throw some kind of except if called when there are no Events in the Que.
216  void RemoveNextEvent();
217 
218  /// @brief Adds an event of any kind to the end of the Event Queue.
219  /// @param EventToAdd This is a pointer to an Event.
220  /// @details This adds the existing event to the Queue. Be careful this is not delete, and does not go out of scope. Deleting the Event is now the
221  /// responsibilty of the code that pulls it out of Event Manager.
222  void AddEvent(EventBase* EventToAdd);
223 
224  /// @brief Removes an event of any kind from the Event Queue.
225  /// @param EventToRemove Pointer to the event that will be removed.
226  /// @details In most cases you will want to use the PopNextEvent() methods when going through events. In some expert use cases however you may want
227  /// to remove a specific event at an arbitrary place in the Queue. This is the method for doing so.
228  void RemoveEvent(EventBase* EventToRemove);
229 
230  /// @brief Pulls Events from the all the subsystems for use in the EventManager.
231  /// @details The work this function does is already performed in the main loop. This only really needs to be used
232  /// If a game developer chooses to use his own main loop. This adds system events, like EventQuit and Other Windows manager events,
233  /// and if any user input event actions, this generates one EventUserInput that stores everythin that happened.
234  void UpdateEvents();
235 
236  ///////////////////////////////////////////////////////////////////////////////
237  // Filtered management functions - GameWindow Events
238 
239  /// @brief Returns a pointer to the Next GameWindow event
240  /// @details This Filtered event management function returns a pointer to the next GameWindow event. It is inadvisable to use
241  /// this for performance reasons because it runs in linear time relative to the amount of events. However, it will return an immediately
242  /// usable pointer for case where an extreme level of performance is not required. This returns a pointer to 0 if there are no GameWindow
243  /// events in the que.
244  /// @return A pointer to a EventGameWindow, that still needs to be removed from the event manager and deleted.
245  EventGameWindow* GetNextGameWindowEvent();
246 
247  /// @brief Returns a pointer to the Next GameWindow event and removes it from the Que
248  /// @details This Filtered event management function returns a pointer to the next GameWindow event. It is inadvisable to use
249  /// this for performance reasons because it runs in linear time relative to the amount of events. However, it will return an immediately
250  /// usable pointer for case where an extreme level of performance is not required. This returns a pointer to 0 if there are no GameWindow
251  /// events in the que. This also removes the returned pointer form the Que.
252  /// @return A pointer to a EventGameWindow, that still needs to be removed from the event manager and deleted.
253  EventGameWindow* PopNextGameWindowEvent();
254 
255  /// @brief Removes the First GameWindow Event From the que without looking at it.
256  /// @details This together with GetNextGameWindowEvent() are the pretty much same as call PopNextGameWindowEvent().
257  /// @warning If you did not call GetNextGameWindowEvent() and haven't deleted or stored, or somehow dealt with this pointer, then this is a memory leak.
258  /// Don't use this unless you are certain you have taken care of the pointer appropriately
259  /// @exception This can throw any STL exception a queue could. And with likely throw some kind of except if called when there are no Events in the Que.
260  void RemoveNextGameWindowEvent();
261 
262  /// @brief This returns a complete list of all the Render Time events.
263  /// @details This finds all the EventUserInput Events then creates a new list and returns that. This runs in linear time relative to the amounts of events.
264  /// @return This returns a list<EventGameWindow*> pointer which is this a subset of this classes event pointer list. Use this carefully, it can cause errors if used improperly. This list pointer must be deleted, but not the events in it.
265  std::list<EventGameWindow*>* GetAllGameWindowEvents();
266 
267  ///////////////////////////////////////////////////////////////////////////////
268  // Filtered management functions - User Input Events
269 
270  /// @brief Returns a pointer to the Next User Input event
271  /// @details This Filtered event management function returns a pointer to the next User Input event. It is inadvisable to use
272  /// this for performance reasons because it runs in linear time relative to the amount of events. However, it will return an immediately
273  /// usable pointer for case where an extreme level of performance is not required. This returns a pointer to 0 if there are no User Input
274  /// events in the que.
275  /// @return A pointer to a EventUserInput, that still needs to be removed from the event manager and deleted.
276  EventUserInput* GetNextUserInputEvent();
277 
278  /// @brief Returns a pointer to the Next User Input event and removes it from the Que
279  /// @details This Filtered event management function returns a pointer to the next User Input event. It is inadvisable to use
280  /// this for performance reasons because it runs in linear time relative to the amount of events. However, it will return an immediately
281  /// usable pointer for case where an extreme level of performance is not required. This returns a pointer to 0 if there are no User Input
282  /// events in the que. This also removes the returned pointer form the Que.
283  /// @return A pointer to a EventUserInput, that still needs to be removed from the event manager and deleted.
284  EventUserInput* PopNextUserInputEvent();
285 
286  /// @brief Removes the First User Input Event From the que without looking at it.
287  /// @details This together with GetNextUserInputEvent() are the pretty much same as call PopNextUserInputEvent().
288  /// @warning If you did not call GetNextUserInputEvent() and haven't deleted or stored, or somehow dealt with this pointer, then this is a memory leak.
289  /// Don't use this unless you are certain you have taken care of the pointer appropriately
290  /// @exception This can throw any STL exception a queue could. And with likely throw some kind of except if called when there are no Events in the Que.
291  void RemoveNextUserInputEvent();
292 
293  /// @brief This returns a complete list of all the User Input events.
294  /// @details This finds all the EventUserInput Events then creates a new list and returns that. This runs in linear time relative to the amounts of events.
295  /// @return This returns a std::list<EventUserInput*> pointer which is this a subset of this classes event pointer list. Use this carefully, it can cause errors if used improperly. Additionally this list pointer must be deleted, but not the events in it.
296  std::list<EventUserInput*>* GetAllUserInputEvents();
297 
298  ///////////////////////////////////////////////////////////////////////////////
299  // Filtered management functions - Quit Events
300 
301  /// @brief Returns a pointer to the Next EventQuit
302  /// @details This Filtered event management function returns a pointer to the next EventQuit. It is inadvisable to use
303  /// this for performance reasons because it runs in linear time relative to the amount of events. However, it will return an immediately
304  /// usable pointer for case where an extreme level of performance is not required. This returns a pointer to 0 if there are no EventQuit
305  /// events in the que.
306  /// @return A pointer to a EventQuit, that still needs to be removed from the event manager and deleted.
307  EventQuit* GetNextQuitEvent();
308 
309  /// @brief Returns a pointer to the Next EventQuit and removes it from the Que.
310  /// @details This Filtered event management function returns a pointer to the next EventQuit. It is inadvisable to use
311  /// this for performance reasons because it runs in linear time relative to the amount of events. However, it will return an immediately
312  /// usable pointer for case where an extreme level of performance is not required. This returns a pointer to 0 if there are no EventQuit
313  /// events in the que. This also removes the returned pointer form the Que.
314  /// @return A pointer to a EventQuit, that still needs to be removed from the event manager and deleted.
315  EventQuit* PopNextQuitEvent();
316 
317  /// @brief Removes the First EventQuit From the que without looking at it.
318  /// @details This together with GetNextQuitEvent() are the pretty much same as call PopNextQuitEvent().
319  /// @warning If you did not call GetNextQuitEvent() and haven't deleted or stored, or somehow dealt with this pointer, then this is a memory leak.
320  /// Don't use this unless you are certain you have taken care of the pointer appropriately
321  /// @exception This can throw any STL exception a queue could. And with likely throw some kind of except if called when there are no Events in the Que.
322  void RemoveNextQuitEvent();
323 
324  /// @brief This returns a complete list of all the quit events.
325  /// @details This finds all the EventQuit Events then creates a new list and returns that. This runs in linear time relative to the amounts of events.
326  /// @warning Something is wrong if you have more than a few quit events. These should be checked for in each iteration of the main loop.
327  /// @return This returns a std::list<EventQuit*> pointer which is this a subset of this classes event pointer list. Use this carefully, it can cause errors if used improperly. Additionally this list pointer must be deleted, but not the events in it.
328  std::list<EventQuit*>* GetAllQuitEvents();
329 
330  ///////////////////////////////////////////////////////////////////////////////
331  // Filtered management functions - You choose YAYYYY!!!
332 
333  /// @brief Returns a pointer to the Next kind event of the Specified type
334  /// @param SpecificType This is a Event::EventType that defines the type you want this to work with
335  /// @details This and the other NextSpecificEvent functions are the core of the Event Filtering System. In general the other filtering
336  /// functions call one of these and does very little work on their own. \n This performs a linear search starting with the oldest (first entered
337  /// Events) and simply checks if it the of the correct type. Then this returns a pointer to the next event of the specified type, or returns
338  /// a pointer to 0 if there are none of the correct pointers in the Que. It is inadvisable to use
339  /// this for performance reasons because it runs in linear time relative to the amount of events.
340  /// @return A pointer to a EventUserInput, that still needs to be removed from the event manager and deleted.
341  EventBase* GetNextSpecificEvent(EventBase::EventType SpecificType);
342 
343  /// @brief Returns a pointer to the Next kind event of the Specified type, and removes it from the Que
344  /// @param SpecificType This is a Event::EventType that defines the type you want this to work with
345  /// @details This is just like GetNextSpecificEvent(Event::EventType SpecificType) but it also removes the item from the Que.
346  /// @return A pointer to a EventUserInput, that still needs to be removed from the event manager and deleted.
347  EventBase* PopNextSpecificEvent(EventBase::EventType SpecificType);
348 
349  /// @brief Returns a pointer to the Next kind event of the Specified type, and removes it from the Que
350  /// @param SpecificType This is a Event::EventType that defines the type you want this to work with
351  /// @details This is just like PopNextSpecificEvent(Event::EventType SpecificType) but exept it doesn't bother with any of the needed
352  /// structure involved with returning data, and just removes the specific event from the Queue.
353  /// @warning If you did not call GetNextSpecificEvent(Event::EventType SpecificType) and haven't deleted or stored, or somehow dealt with
354  /// this pointer, then this is a memory leak. Don't use this unless you are certain you have taken care of the pointer appropriately.
355  /// @exception This can throw any STL exception a queue could. And with likely throw some kind of except if called when there are no Events in the Que.
356  void RemoveNextSpecificEvent(EventBase::EventType SpecificType);
357 
358  /// @brief This returns a complete list of all the specified events.
359  /// @details This finds all the events that are of the specified type in the event manager, then creates a new list
360  /// and return that. This runs in linear time relative to the amounts of events.
361  /// @warning The pointers contained in this list must be used carefully. Do not delete them, this will cause errors.
362  /// @return This returns a std::list<EventBase*> pointer which is this a subset of this classes event pointer list. Use this carefully, it can cause errors if used improperly. Additionally this list pointer must be deleted, but not the events in it.
363  std::list<EventBase*>* GetAllSpecificEvents(EventBase::EventType SpecificType);
364 
365  /// @brief This removes all the events of the specified type.
366  /// @details This finds all the events that are of the specified type in the event manager, then removes them.
367  /// @warning This does not delete the events. This is a memory leak unless used with GetAllSpecificEvents so that the events can be tracked indeendantly, and deleted.
368  void RemoveAllSpecificEvents(EventBase::EventType SpecificType);
369 
370  ///////////////////////////////////////////////////////////////////////////////
371  // Polling management functions
372 
373  /// @brief Generates extra events each iteration of the main loop, based on user input polling
374  /// @param InputToTryPolling This accepts a MetaCode and will try to watch for occurences like this one
375  /// @details This will trigger the input system to generate an event (or add to an exiting event) when polling for the given kind of event. Each
376  /// Iteration of the main loop there will be a EventUserInput that created. That Event will Include all the normal metacodes for user input
377  /// that happened, and it will also have a meta code for each time this function was called. The added metacode may be partialky ignored, the
378  /// Metavalue is almost always ignored, and in a situation where the can only be one of a given input on a system, the ID is ignore and 0 is assumed.
379  /// @exception "Unsupported Polling Check on this Platform" When the metacode passed cannot be polled on this platform
380  void AddPollingCheck(const Input::MetaCode& InputToTryPolling);
381 
382  /// @brief Removes Events from the list(s) of what needs to be polled
383  /// @param InputToStopPolling This accepts a MetaCode and will try to Remove Watches like this one
384  /// @details This will remove any check for polling that share the same inputcode and ID. This
385  /// @exception "Polling check not present" Is thrown
386  void RemovePollingCheck(const Input::MetaCode& InputToStopPolling);
387 
388  ///////////////////////////////////////////////////////////////////////////////
389  // Utility
390 
391  /// @copydoc ManagerBase::Initialize()
392  virtual void Initialize();
393  /// @copydoc ManagerBase::Deinitialize()
394  virtual void Deinitialize();
395 
396  /// @brief Gets the work unit responsible for pumping events from the OS.
397  /// @return Returns a pointer to the ActorUpdateWorkUnit used by this manager.
398  EventPumpWorkUnit* GetEventPumpWork();
399 
400  ///////////////////////////////////////////////////////////////////////////////
401  // Type Identifier Methods
402 
403  /// @copydoc ManagerBase::GetInterfaceType()
404  virtual ManagerType GetInterfaceType() const;
405  /// @copydoc ManagerBase::GetImplementationTypeName()
406  virtual String GetImplementationTypeName() const;
407  };//EventManager
408 
409  ///////////////////////////////////////////////////////////////////////////////
410  /// @class DefaultEventManagerFactory
411  /// @headerfile eventmanager.h
412  /// @brief A factory responsible for the creation and destruction of the default eventmanager.
413  ///////////////////////////////////////
415  {
416  public:
417  /// @brief Class constructor.
419  /// @brief Class destructor.
420  virtual ~DefaultEventManagerFactory();
421 
422  /// @copydoc ManagerFactory::GetManagerTypeName()
423  String GetManagerTypeName() const;
424 
425  /// @copydoc ManagerFactory::CreateManager(NameValuePairList&)
426  ManagerBase* CreateManager(NameValuePairList& Params);
427  /// @copydoc ManagerFactory::CreateManager(XML::Node&)
428  ManagerBase* CreateManager(XML::Node& XMLNode);
429  /// @copydoc ManagerFactory::DestroyManager(ManagerBase*)
430  void DestroyManager(ManagerBase* ToBeDestroyed);
431  };//DefaultEventManagerFactory
432 }//Mezzanine
433 
434 #endif