OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
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 {
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 
81  if(zoomDelta != 0.0)
82  {
83  m_zoom *= 1.0 + zoomDelta;
84  us.requestRedraw();
85  }
86  }
87  else
88  {
89  switch( ea.getScrollingMotion() )
90  {
91  case osgGA::GUIEventAdapter::SCROLL_UP:
92  zoomDelta = 0.05;
93  break;
94  case osgGA::GUIEventAdapter::SCROLL_DOWN:
95  zoomDelta = -0.05;
96  break;
97  case osgGA::GUIEventAdapter::SCROLL_2D:
98  zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
99  break;
100  // case osgGA::GUIEventAdapter::SCROLL_LEFT:
101  // case osgGA::GUIEventAdapter::SCROLL_RIGHT:
102  // case osgGA::GUIEventAdapter::SCROLL_NONE:
103  default:
104  // do nothing
105  zoomDelta = 0.0;
106  break;
107  }
108  }
109 
110  if(zoomDelta != 0.0)
111  {
112  m_zoom *= 1.0 + zoomDelta;
113  us.requestRedraw();
114  }
115 
116  us.requestContinuousUpdate( false );
117  return true;
118 }
119 
120 bool WGEZoomTrackballManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
121 {
122  _thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.
123 
124  if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested() )
125  {
126  // We set the scene to the manipulator home position if the scene
127  // requests to do so. See WGEScene for more details.
128  home( 0 );
129  return true;
130  }
131  else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL || ea.getKey() == 45 || ea.getKey() == 43 )
132  {
133  return zoom( ea, us );
134  }
135  // NOTE: we need to ignore the right mouse-button drag! This manipulates the underlying Trackball Manipulator while, at the same time, is
136  // used for moving ROIS! Zooming is done using Scroll Wheel or +/- keys.
137  else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
138  {
139  if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
140  {
141  return true;
142  }
143  else if( ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && ( m_paintMode == 1 ) )
144  {
145  return true;
146  }
147  else
148  {
149  return TrackballManipulator::handle( ea, us );
150  }
151  }
152  else
153  {
154  return TrackballManipulator::handle( ea, us );
155  }
156 }
157 
159 {
160  m_paintMode = mode;
161 }
162 
164 {
165  m_allowThrow = allowThrow;
166 }
167 
169 {
170  return m_allowThrow;
171 }
172