1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
25
#include "WGE2DManipulator.h"
28
WGE2DManipulator::WGE2DManipulator()
29
: m_positionX( -64.0 ),
35
const char* WGE2DManipulator::className() const
37
return "WGE2DManipulator";
40
void WGE2DManipulator::setByMatrix( const osg::Matrixd& matrix )
42
m_positionX = matrix.getTrans().x();
43
m_positionY = matrix.getTrans().y();
44
m_zoom = 1.0 / matrix.getScale().x();
47
void WGE2DManipulator::setByInverseMatrix( const osg::Matrixd& matrix )
49
m_positionX = -matrix.getTrans().x();
50
m_positionY = -matrix.getTrans().y();
51
m_zoom = matrix.getScale().x();
54
osg::Matrixd WGE2DManipulator::getMatrix() const
56
return osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 ) * osg::Matrixd::translate( m_positionX, m_positionY, 0.0 );
59
osg::Matrixd WGE2DManipulator::getInverseMatrix() const
61
return osg::Matrixd::translate( -m_positionX, -m_positionY, 0.0 ) * osg::Matrixd::scale( m_zoom, m_zoom, 1.0 );
64
void WGE2DManipulator::home( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ) // NOLINT We can not change the interface of OSG
71
flushMouseEventStack();
74
void WGE2DManipulator::init( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ) // NOLINT We can not change the interface of OSG
76
flushMouseEventStack();
78
us.requestContinuousUpdate( false );
81
bool WGE2DManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
84
switch( ea.getEventType() )
86
case( osgGA::GUIEventAdapter::PUSH ):
92
case( osgGA::GUIEventAdapter::DRAG ):
102
case( osgGA::GUIEventAdapter::SCROLL ):
111
// TODO(cornimueller): Also allow to zoom using +/- keys.
112
case( osgGA::GUIEventAdapter::KEYDOWN ):
114
if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Space )
132
void WGE2DManipulator::getUsage( osg::ApplicationUsage& usage ) const // NOLINT We can not change the interface of OSG
134
usage.addKeyboardMouseBinding( "Space", "Reset the view to home" );
137
WGE2DManipulator::~WGE2DManipulator()
141
void WGE2DManipulator::flushMouseEventStack()
147
void WGE2DManipulator::addMouseEvent( const osgGA::GUIEventAdapter& ea )
153
bool WGE2DManipulator::calcMovement()
155
bool changed = false;
156
// return if less then two events have been added.
157
if( m_ga_t0.valid() && m_ga_t1.valid() )
159
unsigned int buttonMask = m_ga_t0->getButtonMask();
160
if( buttonMask == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON
161
|| buttonMask == ( osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON | osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON ) )
163
m_positionX += ( m_ga_t1->getX() - m_ga_t0->getX() ) / m_zoom;
164
m_positionY += ( m_ga_t1->getY() - m_ga_t0->getY() ) / m_zoom;
172
bool WGE2DManipulator::zoom( const osgGA::GUIEventAdapter& ea )
174
bool changed = false;
177
switch( ea.getScrollingMotion() )
179
case osgGA::GUIEventAdapter::SCROLL_UP:
182
case osgGA::GUIEventAdapter::SCROLL_DOWN:
185
case osgGA::GUIEventAdapter::SCROLL_2D:
186
zoomDelta = -0.05 / 120.0 * ea.getScrollingDeltaY();
188
// case osgGA::GUIEventAdapter::SCROLL_LEFT:
189
// case osgGA::GUIEventAdapter::SCROLL_RIGHT:
190
// case osgGA::GUIEventAdapter::SCROLL_NONE:
196
if( zoomDelta != 0.0 )
198
m_zoom *= 1.0 + zoomDelta;
199
// TODO(cornimueller): Correct m_positionX and m_positionY to zoom into the current
200
// mouse position, not into the lower left corner.