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