arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

ManipEventBase.h

Go to the documentation of this file.
00001 ///////////////////////////////////////////////////////////////////////////
00002 //              _____________  ______________________    __   __  _____
00003 //             /  ________  |  |   ___   ________   /   |  \ /  \   |
00004 //            |  |       |  |_ |  |_ |  |       /  /    \__  |      |
00005 //            |  |  ___  |  || |  || |  |      /  /        | |      |
00006 //            |  | |   \ |  || |  || |  |     /  /      \__/ \__/ __|__
00007 //            |  | |_@  ||  || |  || |  |    /  /          Institute
00008 //            |  |___/  ||  ||_|  || |  |   /  /_____________________
00009 //             \_______/  \______/ | |__|  /___________________________
00010 //                        |  |__|  |
00011 //                         \______/
00012 //                    University of Utah       
00013 //                           2003
00014 ///////////////////////////////////////////////////////////////////////////
00015 
00016 //ManipEventBase.h
00017 // Joe Kniss
00018 
00019 #ifndef __MANIP_EVENT_BASE_DOT_H
00020 #define __MANIP_EVENT_BASE_DOT_H
00021 
00022 #include "../include/smartptr.h"
00023 #include <map>
00024 
00025 namespace gutz {
00026 
00027 class MouseEvent;
00028 class MouseMoveEvent;
00029 
00030 ///////////////////////////////////////////////////////////////////
00031 /// Generic Manipulator Event Interface class.  Defines the functions
00032 ///  that MUST be defined for a Manipulator Constraint/Event handler.
00033 ///  All Events should sub-class from this guy.
00034 ///
00035 /// Template parameter MT is the ManipType, right now this is either
00036 /// a gutz::Manip or a gutz::Camera.  
00037 /// See gutz::EventMap for a containter class.  
00038 ///
00039 /// The role of this class is to provide a common interface for
00040 /// events, this way we can map them etc... Notice that we have a 
00041 /// pure virutal clone.  This means that we would like to copy
00042 /// an event with out knowing anything about it.  Since the 
00043 /// copy constructor just won't due, you have to clone. 
00044 /// 
00045 /// Although the name of this class is ManipEventBase, it is also
00046 /// the base for Camera Events too.
00047 ///////////////////////////////////////////////////////////////////
00048 template<class MT>
00049 class ManipEventBase : public gutz::Counted
00050 {
00051 public:
00052    typedef MT ManipType; ///< either a Camera or a Manip (LIGHT??)
00053 
00054    virtual ~ManipEventBase() {}
00055 
00056    ///@name Clone
00057    ///@{
00058    virtual ManipEventBase<MT> *clone() const = 0;
00059    ///@}
00060 
00061    ///@name Event Handlers
00062    ///  Return true if the event had an error, false if everything was ok!
00063    ///@{
00064 
00065    /// should be called on a Mouse-down event, starts the event
00066    virtual bool startEvent(ManipType *m, const MouseEvent &me) = 0;
00067    /// should be called when the mouse moves
00068    virtual bool handleEvent(ManipType *m, const MouseMoveEvent &mme) = 0;
00069    /// called when the event is over, may or may not be on a Mouse-up event
00070    virtual void endEvent(ManipType *m, const MouseEvent &me) = 0;
00071    ///@}
00072 
00073    ///@name Tumble
00074    ///  optional event handler, means "repeat last event"
00075    ///@{
00076    virtual bool tumble(ManipType *m) {return false;}
00077    ///@}
00078 
00079    ///@name Speed
00080    ///@{
00081    float getSpeed() const      { return _speed; }
00082    void  setSpeed(float speed) { _speed = speed; }
00083    ///@}
00084 
00085 protected:
00086    ManipEventBase(float speed = 1.0f): _speed(speed) {}
00087    ManipEventBase(const ManipEventBase &m): _speed(m._speed) {}
00088    ManipEventBase &operator=(const ManipEventBase &m) 
00089    { _speed = m._speed; return *this; }
00090 
00091    float _speed;
00092 };
00093 
00094 ///////////////////////////////////////////////////////////////////
00095 /// Generic Manipulator Event Mapper.  It is simplest if manipulators
00096 /// sub-class from this so that we are not duplicating the interface
00097 /// all over the place. 
00098 ///
00099 /// Template parameter MT is the ManipType, right now this is either
00100 /// a gutz::Manip or a gutz::Camera. 
00101 ///
00102 /// Only stores SmartPtrs not the acutal object, therefore copy
00103 /// and asignment only copies the pointers, does not create 
00104 /// new events. 
00105 ///
00106 /// The role of this class is to provide the interface and 
00107 /// event handling for all manipulator objects.  It maps event
00108 /// handlers (see gutz::ManipEventBase) to buttons and keys
00109 /// defined in gutzKeyMouse.h  It also handles mouse events
00110 /// and forwards them to the appropriate event handler.  I chose
00111 /// to make this class a base for Manip and Camera since I really
00112 /// don't want to duplicate the interface in both of these classes
00113 /// and it wouldn't make sense to have more than one of them in
00114 /// any class.  Plus it facilitates building and copying events
00115 /// without having to create a manipulator or a camera. 
00116 ///////////////////////////////////////////////////////////////////
00117 template<class MT>
00118 class EventMap : 
00119    public std::map<unsigned int, 
00120                    gutz::SmartPtr< ManipEventBase<MT> >, 
00121                    std::less<unsigned int> >
00122 {
00123 public:
00124    typedef MT                         ManipType;
00125    typedef ManipEventBase<MT>         ManipEventType;
00126    typedef SmartPtr< ManipEventType > ManipEventTypeSP;
00127    typedef std::map<unsigned int, ManipEventTypeSP, std::less<unsigned int> > BaseType;
00128    typedef typename BaseType::iterator Iter;
00129    typedef typename BaseType::const_iterator CIter;
00130    
00131    EventMap() : BaseType() {}
00132    EventMap(const EventMap &em) : BaseType(em) {}
00133    virtual ~EventMap() {}
00134    EventMap &operator=(const EventMap &em)
00135    {
00136       BaseType::operator=(em);
00137       return *this;
00138    }
00139 
00140    /////////////////////////////////////////////////////
00141    ///@name [] Accessors
00142    ///@{
00143 
00144    /// const, returns a pointer to the event, only if it 
00145    /// is mapped!  This is good since we don't want to add 
00146    /// an event just to check if it exists!!
00147    ManipEventTypeSP operator[](unsigned int key) const 
00148    {
00149       CIter ei = find(key);
00150       if( ei == end() ) return ManipEventTypeSP(0);
00151       return (*ei).second;
00152    }
00153 
00154    /// standard non-const, same as the map [] operator
00155    ManipEventTypeSP &operator[](unsigned int key)
00156    { 
00157       return BaseType::operator[](key);
00158    }
00159    ///@}
00160    /////////////////////////////////////////////////////
00161 
00162    /////////////////////////////////////////////////////
00163    ///@name Event Interface
00164    ///@{
00165    ManipEventTypeSP getEvent(const gutz::MouseEvent &me) const
00166    { 
00167       return operator[]( me.getButton() );
00168    }
00169    
00170    void             mapEvent(unsigned int button, ManipEventType *met)
00171    {
00172       operator[](button) = met;
00173    }
00174    ///@}
00175    /////////////////////////////////////////////////////
00176 
00177    /////////////////////////////////////////////////////
00178    ///@name Event Interface
00179    ///@{
00180    bool mouse(ManipType *m, const gutz::MouseEvent &me)
00181    {
00182       if( ! (_lastEvent = getEvent(me)) ) return false;
00183       if( !me.isButtonDown() ) return false;
00184       return _lastEvent->startEvent(m, me);
00185    }
00186    bool move(ManipType *m, const gutz::MouseMoveEvent &mme)
00187    {
00188       if( (!_lastEvent) ) return false;
00189       return _lastEvent->handleEvent(m, mme);
00190    }
00191    void endEvent(ManipType *m, const gutz::MouseEvent &me)
00192    {
00193       if( _lastEvent ) _lastEvent->endEvent(m, me);
00194    }
00195    bool tumble(ManipType *m)
00196    {
00197       if( ! _lastEvent ) return false;
00198       return _lastEvent->tumble(m);
00199    }
00200    ///@}
00201    /////////////////////////////////////////////////////
00202 
00203    /////////////////////////////////////////////////////
00204    ///@name Event Management
00205    ///@{
00206 
00207    /// nuke all events
00208    void clearEvents() { clear(); }
00209    /// deep copy of events from some other map. Will likely be
00210    /// used like:
00211    /// \code
00212    ///   myManip.cloneEvents( someOtherManip );
00213    /// \endcode
00214    /// since Manips are (sub-classes of) EventMaps 
00215    void cloneEvents(const EventMap &em)
00216    {
00217       for(CIter ei = em.begin(); ei != em.end(); ++ei)
00218       {
00219          if( (*ei).second )
00220             mapEvent((*ei).first, (*ei).second->clone());
00221       }
00222    }
00223    /// shallow copy of events from some other map
00224    void shareEvents(const EventMap &em)
00225    {   
00226       for(CIter ei = em.begin(); ei != em.end(); ++ei)
00227       {
00228          if( (*ei).second )
00229             mapEvent((*ei).first, (*ei).second);
00230       }
00231    }
00232    ///@}
00233    /////////////////////////////////////////////////////
00234 
00235 protected:
00236    ManipEventTypeSP _lastEvent;
00237 
00238 };
00239 
00240 
00241 } // end namespace gutz
00242 
00243 #endif
00244 

Send questions, comments, and bug reports to:
jmk