文档章节

osg源码分析osgGA/KeySwitchMatrixManipulator

哈全文
 哈全文
发布于 2016/11/18 21:57
字数 1222
阅读 59
收藏 0

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
*/

#ifndef OSGUTIL_KEYSWITCMATRIXMANIPULATOR
#define OSGUTIL_KEYSWITCMATRIXMANIPULATOR 1

#include <osgGA/Export>
#include <osgGA/CameraManipulator>
#include <osgGA/GUIEventHandler>

namespace osgGA{

class GUIActionAdapter;

/**
KeySwitchMatrixManipulator is a decorator which allows the type of camera manipulator
being used to be switched by pressing a key. E.g. '1' for a TrackballManipultor,
'2' for a DriveManipulator, '3' for a FlightManipulator. The manipulators available,
and the associated switch keys, can be configured.
*/

/**

使用按键来操切换操作器类

**/
class OSGGA_EXPORT KeySwitchMatrixManipulator : public CameraManipulator
{
    public:

    /*

std::pair是一个结构体,定义pair比然后插入到map中比在map中直接插入键值对效率要高,标准的OSG应该用这种方式来操作map;

*/

        typedef std::pair<std::string, osg::ref_ptr<CameraManipulator> > NamedManipulator;
        typedef std::map<int, NamedManipulator> KeyManipMap;

        virtual const char* className() const { return "KeySwitchMatrixManipulator"; }

        /**
        Add a camera manipulator with an associated name, and a key to
        trigger the switch,

添加操作器(漫游器)到场景中,同时添加快捷键和标示,这样操作快捷键就可以调用操作器了;
        */

        void addMatrixManipulator(int key, std::string name, CameraManipulator *cm);

        /**
        Add a camera manipulator with an autogenerated keybinding which is '1' + previous number of camera's registerd.
        */
        void addNumberedMatrixManipulator(CameraManipulator *cm);

        unsigned int getNumMatrixManipulators() const { return _manips.size(); }

        void selectMatrixManipulator(unsigned int num);

        /** Get the complete list of manipulators attached to this keyswitch manipulator.*/
        KeyManipMap& getKeyManipMap() { return _manips; }

        /** Get the const complete list of manipulators attached to this keyswitch manipulator.*/
        const KeyManipMap& getKeyManipMap() const { return _manips; }


        /** Get the current active manipulators.*/
        CameraManipulator* getCurrentMatrixManipulator() { return _current.get(); }

        /** Get the const current active manipulators.*/
        const CameraManipulator* getCurrentMatrixManipulator() const { return _current.get(); }


        /** Get manipulator assigned to a specified index.*/
        CameraManipulator* getMatrixManipulatorWithIndex(unsigned int key);

        /** Get const manipulator assigned to a specified index.*/
        const CameraManipulator* getMatrixManipulatorWithIndex(unsigned int key) const;

        /** Get manipulator assigned to a specified key.*/
        CameraManipulator* getMatrixManipulatorWithKey(unsigned int key);

        /** Get const manipulator assigned to a specified key.*/
        const CameraManipulator* getMatrixManipulatorWithKey(unsigned int key) const;


        // Overrides from CameraManipulator...

        /** set the coordinate frame which callback tells the manipulator which way is up, east and north.*/
        virtual void setCoordinateFrameCallback(CoordinateFrameCallback* cb);

        /** Set the position of the matrix manipulator using a 4x4 Matrix.*/
        virtual void setByMatrix(const osg::Matrixd& matrix) { _current->setByMatrix(matrix); }

        /** set the position of the matrix manipulator using a 4x4 Matrix.*/
        virtual void setByInverseMatrix(const osg::Matrixd& matrix) { _current->setByInverseMatrix(matrix); }

        /** get the position of the manipulator as 4x4 Matrix.*/
        virtual osg::Matrixd getMatrix() const { return _current->getMatrix(); }

        /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
        virtual osg::Matrixd getInverseMatrix() const { return _current->getInverseMatrix(); }

        /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
        virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return _current->getFusionDistanceMode(); }

        /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
        virtual float getFusionDistanceValue() const { return _current->getFusionDistanceValue(); }


        virtual void setNode(osg::Node* n);

        virtual const osg::Node* getNode() const        { return _current->getNode(); }

        virtual osg::Node* getNode()                    { return _current->getNode(); }

