global event handler

This commit is contained in:
Dave Schuyler 2004-01-28 01:55:03 +00:00
parent ffb5bfdc66
commit dcc8d1d552
2 changed files with 38 additions and 6 deletions

View File

@ -22,6 +22,9 @@
TypeHandle EventHandler::_type_handle; TypeHandle EventHandler::_type_handle;
EventHandler *EventHandler::_global_event_handler = 0;
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: EventHandler::Constructor // Function: EventHandler::Constructor
// Access: Public // Access: Public
@ -54,6 +57,7 @@ process_events() {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
void EventHandler:: void EventHandler::
dispatch_event(const CPT_Event &event) { dispatch_event(const CPT_Event &event) {
nassertv(!event.is_null());
// Is the event name defined in the hook table? It will be if // Is the event name defined in the hook table? It will be if
// anyone has ever assigned a hook to this particular event name. // anyone has ever assigned a hook to this particular event name.
Hooks::const_iterator hi; Hooks::const_iterator hi;
@ -66,10 +70,11 @@ dispatch_event(const CPT_Event &event) {
Functions::const_iterator fi; Functions::const_iterator fi;
for (fi = copy_functions.begin(); fi != copy_functions.end(); ++fi) { for (fi = copy_functions.begin(); fi != copy_functions.end(); ++fi) {
if (event_cat.is_spam()) if (event_cat.is_spam()) {
event_cat->spam() << "calling callback 0x" << (void*)(*fi) event_cat->spam() << "calling callback 0x" << (void*)(*fi)
<< " for event '" << event->get_name() << "'" << " for event '" << event->get_name() << "'"
<< endl; << endl;
}
(*fi)(event); (*fi)(event);
} }
} }
@ -107,11 +112,9 @@ write(ostream &out) const {
if ((*hi).first < (*cbhi).first) { if ((*hi).first < (*cbhi).first) {
write_hook(out, *hi); write_hook(out, *hi);
++hi; ++hi;
} else if ((*cbhi).first < (*hi).first) { } else if ((*cbhi).first < (*hi).first) {
write_cbhook(out, *cbhi); write_cbhook(out, *cbhi);
++cbhi; ++cbhi;
} else { } else {
write_hook(out, *hi); write_hook(out, *hi);
write_cbhook(out, *cbhi); write_cbhook(out, *cbhi);
@ -144,9 +147,12 @@ write(ostream &out) const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
bool EventHandler:: bool EventHandler::
add_hook(const string &event_name, EventFunction *function) { add_hook(const string &event_name, EventFunction *function) {
if (event_cat.is_debug()) if (event_cat.is_debug()) {
event_cat.debug() << "adding hook for event '" << event_name event_cat.debug() << "adding hook for event '" << event_name
<< "' with function 0x" << (void*)function << endl; << "' with function 0x" << (void*)function << endl;
}
assert(!event_name.empty());
assert(function);
return _hooks[event_name].insert(function).second; return _hooks[event_name].insert(function).second;
} }
@ -164,6 +170,8 @@ add_hook(const string &event_name, EventFunction *function) {
bool EventHandler:: bool EventHandler::
add_hook(const string &event_name, EventCallbackFunction *function, add_hook(const string &event_name, EventCallbackFunction *function,
void *data) { void *data) {
assert(!event_name.empty());
assert(function);
return _cbhooks[event_name].insert(CallbackFunction(function, data)).second; return _cbhooks[event_name].insert(CallbackFunction(function, data)).second;
} }
@ -175,6 +183,7 @@ add_hook(const string &event_name, EventCallbackFunction *function,
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
bool EventHandler:: bool EventHandler::
has_hook(const string &event_name) const { has_hook(const string &event_name) const {
assert(!event_name.empty());
Hooks::const_iterator hi; Hooks::const_iterator hi;
hi = _hooks.find(event_name); hi = _hooks.find(event_name);
if (hi != _hooks.end()) { if (hi != _hooks.end()) {
@ -204,6 +213,8 @@ has_hook(const string &event_name) const {
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
bool EventHandler:: bool EventHandler::
remove_hook(const string &event_name, EventFunction *function) { remove_hook(const string &event_name, EventFunction *function) {
assert(!event_name.empty());
assert(function);
return _hooks[event_name].erase(function) != 0; return _hooks[event_name].erase(function) != 0;
} }
@ -219,6 +230,8 @@ remove_hook(const string &event_name, EventFunction *function) {
bool EventHandler:: bool EventHandler::
remove_hook(const string &event_name, EventCallbackFunction *function, remove_hook(const string &event_name, EventCallbackFunction *function,
void *data) { void *data) {
assert(!event_name.empty());
assert(function);
return _cbhooks[event_name].erase(CallbackFunction(function, data)) != 0; return _cbhooks[event_name].erase(CallbackFunction(function, data)) != 0;
} }
@ -234,6 +247,17 @@ remove_all_hooks() {
_cbhooks.clear(); _cbhooks.clear();
} }
////////////////////////////////////////////////////////////////////
// Function: EventHandler::make_global_event_handler
// Access: Protected, Static
// Description:
////////////////////////////////////////////////////////////////////
void EventHandler::
make_global_event_handler(EventQueue *queue) {
assert(queue);
_global_event_handler = new EventHandler(queue);
}
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Function: EventHandler::write_hook // Function: EventHandler::write_hook

View File

@ -39,7 +39,8 @@ class EventQueue;
// //
// This class is not necessary when the hooks are // This class is not necessary when the hooks are
// detected and processed entirely by the scripting // detected and processed entirely by the scripting
// language, e.g. via Scheme hooks. // language, e.g. via Scheme hooks or the messenger
// in Python.
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
class EXPCL_PANDAEXPRESS EventHandler : public TypedObject { class EXPCL_PANDAEXPRESS EventHandler : public TypedObject {
public: public:
@ -56,6 +57,8 @@ PUBLISHED:
void write(ostream &out) const; void write(ostream &out) const;
INLINE static EventHandler *get_global_event_handler(EventQueue *queue);
public: public:
bool add_hook(const string &event_name, EventFunction *function); bool add_hook(const string &event_name, EventFunction *function);
bool add_hook(const string &event_name, EventCallbackFunction *function, bool add_hook(const string &event_name, EventCallbackFunction *function,
@ -79,6 +82,9 @@ protected:
CallbackHooks _cbhooks; CallbackHooks _cbhooks;
EventQueue &_queue; EventQueue &_queue;
static EventHandler *_global_event_handler;
static void make_global_event_handler(EventQueue *queue);
private: private:
void write_hook(ostream &out, const Hooks::value_type &hook) const; void write_hook(ostream &out, const Hooks::value_type &hook) const;
void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const; void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const;
@ -102,4 +108,6 @@ private:
static TypeHandle _type_handle; static TypeHandle _type_handle;
}; };
#include "eventHandler.I"
#endif #endif