|
本帖最后由 江湖贫侠 于 2015-11-12 10:24 编辑
我想重载trackballManipulator的handle函数,自己定义按键消息,比如中键旋转场景。但发现当鼠标拖动时,camera只能变化一下,必须再点一次才能再转动一下。
代码如下,不知道问题出在哪里?
- class customize_trackball_manipulator : public osgGA::TrackballManipulator
- {
- public:
- enum event_type_idx{ ROTATION, SCALE, TRANSLATION, NOTHING };
- typedef osgGA::TrackballManipulator inherited;
- customize_trackball_manipulator(int flags = DEFAULT_SETTINGS) :inherited(flags){ setVerticalAxisFixed(false); }
- customize_trackball_manipulator(const customize_trackball_manipulator& om,
- const osg::CopyOp& copyOp = osg::CopyOp::SHALLOW_COPY)
- : inherited(om, copyOp) {}
- META_Object(osgGA::osgGA, customize_trackball_manipulator);
- protected:
- virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
- virtual bool handleMouseMove(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us);
- private:
- bool is_rotation_with_modkey(const osgGA::GUIEventAdapter& ea);
- bool is_rotation_without_modkey(const osgGA::GUIEventAdapter& ea);
- bool is_translation_with_modkey(const osgGA::GUIEventAdapter & ea);
- bool is_scale_with_modkey(const osgGA::GUIEventAdapter & ea);
- bool is_scale_without_modkey(const osgGA::GUIEventAdapter & ea);
- };
复制代码
实现如下:
- bool customize_trackball_manipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
- {
- using namespace osgGA;
- switch (ea.getEventType())
- {
- case GUIEventAdapter::FRAME:
- return handleFrame(ea, us);
- case GUIEventAdapter::RESIZE:
- return handleResize(ea, us);
- default:
- break;
- }
- if (ea.getHandled())
- return false;
- if (ea.getEventType() == GUIEventAdapter::SCROLL){
- if (_flags & PROCESS_MOUSE_WHEEL)
- return handleMouseWheel(ea, us);
- else
- return false;
- }
- return handleMouseMove(ea, us);
- }
- bool customize_trackball_manipulator::handleMouseMove(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us)
- {
- float dx = ea.getX() - _mouseCenterX;
- float dy = ea.getY() - _mouseCenterY;
- if (dx == 0.f && dy == 0.f)
- return false;
- addMouseEvent(ea);
- centerMousePointer(ea, us);
- if (is_scale_with_modkey(ea))
- {
- if (dx > 0)
- {
- zoomModel(_wheelZoomFactor, true);
- us.requestRedraw();
- us.requestContinuousUpdate(isAnimating() || _thrown);
- return true;
- }
- else{
- zoomModel(-_wheelZoomFactor, true);
- us.requestRedraw();
- us.requestContinuousUpdate(isAnimating() || _thrown);
- return true;
- }
- }
- if (is_rotation_with_modkey(ea) || is_rotation_without_modkey(ea))
- {
- if (getVerticalAxisFixed())
- rotateWithFixedVertical(dx, dy);
- else
- rotateTrackball(0.f, 0.f, dx, dy, 1.f);
- us.requestRedraw();
- us.requestContinuousUpdate(isAnimating() || _thrown);
- return true;
- }
- if (is_translation_with_modkey(ea))
- {
- double eventTimeDelta = _ga_t0->getTime() - _ga_t1->getTime();
- if (eventTimeDelta < 0.)
- {
- OSG_WARN << "Manipulator warning: eventTimeDelta = " << eventTimeDelta << std::endl;
- eventTimeDelta = 0.;
- }
- float scale = -0.3f * _distance * getThrowScale(eventTimeDelta);
- panModel(dx*scale, dy*scale);
- return true;
- }
- return false;
- }
复制代码 |
|