AnimatLab  2
Test
VsDraggerHandler.cpp
1 #include "StdAfx.h"
2 
3 #include "VsMouseSpring.h"
4 #include "VsDraggerHandler.h"
5 #include "VsMovableItem.h"
6 #include "VsBody.h"
7 #include "VsJoint.h"
8 #include "VsRigidBody.h"
9 #include "VsStructure.h"
10 #include "VsSimulator.h"
11 #include "VsOsgUserData.h"
12 #include "VsDragger.h"
13 
14 namespace VortexAnimatSim
15 {
16  namespace Visualization
17  {
18 
19 VsDraggerHandler::VsDraggerHandler(Simulator *lpSim, osgViewer::Viewer *osgViewer, osg::Viewport *osgViewport)
20 {
21  m_lpSim = lpSim;
22  m_osgViewer = osgViewer;
23  m_osgViewport = osgViewport;
24  m_osgActiveDragger = NULL;
25 }
26 
27 VsDraggerHandler::~VsDraggerHandler(void)
28 {
29 }
30 
31 
32 bool VsDraggerHandler::handle(const GUIEventAdapter& ea, GUIActionAdapter& aa)
33 {
34  bool bHandled = false;
35 
36  try
37  {
38  float x = ea.getX();
39  float y = ea.getY();
40 
41  //Take an action based on the event type
42  switch(ea.getEventType())
43  {
44  case(GUIEventAdapter::PUSH):
45  {
46  pick(ea, aa);
47  break;
48  }
49 
50  case(GUIEventAdapter::RELEASE):
51  {
52  if(m_osgActiveDragger)
53  {
54  m_osgManipInfo._hitIter = m_osgManipInfo._hitList.begin();
55  m_osgManipInfo.setCamera((osg::Camera *) m_osgViewer->getCamera());
56  m_osgManipInfo.setMousePosition(ea.getX(), ea.getY());
57 
58  bHandled = m_osgActiveDragger->handle(m_osgManipInfo, ea, aa);
59  EndGripDrag();
60  }
61  break;
62  }
63 
64  case(GUIEventAdapter::DRAG):
65  {
66 
67  //If a grip element is selected then process that.
68  if(m_osgActiveDragger)
69  {
70  m_osgManipInfo._hitIter = m_osgManipInfo._hitList.begin();
71  m_osgManipInfo.setCamera((osg::Camera *) m_osgViewer->getCamera());
72  m_osgManipInfo.setMousePosition(ea.getX(), ea.getY());
73 
74  bHandled = m_osgActiveDragger->handle(m_osgManipInfo, ea, aa);
75  }
76  break;
77  }
78 
79  default:
80  bHandled = false;
81  }//end switch statement
82 
83  //If this was a release event then make sure the dragger stuff gets reset.
84  if (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE)
85  {
86  m_osgActiveDragger = NULL;
87  m_osgManipInfo.reset();
88  }
89 
90  } //Eat any erorrs here.
91  catch(CStdErrorInfo oError)
92  {return false;}
93  catch(...)
94  {return false;}
95 
96  return bHandled;
97 }
98 
99 void VsDraggerHandler::EndGripDrag()
100 {
101  if(m_osgActiveDragger && m_osgActiveDragger->getUserData())
102  {
103  VsOsgUserData *osgData = dynamic_cast<VsOsgUserData *>(m_osgActiveDragger->getUserData());
104  if(osgData && osgData->GetMovable())
105  {
106  VsMovableItem *lpItem = osgData->GetVsMovable();
107  lpItem->EndGripDrag();
108  }
109  }
110 }
111 
112 void VsDraggerHandler::pick(const osgGA::GUIEventAdapter& ea, GUIActionAdapter& aa)
113 {
114  osgUtil::LineSegmentIntersector::Intersections intersections;
115 
116  float x = ea.getX();
117  float y = ea.getY();
118 
119  //float local_x, local_y = 0.0;
120  const osg::Camera* camera = m_osgViewer->getCamera();
121 
122  osgUtil::LineSegmentIntersector::CoordinateFrame cf = camera->getViewport() ? osgUtil::Intersector::WINDOW : osgUtil::Intersector::PROJECTION;
123  osg::ref_ptr< osgUtil::LineSegmentIntersector > picker = new osgUtil::LineSegmentIntersector(cf, x, y);
124 
125  m_osgManipInfo.reset();
126 
127  RigidBody *lpBody = NULL;
128  Joint *lpJoint = NULL;
129  if (m_osgViewer->computeIntersections(x,y,intersections))
130  {
131  m_osgManipInfo.setCamera((osg::Camera *) camera);
132  m_osgManipInfo.setMousePosition(x, y);
133 
134 #pragma region LineSegmentIntersector
135 
136  for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
137  hitr != intersections.end();
138  ++hitr)
139  {
140  //Add the intersection to the manipulator info
141  m_osgManipInfo.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
142  }
143 #pragma endregion
144 
145  for (osg::NodePath::iterator itr = m_osgManipInfo._hitList.front().first.begin();
146  itr != m_osgManipInfo._hitList.front().first.end();
147  ++itr)
148  {
149  osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr);
150  if (dragger)
151  {
152 
153  dragger->handle(m_osgManipInfo, ea, aa);
154  m_osgActiveDragger = dragger;
155  break;
156  }
157  }
158 
159  }
160 }
161 
162  }// end Visualization
163 }// end VortexAnimatSim
virtual bool handle(const GUIEventAdapter &ea, GUIActionAdapter &us)
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
Declares the vortex structure class.