From dcc8d1d552d8bf35d9307d42dc91ef11bda5512a Mon Sep 17 00:00:00 2001 From: Dave Schuyler Date: Wed, 28 Jan 2004 01:55:03 +0000 Subject: [PATCH] global event handler --- panda/src/event/eventHandler.cxx | 34 +++++++++++++++++++++++++++----- panda/src/event/eventHandler.h | 10 +++++++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/panda/src/event/eventHandler.cxx b/panda/src/event/eventHandler.cxx index 29147d7298..b0e111c40b 100644 --- a/panda/src/event/eventHandler.cxx +++ b/panda/src/event/eventHandler.cxx @@ -22,6 +22,9 @@ TypeHandle EventHandler::_type_handle; +EventHandler *EventHandler::_global_event_handler = 0; + + //////////////////////////////////////////////////////////////////// // Function: EventHandler::Constructor // Access: Public @@ -54,6 +57,7 @@ process_events() { //////////////////////////////////////////////////////////////////// void EventHandler:: dispatch_event(const CPT_Event &event) { + nassertv(!event.is_null()); // Is the event name defined in the hook table? It will be if // anyone has ever assigned a hook to this particular event name. Hooks::const_iterator hi; @@ -66,10 +70,11 @@ dispatch_event(const CPT_Event &event) { Functions::const_iterator fi; for (fi = copy_functions.begin(); fi != copy_functions.end(); ++fi) { - if (event_cat.is_spam()) - event_cat->spam() << "calling callback 0x" << (void*)(*fi) + if (event_cat.is_spam()) { + event_cat->spam() << "calling callback 0x" << (void*)(*fi) << " for event '" << event->get_name() << "'" << endl; + } (*fi)(event); } } @@ -107,11 +112,9 @@ write(ostream &out) const { if ((*hi).first < (*cbhi).first) { write_hook(out, *hi); ++hi; - } else if ((*cbhi).first < (*hi).first) { write_cbhook(out, *cbhi); ++cbhi; - } else { write_hook(out, *hi); write_cbhook(out, *cbhi); @@ -144,9 +147,12 @@ write(ostream &out) const { //////////////////////////////////////////////////////////////////// bool EventHandler:: 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 << "' with function 0x" << (void*)function << endl; + } + assert(!event_name.empty()); + assert(function); return _hooks[event_name].insert(function).second; } @@ -164,6 +170,8 @@ add_hook(const string &event_name, EventFunction *function) { bool EventHandler:: add_hook(const string &event_name, EventCallbackFunction *function, void *data) { + assert(!event_name.empty()); + assert(function); return _cbhooks[event_name].insert(CallbackFunction(function, data)).second; } @@ -175,6 +183,7 @@ add_hook(const string &event_name, EventCallbackFunction *function, //////////////////////////////////////////////////////////////////// bool EventHandler:: has_hook(const string &event_name) const { + assert(!event_name.empty()); Hooks::const_iterator hi; hi = _hooks.find(event_name); if (hi != _hooks.end()) { @@ -204,6 +213,8 @@ has_hook(const string &event_name) const { //////////////////////////////////////////////////////////////////// bool EventHandler:: remove_hook(const string &event_name, EventFunction *function) { + assert(!event_name.empty()); + assert(function); return _hooks[event_name].erase(function) != 0; } @@ -219,6 +230,8 @@ remove_hook(const string &event_name, EventFunction *function) { bool EventHandler:: remove_hook(const string &event_name, EventCallbackFunction *function, void *data) { + assert(!event_name.empty()); + assert(function); return _cbhooks[event_name].erase(CallbackFunction(function, data)) != 0; } @@ -234,6 +247,17 @@ remove_all_hooks() { _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 diff --git a/panda/src/event/eventHandler.h b/panda/src/event/eventHandler.h index 8ad409f024..152b84bd80 100644 --- a/panda/src/event/eventHandler.h +++ b/panda/src/event/eventHandler.h @@ -39,7 +39,8 @@ class EventQueue; // // This class is not necessary when the hooks are // 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 { public: @@ -56,6 +57,8 @@ PUBLISHED: void write(ostream &out) const; + INLINE static EventHandler *get_global_event_handler(EventQueue *queue); + public: bool add_hook(const string &event_name, EventFunction *function); bool add_hook(const string &event_name, EventCallbackFunction *function, @@ -79,6 +82,9 @@ protected: CallbackHooks _cbhooks; EventQueue &_queue; + static EventHandler *_global_event_handler; + static void make_global_event_handler(EventQueue *queue); + private: void write_hook(ostream &out, const Hooks::value_type &hook) const; void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const; @@ -102,4 +108,6 @@ private: static TypeHandle _type_handle; }; +#include "eventHandler.I" + #endif