OpenWalnut  1.5.0dev
WGEAdvancedManipulator.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2017 OpenWalnut Community
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 #include "WGEAdvancedManipulator.h"
26 #include "WGraphicsEngine.h"
27 
29  TrackballManipulator(),
30  m_zoom( 100.0 ),
31  m_allowThrow( false ),
32  m_paintMode( 0 )
33 {
34  setTrackballSize( .3 ); // changes the effect of a mouse move for rotation
35 }
36 
37 void WGEAdvancedManipulator::setByMatrix( const osg::Matrixd& matrix )
38 {
39  m_zoom = 1.0 / matrix.getScale()[0];
40 
41  // The zoom needs to be undone before forwarding the matrix.
42  TrackballManipulator::setByMatrix( osg::Matrixd::inverse( osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) ) * matrix );
43 }
44 
46 {
47  return osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) * TrackballManipulator::getMatrix();
48 }
49 
51 {
52  return TrackballManipulator::getMatrix();
53 }
54 
56 {
57  return TrackballManipulator::getInverseMatrix() * osg::Matrixd::scale( m_zoom, m_zoom, m_zoom );
58 }
59 
60 void WGEAdvancedManipulator::home( double /* currentTime */ )
61 {
62  osg::Vec3d center;
63  osg::Vec3d dummy;
64  TrackballManipulator::getHomePosition( dummy, center, dummy );
65 
66  m_zoom = 100.0 / center[0];
67 
68  TrackballManipulator::home( 0 /* currentTime */ );
69 }
70 
71 bool WGEAdvancedManipulator::zoom( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
72 {
73  double zoomDelta = 0.0;
74 
75  if( ea.getKey() && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
76  {
77  if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus )
78  {
79  zoomDelta = -0.05;
80  }
81  if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
82  {
83  zoomDelta = 0.05;
84  }
85  if( zoomDelta != 0.0 )
86  {
87  m_zoom *= 1.0 + zoomDelta;
88  us.requestRedraw();
89  }
90  }
91  else
92  {
93  if( ea.getHandled() )
94  {
95  return true;
96  }
97 
98  switch( ea.getScrollingMotion() )
99  {
100  case osgGA::GUIEventAdapter::SCROLL_UP:
101  zoomDelta = 0.05;
102  break;
103  case osgGA::GUIEventAdapter::SCROLL_DOWN:
104  zoomDelta = -0.05;
105  break;
106  case osgGA::GUIEventAdapter::SCROLL_2D:
107  zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
108  break;
109  // case osgGA::GUIEventAdapter::SCROLL_LEFT:
110  // case osgGA::GUIEventAdapter::SCROLL_RIGHT:
111  // case osgGA::GUIEventAdapter::SCROLL_NONE:
112  default:
113  // do nothing
114  zoomDelta = 0.0;
115  break;
116  }
117  }
118 
119  if( zoomDelta != 0.0 )
120  {
121  m_zoom *= 1.0 + zoomDelta;
122  us.requestRedraw();
123  }
124 
125  us.requestContinuousUpdate( false );
126  return true;
127 }
128 
129 bool WGEAdvancedManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
130 {
131  _thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.
132 
133  if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested()
134  || ( ea.getKey() == osgGA::GUIEventAdapter::KEY_Space
135  && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
136  )
137  {
138  // We set the scene to the manipulator home position if the scene
139  // requests to do so. See WGEScene for more details.
140  home( 0 );
141  us.requestRedraw();
142  return true;
143  }
144  else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL
145  || ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus
146  || ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
147  {
148  return zoom( ea, us );
149  }
150  // NOTE: we need to ignore the right mouse-button drag! This manipulates the underlying Trackball Manipulator while, at the same time, is
151  // used for moving ROIS! Zooming is done using Scroll Wheel or +/- keys.
152  else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
153  {
154  if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
155  {
156  return true;
157  }
158  else if( ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && ( m_paintMode == 1 ) )
159  {
160  return true;
161  }
162  else
163  {
164  return TrackballManipulator::handle( ea, us );
165  }
166  }
167  else
168  {
169  return TrackballManipulator::handle( ea, us );
170  }
171 }
172 
174 {
175  m_paintMode = mode;
176 }
177 
178 void WGEAdvancedManipulator::setThrow( bool allowThrow )
179 {
180  m_allowThrow = allowThrow;
181 }
182 
184 {
185  return m_allowThrow;
186 }
void setPaintMode(int mode)
setter for paint mode when set to something different from 0, a left drag should move the scene
bool getThrow() const
Checks whether throwing is active.
WGEAdvancedManipulator()
Default constructor.
void setThrow(bool allowThrow=true)
En-/Disables throwing.
bool zoom(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Handles events related to zooming.
bool m_allowThrow
Do we want the auto-rotation thingy?
virtual osg::Matrixd getMatrix() const
Get the position of the manipulator as 4x4 matrix.
virtual osg::Matrixd getInverseMatrix() const
Get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model...
virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
Handle events, return true if handled, false otherwise.
virtual osg::Matrixd getMatrixWithoutZoom() const
Get the manipulator only containing rotation and translation.
virtual void setByMatrix(const osg::Matrixd &matrix)
Set the position of the manipulator using a 4x4 matrix.
virtual void home(double currentTime)
Move the camera to the default position.
static std::shared_ptr< WGraphicsEngine > getGraphicsEngine()
Returns instance of the graphics engine.