AnimatLab  2
Test
OsgDraggerHandler.cpp
1 #include "StdAfx.h"
2 
3 #include "OsgMovableItem.h"
4 #include "OsgBody.h"
5 #include "OsgRigidBody.h"
6 #include "OsgJoint.h"
7 #include "OsgMouseSpring.h"
8 #include "OsgDraggerHandler.h"
9 #include "OsgStructure.h"
10 #include "OsgUserData.h"
11 #include "OsgDragger.h"
12 #include "OsgSimulator.h"
13 
14 namespace OsgAnimatSim
15 {
16  namespace Visualization
17  {
18 
19 OsgDraggerHandler::OsgDraggerHandler(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 OsgDraggerHandler::~OsgDraggerHandler(void)
28 {
29 }
30 
31 
32 bool OsgDraggerHandler::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 OsgDraggerHandler::StartGripDrag()
100 {
101  if(m_osgActiveDragger && m_osgActiveDragger->getUserData())
102  {
103  OsgUserData *osgData = dynamic_cast<OsgUserData *>(m_osgActiveDragger->getUserData());
104  if(osgData && osgData->GetMovable())
105  {
106  OsgMovableItem *lpItem = osgData->GetOsgMovable();
107  if(lpItem && m_lpSim)
108  {
109  m_lpSim->InDrag(true);
110  lpItem->StartGripDrag();
111  }
112  }
113  }
114 }
115 
116 void OsgDraggerHandler::EndGripDrag()
117 {
118  if(m_osgActiveDragger && m_osgActiveDragger->getUserData())
119  {
120  OsgUserData *osgData = dynamic_cast<OsgUserData *>(m_osgActiveDragger->getUserData());
121  if(osgData && osgData->GetMovable())
122  {
123  OsgMovableItem *lpItem = osgData->GetOsgMovable();
124  if(lpItem && m_lpSim)
125  {
126  m_lpSim->InDrag(false);
127  lpItem->EndGripDrag();
128  }
129  }
130  }
131 }
132 
133 void OsgDraggerHandler::pick(const osgGA::GUIEventAdapter& ea, GUIActionAdapter& aa)
134 {
135  osgUtil::LineSegmentIntersector::Intersections intersections;
136 
137  float x = ea.getX();
138  float y = ea.getY();
139 
140  //float local_x, local_y = 0.0;
141  const osg::Camera* camera = m_osgViewer->getCamera();
142 
143  osgUtil::LineSegmentIntersector::CoordinateFrame cf = camera->getViewport() ? osgUtil::Intersector::WINDOW : osgUtil::Intersector::PROJECTION;
144  osg::ref_ptr< osgUtil::LineSegmentIntersector > picker = new osgUtil::LineSegmentIntersector(cf, x, y);
145 
146  m_osgManipInfo.reset();
147 
148  RigidBody *lpBody = NULL;
149  Joint *lpJoint = NULL;
150  if (m_osgViewer->computeIntersections(x,y,intersections))
151  {
152  m_osgManipInfo.setCamera((osg::Camera *) camera);
153  m_osgManipInfo.setMousePosition(x, y);
154 
155 #pragma region LineSegmentIntersector
156 
157  for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
158  hitr != intersections.end();
159  ++hitr)
160  {
161  //Add the intersection to the manipulator info
162  m_osgManipInfo.addIntersection(hitr->nodePath, hitr->getLocalIntersectPoint());
163  }
164 #pragma endregion
165 
166  for (osg::NodePath::iterator itr = m_osgManipInfo._hitList.front().first.begin();
167  itr != m_osgManipInfo._hitList.front().first.end();
168  ++itr)
169  {
170  osgManipulator::Dragger* dragger = dynamic_cast<osgManipulator::Dragger*>(*itr);
171  if (dragger)
172  {
173 
174  dragger->handle(m_osgManipInfo, ea, aa);
175  m_osgActiveDragger = dragger;
176  StartGripDrag();
177  break;
178  }
179  }
180 
181  }
182 }
183 
184  }// end Visualization
185 }// end OsgAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual bool handle(const GUIEventAdapter &ea, GUIActionAdapter &us)
Declares the vortex structure class.