Oxygine  1
2g game engine
EventDispatcher.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include <list>
4 #include "core/Object.h"
5 #include "closure/closure.h"
6 
7 namespace oxygine
8 {
9 #define makefourcc(a,b,c,d) ( ((unsigned int)a) | (((unsigned int)b)<< 8) | (((unsigned int)c)<<16) | (((unsigned int)d)<<24))
10 
11 #ifdef OX_HAS_CPP11
12  inline int error_eventID_should_be_size_of_4_chars(int x) { return x; }
13  constexpr size_t constStringLength(const char* str) { return (*str == 0) ? 0 : constStringLength(str + 1) + 1; }
14  constexpr int EventIDc11(const char* str) { return constStringLength(str) == 4 ? makefourcc(str[0], str[1], str[2], str[3]) : error_eventID_should_be_size_of_4_chars(0); }
15 #endif
16 
17  //eventID('_', '_', '_', '_')
18 #define eventID(a,b,c,d) makefourcc(a,b,c,d)
19 
20  /*sysEventID is used for system Oxygine events, use 'eventID' for custom game events*/
21 #define sysEventID(b,c,d) makefourcc(0xA,b,c,d)
22 
23 #define EventID(str) EventIDc11(str)
24 
25 
26  DECLARE_SMART(EventDispatcher, spEventDispatcher);
27  class EventDispatcher: public Object
28  {
29  INHERITED(Object);
30  public:
31  EventDispatcher(const EventDispatcher& ed): inherited(ed), _lastID(0), _listeners(0) {}
33  ~EventDispatcher();
34 
35  int addEventListener(eventType, const EventCallback&);
36 
38  void removeEventListener(eventType, const EventCallback&);
39 
41  void removeEventListener(int id);
42 
44  bool hasEventListeners(void* CallbackThis);
45 
47  bool hasEventListeners(eventType, const EventCallback&);
48 
50  void removeEventListeners(void* CallbackThis);
51 
54 
55  virtual void dispatchEvent(Event* event);
56 
57  int getListenersCount() const;
58  int getLastListenerID() const { return _lastID; }
59 
60  const EventCallback& getListenerByIndex(int index) const { return (*_listeners)[index].cb; }
61 
62 
63  protected:
64 
65  struct listenerbase
66  {
67  EventCallback cb;
68  int id;
69  };
70 
71  struct listener : public listenerbase
72  {
73  eventType type;
74  };
75 
76  int _lastID;
77 
78  typedef std::vector<listener> listeners;
79  listeners* _listeners;
80  };
81 }
void removeEventListeners(void *CallbackThis)
Definition: Event.h:10
bool hasEventListeners(void *CallbackThis)
–oxgl-end–!
Definition: Actor.h:14
Definition: Object.h:83
Definition: EventDispatcher.h:65
Definition: EventDispatcher.h:71
void removeEventListener(eventType, const EventCallback &)
Definition: EventDispatcher.h:27