Commit 2d548599 authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[ADD] new camera manipulator with improved home() function

.. more work on the manipulator plannes
parent 506e8ea5
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2017 OpenWalnut Community
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WGEAdvancedManipulator.h"
#include "WGraphicsEngine.h"
WGEAdvancedManipulator::WGEAdvancedManipulator():
TrackballManipulator(),
m_zoom( 100.0 ),
m_allowThrow( false ),
m_paintMode( 0 )
{
setTrackballSize( .3 ); // changes the effect of a mouse move for rotation
}
void WGEAdvancedManipulator::setByMatrix( const osg::Matrixd& matrix )
{
m_zoom = 1.0 / matrix.getScale()[0];
// The zoom needs to be undone before forwarding the matrix.
TrackballManipulator::setByMatrix( osg::Matrixd::inverse( osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) ) * matrix );
}
osg::Matrixd WGEAdvancedManipulator::getMatrix() const
{
return osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) * TrackballManipulator::getMatrix();
}
osg::Matrixd WGEAdvancedManipulator::getMatrixWithoutZoom() const
{
return TrackballManipulator::getMatrix();
}
osg::Matrixd WGEAdvancedManipulator::getInverseMatrix() const
{
return TrackballManipulator::getInverseMatrix() * osg::Matrixd::scale( m_zoom, m_zoom, m_zoom );
}
void WGEAdvancedManipulator::home( double /* currentTime */ )
{
osg::Vec3d center;
osg::Vec3d dummy;
TrackballManipulator::getHomePosition( dummy, center, dummy);
m_zoom = 100.0 / center[0];
TrackballManipulator::home( 0 /* currentTime */ );
}
bool WGEAdvancedManipulator::zoom( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
{
double zoomDelta = 0.0;
if( ea.getKey() && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
{
if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus )
{
zoomDelta = -0.05;
}
if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
{
zoomDelta = 0.05;
}
if( zoomDelta != 0.0 )
{
m_zoom *= 1.0 + zoomDelta;
us.requestRedraw();
}
}
else
{
if( ea.getHandled() )
{
return true;
}
switch( ea.getScrollingMotion() )
{
case osgGA::GUIEventAdapter::SCROLL_UP:
zoomDelta = 0.05;
break;
case osgGA::GUIEventAdapter::SCROLL_DOWN:
zoomDelta = -0.05;
break;
case osgGA::GUIEventAdapter::SCROLL_2D:
zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
break;
// case osgGA::GUIEventAdapter::SCROLL_LEFT:
// case osgGA::GUIEventAdapter::SCROLL_RIGHT:
// case osgGA::GUIEventAdapter::SCROLL_NONE:
default:
// do nothing
zoomDelta = 0.0;
break;
}
}
if( zoomDelta != 0.0 )
{
m_zoom *= 1.0 + zoomDelta;
us.requestRedraw();
}
us.requestContinuousUpdate( false );
return true;
}
bool WGEAdvancedManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
{
_thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.
if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested()
|| ( ea.getKey() == osgGA::GUIEventAdapter::KEY_Space
&& ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
)
{
// We set the scene to the manipulator home position if the scene
// requests to do so. See WGEScene for more details.
home( 0 );
us.requestRedraw();
return true;
}
else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL
|| ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus
|| ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
{
return zoom( ea, us );
}
// NOTE: we need to ignore the right mouse-button drag! This manipulates the underlying Trackball Manipulator while, at the same time, is
// used for moving ROIS! Zooming is done using Scroll Wheel or +/- keys.
else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
{
if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
{
return true;
}
else if( ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && ( m_paintMode == 1 ) )
{
return true;
}
else
{
return TrackballManipulator::handle( ea, us );
}
}
else
{
return TrackballManipulator::handle( ea, us );
}
}
void WGEAdvancedManipulator::setPaintMode( int mode )
{
m_paintMode = mode;
}
void WGEAdvancedManipulator::setThrow( bool allowThrow )
{
m_allowThrow = allowThrow;
}
bool WGEAdvancedManipulator::getThrow() const
{
return m_allowThrow;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2017 OpenWalnut Community
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WGEADVANCEDMANIPULATOR_H
#define WGEADVANCEDMANIPULATOR_H
#include <osgGA/TrackballManipulator>
/**
* TrackballManipulator with added mouse wheel zoom. Home function
* will also choose appropriate zoom for ALL nodes in the scene graph.
* This means also for the deactivated ones.
*/
class WGEAdvancedManipulator: public osgGA::TrackballManipulator
{
public:
/**
* Default constructor
*/
WGEAdvancedManipulator();
/**
* Set the position of the manipulator using a 4x4 matrix.
* \param matrix position of the manipulator
*/
virtual void setByMatrix( const osg::Matrixd& matrix );
/**
* Get the position of the manipulator as 4x4 matrix.
*
* \return the matrix representing the transformation
*/
virtual osg::Matrixd getMatrix() const;
/**
* Get the manipulator only containing rotation and translation.
*
* \return the matrix with translation and rotation.
*/
virtual osg::Matrixd getMatrixWithoutZoom() const;
/**
* Get the position of the manipulator as a inverse matrix of the
* manipulator, typically used as a model view matrix.
*
* \return the inverse matrix
*/
virtual osg::Matrixd getInverseMatrix() const;
/**
* Move the camera to the default position.
* \param currentTime Variable specified in osg class but not used.
*/
virtual void home( double currentTime );
/**
* Handle events, return true if handled, false otherwise.
* \param ea Event class for storing Keyboard, mouse and window events.
* \param us Interface by which GUIEventHandlers may request actions of the GUI system
*
* \return true if event was handled
*/
virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
/**
* Get zoom factor.
*
* \return the current zoom factor
*/
double getZoom() const;
/**
* Set zoom factor.
* \param zoom the zoom factor to be set.
*/
void setZoom( double zoom );
/**
* setter for paint mode
* when set to something different from 0, a left drag should move the scene
*
* \param mode the mode
*/
void setPaintMode( int mode );
/**
* En-/Disables throwing.
*
* \param allowThrow if true, throwing is enabled.
*/
void setThrow( bool allowThrow = true );
/**
* Checks whether throwing is active.
*
* \return true if active
*/
bool getThrow() const;
protected:
private:
/**
* Handles events related to zooming.
* \param ea Event class for storing Keyboard, mouse and window events.
* \param us Interface by which GUIEventHandlers may request actions of the GUI system
*
* \return true if event was handled
*/
bool zoom( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
double m_zoom; //!< Zoom factor.
bool m_allowThrow; //!< Do we want the auto-rotation thingy?
int m_paintMode; //!<paint mode
};
inline double WGEAdvancedManipulator::getZoom() const
{
return m_zoom;
}
inline void WGEAdvancedManipulator::setZoom( double zoom )
{
m_zoom = zoom;
}
#endif // WGEADVANCEDMANIPULATOR_H
......@@ -38,6 +38,7 @@
#include "core/common/WLogger.h"
#include "core/common/WColor.h"
#include "core/graphicsEngine/WGE2DManipulator.h"
#include "core/graphicsEngine/WGEAdvancedManipulator.h"
#include "core/graphicsEngine/WGENoOpManipulator.h"
#include "core/graphicsEngine/WGEViewer.h"
#include "core/graphicsEngine/WGEZoomTrackballManipulator.h"
......@@ -157,19 +158,26 @@ void WQtGLWidget::setCameraManipulator( WQtGLWidget::CameraManipulators manipula
switch( manipulator )
{
case TWO_D:
WLogger::getLogger()->addLogMessage( "Switched to OSG manipulator \"WGE2D\".",
WLogger::getLogger()->addLogMessage( "Switched to camera manipulator \"WGE2D\".",
"WQtGLWidget(" + m_Viewer->getName() + ")",
LL_DEBUG );
m_Viewer->setCameraManipulator( new( WGE2DManipulator ) );
break;
case NO_OP:
WLogger::getLogger()->addLogMessage( "Switched to OSG manipulator \"WGENoOp\".",
WLogger::getLogger()->addLogMessage( "Switched to camera manipulator \"WGENoOp\".",
"WQtGLWidget(" + m_Viewer->getName() + ")",
LL_DEBUG );
m_Viewer->setCameraManipulator( new( WGENoOpManipulator ) );
break;
case ADVANCED:
WLogger::getLogger()->addLogMessage( "Switched to camera manipulator \"ADVANCED\".",
"WQtGLWidget(" + m_Viewer->getName() + ")",
LL_DEBUG );
m_Viewer->setCameraManipulator( new( WGEAdvancedManipulator ) );
break;
case TRACKBALL:
default:
WLogger::getLogger()->addLogMessage( "Switched to OSG manipulator \"WGEZoomTrackball\".",
......@@ -293,6 +301,9 @@ void WQtGLWidget::keyReleaseEvent( QKeyEvent* event )
case Qt::Key_2:
setCameraManipulator( TWO_D );
break;
case Qt::Key_3:
setCameraManipulator( ADVANCED );
break;
}
switch( event->modifiers() )
......
......@@ -80,7 +80,7 @@ public:
*/
enum CameraManipulators
{
TRACKBALL, TWO_D, NO_OP
TRACKBALL, TWO_D, NO_OP, ADVANCED
};
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment