|
我写了一个EventHanlder来选中节点 结果addEventHandler后 没有任何效果呢.是不是有什么特殊的地方要处理啊.
头文件:- /*
- * QNodePickHandler.h
- *
- * Created on: Mar 21, 2009
- * Author: Administrator
- */
- #ifndef QNODEPICKHANDLER_H_
- #define QNODEPICKHANDLER_H_
- #include <osgViewer/Viewer>
- #include <osgViewer/View>
- #include <osg/Node>
- #include <osg/Geode>
- #include <osg/Group>
- #include <osgDB/ReadFile>
- #include <osgDB/WriteFile>
- #include <osgFX/Scribe>
- #include <osgGA/GUIEventHandler>
- using namespace osg;
- using namespace osgGA;
- using namespace osgFX;
- using namespace osgViewer;
- class QNodePickHandler:public GUIEventHandler
- {
- public:
- QNodePickHandler();
- virtual ~QNodePickHandler();
- osgViewer::View* getViewer() const;
- void setViewer(osgViewer::View* viewer);
- bool handle(const GUIEventAdapter& ea,const GUIActionAdapter& aa);
- void enable(bool enable);
- bool isEnable() const;
- private:
- void pick(float x,float y);
- private:
- bool _enabled;
- float _x;
- float _y;
- osgViewer::View* _viewer;
- };
- #endif /* QNODEPICKHANDLER_H_ */
复制代码 实现:- /*
- * QNodePickHandler.cpp
- *
- * Created on: Mar 21, 2009
- * Author: Administrator
- */
- #include "QNodePickHandler.h"
- QNodePickHandler::QNodePickHandler()
- {
- _x = 0.0f;
- _y = 0.0f;
- _enabled = true;
- }
- QNodePickHandler::~QNodePickHandler()
- {
- // TODO Auto-generated destructor stub
- }
- osgViewer::View* QNodePickHandler::getViewer() const
- {
- return _viewer;
- }
- void QNodePickHandler::setViewer(osgViewer::View* viewer)
- {
- _viewer = viewer;
- }
- bool QNodePickHandler::handle(const GUIEventAdapter& ea,
- const GUIActionAdapter&aa)
- {
- if (!_viewer||!_enabled)
- return false;
- switch (ea.getEventType())
- {
- case GUIEventAdapter::PUSH: {
- _x = ea.getX();
- _y = ea.getY();
- break;
- }
- case GUIEventAdapter::RELEASE: {
- if (_x == ea.getX() && _y == ea.getY()) {
- pick(_x, _y);
- }
- break;
- }
- default:
- break;
- }
- return false;
- }
- void QNodePickHandler::pick(float x, float y)
- {
- Node* node = NULL;
- Group* parent = new Group();
- osgUtil::LineSegmentIntersector::Intersections intersections;
- if (_viewer->computeIntersections(x, y, intersections)) {
- osgUtil::LineSegmentIntersector::Intersection inter =
- *intersections.begin();
- NodePath& nodePath = inter.nodePath;
- if (nodePath.size() > 0)
- node = nodePath[nodePath.size() - 1];
- if (nodePath.size() > 1)
- parent = dynamic_cast<Group*> (nodePath[nodePath.size() - 2]);
- if (parent && node) {
- Scribe* parentAsScribe = dynamic_cast<Scribe*> (parent);
- if (!parentAsScribe) {
- Scribe* scribe = new Scribe();
- scribe->addChild(node);
- parent->replaceChild(node, scribe);
- } else {
- Node::ParentList parentList = parentAsScribe->getParents();
- for (Node::ParentList::iterator itr = parentList.begin(); itr
- != parentList.end(); ++itr) {
- (*itr)->replaceChild(parentAsScribe, node);
- }
- }
- }
- }
- }
- void QNodePickHandler::enable(bool enable)
- {
- _enabled=enable;
- }
- bool QNodePickHandler::isEnable() const
- {
- return _enabled;
- }
复制代码 |
|