OpenWalnut  1.4.0
WGEZoomTrackballManipulator.cpp
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 #include "WGEZoomTrackballManipulator.h"
26 #include "WGraphicsEngine.h"
27 
29  TrackballManipulator(),
30  m_zoom( 1.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 WGEZoomTrackballManipulator::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 WGEZoomTrackballManipulator::home( double /* currentTime */ )
61 {
62  m_zoom = 1.0;
63  TrackballManipulator::home( 0 /* currentTime */ );
64 }
65 
66 bool WGEZoomTrackballManipulator::zoom( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
67 {
68  double zoomDelta = 0.0;
69 
70  if( ea.getKey() && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
71  {
72  if( ea.getKey() == 45 ) // -
73  {
74  zoomDelta = -0.05;
75  }
76  if( ea.getKey() == 43 ) // +
77  {
78  zoomDelta = 0.05;
79  }
80  if( zoomDelta != 0.0 )
81  {
82  m_zoom *= 1.0 + zoomDelta;
83  us.requestRedraw();
84  }
85  }
86  else
87  {
88  if( ea.getHandled() )
89  {
90  return true;
91  }
92 
93  switch( ea.getScrollingMotion() )
94  {
95  case osgGA::GUIEventAdapter::SCROLL_UP:
96  zoomDelta = 0.05;
97  break;
98  case osgGA::GUIEventAdapter::SCROLL_DOWN:
99  zoomDelta = -0.05;
100  break;
101  case osgGA::GUIEventAdapter::SCROLL_2D:
102  zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
103  break;
104  // case osgGA::GUIEventAdapter::SCROLL_LEFT:
105  // case osgGA::GUIEventAdapter::SCROLL_RIGHT:
106  // case osgGA::GUIEventAdapter::SCROLL_NONE:
107  default:
108  // do nothing
109  zoomDelta = 0.0;
110  break;
111  }
112  }
113 
114  if( zoomDelta != 0.0 )
115  {
116  m_zoom *= 1.0 + zoomDelta;
117  us.requestRedraw();
118  }
119 
120  us.requestContinuousUpdate( false );
121  return true;
122 }
123 
124 bool WGEZoomTrackballManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
125 {
126  _thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.
127 
128  if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested() )
129  {
130  // We set the scene to the manipulator home position if the scene
131  // requests to do so. See WGEScene for more details.
132  home( 0 );
133  return true;
134  }
135  else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL || ea.getKey() == 45 || ea.getKey() == 43 )
136  {
137  return zoom( ea, us );
138  }
139  // NOTE: we need to ignore the right mouse-button drag! This manipulates the underlying Trackball Manipulator while, at the same time, is
140  // used for moving ROIS! Zooming is done using Scroll Wheel or +/- keys.
141  else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
142  {
143  if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
144  {
145  return true;
146  }
147  else if( ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && ( m_paintMode == 1 ) )
148  {
149  return true;
150  }
151  else
152  {
153  return TrackballManipulator::handle( ea, us );
154  }
155  }
156  else
157  {
158  return TrackballManipulator::handle( ea, us );
159  }
160 }
161 
163 {
164  m_paintMode = mode;
165 }
166 
168 {
169  m_allowThrow = allowThrow;
170 }
171 
173 {
174  return m_allowThrow;
175 }
176