OpenWalnut  1.5.0dev
WMouseLocationHandler.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WMOUSELOCATIONHANDLER_H
26 #define WMOUSELOCATIONHANDLER_H
27 
28 #include <string>
29 #include <utility>
30 
31 #include <boost/signals2/signal.hpp>
32 
33 #include <osgViewer/View>
34 
35 #include "../common/math/linearAlgebra/WVectorFixed.h"
36 
37 
38 /**
39  * Class to handle providing information about pixel position of mouse.
40  */
41 class WMouseLocationHandler: public osgGA::GUIEventHandler
42 {
43 public:
44  /**
45  * Constructor that initalizes members with sensible defaults.
46  */
48 
49  /**
50  * Constructor that initalizes members with sensible defaults and sets the name of the viewer
51  *
52  * \param viewerName name of the viewer
53  */
54  explicit WMouseLocationHandler( std::string viewerName );
55 
56  /**
57  * Deals with the events found by the osg.
58  * \param ea Event class for storing Keyboard, mouse and window events.
59  * \param aa Interface by which GUIEventHandlers may request actions of the GUI system
60  *
61  * \return true if the event was handled.
62  */
63  bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa );
64 
65  /**
66  * This class holds the information about the mouse pointer location in a certain viewe.
67  */
69  {
70  public:
71  /**
72  * Initializing members.
73  *
74  * \param viewerName Name of the osg::viewer to which the infos belong
75  * \param pixelCoords The pixel position of the mouse pointer in the viewer.
76  */
77  WMouseLocationInfo( std::string viewerName,
78  std::pair< float, float > pixelCoords ) :
79  m_viewerName( viewerName ),
80  m_pixelCoords( pixelCoords )
81  {
82  }
83  /**
84  * Default initializing members.
85  */
87  m_viewerName( "" ),
88  m_pixelCoords( std::make_pair( 0.0, 0.0 ) )
89  {
90  }
91  /**
92  * Obtain the mouse location in pixel coordinates
93  *
94  * \return The position of the mouse pointer in the viewer.
95  */
96  inline WVector2d getPixel() const
97  {
98  WVector2d pos;
99  pos[0] = m_pixelCoords.first;
100  pos[1] = m_pixelCoords.second;
101  return pos;
102  }
103  private:
104  std::string m_viewerName; //!< name of the viewer
105  std::pair< float, float > m_pixelCoords; //!< Pixel coordinates of the mouse.
106  };
107 
108  /**
109  * \return the m_locationSignal to for registering to it.
110  */
111  boost::signals2::signal< void( WMouseLocationInfo ) >* getLocationSignal();
112 
113  /**
114  * Gives information about the mouse location.
115  *
116  * \return info the location information
117  */
119 
120 
121 protected:
122  /**
123  * Virtual destructor needed because of virtual function.
124  *
125  * This desctructor is protected to avoid accidentally deleting
126  * an instance of WMouseLocationHandler.
127  * This follows the philosophy of OSG to avoid problems in multithreaded
128  * environments, since these pointers are used deep in the OSG where
129  * a deletion could cause a segfault.
130  */
131  virtual ~WMouseLocationHandler();
132 
133 private:
134  boost::signals2::signal< void( WMouseLocationInfo ) > m_locationSignal; //!< One can register to this signal to receive location events.
135 
136  WMouseLocationInfo m_mouseLocation; //!< Representation of mouse location
137  std::string m_viewerName; //!< which viewer sends the signal
138 };
139 
140 #endif // WMOUSELOCATIONHANDLER_H
This class holds the information about the mouse pointer location in a certain viewe.
WMouseLocationInfo(std::string viewerName, std::pair< float, float > pixelCoords)
Initializing members.
WMouseLocationInfo()
Default initializing members.
std::pair< float, float > m_pixelCoords
Pixel coordinates of the mouse.
WVector2d getPixel() const
Obtain the mouse location in pixel coordinates.
Class to handle providing information about pixel position of mouse.
WMouseLocationHandler()
Constructor that initalizes members with sensible defaults.
boost::signals2::signal< void(WMouseLocationInfo) > m_locationSignal
One can register to this signal to receive location events.
WMouseLocationInfo m_mouseLocation
Representation of mouse location.
std::string m_viewerName
which viewer sends the signal
WMouseLocationInfo getLocationInfo()
Gives information about the mouse location.
bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
Deals with the events found by the osg.
boost::signals2::signal< void(WMouseLocationInfo) > * getLocationSignal()
virtual ~WMouseLocationHandler()
Virtual destructor needed because of virtual function.