OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WPickInfo.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 WPICKINFO_H
26 #define WPICKINFO_H
27 
28 #include <string>
29 #include <utility>
30 
31 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
32 #include "../common/WDefines.h"
33 #include "WExportWGE.h"
34 
35 
36 /**
37  * Encapsulates info for pick action.
38  */
39 class WGE_EXPORT WPickInfo
40 {
41 public:
42  /**
43  * Different types of modifier keys.
44  */
46  {
47  NONE,
48  SHIFT,
49  STRG,
50  ALT,
51  WIN
52  };
53 
54  /**
55  * Different types of mouse buttons.
56  */
57  typedef enum
58  {
59  NOMOUSE,
60  MOUSE_LEFT,
61  MOUSE_RIGHT,
62  MOUSE_MIDDLE,
63  MOUSE4,
64  MOUSE5
65  }
66  WMouseButton;
67 
68  /**
69  * Creates an object with the needed information.
70  * \param name name of picked object
71  * \param viewerName name of the viewer
72  * \param pickPosition position where object was hit
73  * \param pixelCoords pixel coordinates of the mouse
74  * \param modKey relevant modifier key pressed during the pick
75  * \param mButton mouse button that initiated the pick
76  * \param pickNormal normal at position where object was hit. (0,0,0) means not set.
77  */
78  inline WPickInfo( std::string name,
79  std::string viewerName,
80  WPosition pickPosition,
81  std::pair< float, float > pixelCoords,
82  modifierKey modKey,
83  WMouseButton mButton = WPickInfo::MOUSE_LEFT,
84  WVector3d pickNormal = WVector3d() );
85 
86  /**
87  * Creates an object with the empty name, zero position and no modkey.
88  */
89  inline WPickInfo();
90 
91  /**
92  * Get the modifier key associated with the pick
93  *
94  * \return the mod key
95  */
96  inline modifierKey getModifierKey() const;
97 
98  /**
99  * Get the mouse button associated with the pick
100  *
101  * \return the mouse button
102  */
103  inline WMouseButton getMouseButton() const;
104 
105  /**
106  * Set the modifier key associated with the pick
107  * \param modKey new modifier key
108  */
109  inline void setModifierKey( const modifierKey& modKey );
110 
111  /**
112  * Set the modifier key associated with the pick
113  * \param mButton new mouse button
114  */
115  inline void setMouseButton( const WMouseButton& mButton );
116 
117 
118  /**
119  * Get name of picked object.
120  *
121  * \return object name
122  */
123  inline std::string getName() const;
124 
125  /**
126  * Get name of the viewer.
127  *
128  * \return viewer name
129  */
130  inline std::string getViewerName() const;
131 
132  /**
133  * Get position where object was hit.
134  *
135  * \return the pick position
136  */
137  inline WPosition getPickPosition() const;
138 
139  /**
140  * Get normal at position where object was hit.
141  *
142  * \return pick normal
143  */
144  inline WVector3d getPickNormal() const;
145 
146  /**
147  * Returns the picked pixel coordinates in screen-space.
148  *
149  * \return the coordinates
150  */
151  inline WVector2d getPickPixel() const;
152 
153  /**
154  * Tests two pick infos for equality
155  * \param rhs right hand side of comparison
156  *
157  * \return true if equal
158  */
159  inline bool operator==( WPickInfo rhs ) const;
160 
161  /**
162  * Tests two pick infos for inequality
163  *
164  * \param rhs right hand side of comparison
165  *
166  * \return true if not equal
167  */
168  inline bool operator!=( WPickInfo rhs ) const;
169 
170 protected:
171 private:
172 
173  std::string m_name; //!< name of picked object.
174  std::string m_viewerName; //!< name of the viewer
175  WPosition m_pickPosition; //!< position where object was hit.
176  std::pair< float, float > m_pixelCoords; //!< Pixel coordinates of the mouse.
177  modifierKey m_modKey; //!< modifier key associated with the pick
178  WMouseButton m_mouseButton; //!< which mouse button was used for the pick
179  WVector3d m_pickNormal; //!< normal at position where object was hit.
180 };
181 
182 WPickInfo::WPickInfo( std::string name,
183  std::string viewerName,
184  WPosition pickPosition,
185  std::pair< float, float > pixelCoords,
186  modifierKey modKey,
187  WMouseButton mButton,
188  WVector3d pickNormal ) :
189  m_name( name ),
190  m_viewerName( viewerName ),
191  m_pickPosition( pickPosition ),
192  m_pixelCoords( pixelCoords ),
193  m_modKey( modKey ),
194  m_mouseButton( mButton ),
195  m_pickNormal( pickNormal )
196 {
197 }
198 
200  m_name( "" ),
201  m_viewerName( "" ),
202  m_pickPosition( WPosition() ),
203  m_pixelCoords( std::make_pair( 0.0, 0.0 ) ),
204  m_modKey( WPickInfo::NONE ),
205  m_mouseButton( WPickInfo::MOUSE_LEFT )
206 {
207 }
208 
210 {
211  return m_modKey;
212 }
213 
215 {
216  m_modKey = modKey;
217 }
218 
220 {
221  return m_mouseButton;
222 }
223 
225 {
226  m_mouseButton = mButton;
227 }
228 
229 std::string WPickInfo::getName() const
230 {
231  return m_name;
232 }
233 
234 std::string WPickInfo::getViewerName() const
235 {
236  return m_viewerName;
237 }
238 
240 {
241  return m_pickPosition;
242 }
243 
245 {
246  return m_pickNormal;
247 }
248 
249 inline bool WPickInfo::operator==( WPickInfo rhs ) const
250 {
251  return ( this->m_name == rhs.m_name
252  && this->m_pickPosition == rhs.m_pickPosition
253  && this->m_modKey == rhs.m_modKey );
254 }
255 
256 inline bool WPickInfo::operator!=( WPickInfo rhs ) const
257 {
258  return !( *this == rhs );
259 }
260 
262 {
263  WVector2d v;
264  v[0] = m_pixelCoords.first;
265  v[1] = m_pixelCoords.second;
266  return v;
267 }
268 
269 #endif // WPICKINFO_H