15 #include "eventHandler.h"
16 #include "eventQueue.h"
17 #include "config_event.h"
21 EventHandler *EventHandler::_global_event_handler = NULL;
30 EventHandler(
EventQueue *ev_queue) : _queue(*ev_queue) {
43 while (!_queue.is_queue_empty()) {
56 nassertv(event != (
Event *)NULL);
60 Hooks::const_iterator hi;
61 hi = _hooks.find(event->get_name());
63 if (hi != _hooks.end()) {
68 Functions::const_iterator fi;
69 for (fi = copy_functions.begin(); fi != copy_functions.end(); ++fi) {
70 if (event_cat.is_spam()) {
72 <<
"calling callback 0x" << (
void*)(*fi)
73 <<
" for event '" <<
event->get_name() <<
"'"
81 CallbackHooks::const_iterator chi;
82 chi = _cbhooks.find(event->get_name());
84 if (chi != _cbhooks.end()) {
88 CallbackFunctions::const_iterator cfi;
89 for (cfi = copy_functions.begin(); cfi != copy_functions.end(); ++cfi) {
90 ((*cfi).first)(event, (*cfi).second);
102 write(ostream &out)
const {
103 Hooks::const_iterator hi;
106 CallbackHooks::const_iterator chi;
107 chi = _cbhooks.begin();
109 while (hi != _hooks.end() && chi != _cbhooks.end()) {
110 if ((*hi).first < (*chi).first) {
111 write_hook(out, *hi);
113 }
else if ((*chi).first < (*hi).first) {
114 write_cbhook(out, *chi);
117 write_hook(out, *hi);
118 write_cbhook(out, *chi);
124 while (hi != _hooks.end()) {
125 write_hook(out, *hi);
129 while (chi != _cbhooks.end()) {
130 write_cbhook(out, *chi);
147 add_hook(
const string &event_name, EventFunction *
function) {
148 if (event_cat.is_debug()) {
150 <<
"adding hook for event '" << event_name
151 <<
"' with function 0x" << (
void*)
function << endl;
153 assert(!event_name.empty());
155 return _hooks[event_name].insert(
function).second;
170 add_hook(
const string &event_name, EventCallbackFunction *
function,
172 assert(!event_name.empty());
174 return _cbhooks[event_name].insert(CallbackFunction(
function, data)).second;
185 assert(!event_name.empty());
186 Hooks::const_iterator hi;
187 hi = _hooks.find(event_name);
188 if (hi != _hooks.end()) {
189 if (!(*hi).second.empty()) {
194 CallbackHooks::const_iterator chi;
195 chi = _cbhooks.find(event_name);
196 if (chi != _cbhooks.end()) {
197 if (!(*chi).second.empty()) {
215 assert(!event_name.empty());
217 return _hooks[event_name].erase(
function) != 0;
230 remove_hook(
const string &event_name, EventCallbackFunction *
function,
232 assert(!event_name.empty());
234 return _cbhooks[event_name].erase(CallbackFunction(
function, data)) != 0;
246 assert(!event_name.empty());
247 bool any_removed =
false;
249 Hooks::iterator hi = _hooks.find(event_name);
250 if (hi != _hooks.end()) {
251 if (!(*hi).second.empty()) {
257 CallbackHooks::iterator chi = _cbhooks.find(event_name);
258 if (chi != _cbhooks.end()) {
259 if (!(*chi).second.empty()) {
276 bool any_removed =
false;
278 CallbackHooks::iterator chi;
279 for (chi = _cbhooks.begin(); chi != _cbhooks.end(); ++chi) {
281 CallbackFunctions::iterator cfi;
284 for (cfi = funcs.begin(); cfi != funcs.end(); ++cfi) {
285 if ((*cfi).second == data) {
288 new_funcs.insert(*cfi);
291 funcs.swap(new_funcs);
315 make_global_event_handler() {
326 write_hook(ostream &out,
const EventHandler::Hooks::value_type &hook)
const {
327 if (!hook.second.empty()) {
328 out << hook.first <<
" has " << hook.second.size() <<
" functions.\n";
338 write_cbhook(ostream &out,
const EventHandler::CallbackHooks::value_type &hook)
const {
339 if (!hook.second.empty()) {
340 out << hook.first <<
" has " << hook.second.size() <<
" callback functions.\n";
void remove_all_hooks()
Removes all hooks assigned to all events.
A class to monitor events from the C++ side of things.
bool has_hook(const string &event_name) const
Returns true if there is any hook added on the indicated event name, false otherwise.
bool add_hook(const string &event_name, EventFunction *function)
Adds the indicated function to the list of those that will be called when the named event is thrown...
static EventQueue * get_global_event_queue()
Returns a pointer to the one global EventQueue object.
virtual void dispatch_event(const Event *)
Calls the hooks assigned to the indicated single event.
A queue of pending events.
bool remove_hooks_with(void *data)
Removes all CallbackFunction hooks that have the indicated pointer as the associated data pointer...
bool remove_hooks(const string &event_name)
Removes all functions from the named event hook.
A named event, possibly with parameters.
This is our own Panda specialization on the default STL set.
bool remove_hook(const string &event_name, EventFunction *function)
Removes the indicated function from the named event hook.
TypeHandle is the identifier used to differentiate C++ class types.
void process_events()
The main processing loop of the EventHandler.