OpenWalnut  1.4.0
WPickHandler.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WPICKHANDLER_H
00026 #define WPICKHANDLER_H
00027 
00028 #include <string>
00029 
00030 #include <boost/signals2/signal.hpp>
00031 
00032 #include <osgViewer/View>
00033 
00034 
00035 #include "WPickInfo.h"
00036 
00037 
00038 /**
00039  * Class to handle events with a pick.
00040  *
00041  * The handler ignores any geometry whose name starts with an underscore ("_").
00042  */
00043 class WPickHandler: public osgGA::GUIEventHandler
00044 {
00045 public:
00046     /**
00047      * Constructor that initalizes members with sensible defaults.
00048      */
00049     WPickHandler();
00050 
00051     /**
00052      * Constructor that initalizes members with sensible defaults and sets the name of the viewer
00053      *
00054      * \param viewerName name of the viewer
00055      */
00056     explicit WPickHandler( std::string viewerName );
00057 
00058     /**
00059      * Deals with the events found by the osg.
00060      * \param ea Event class for storing Keyboard, mouse and window events.
00061      * \param aa Interface by which GUIEventHandlers may request actions of the GUI system
00062      *
00063      * \return true if the event was handled.
00064      */
00065     bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa );
00066 
00067     /**
00068      * Send a pick signal with the pick information as string
00069      * \param view the view in which we pick.
00070      * \param ea Event class for storing Keyboard, mouse and window events.
00071      */
00072     virtual void pick( osgViewer::View* view, const osgGA::GUIEventAdapter& ea );
00073 
00074     /**
00075      * Send a pick signal with the string "unpick"
00076      */
00077     virtual void unpick();
00078 
00079     /**
00080      * Gives information about the picked object.
00081      *
00082      * \return info object for this hit
00083      */
00084     WPickInfo getHitResult();
00085 
00086     /**
00087      * returns the m_pickSignal to for registering to it.
00088      */
00089     boost::signals2::signal1< void, WPickInfo >* getPickSignal();
00090 
00091     /**
00092      * setter for paint mode
00093      * \param mode the paint mode
00094      */
00095     void setPaintMode( int mode );
00096 
00097 protected:
00098     /**
00099      * Virtual destructor needed because of virtual function.
00100      *
00101      * This desctructor is protected to avoid accidentally deleting
00102      * a instance of WPickHandler.
00103      * This follows the philosophy of OSG to avoid problems in multithreaded
00104      * environments, since these pointers are used deep in the OSG where
00105      * a deletion could cause a segfault.
00106      */
00107     virtual ~WPickHandler();
00108 
00109     WPickInfo m_hitResult; //!< Textual representation of the result of a pick.
00110     WPickInfo m_startPick; //!< indicates what was first picked. Should be "" after unpick.
00111     bool m_shift; //!< is shift pressed?
00112     bool m_ctrl; //!< is ctrl pressed?
00113     std::string m_viewerName; //!< which viewer sends the signal
00114     int m_paintMode; //!< the paint mode
00115     WPickInfo::WMouseButton m_mouseButton; //!< stores mouse button that initiated the pick
00116 
00117     bool m_inPickMode;  //!< if true, the pick handler currently is in pick mode.
00118 
00119     int32_t m_scrollWheel; //!< the virtual value of the scrollwheel
00120 
00121 private:
00122     /**
00123      * Sets the current modifiers to the provided pickInfo
00124      *
00125      * \param pickInfo This pickInfo will be updated.
00126      */
00127     void updatePickInfoModifierKeys( WPickInfo* pickInfo );
00128 
00129     boost::signals2::signal1< void, WPickInfo > m_pickSignal; //!< One can register to this signal to receive pick events.
00130 };
00131 
00132 #endif  // WPICKHANDLER_H