WGEZoomTrackballManipulator.cpp 5.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

25
#include "../common/WPreferences.h"
26 27
#include "WGEZoomTrackballManipulator.h"
#include "WGraphicsEngine.h"
28 29

WGEZoomTrackballManipulator::WGEZoomTrackballManipulator():
30
    TrackballManipulator(),
31 32 33
    m_zoom( 1.0 ),
    m_allowThrow( false ),
    m_paintMode( 0 )
34
{
35
    setTrackballSize( .3 ); // changes the effect of a mouse move for rotation
36
    WPreferences::getPreference( "ge.zoomTrackballManipulator.allowThrow", &m_allowThrow );
37 38 39 40 41
}

void WGEZoomTrackballManipulator::setByMatrix( const osg::Matrixd& matrix )
{
    m_zoom = 1.0 / matrix.getScale()[0];
42 43

    // The zoom needs to be undone before forwarding the matrix.
44
    TrackballManipulator::setByMatrix( osg::Matrixd::inverse( osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) ) * matrix );
45 46 47 48
}

osg::Matrixd WGEZoomTrackballManipulator::getMatrix() const
{
49 50 51 52 53 54
    return osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) * TrackballManipulator::getMatrix();
}

osg::Matrixd WGEZoomTrackballManipulator::getMatrixWithoutZoom() const
{
    return TrackballManipulator::getMatrix();
55 56 57 58
}

osg::Matrixd WGEZoomTrackballManipulator::getInverseMatrix() const
{
59
    return TrackballManipulator::getInverseMatrix() * osg::Matrixd::scale( m_zoom, m_zoom, m_zoom );
60 61
}

Alexander Wiebel's avatar
Alexander Wiebel committed
62
void WGEZoomTrackballManipulator::home( double /* currentTime */ )
63 64
{
    m_zoom = 1.0;
Alexander Wiebel's avatar
Alexander Wiebel committed
65
    TrackballManipulator::home( 0 /* currentTime */ );
66 67
}

Alexander Wiebel's avatar
Alexander Wiebel committed
68
bool WGEZoomTrackballManipulator::zoom( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
69
{
Alexander Wiebel's avatar
Alexander Wiebel committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    double zoomDelta = 0.0;

    if( ea.getKey() && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
    {
        if( ea.getKey() == 45 ) // -
        {
            zoomDelta = -0.05;
        }
        if( ea.getKey() == 43 ) // +
        {
            zoomDelta = 0.05;
        }

        if (zoomDelta != 0.0)
        {
            m_zoom *= 1.0 + zoomDelta;
            us.requestRedraw();
        }
    }
    else
90 91 92 93
    {
        switch( ea.getScrollingMotion() )
        {
            case osgGA::GUIEventAdapter::SCROLL_UP:
94
                zoomDelta = 0.05;
95 96
                break;
            case osgGA::GUIEventAdapter::SCROLL_DOWN:
97
                zoomDelta = -0.05;
98 99
                break;
            case osgGA::GUIEventAdapter::SCROLL_2D:
100
                zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
101
                break;
Alexander Wiebel's avatar
Alexander Wiebel committed
102 103 104
                // case osgGA::GUIEventAdapter::SCROLL_LEFT:
                // case osgGA::GUIEventAdapter::SCROLL_RIGHT:
                // case osgGA::GUIEventAdapter::SCROLL_NONE:
105 106 107 108 109
            default:
                // do nothing
                zoomDelta = 0.0;
                break;
        }
Alexander Wiebel's avatar
Alexander Wiebel committed
110
    }
111

Alexander Wiebel's avatar
Alexander Wiebel committed
112 113 114 115 116 117 118 119 120
    if (zoomDelta != 0.0)
    {
        m_zoom *= 1.0 + zoomDelta;
        us.requestRedraw();
    }

    us.requestContinuousUpdate( false );
    return true;
}
121

Alexander Wiebel's avatar
Alexander Wiebel committed
122 123
bool WGEZoomTrackballManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
{
124 125
    _thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.

126 127 128 129 130 131 132 133
    if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested() )
    {
        // We set the scene to the manipulator home position if the scene
        // requests to do so. See WGEScene for more details.
        home( 0 );
        return true;
    }
    else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL || ea.getKey() == 45 ||  ea.getKey() == 43 )
Alexander Wiebel's avatar
Alexander Wiebel committed
134 135
    {
        return zoom( ea, us );
136
    }
137 138
    // 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.
139
    else if ( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
140
    {
141 142 143 144 145 146 147 148 149 150 151 152
        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 );
        }
153
    }
154 155 156 157 158
    else
    {
        return TrackballManipulator::handle( ea, us );
    }
}
159 160 161 162 163

void WGEZoomTrackballManipulator::setPaintMode( int mode )
{
    m_paintMode = mode;
}