class testHandler public osgGA GUIEventHandler public testHandler Note

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class testHandler : public osgGA::GUIEventHandler
{
public:
testHandler(Notebook *):
_mx(0.0),_my(0.0),
_usePolytopeIntersector(false),
_useWindowCoordinates(false) {}
~testHandler() {}
Notebook* _info;
testHandler::testHandler(Notebook * a){
_info = a;
}
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if (!viewer) return false;
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYUP):
{
// Надо бы разобраться с сочетаниями клавиш. Так не работает
if (ea.getKey()=='s' && (ea.getKey() == osgGA::GUIEventAdapter::KEY_Control_R || ea.getKey() == osgGA::GUIEventAdapter::KEY_Control_L))
osgDB::writeNodeFile(*(viewer->getSceneData()), "saved_model.osg");
// Удаление текущего объекта при нажатии на делит или бекспейс
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Delete || ea.getKey()==osgGA::GUIEventAdapter::KEY_BackSpace)
{
DeleteSelectedNodesVisitor dsnv;
viewer->getSceneData()->accept(dsnv);
dsnv.pruneSelectedNodes();
}
return false;
}
// Движение мыши и нажатие (т.е. пока кнопка не отпущена)
case(osgGA::GUIEventAdapter::PUSH): case(osgGA::GUIEventAdapter::MOVE):
{
// Обновление координат указателя
_mx = ea.getX();
_my = ea.getY();
return false;
}
// Момент отпускания копки мыши
case(osgGA::GUIEventAdapter::RELEASE):
{
// Выделяем объект, если мышкой в этот момент не двигали
if (_mx == ea.getX() && _my == ea.getY())
pick(ea,viewer);
return true;
}
// На другие события не реагируем
default:
return false;
}
}
void pick(const osgGA::GUIEventAdapter& ea, osgViewer::Viewer* viewer)
{
osg::Node* scene = viewer->getSceneData();
if (!scene) return;
osg::notify(osg::NOTICE)<<std::endl;
osg::Node* node = 0;
osg::Group* parent = 0;
if (_usePolytopeIntersector)
{
osgUtil::PolytopeIntersector* picker;
if (_useWindowCoordinates)
{
// use window coordinates
// remap the mouse x,y into viewport coordinates.
osg::Viewport* viewport = viewer->getCamera()->getViewport();
double mx = viewport->x() + (int)((double )viewport->width()*(ea.getXnormalized()*0.5+0.5));
double my = viewport->y() + (int)((double )viewport->height()*(ea.getYnormalized()*0.5+0.5));
// half width, height.
double w = 5.0f;
double h = 5.0f;
picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h );
} else {
double mx = ea.getXnormalized();
double my = ea.getYnormalized();
double w = 0.05;
double h = 0.05;
picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::PROJECTION, mx-w, my-h, mx+w, my+h );
}
osgUtil::IntersectionVisitor iv(picker);
viewer->getCamera()->accept(iv);
if (picker->containsIntersections())
{
osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection();
osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl
<<" Distance to ref. plane "<<intersection.distance
<<", max. dist "<<intersection.maxDistance
<<", primitive index "<<intersection.primitiveIndex
<<", numIntersectionPoints "
<<intersection.numIntersectionPoints
<<std::endl;
osg::NodePath& nodePath = intersection.nodePath;
node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
if(node){
int index = indexOf(Map, node->getName());
if( index > -1 ){
printf("asdasdsad\n");
toggleScribe(parent, node);
}
}
}
}
else
{
osgUtil::LineSegmentIntersector* picker;
if (!_useWindowCoordinates)
{
// use non dimensional coordinates - in projection/clip space
picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::PROJECTION, ea.getXnormalized(),ea.getYnormalized() );
} else {
// use window coordinates
// remap the mouse x,y into viewport coordinates.
osg::Viewport* viewport = viewer->getCamera()->getViewport();
float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f));
float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f));
picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, mx, my );
}
osgUtil::IntersectionVisitor iv(picker);
viewer->getCamera()->accept(iv);
if (picker->containsIntersections())
{
osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection();
//osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl;
//if (node) std::cout<<" Hits "<< getMapIndex(node->getName())<<" nodePath size"<<nodePath.size()<<std::endl;
osg::NodePath& nodePath = intersection.nodePath;
node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
if(node){
int index = indexOf(Map, node->getName());
if( index > -1 ){
printf("asdasdsad\n");
toggleScribe(parent, node);
}
}
}
}
// now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
}
void toggleScribe(osg::Group* parent, osg::Node* node) {
if (!parent || !node) return;
std::cout<<" node is "<< node->getName() <<std::endl;
std::cout<<" parent "<<parent->className()<<std::endl;
osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
if (!parentAsScribe)
{
// node not already picked, so highlight it with an osgFX::Scribe
osgFX::Scribe* scribe = new osgFX::Scribe();
scribe->addChild(node);
parent->replaceChild(node,scribe);
}
else
{
// node already picked so we want to remove scribe to unpick it.
osg::Node::ParentList parentList = parentAsScribe->getParents();
for(osg::Node::ParentList::iterator itr=parentList.begin();
itr!=parentList.end();
++itr)
{
(*itr)->replaceChild(parentAsScribe,node);
}
}
}
void saveSelectedModel(osg::Node* scene)
{
if (!scene) return;
CreateModelToSaveVisitor cmtsv;
scene->accept(cmtsv);
if (cmtsv._group->getNumChildren()>0)
{
std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl;
osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg");
}
}
protected:
float _mx,_my;
bool _usePolytopeIntersector;
bool _useWindowCoordinates;
};