        virtual void setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition=false);

        virtual void setAutoComputeHomePosition(bool flag);

        virtual void computeHomePosition();

        virtual void home(const GUIEventAdapter& ee,GUIActionAdapter& aa);

        virtual void init(const GUIEventAdapter& ee,GUIActionAdapter& aa) { if (_current.valid()) _current->init(ee,aa); }

        virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);

        /** Get the keyboard and mouse usage of this manipulator.*/
        virtual void getUsage(osg::ApplicationUsage& usage) const;

    private:

        KeyManipMap _manips;

        osg::ref_ptr<CameraManipulator> _current;
};

}

#endif

//////////////////////////////////////源码////////////////////////////////////////////////////////////////

#include <osgGA/KeySwitchMatrixManipulator>
#include <osg/Notify>

using namespace osgGA;

void KeySwitchMatrixManipulator::addMatrixManipulator(int key, std::string name, CameraManipulator *cm)
{
    if(!cm) return;

    _manips[key]=std::make_pair(name,osg::ref_ptr<CameraManipulator>(cm));

    if(!_current)
    {
        _current=cm;
        _current->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition);
        _current->setNode(0);
        _current->setCoordinateFrameCallback(getCoordinateFrameCallback());
        _current->setByMatrix(getMatrix());
    }
}

void KeySwitchMatrixManipulator::addNumberedMatrixManipulator(CameraManipulator *cm)
{
    if(!cm) return;
    addMatrixManipulator('1'+_manips.size(),cm->className(),cm);
}

void KeySwitchMatrixManipulator::selectMatrixManipulator(unsigned int num)
{
    unsigned int manipNo = 0;
    KeyManipMap::iterator itr;
    for(itr=_manips.begin();
        manipNo!=num && itr!=_manips.end();
        ++itr,++manipNo)
    {
    }

    if (itr!=_manips.end())
    {
        itr->second.second->setHomePosition(_homeEye,_homeCenter,_homeUp,_autoComputeHomePosition);

        if (_current.valid())
        {
            if ( !itr->second.second->getCoordinateFrameCallback() )
            {
                itr->second.second->setCoordinateFrameCallback(_current->getCoordinateFrameCallback());
            }

            if ( !itr->second.second->getNode() )
            {
                itr->second.second->setNode(_current->getNode());
            }
            itr->second.second->setByMatrix(_current->getMatrix());
        }
        _current = itr->second.second;
    }
}

void KeySwitchMatrixManipulator::setNode(osg::Node* node)
{
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setNode(node);
    }
}

void KeySwitchMatrixManipulator::setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition)
{
    CameraManipulator::setHomePosition(eye, center, up, autoComputeHomePosition);
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setHomePosition(eye, center, up, autoComputeHomePosition);
    }
}

void KeySwitchMatrixManipulator::setAutoComputeHomePosition(bool flag)
{
    _autoComputeHomePosition = flag;
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setAutoComputeHomePosition(flag);
    }
}

void KeySwitchMatrixManipulator::computeHomePosition()
{
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->computeHomePosition();
    }
}

void KeySwitchMatrixManipulator::home(const GUIEventAdapter& ee,GUIActionAdapter& aa)
{
    // call home for all child manipulators
    // (this can not be done just for current manipulator,
    // because it is not possible to transfer some manipulator
    // settings across manipulators using just MatrixManipulator interface
    // (one problematic variable is for example OrbitManipulator::distance
    // that can not be passed by setByMatrix method),
    // thus we have to call home on all of them)
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->home(ee,aa);
    }
}

void KeySwitchMatrixManipulator::setCoordinateFrameCallback(CoordinateFrameCallback* cb)
{
    _coordinateFrameCallback = cb;
    for(KeyManipMap::iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        itr->second.second->setCoordinateFrameCallback(cb);
    }
}

CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index)
{
    unsigned i=0;
    for(KeyManipMap::iterator itr = _manips.begin();
         itr != _manips.end();
         ++itr, ++i)
    {
        if (i==index) return itr->second.second.get();
    }
    return 0;
}

const CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithIndex(unsigned int index) const
{
    unsigned i=0;
    for(KeyManipMap::const_iterator itr = _manips.begin();
         itr != _manips.end();
         ++itr, ++i)
    {
        if (i==index) return itr->second.second.get();
    }
    return 0;
}

CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key)
{
    KeyManipMap::iterator itr = _manips.find(key);
    if (itr!=_manips.end()) return itr->second.second.get();
    else return 0;
}

const CameraManipulator* KeySwitchMatrixManipulator::getMatrixManipulatorWithKey(unsigned int key) const
{
    KeyManipMap::const_iterator itr = _manips.find(key);
    if (itr!=_manips.end()) return itr->second.second.get();
    else return 0;
}

bool KeySwitchMatrixManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa)
{
    if (!_current) return false;

    bool handled = false;

    if (!ea.getHandled() && ea.getEventType()==GUIEventAdapter::KEYDOWN)
    {

        KeyManipMap::iterator it=_manips.find(ea.getKey());
        if(it != _manips.end())
        {
            OSG_INFO<<"Switching to manipulator: "<<(*it).second.first<<std::endl;
            if ( !it->second.second->getNode() )
            {
                it->second.second->setNode(_current->getNode());
            }
            it->second.second->setByMatrix(_current->getMatrix());
            it->second.second->init(ea,aa);
            _current = it->second.second;

            handled = true;
        }
    }

    return _current->handle(ea,aa) || handled;
}

void KeySwitchMatrixManipulator::getUsage(osg::ApplicationUsage& usage) const
{
    for(KeyManipMap::const_iterator itr=_manips.begin();
        itr!=_manips.end();
        ++itr)
    {
        std::string key; key += (char)(itr->first);
        std::string explanation(std::string("Select '")+itr->second.first+std::string("' camera manipulator"));
        if (_current==itr->second.second) explanation += " (default)";

        usage.addKeyboardMouseBinding(key,explanation);
        itr->second.second->getUsage(usage);
    }
}

© 著作权归作者所有

共有 人打赏支持
哈全文
粉丝 14
博文 92
码字总数 22122
作品 0
海淀
其他
私信 提问
osg源码分析osgViewer/Viewer

/ --c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGra......

哈全文
2016/11/18
26
0
OSG向场景中添加osgParticle粒子效果

目的: 向场景中添加自定义的osgParticle实例,模拟坦克模型在地形上运动时产生的烟尘。 ----------------------------- 概述: 添加粒子效果可以有效提高仿真程序的外观和真实性。粒子引擎一...

长平狐
2012/10/08
1K
0
osg的一些小问题 关于漫游器的 求解答

分析了一个漫游器 , 其中有一点不是很明白,就是在交互操作的时候 响应键盘的WASD的字母,例如 W 前进 ChangePosition(osg::vec3(0,m_speed*sinf(osg::PI_2+m_Vposition.v[2]),0)) ChangePo...

追寻灯火阑珊
2014/05/09
328
0
nginx统一入口 多服务出口

nginx配置多ip和端口统一调用入口logformat中$upstreamaddr 标识打印转发的url地址配置upstream和location http { tcp_nopush on; keepalive_timeout 0; gzip on; charset koi8-r; access_l......

GoldenVein
09/25
0
0
lemonrong/osg3DViewer

osg3DViewer A 3D File Viewer based on OSG. Use QT UI. Created for self learning for OSG. It's re-writed from osGraphX. CMake files was changed. It's more easy to build by cmake.......

lemonrong
2016/05/07
0
0

没有更多内容

加载失败,请刷新页面

加载更多

HashTable和Vector为什么逐渐被废弃

HashTable,不允许键值为null,还一个就是put方法使用sychronized方法进行线程同步,单线程无需同步,多线程可用concurren包的类型。 如编程思想里面说的作为工具类,封闭性做的不好没有一个...

noob_chr
昨天
0
0
Win10 下安装Win7双系统

很多人买了预装64位Win8/8.1的电脑后想重装(或者再安装一个)Win7系统,但是折腾半天发现以前的方法根本不奏效。这是因为预装Win8/8.1的电脑统一采用了UEFI+GPT引导模式,传统的BIOS(Legacy...

yaly
昨天
1
0

中国龙-扬科
昨天
1
0
假若明天来临——《AI.未来》读后感3900字

假若明天来临——《AI.未来》读后感3900字: 你有没有想过,如果有一天你被确诊为癌症患者,你会做些什么?你有没有想过,在你百年之后,你希望你的墓碑上刻写着什么内容? 在我翻开李开复老...

原创小博客
昨天
1
0
tomcat线程模型

Connector结构 BIO模式 NIO模式

grace_233
昨天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部