Panda3D
 All Classes Functions Variables Enumerations
eventHandler.h
1 // Filename: eventHandler.h
2 // Created by: drose (08Feb99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef EVENTHANDLER_H
16 #define EVENTHANDLER_H
17 
18 #include "pandabase.h"
19 
20 #include "event.h"
21 #include "pt_Event.h"
22 
23 #include "pset.h"
24 #include "pmap.h"
25 
26 class EventQueue;
27 
28 ////////////////////////////////////////////////////////////////////
29 // Class : EventHandler
30 // Description : A class to monitor events from the C++ side of
31 // things. It maintains a set of "hooks", function
32 // pointers assigned to event names, and calls the
33 // appropriate hooks when the matching event is
34 // detected.
35 //
36 // This class is not necessary when the hooks are
37 // detected and processed entirely by the scripting
38 // language, e.g. via Scheme hooks or the messenger
39 // in Python.
40 ////////////////////////////////////////////////////////////////////
41 class EXPCL_PANDA_EVENT EventHandler : public TypedObject {
42 public:
43  // Define a function type suitable for receiving events.
44  typedef void EventFunction(const Event *);
45  typedef void EventCallbackFunction(const Event *, void *);
46 
47 PUBLISHED:
48  EventHandler(EventQueue *ev_queue);
49 
50  void process_events();
51 
52  virtual void dispatch_event(const Event *);
53 
54  void write(ostream &out) const;
55 
56  INLINE static EventHandler *get_global_event_handler(EventQueue *queue = NULL);
57 
58 public:
59  bool add_hook(const string &event_name, EventFunction *function);
60  bool add_hook(const string &event_name, EventCallbackFunction *function,
61  void *data);
62  bool has_hook(const string &event_name) const;
63  bool remove_hook(const string &event_name, EventFunction *function);
64  bool remove_hook(const string &event_name, EventCallbackFunction *function,
65  void *data);
66 
67  bool remove_hooks(const string &event_name);
68  bool remove_hooks_with(void *data);
69 
70  void remove_all_hooks();
71 
72 protected:
73 
76  typedef pair<EventCallbackFunction*, void*> CallbackFunction;
79 
80  Hooks _hooks;
81  CallbackHooks _cbhooks;
82  EventQueue &_queue;
83 
84  static EventHandler *_global_event_handler;
85  static void make_global_event_handler();
86 
87 private:
88  void write_hook(ostream &out, const Hooks::value_type &hook) const;
89  void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const;
90 
91 
92 public:
93  static TypeHandle get_class_type() {
94  return _type_handle;
95  }
96  static void init_type() {
98  register_type(_type_handle, "EventHandler",
99  TypedObject::get_class_type());
100  }
101  virtual TypeHandle get_type() const {
102  return get_class_type();
103  }
104  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
105 
106 private:
107  static TypeHandle _type_handle;
108 };
109 
110 #include "eventHandler.I"
111 
112 #endif
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition: typedObject.cxx:52
A class to monitor events from the C++ side of things.
Definition: eventHandler.h:41
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
A queue of pending events.
Definition: eventQueue.h:32
A named event, possibly with parameters.
Definition: event.h:36
This is our own Panda specialization on the default STL set.
Definition: pset.h:52
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85