OpenWalnut  1.5.0dev
WGE2DManipulator.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 WGE2DMANIPULATOR_H
26 #define WGE2DMANIPULATOR_H
27 
28 #include <osg/Version>
29 
30 // OSG interface changed in 2.9.7, to make it compile also with those versions we do this:
31 // OSG_MIN_VERSION_REQUIRED(2, 9, 8) macro is not available in e.g. OSG 2.8.1, hence we use the old way
32 #if( ( OPENSCENEGRAPH_MAJOR_VERSION > 2 ) || ( OPENSCENEGRAPH_MAJOR_VERSION == 2 && ( OPENSCENEGRAPH_MINOR_VERSION > 9 || \
33  ( OPENSCENEGRAPH_MINOR_VERSION == 9 && OPENSCENEGRAPH_PATCH_VERSION >= 8 ) ) ) )
34  #include <osgGA/CameraManipulator>
35  namespace osgGA
36  {
37  typedef CameraManipulator MatrixManipulator;
38  }
39 #else
40  #include <osgGA/MatrixManipulator>
41 #endif
42 
43 
44 
45 /**
46  * A manipulator which changes the view of a 2D scene. Does things like panning
47  * and zooming.
48  */
49 class WGE2DManipulator : public osgGA::MatrixManipulator
50 {
51 public:
52  /**
53  * Constructor
54  */
56 
57  /**
58  * Return the name of the object's class type.
59  *
60  * \return the name of the object's class type
61  */
62  virtual const char* className() const;
63 
64  /**
65  * Set the position of the matrix manipulator using a 4x4 matrix.
66  *
67  * \param matrix a 4x4 matrix
68  */
69  virtual void setByMatrix( const osg::Matrixd& matrix );
70 
71  /**
72  * Set the position of the matrix manipulator using a 4x4 matrix.
73  *
74  * \param matrix a 4x4 matrix
75  */
76  virtual void setByInverseMatrix( const osg::Matrixd& matrix );
77 
78  /**
79  * Get the position of the manipulator as 4x4 matrix.
80  *
81  * \return the position of the manipulator as 4x4 matrix
82  */
83  virtual osg::Matrixd getMatrix() const;
84 
85  /**
86  * Get the position of the manipulator as a inverse matrix of the
87  * manipulator, typically used as a model view matrix.
88  *
89  * \return the position of the manipulator as a inverse matrix
90  */
91  virtual osg::Matrixd getInverseMatrix() const;
92 
93  /**
94  * Move the camera to the default position.
95  *
96  * \param us the action adapter used to request actions of the GUI
97  */
98  virtual void home( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ); // NOLINT We can not change the interface of OSG
99 
100  /**
101  * Start/restart the manipulator.
102  *
103  * \param us the action adapter used to request actions of the GUI
104  */
105  virtual void init( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ); // NOLINT We can not change the interface of OSG
106 
107  /**
108  * Handle events
109  *
110  * \param ea event class for storing keyboard, mouse and window events
111  * \param us the action adapter used to request actions of the GUI
112  * \return true if handled, false otherwise
113  */
114  virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
115 
116  /**
117  * Get the keyboard and mouse usage of this manipulator.
118  *
119  * \param usage the application usage
120  */
121  virtual void getUsage( osg::ApplicationUsage& usage ) const; // NOLINT We can not change the interface of OSG
122 
123 protected:
124  /**
125  * Destructor
126  *
127  * Note, is protected so that objects cannot be deleted other than by being
128  * dereferenced and the reference count being zero (see osg::Referenced),
129  * preventing the deletion of objects which are still in use.
130  */
131  virtual ~WGE2DManipulator();
132 
133  /**
134  * Reset the internal GUIEvent stack.
135  */
136  void flushMouseEventStack();
137 
138  /**
139  * Add the current mouse GUIEvent to the internal stack.
140  *
141  * \param ea the current event class with a mouse event
142  */
143  void addMouseEvent( const osgGA::GUIEventAdapter& ea );
144 
145  /**
146  * Calculate the movement of the camera for the given mouse movement.
147  *
148  * \return true is camera has moved and a redraw is required
149  */
150  bool calcMovement();
151 
152  /**
153  * The older event from the internal event stack.
154  */
155  osg::ref_ptr< const osgGA::GUIEventAdapter > m_ga_t1;
156 
157  /**
158  * The newer event from the internal event stack.
159  */
160  osg::ref_ptr< const osgGA::GUIEventAdapter > m_ga_t0;
161 
162 private:
163  /**
164  * Handles events related to zooming.
165  *
166  * \param ea event class for storing keyboard, mouse and window events
167  *
168  * \return true if event was handled
169  */
170  bool zoom( const osgGA::GUIEventAdapter& ea );
171 
172  /**
173  * The x-position of the viewing window's lower left corner.
174  */
175  double m_positionX;
176 
177  /**
178  * The y-position of the viewing window's lower left corner.
179  */
180  double m_positionY;
181 
182  /**
183  * zoom factor
184  */
185  double m_zoom;
186 };
187 
188 #endif // WGE2DMANIPULATOR_H
A manipulator which changes the view of a 2D scene.
double m_zoom
zoom factor
bool calcMovement()
Calculate the movement of the camera for the given mouse movement.
virtual const char * className() const
Return the name of the object's class type.
bool zoom(const osgGA::GUIEventAdapter &ea)
Handles events related to zooming.
void flushMouseEventStack()
Reset the internal GUIEvent stack.
osg::ref_ptr< const osgGA::GUIEventAdapter > m_ga_t0
The newer event from the internal event stack.
virtual void setByInverseMatrix(const osg::Matrixd &matrix)
Set the position of the matrix manipulator using a 4x4 matrix.
virtual void getUsage(osg::ApplicationUsage &usage) const
Get the keyboard and mouse usage of this manipulator.
void addMouseEvent(const osgGA::GUIEventAdapter &ea)
Add the current mouse GUIEvent to the internal stack.
virtual void home(const osgGA::GUIEventAdapter &, osgGA::GUIActionAdapter &us)
Move the camera to the default position.
virtual osg::Matrixd getMatrix() const
Get the position of the manipulator as 4x4 matrix.
WGE2DManipulator()
Constructor.
double m_positionX
The x-position of the viewing window's lower left corner.
virtual void setByMatrix(const osg::Matrixd &matrix)
Set the position of the matrix manipulator using a 4x4 matrix.
virtual ~WGE2DManipulator()
Destructor.
virtual osg::Matrixd getInverseMatrix() const
Get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model...
osg::ref_ptr< const osgGA::GUIEventAdapter > m_ga_t1
The older event from the internal event stack.
virtual void init(const osgGA::GUIEventAdapter &, osgGA::GUIActionAdapter &us)
Start/restart the manipulator.
virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Handle events.
double m_positionY
The y-position of the viewing window's lower left corner.