StandardManipulator 8.81 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library 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
 * OpenSceneGraph Public License for more details.
 *
 * StandardManipulator code Copyright (C) 2010 PCJohn (Jan Peciva)
 * while some pieces of code were taken from OSG.
 * Thanks to company Cadwork (www.cadwork.ch) and
 * Brno University of Technology (www.fit.vutbr.cz) for open-sourcing this work.
*/

#ifndef OSGGA_CAMERA_MANIPULATOR
#define OSGGA_CAMERA_MANIPULATOR 1

#include <osgGA/CameraManipulator>


namespace osgGA {


/** StandardManipulator class provides basic functionality
    for user controlled manipulation.*/
class OSGGA_EXPORT StandardManipulator : public CameraManipulator
{
        typedef CameraManipulator inherited;

    public:

        // flags
        enum UserInteractionFlags
        {
            UPDATE_MODEL_SIZE = 0x01,
            COMPUTE_HOME_USING_BBOX = 0x02,
            PROCESS_MOUSE_WHEEL = 0x04,
            SET_CENTER_ON_WHEEL_FORWARD_MOVEMENT = 0x08,
            DEFAULT_SETTINGS = UPDATE_MODEL_SIZE /*| COMPUTE_HOME_USING_BBOX*/ | PROCESS_MOUSE_WHEEL
        };

        StandardManipulator( int flags = DEFAULT_SETTINGS );
        StandardManipulator( const StandardManipulator& m,
                             const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY );

        // We are not using META_Object as this is abstract class.
        // Use META_Object(osgGA,YourManipulator); in your descendant non-abstract classes.
        virtual const char* className() const { return "StandardManipulator"; }

        /** Sets manipulator by eye position and eye orientation.*/
        virtual void setTransformation( const osg::Vec3d& eye, const osg::Quat& rotation ) = 0;

        /** Sets manipulator by eye position, center of rotation, and up vector.*/
        virtual void setTransformation( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up ) = 0;

        /** Gets manipulator's eye position and eye orientation.*/
        virtual void getTransformation( osg::Vec3d& eye, osg::Quat& rotation ) const = 0;

        /** Gets manipulator's focal center, eye position, and up vector.*/
        virtual void getTransformation( osg::Vec3d& eye, osg::Vec3d& center, osg::Vec3d& up ) const = 0;

        virtual void setNode( osg::Node* );
        virtual const osg::Node* getNode() const;
        virtual osg::Node* getNode();

        virtual void setVerticalAxisFixed( bool value );
        inline bool getVerticalAxisFixed() const;
        inline bool getAllowThrow() const;
        virtual void setAllowThrow( bool allowThrow );

        virtual void setAnimationTime( const double t );
        double getAnimationTime() const;
        bool isAnimating() const;
        virtual void finishAnimation();

        virtual void home( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual void home( double );

        virtual void init( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual void getUsage( osg::ApplicationUsage& usage ) const;

    protected:

        virtual bool handleFrame( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleResize( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleMouseMove( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleMouseDrag( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleMousePush( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleMouseRelease( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleKeyDown( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleKeyUp( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleMouseWheel( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool handleMouseDeltaMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );

        virtual bool performMovement();
        virtual bool performMovementLeftMouseButton( const double eventTimeDelta, const double dx, const double dy );
        virtual bool performMovementMiddleMouseButton( const double eventTimeDelta, const double dx, const double dy );
        virtual bool performMovementRightMouseButton( const double eventTimeDelta, const double dx, const double dy );
        virtual bool performMouseDeltaMovement( const float dx, const float dy );
        virtual bool performAnimationMovement( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual void applyAnimationStep( const double currentProgress, const double prevProgress );

        void addMouseEvent( const osgGA::GUIEventAdapter& ea );
        void flushMouseEventStack();
        virtual bool isMouseMoving() const;
        float getThrowScale( const double eventTimeDelta ) const;
        virtual void centerMousePointer( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );

        static void rotateYawPitch( osg::Quat& rotation, const double yaw, const double pitch,
                                    const osg::Vec3d& localUp = osg::Vec3d( 0.,0.,0.) );
        static void fixVerticalAxis( osg::Quat& rotation, const osg::Vec3d& localUp, bool disallowFlipOver );
        void fixVerticalAxis( osg::Vec3d& eye, osg::Quat& rotation, bool disallowFlipOver );
        static void fixVerticalAxis( const osg::Vec3d& forward, const osg::Vec3d& up, osg::Vec3d& newUp,
                                     const osg::Vec3d& localUp, bool disallowFlipOver );
        virtual bool setCenterByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );
        virtual bool startAnimationByMousePointerIntersection( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us );

        // mouse state
        bool _thrown;
        bool _allowThrow;
        float _mouseCenterX, _mouseCenterY;

        // internal event stack comprising last two mouse events.
        osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t1;
        osg::ref_ptr< const osgGA::GUIEventAdapter > _ga_t0;

        /** The approximate amount of time it is currently taking to draw a frame.
            * This is used to compute the delta in translation/rotation during a thrown display update.
            * It allows us to match an delta in position/rotation independent of the rendering frame rate.
            */
        double _delta_frame_time;

        /** The time the last frame started.
            * Used when _rate_sensitive is true so that we can match display update rate to rotation/translation rate.
            */
        double _last_frame_time;

        // scene data
        osg::ref_ptr< osg::Node > _node;
        double _modelSize;
        bool _verticalAxisFixed;

        // animation stuff
        class OSGGA_EXPORT AnimationData : public osg::Referenced {
        public:
            double _animationTime;
            bool _isAnimating;
            double _startTime;
            double _phase;
            AnimationData();
            void start( const double startTime );
        };
        osg::ref_ptr< AnimationData > _animationData;
        virtual void allocAnimationData() { _animationData = new AnimationData(); }

        // flags
        int _flags;

        // flags indicating that a value is relative to model size
        int _relativeFlags;
        inline bool getRelativeFlag( int index ) const;
        inline void setRelativeFlag( int index, bool value );
        static int numRelativeFlagsAllocated;
        static int allocateRelativeFlag();
};


//
//  inline methods
//

inline bool StandardManipulator::getRelativeFlag( int index ) const
{
    return ( _relativeFlags & (0x01 << index) ) != 0;
}

inline void StandardManipulator::setRelativeFlag( int index, bool value )
{
    if( value )  _relativeFlags |= (0x01 << index);
    else  _relativeFlags &= (~0x01 << index);
}

/// Returns whether manipulator preserves camera's "UP" vector.
inline bool StandardManipulator::getVerticalAxisFixed() const
{
    return _verticalAxisFixed;
}

/// Returns true if the camera can be thrown, false otherwise. It defaults to true.
inline bool StandardManipulator::getAllowThrow() const
{
    return _allowThrow;
}


}

#endif /* OSGGA_CAMERA_MANIPULATOR */