Commit cb725907 authored by cornimueller's avatar cornimueller
Browse files

[ADD] Added a new Manipulator for 2D views. Currently only panning works (middle mouse button).

parent 296a4c02
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// 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 "WGE2DManipulator.h"
WGE2DManipulator::WGE2DManipulator()
: m_positionX( 0.0 ),
m_positionY( 0.0 )
{
}
const char* WGE2DManipulator::className() const
{
return "WGE2DManipulator";
}
void WGE2DManipulator::setByMatrix( const osg::Matrixd& matrix )
{
m_positionX = matrix.getTrans().x();
m_positionY = matrix.getTrans().y();
}
void WGE2DManipulator::setByInverseMatrix( const osg::Matrixd& matrix )
{
m_positionX = -matrix.getTrans().x();
m_positionY = -matrix.getTrans().y();
}
osg::Matrixd WGE2DManipulator::getMatrix() const
{
return osg::Matrixd::translate( m_positionX, m_positionY, 0.0 );
}
osg::Matrixd WGE2DManipulator::getInverseMatrix() const
{
return osg::Matrixd::translate( -m_positionX, -m_positionY, 0.0 );
}
void WGE2DManipulator::home( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ) // NOLINT We can not change the interface of OSG
{
m_positionX = 0.0;
m_positionY = 0.0;
us.requestRedraw();
flushMouseEventStack();
}
void WGE2DManipulator::init( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ) // NOLINT We can not change the interface of OSG
{
flushMouseEventStack();
us.requestContinuousUpdate( false );
}
bool WGE2DManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
{
bool handled;
switch( ea.getEventType() )
{
case( osgGA::GUIEventAdapter::PUSH ):
{
addMouseEvent( ea );
handled = true;
break;
}
case( osgGA::GUIEventAdapter::DRAG ):
{
addMouseEvent( ea );
if( calcMovement() )
{
us.requestRedraw();
}
handled = true;
break;
}
case( osgGA::GUIEventAdapter::KEYDOWN ):
{
if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Space )
{
home( ea, us );
handled = true;
}
else
{
handled = false;
}
break;
}
default:
handled = false;
}
return handled;
}
void WGE2DManipulator::getUsage( osg::ApplicationUsage& usage ) const // NOLINT We can not change the interface of OSG
{
usage.addKeyboardMouseBinding( "Space", "Reset the view to home" );
}
WGE2DManipulator::~WGE2DManipulator()
{
}
void WGE2DManipulator::flushMouseEventStack()
{
m_ga_t1 = NULL;
m_ga_t0 = NULL;
}
void WGE2DManipulator::addMouseEvent( const osgGA::GUIEventAdapter& ea )
{
m_ga_t1 = m_ga_t0;
m_ga_t0 = &ea;
}
bool WGE2DManipulator::calcMovement()
{
bool changed = false;
// return if less then two events have been added.
if( m_ga_t0.valid() && m_ga_t1.valid() )
{
unsigned int buttonMask = m_ga_t0->getButtonMask();
if( buttonMask == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON
|| buttonMask == ( osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON | osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON ) )
{
m_positionX += m_ga_t1->getX() - m_ga_t0->getX();
m_positionY += m_ga_t1->getY() - m_ga_t0->getY();
changed = true;
}
}
return changed;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// 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 WGE2DMANIPULATOR_H
#define WGE2DMANIPULATOR_H
#include <osgGA/MatrixManipulator>
/**
* A manipulator which changes the view of a 2D scene. Does things like panning
* and zooming.
*/
class WGE2DManipulator : public osgGA::MatrixManipulator
{
public:
/**
* Constructor
*/
WGE2DManipulator();
/**
* Return the name of the object's class type.
*
* \return the name of the object's class type
*/
virtual const char* className() const;
/**
* Set the position of the matrix manipulator using a 4x4 matrix.
*
* \param matrix a 4x4 matrix
*/
virtual void setByMatrix( const osg::Matrixd& matrix );
/**
* Set the position of the matrix manipulator using a 4x4 matrix.
*
* \param matrix a 4x4 matrix
*/
virtual void setByInverseMatrix( const osg::Matrixd& matrix );
/**
* Get the position of the manipulator as 4x4 matrix.
*
* \return the position of the manipulator as 4x4 matrix
*/
virtual osg::Matrixd getMatrix() const;
/**
* Get the position of the manipulator as a inverse matrix of the
* manipulator, typically used as a model view matrix.
*
* \return the position of the manipulator as a inverse matrix
*/
virtual osg::Matrixd getInverseMatrix() const;
/**
* Move the camera to the default position.
*
* \param us the action adapter used to request actions of the GUI
*/
virtual void home( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ); // NOLINT We can not change the interface of OSG
/**
* Start/restart the manipulator.
*
* \param us the action adapter used to request actions of the GUI
*/
virtual void init( const osgGA::GUIEventAdapter& /*ea*/, osgGA::GUIActionAdapter& us ); // NOLINT We can not change the interface of OSG
/**
* Handle events
*
* \param ea event class for storing Keyboard, mouse and window events
* \param us the action adapter used to request actions of the GUI
* \return true if handled, false otherwise
*/
virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
/**
* Get the keyboard and mouse usage of this manipulator.
*
* \param usage the application usage
*/
virtual void getUsage( osg::ApplicationUsage& usage ) const; // NOLINT We can not change the interface of OSG
protected:
/**
* Destructor
*
* Note, is protected so that objects cannot be deleted other than by being
* dereferenced and the reference count being zero (see osg::Referenced),
* preventing the deletion of objects which are still in use.
*/
~WGE2DManipulator();
/**
* Reset the internal GUIEvent stack.
*/
void flushMouseEventStack();
/**
* Add the current mouse GUIEvent to the internal stack.
*
* \param ea the current event class with a mouse event
*/
void addMouseEvent( const osgGA::GUIEventAdapter& ea );
/**
* Calculate the movement of the camera for the given mouse movement.
*
* \return true is camera has moved and a redraw is required
*/
bool calcMovement();
/**
* The older event from the internal event stack.
*/
osg::ref_ptr< const osgGA::GUIEventAdapter > m_ga_t1;
/**
* The newer event from the internal event stack.
*/
osg::ref_ptr< const osgGA::GUIEventAdapter > m_ga_t0;
private:
/**
* The x-position of the viewing window's lower left corner.
*/
double m_positionX;
/**
* The y-position of the viewing window's lower left corner.
*/
double m_positionY;
};
#endif // WGE2DMANIPULATOR_H
......@@ -41,6 +41,8 @@
#include <osgDB/ReadFile>
#include "exceptions/WGEInitFailed.h"
#include "WGE2DManipulator.h"
#include "WGEZoomTrackballManipulator.h"
#include "WPickHandler.h"
#include "../kernel/WKernel.h"
......@@ -58,10 +60,19 @@ WGEViewer::WGEViewer( std::string name, osg::ref_ptr<WindowData> wdata, int x, i
m_View->setCamera( new WGECamera( width, height, projectionMode ) );
m_View->getCamera()->setGraphicsContext( m_GraphicsContext );
if( projectionMode == WGECamera::ORTHOGRAPHIC || projectionMode == WGECamera::PERSPECTIVE )
// camera manipulator
switch( projectionMode )
{
// camera manipulator
m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
case( WGECamera::ORTHOGRAPHIC ):
case( WGECamera::PERSPECTIVE ):
m_View->setCameraManipulator( new WGEZoomTrackballManipulator() );
break;
case( WGECamera::TWO_D ):
m_View->setCameraManipulator( new WGE2DManipulator() );
break;
default:
throw WGEInitFailed( "Unknown projection mode" );
}
// add the stats handler
......
......@@ -44,7 +44,6 @@
#include "WGEGraphicsWindow.h"
#include "WGECamera.h"
#include "WPickHandler.h"
#include "WGEZoomTrackballManipulator.h"
class WColor;
......
......@@ -29,7 +29,9 @@
#include "WQtGLWidget.h"
#include "../../graphicsEngine/WGE2DManipulator.h"
#include "../../graphicsEngine/WGEViewer.h"
#include "../../graphicsEngine/WGEZoomTrackballManipulator.h"
#include "../../common/WFlag.h"
#include "../../common/WConditionOneShot.h"
#include "../../kernel/WKernel.h"
......@@ -123,9 +125,14 @@ void WQtGLWidget::setCameraManipulator( WQtGLWidget::CameraManipulators manipula
m_Viewer->setCameraManipulator( new( osgGA::UFOManipulator ) );
break;
case TWO_D:
std::cout << "selected 2d manipulator" << std::endl;
m_Viewer->setCameraManipulator( new( WGE2DManipulator ) );
break;
case TRACKBALL:
default:
std::cout << "selected OpwnWalnut's zoom trackball manipulator" << std::endl;
std::cout << "selected OpenWalnut's zoom trackball manipulator" << std::endl;
m_Viewer->setCameraManipulator( new( WGEZoomTrackballManipulator ) );
break;
......@@ -216,6 +223,9 @@ void WQtGLWidget::keyReleaseEvent( QKeyEvent* event )
case Qt::Key_5:
setCameraManipulator( UFO );
break;
case Qt::Key_6:
setCameraManipulator( TWO_D );
break;
}
m_Viewer->keyEvent( WGEViewer::KEYRELEASE, *event->text().toAscii().data() );
......
......@@ -80,7 +80,7 @@ public:
*/
enum CameraManipulators
{
TRACKBALL, TERRAIN, UFO, DRIVE, FLIGHT
TRACKBALL, TERRAIN, UFO, DRIVE, FLIGHT, TWO_D
};
/**
......
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