自定义事件处理器
- osgGA::GUIEventHandler 事件处理器/事件遍历器的回调
- 重载
- handle
-
参数osgGA::GUIEventAdapter
- getEventType
- getModKeyMask 返回MODKEY_CTRL/MODKEY_SHIFT/MODKEY_ALT
- 注意:相应set方法不适合在handle中出现,用于osg底层显示系统向事件队列添加事件
-
参数osgGA::GUIActionAdapter
- osgViewer::Viewer* viewer = dynamic_castosgViewer::Viewer*(&aa);
-
- handle
- 重载
- 事件处理管理
- addEventHandler
- removeEventHandler
- 步骤
- 自定义事件处理器
- viewer中加入自定义事件处理器
- 取消viewer主相机控制器
- viewer.getCamera()->setAllowEventFocus(false);
- 设置viewer的ViewMatrix
- viewer.getCamera()->setViewMatrixAsLookAt(osg::Vec3(0.0f,-100.0f,0.0f), osg::Vec3(), osg::Z_AXIS );
示例
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgGA/GUIEventHandler>
#include <osgViewer/Viewer>
class ModelController : public osgGA::GUIEventHandler
{
public:
ModelController(osg::MatrixTransform* node)
: _model(node)
{}
virtual bool handle(const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa);
protected:
osg::ref_ptr<osg::MatrixTransform> _model;
};
bool ModelController::handle(const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter& aa)
{
if (!_model) return false;
osg::Matrix matrix = _model->getMatrix();
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
switch (ea.getKey())
{
case 'a': case 'A':
matrix *= osg::Matrix::rotate(-0.1f, osg::Z_AXIS);
break;
case 'd': case 'D':
matrix *= osg::Matrix::rotate(0.1f, osg::Z_AXIS);
break;
case 'w': case 'W':
matrix *= osg::Matrix::rotate(-0.1f, osg::X_AXIS);
break;
case 's': case 'S':
matrix *= osg::Matrix::rotate(0.1f, osg::X_AXIS)