Skip to content

Commit dcc8d1d

Browse files
committed
global event handler
1 parent ffb5bfd commit dcc8d1d

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

panda/src/event/eventHandler.cxx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
TypeHandle EventHandler::_type_handle;
2424

25+
EventHandler *EventHandler::_global_event_handler = 0;
26+
27+
2528
////////////////////////////////////////////////////////////////////
2629
// Function: EventHandler::Constructor
2730
// Access: Public
@@ -54,6 +57,7 @@ process_events() {
5457
////////////////////////////////////////////////////////////////////
5558
void EventHandler::
5659
dispatch_event(const CPT_Event &event) {
60+
nassertv(!event.is_null());
5761
// Is the event name defined in the hook table? It will be if
5862
// anyone has ever assigned a hook to this particular event name.
5963
Hooks::const_iterator hi;
@@ -66,10 +70,11 @@ dispatch_event(const CPT_Event &event) {
6670

6771
Functions::const_iterator fi;
6872
for (fi = copy_functions.begin(); fi != copy_functions.end(); ++fi) {
69-
if (event_cat.is_spam())
70-
event_cat->spam() << "calling callback 0x" << (void*)(*fi)
73+
if (event_cat.is_spam()) {
74+
event_cat->spam() << "calling callback 0x" << (void*)(*fi)
7175
<< " for event '" << event->get_name() << "'"
7276
<< endl;
77+
}
7378
(*fi)(event);
7479
}
7580
}
@@ -107,11 +112,9 @@ write(ostream &out) const {
107112
if ((*hi).first < (*cbhi).first) {
108113
write_hook(out, *hi);
109114
++hi;
110-
111115
} else if ((*cbhi).first < (*hi).first) {
112116
write_cbhook(out, *cbhi);
113117
++cbhi;
114-
115118
} else {
116119
write_hook(out, *hi);
117120
write_cbhook(out, *cbhi);
@@ -144,9 +147,12 @@ write(ostream &out) const {
144147
////////////////////////////////////////////////////////////////////
145148
bool EventHandler::
146149
add_hook(const string &event_name, EventFunction *function) {
147-
if (event_cat.is_debug())
150+
if (event_cat.is_debug()) {
148151
event_cat.debug() << "adding hook for event '" << event_name
149152
<< "' with function 0x" << (void*)function << endl;
153+
}
154+
assert(!event_name.empty());
155+
assert(function);
150156
return _hooks[event_name].insert(function).second;
151157
}
152158

@@ -164,6 +170,8 @@ add_hook(const string &event_name, EventFunction *function) {
164170
bool EventHandler::
165171
add_hook(const string &event_name, EventCallbackFunction *function,
166172
void *data) {
173+
assert(!event_name.empty());
174+
assert(function);
167175
return _cbhooks[event_name].insert(CallbackFunction(function, data)).second;
168176
}
169177

@@ -175,6 +183,7 @@ add_hook(const string &event_name, EventCallbackFunction *function,
175183
////////////////////////////////////////////////////////////////////
176184
bool EventHandler::
177185
has_hook(const string &event_name) const {
186+
assert(!event_name.empty());
178187
Hooks::const_iterator hi;
179188
hi = _hooks.find(event_name);
180189
if (hi != _hooks.end()) {
@@ -204,6 +213,8 @@ has_hook(const string &event_name) const {
204213
////////////////////////////////////////////////////////////////////
205214
bool EventHandler::
206215
remove_hook(const string &event_name, EventFunction *function) {
216+
assert(!event_name.empty());
217+
assert(function);
207218
return _hooks[event_name].erase(function) != 0;
208219
}
209220

@@ -219,6 +230,8 @@ remove_hook(const string &event_name, EventFunction *function) {
219230
bool EventHandler::
220231
remove_hook(const string &event_name, EventCallbackFunction *function,
221232
void *data) {
233+
assert(!event_name.empty());
234+
assert(function);
222235
return _cbhooks[event_name].erase(CallbackFunction(function, data)) != 0;
223236
}
224237

@@ -234,6 +247,17 @@ remove_all_hooks() {
234247
_cbhooks.clear();
235248
}
236249

250+
////////////////////////////////////////////////////////////////////
251+
// Function: EventHandler::make_global_event_handler
252+
// Access: Protected, Static
253+
// Description:
254+
////////////////////////////////////////////////////////////////////
255+
void EventHandler::
256+
make_global_event_handler(EventQueue *queue) {
257+
assert(queue);
258+
_global_event_handler = new EventHandler(queue);
259+
}
260+
237261

238262
////////////////////////////////////////////////////////////////////
239263
// Function: EventHandler::write_hook

panda/src/event/eventHandler.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class EventQueue;
3939
//
4040
// This class is not necessary when the hooks are
4141
// detected and processed entirely by the scripting
42-
// language, e.g. via Scheme hooks.
42+
// language, e.g. via Scheme hooks or the messenger
43+
// in Python.
4344
////////////////////////////////////////////////////////////////////
4445
class EXPCL_PANDAEXPRESS EventHandler : public TypedObject {
4546
public:
@@ -56,6 +57,8 @@ class EXPCL_PANDAEXPRESS EventHandler : public TypedObject {
5657

5758
void write(ostream &out) const;
5859

60+
INLINE static EventHandler *get_global_event_handler(EventQueue *queue);
61+
5962
public:
6063
bool add_hook(const string &event_name, EventFunction *function);
6164
bool add_hook(const string &event_name, EventCallbackFunction *function,
@@ -79,6 +82,9 @@ class EXPCL_PANDAEXPRESS EventHandler : public TypedObject {
7982
CallbackHooks _cbhooks;
8083
EventQueue &_queue;
8184

85+
static EventHandler *_global_event_handler;
86+
static void make_global_event_handler(EventQueue *queue);
87+
8288
private:
8389
void write_hook(ostream &out, const Hooks::value_type &hook) const;
8490
void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const;
@@ -102,4 +108,6 @@ class EXPCL_PANDAEXPRESS EventHandler : public TypedObject {
102108
static TypeHandle _type_handle;
103109
};
104110

111+
#include "eventHandler.I"
112+
105113
#endif

0 commit comments

Comments
 (0)