eventHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef EVENTHANDLER_H
  15. #define EVENTHANDLER_H
  16. #include "pandabase.h"
  17. #include "event.h"
  18. #include "pt_Event.h"
  19. #include "pset.h"
  20. #include "pmap.h"
  21. class EventQueue;
  22. ////////////////////////////////////////////////////////////////////
  23. // Class : EventHandler
  24. // Description : A class to monitor events from the C++ side of
  25. // things. It maintains a set of "hooks", function
  26. // pointers assigned to event names, and calls the
  27. // appropriate hooks when the matching event is
  28. // detected.
  29. //
  30. // This class is not necessary when the hooks are
  31. // detected and processed entirely by the scripting
  32. // language, e.g. via Scheme hooks or the messenger
  33. // in Python.
  34. ////////////////////////////////////////////////////////////////////
  35. class EXPCL_PANDA_EVENT EventHandler : public TypedObject {
  36. public:
  37. // Define a function type suitable for receiving events.
  38. typedef void EventFunction(const Event *);
  39. typedef void EventCallbackFunction(const Event *, void *);
  40. PUBLISHED:
  41. EventHandler(EventQueue *ev_queue);
  42. void process_events();
  43. virtual void dispatch_event(const Event *);
  44. void write(ostream &out) const;
  45. INLINE static EventHandler *get_global_event_handler(EventQueue *queue = NULL);
  46. public:
  47. bool add_hook(const string &event_name, EventFunction *function);
  48. bool add_hook(const string &event_name, EventCallbackFunction *function,
  49. void *data);
  50. bool has_hook(const string &event_name) const;
  51. bool remove_hook(const string &event_name, EventFunction *function);
  52. bool remove_hook(const string &event_name, EventCallbackFunction *function,
  53. void *data);
  54. bool remove_hooks(const string &event_name);
  55. bool remove_hooks_with(void *data);
  56. void remove_all_hooks();
  57. protected:
  58. typedef pset<EventFunction *> Functions;
  59. typedef pmap<string, Functions> Hooks;
  60. typedef pair<EventCallbackFunction*, void*> CallbackFunction;
  61. typedef pset<CallbackFunction> CallbackFunctions;
  62. typedef pmap<string, CallbackFunctions> CallbackHooks;
  63. Hooks _hooks;
  64. CallbackHooks _cbhooks;
  65. EventQueue &_queue;
  66. static EventHandler *_global_event_handler;
  67. static void make_global_event_handler();
  68. private:
  69. void write_hook(ostream &out, const Hooks::value_type &hook) const;
  70. void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const;
  71. public:
  72. static TypeHandle get_class_type() {
  73. return _type_handle;
  74. }
  75. static void init_type() {
  76. TypedObject::init_type();
  77. register_type(_type_handle, "EventHandler",
  78. TypedObject::get_class_type());
  79. }
  80. virtual TypeHandle get_type() const {
  81. return get_class_type();
  82. }
  83. virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
  84. private:
  85. static TypeHandle _type_handle;
  86. };
  87. #include "eventHandler.I"
  88. #endif