00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef EVENTHANDLER_H
00016 #define EVENTHANDLER_H
00017
00018 #include "pandabase.h"
00019
00020 #include "event.h"
00021 #include "pt_Event.h"
00022
00023 #include "pset.h"
00024 #include "pmap.h"
00025
00026 class EventQueue;
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 class EXPCL_PANDA_EVENT EventHandler : public TypedObject {
00042 public:
00043
00044 typedef void EventFunction(const Event *);
00045 typedef void EventCallbackFunction(const Event *, void *);
00046
00047 PUBLISHED:
00048 EventHandler(EventQueue *ev_queue);
00049
00050 void process_events();
00051
00052 virtual void dispatch_event(const Event *);
00053
00054 void write(ostream &out) const;
00055
00056 INLINE static EventHandler *get_global_event_handler(EventQueue *queue = NULL);
00057
00058 public:
00059 bool add_hook(const string &event_name, EventFunction *function);
00060 bool add_hook(const string &event_name, EventCallbackFunction *function,
00061 void *data);
00062 bool has_hook(const string &event_name) const;
00063 bool remove_hook(const string &event_name, EventFunction *function);
00064 bool remove_hook(const string &event_name, EventCallbackFunction *function,
00065 void *data);
00066
00067 bool remove_hooks(const string &event_name);
00068 bool remove_hooks_with(void *data);
00069
00070 void remove_all_hooks();
00071
00072 protected:
00073
00074 typedef pset<EventFunction *> Functions;
00075 typedef pmap<string, Functions> Hooks;
00076 typedef pair<EventCallbackFunction*, void*> CallbackFunction;
00077 typedef pset<CallbackFunction> CallbackFunctions;
00078 typedef pmap<string, CallbackFunctions> CallbackHooks;
00079
00080 Hooks _hooks;
00081 CallbackHooks _cbhooks;
00082 EventQueue &_queue;
00083
00084 static EventHandler *_global_event_handler;
00085 static void make_global_event_handler();
00086
00087 private:
00088 void write_hook(ostream &out, const Hooks::value_type &hook) const;
00089 void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const;
00090
00091
00092 public:
00093 static TypeHandle get_class_type() {
00094 return _type_handle;
00095 }
00096 static void init_type() {
00097 TypedObject::init_type();
00098 register_type(_type_handle, "EventHandler",
00099 TypedObject::get_class_type());
00100 }
00101 virtual TypeHandle get_type() const {
00102 return get_class_type();
00103 }
00104 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00105
00106 private:
00107 static TypeHandle _type_handle;
00108 };
00109
00110 #include "eventHandler.I"
00111
00112 #endif