osgGA::KeySwitchMatrixManipulator按键选择切换操作器使用

 osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; 
 keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); 
 keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); 
 keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); 
 keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
 viewer.setCameraManipulator( keyswitchManipulator.get() ); 

申请一个使用按键来切换操作器的类,即:osgGA::KeySwitchMatrixManipulator,意思是这样的,往这个类中添操作器,添的时候带个标识和快捷键,然后再把这个类添加到viewer当中,这样viewer运行的时候就可以通过按键来换操作器了,真方便。


源代码分析:osgGA\KeySwitchMatrixManipulator头文件

 public:

        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);
    private:

        KeyManipMap _manips;

        osg::ref_ptr<CameraManipulator> _current;

用了一个map映射(C#中有Dictionary词典), map对象是模板类,需要关键字key和存储对象value两个模板参数,map中的key和value是一个pair结构中的两个分量
源文件

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());//找到按键如'E'
        if(it != _manips.end())//不为空
        {
            CameraManipulator* selectedManipulator = it->second.second.get();//it为一个pair,key为pair.first value为pair.second
            if (selectedManipulator!=_current)
            {            
                OSG_INFO<<"Switching to manipulator: "<<it->second.first<<std::endl;
                if ( !selectedManipulator->getNode() )
                {
                    selectedManipulator->setNode(_current->getNode());
                }
                selectedManipulator->setByMatrix(_current->getMatrix());//设置当前矩阵
                selectedManipulator->init(ea,aa);
                
                _current = selectedManipulator;
            }
            handled = true;
        }
    }

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

添加数字切换

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





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值