Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/event/eventHandler.h

Go to the documentation of this file.
00001 // Filename: eventHandler.h
00002 // Created by:  drose (08Feb99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef EVENTHANDLER_H
00020 #define EVENTHANDLER_H
00021 
00022 #include <pandabase.h>
00023 
00024 #include "event.h"
00025 #include "pt_Event.h"
00026 
00027 #include "pset.h"
00028 #include "pmap.h"
00029 
00030 class EventQueue;
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //       Class : EventHandler
00034 // Description : A class to monitor events from the C++ side of
00035 //               things.  It maintains a set of "hooks", function
00036 //               pointers assigned to event names, and calls the
00037 //               appropriate hooks when the matching event is
00038 //               detected.
00039 //
00040 //               This class is not necessary when the hooks are
00041 //               detected and processed entirely by the scripting
00042 //               language, e.g. via Scheme hooks.
00043 ////////////////////////////////////////////////////////////////////
00044 class EXPCL_PANDAEXPRESS EventHandler : public TypedObject {
00045 public:
00046   // Define a function type suitable for receiving events.
00047   typedef void EventFunction(CPT_Event);
00048   typedef void EventCallbackFunction(CPT(Event), void*);
00049 
00050 PUBLISHED:
00051   EventHandler(EventQueue *queue);
00052 
00053   void process_events();
00054 
00055   virtual void dispatch_event(const CPT_Event &event);
00056 
00057   void write(ostream &out) const;
00058 
00059 public:
00060   bool add_hook(const string &event_name, EventFunction *function);
00061   bool add_hook(const string &event_name, EventCallbackFunction *function,
00062                 void*);
00063   bool remove_hook(const string &event_name, EventFunction *function);
00064   bool remove_hook(const string &event_name, EventCallbackFunction *function,
00065                    void*);
00066 
00067   void remove_all_hooks();
00068 
00069 protected:
00070 
00071   typedef pset<EventFunction *> Functions;
00072   typedef pmap<string, Functions> Hooks;
00073   typedef pair<EventCallbackFunction*, void*> CallbackFunction;
00074   typedef pset<CallbackFunction> CallbackFunctions;
00075   typedef pmap<string, CallbackFunctions> CallbackHooks;
00076 
00077   Hooks _hooks;
00078   CallbackHooks _cbhooks;
00079   EventQueue &_queue;
00080 
00081 private:
00082   void write_hook(ostream &out, const Hooks::value_type &hook) const;
00083   void write_cbhook(ostream &out, const CallbackHooks::value_type &hook) const;
00084 
00085 
00086 public:
00087   static TypeHandle get_class_type() {
00088     return _type_handle;
00089   }
00090   static void init_type() {
00091     TypedObject::init_type();
00092     register_type(_type_handle, "EventHandler",
00093                   TypedObject::get_class_type());
00094   }
00095   virtual TypeHandle get_type() const {
00096     return get_class_type();
00097   }
00098   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00099 
00100 private:
00101   static TypeHandle _type_handle;
00102 };
00103 
00104 #endif

Generated on Fri May 2 00:38:15 2003 for Panda by doxygen1.3