Panda3D
|
00001 // Filename: eventHandler.h 00002 // Created by: drose (08Feb99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 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 // Class : EventHandler 00030 // Description : A class to monitor events from the C++ side of 00031 // things. It maintains a set of "hooks", function 00032 // pointers assigned to event names, and calls the 00033 // appropriate hooks when the matching event is 00034 // detected. 00035 // 00036 // This class is not necessary when the hooks are 00037 // detected and processed entirely by the scripting 00038 // language, e.g. via Scheme hooks or the messenger 00039 // in Python. 00040 //////////////////////////////////////////////////////////////////// 00041 class EXPCL_PANDA_EVENT EventHandler : public TypedObject { 00042 public: 00043 // Define a function type suitable for receiving events. 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