OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Dragger.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 //osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
14 
15 #ifndef OSGMANIPULATOR_DRAGGER
16 #define OSGMANIPULATOR_DRAGGER 1
17 
18 #include <osgManipulator/Constraint>
19 #include <osgManipulator/Command>
20 
21 #include <osg/BoundingSphere>
22 #include <osg/MatrixTransform>
23 #include <osgUtil/SceneView>
24 #include <osgUtil/IntersectVisitor>
25 #include <osgGA/GUIEventAdapter>
26 #include <osgGA/GUIActionAdapter>
27 
28 namespace osgManipulator
29 {
30 
31 class CompositeDragger;
32 class MotionCommand;
33 class TranslateInLineCommand;
34 class TranslateInPlaneCommand;
35 class Scale1DCommand;
36 class Scale2DCommand;
37 class ScaleUniformCommand;
38 class Rotate3DCommand;
39 
42 
43 
45 {
46  public:
47 
49  {
50  HANDLE_TRANSLATE_IN_LINE = 1<<0,
51  HANDLE_TRANSLATE_IN_PLANE = 1<<1,
52  HANDLE_SCALED_1D = 1<<2,
53  HANDLE_SCALED_2D = 1<<3,
54  HANDLE_SCALED_UNIFORM = 1<<4,
55  HANDLE_ROTATE_3D = 1<<5,
56  HANDLE_ALL = 0x8ffffff
57  };
58 
59  DraggerTransformCallback(osg::MatrixTransform* transform, int handleCommandMask = HANDLE_ALL);
60 
61  virtual bool receive(const MotionCommand&);
62  virtual bool receive(const TranslateInLineCommand& command);
63  virtual bool receive(const TranslateInPlaneCommand& command);
64  virtual bool receive(const Scale1DCommand& command);
65  virtual bool receive(const Scale2DCommand& command);
66  virtual bool receive(const ScaleUniformCommand& command);
67  virtual bool receive(const Rotate3DCommand& command);
68 
69  osg::MatrixTransform* getTransform() { return _transform.get(); }
70  const osg::MatrixTransform* getTransform() const { return _transform.get(); }
71 
72  protected:
73 
74  unsigned int _handleCommandMask;
75 
78 
81 };
82 
83 
85 {
86  public:
87 
88  PointerInfo();
89 
90  PointerInfo(const PointerInfo& rhs):
91  _hitList(rhs._hitList),
92  _nearPoint(rhs._nearPoint),
93  _farPoint(rhs._farPoint),
94  _eyeDir(rhs._eyeDir)
95  {
96  _hitIter = _hitList.begin();
97  }
98 
99  void reset()
100  {
101  _hitList.clear();
102  _hitIter = _hitList.begin();
103  setCamera(0);
104  }
105 
106 
107  bool completed() const { return _hitIter==_hitList.end(); }
108 
109  void next()
110  {
111  if (!completed()) ++_hitIter;
112  }
113 
114  typedef std::pair<osg::NodePath, osg::Vec3d> NodePathIntersectionPair;
115  typedef std::list< NodePathIntersectionPair> IntersectionList;
116 
117 
118  osg::Vec3d getLocalIntersectPoint() const { return _hitIter->second; }
119 
120 
121 
122  void setNearFarPoints (osg::Vec3d nearPoint, osg::Vec3d farPoint) {
123  _nearPoint = nearPoint;
124  _farPoint=farPoint;
125  _eyeDir = farPoint - nearPoint;
126  }
127 
128  const osg::Vec3d& getEyeDir() const {return _eyeDir;}
129 
130  void getNearFarPoints( osg::Vec3d& nearPoint, osg::Vec3d& farPoint) const {
131  nearPoint = _nearPoint;
132  farPoint = _farPoint;
133  }
134 
135  bool contains(const osg::Node* node) const;
136 
137  void setCamera(osg::Camera* camera)
138  {
139 
140  if (camera)
141  {
142  _MVPW = camera->getViewMatrix() * camera->getProjectionMatrix();
143  if (camera->getViewport()) _MVPW.postMult(camera->getViewport()->computeWindowMatrix());
144  _inverseMVPW.invert(_MVPW);
145  osg::Vec3d eye, center, up;
146  camera->getViewMatrix().getLookAt(eye, center, up);
147  _eyeDir = eye - center;
148 
149  }
150  else
151  {
152  _MVPW.makeIdentity();
153  _inverseMVPW.makeIdentity();
154  _eyeDir = osg::Vec3d(0,0,1);
155  }
156 
157  }
158 
159  void addIntersection(const osg::NodePath& nodePath, const osg::Vec3d& intersectionPoint)
160  {
161  bool needToResetHitIter = _hitList.empty();
162  _hitList.push_back(NodePathIntersectionPair(nodePath, intersectionPoint));
163  if (needToResetHitIter) _hitIter = _hitList.begin();
164  }
165 
166  void setMousePosition(float pixel_x, float pixel_y)
167  {
168  projectWindowXYIntoObject(osg::Vec2d(pixel_x, pixel_y), _nearPoint, _farPoint);
169  }
170 
171  protected:
172  bool projectWindowXYIntoObject(const osg::Vec2d& windowCoord, osg::Vec3d& nearPoint, osg::Vec3d& farPoint) const;
173 
174  public:
175  IntersectionList _hitList;
176  IntersectionList::const_iterator _hitIter;
177 
178  protected:
179 
182 
185 
186 };
187 
195 {
196  public:
197 
198 
200 
201 
206  virtual void setParentDragger(Dragger* parent) { _parentDragger = parent; }
207  Dragger* getParentDragger() { return _parentDragger; }
208  const Dragger* getParentDragger() const { return _parentDragger; }
209 
211  virtual const CompositeDragger* getComposite() const { return 0; }
212 
214  virtual CompositeDragger* getComposite() { return 0; }
215 
216 
217  void setHandleEvents(bool flag);
218  bool getHandleEvents() const { return _handleEvents; }
219 
220  void setActivationModKeyMask(unsigned int mask) { _activationModKeyMask = mask; }
221  unsigned int getActivationModKeyMask() const { return _activationModKeyMask; }
222 
223  void setActivationMouseButtonMask(unsigned int mask) { _activationMouseButtonMask = mask; }
224  unsigned int getActivationMouseButtonMask() const { return _activationMouseButtonMask; }
225 
226  void setActivationKeyEvent(int key) { _activationKeyEvent = key; }
227  int getActivationKeyEvent() const { return _activationKeyEvent; }
228 
229 
230  virtual void traverse(osg::NodeVisitor& nv);
231 
232  virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
233  virtual bool handle(const PointerInfo&, const osgGA::GUIEventAdapter&, osgGA::GUIActionAdapter&) { return false; }
234 
235 
236  typedef std::vector< osg::ref_ptr<Constraint> > Constraints;
237 
238  void addConstraint(Constraint* constraint);
239  void removeConstraint(Constraint* constraint);
240 
241  Constraints& getConstraints() { return _constraints; }
242  const Constraints& getConstraints() const { return _constraints; }
243 
244 
245  typedef std::vector< osg::ref_ptr<DraggerCallback> > DraggerCallbacks;
246 
247  void addDraggerCallback(DraggerCallback* dc);
248  void removeDraggerCallback(DraggerCallback* dc);
249 
250  DraggerCallbacks& getDraggerCallbacks() { return _draggerCallbacks; }
251  const DraggerCallbacks& getDraggerCallbacks() const { return _draggerCallbacks; }
252 
253  void addTransformUpdating(MatrixTransform* transform, int handleCommandMask = DraggerTransformCallback::HANDLE_ALL);
254  void removeTransformUpdating(MatrixTransform* transform);
255 
257  virtual void setupDefaultGeometry() {}
258 
259  virtual bool receive(const MotionCommand& command);
260  virtual void dispatch(MotionCommand& command);
261 
262  void setDraggerActive(bool active) { _draggerActive = active; }
263  bool getDraggerActive() const { return _draggerActive; }
264 
269  virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask) { _intersectionMask = intersectionMask; }
270  osg::Node::NodeMask getIntersectionMask() const { return _intersectionMask; }
271 
272  protected:
273 
274  Dragger();
275  Dragger(const Dragger& rhs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
276 
277  virtual ~Dragger();
278 
279 
282 
283  unsigned int _activationModKeyMask;
289 
291 
293 
295  Constraints _constraints;
296  DraggerCallbacks _draggerCallbacks;
298 
299 };
300 
301 
307 {
308  public:
309 
311 
312  typedef std::vector< osg::ref_ptr<Dragger> > DraggerList;
313 
314  virtual const CompositeDragger* getComposite() const { return this; }
315  virtual CompositeDragger* getComposite() { return this; }
316 
317  virtual void setParentDragger(Dragger* parent);
318 
319  virtual bool handle(const PointerInfo& pi, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
320 
321  // Composite-specific methods below
322  virtual bool addDragger(Dragger* dragger);
323  virtual bool removeDragger(Dragger* dragger);
324  unsigned int getNumDraggers() const { return _draggerList.size(); }
325  Dragger* getDragger(unsigned int i) { return _draggerList[i].get(); }
326  const Dragger* getDragger(unsigned int i) const { return _draggerList[i].get(); }
327  bool containsDragger(const Dragger* dragger) const;
328  DraggerList::iterator findDragger(const Dragger* dragger);
329 
330  virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask);
331 
332  protected:
333 
336 
337  virtual ~CompositeDragger() {}
338 
339  DraggerList _draggerList;
340 };
341 
349 
354 
355 }
356 
357 #endif
bool getHandleEvents() const
Definition: Dragger.h:218
void getNearFarPoints(osg::Vec3d &nearPoint, osg::Vec3d &farPoint) const
Definition: Dragger.h:130
PointerInfo(const PointerInfo &rhs)
Definition: Dragger.h:90
Dragger * getDragger(unsigned int i)
Definition: Dragger.h:325
std::pair< osg::NodePath, osg::Vec3d > NodePathIntersectionPair
Definition: Dragger.h:114
unsigned int getActivationMouseButtonMask() const
Definition: Dragger.h:224
virtual META_Node(osgManipulator, CompositeDragger) typedef std const CompositeDragger * getComposite() const
Definition: Dragger.h:314
Vec3f postMult(const Vec3f &v) const
Definition: Matrixd.h:596
bool _activationPermittedByModKeyMask
Definition: Dragger.h:286
osg::Matrixd & getProjectionMatrix()
Definition: Camera.h:216
virtual void setupDefaultGeometry()
Definition: Dragger.h:257
osg::ref_ptr< DraggerCallback > _selfUpdater
Definition: Dragger.h:294
#define OSGMANIPULATOR_EXPORT
Definition: Export.h:27
const Constraints & getConstraints() const
Definition: Dragger.h:242
bool _activationPermittedByKeyEvent
Definition: Dragger.h:288
int getActivationKeyEvent() const
Definition: Dragger.h:227
bool getDraggerActive() const
Definition: Dragger.h:263
const osg::Matrix computeWindowMatrix() const
Definition: Viewport.h:129
virtual CompositeDragger * getComposite()
Definition: Dragger.h:315
bool _activationPermittedByMouseButtonMask
Definition: Dragger.h:287
DraggerCallbacks & getDraggerCallbacks()
Definition: Dragger.h:250
void setActivationModKeyMask(unsigned int mask)
Definition: Dragger.h:220
Constraints & getConstraints()
Definition: Dragger.h:241
#define META_Node(library, name)
Definition: Node.h:59
std::vector< osg::ref_ptr< DraggerCallback > > DraggerCallbacks
Definition: Dragger.h:245
virtual const CompositeDragger * getComposite() const
Definition: Dragger.h:211
virtual bool handle(const PointerInfo &, const osgGA::GUIEventAdapter &, osgGA::GUIActionAdapter &)
Definition: Dragger.h:233
const osg::Vec3d & getEyeDir() const
Definition: Dragger.h:128
Dragger * _parentDragger
Definition: Dragger.h:292
void setDraggerActive(bool active)
Definition: Dragger.h:262
void setMousePosition(float pixel_x, float pixel_y)
Definition: Dragger.h:166
unsigned int _activationMouseButtonMask
Definition: Dragger.h:284
void setCamera(osg::Camera *camera)
Definition: Dragger.h:137
OSGMANIPULATOR_EXPORT void computeNodePathToRoot(osg::Node &node, osg::NodePath &np)
const Viewport * getViewport() const
Definition: Camera.h:156
unsigned int getNumDraggers() const
Definition: Dragger.h:324
void getLookAt(Vec3f &eye, Vec3f &center, Vec3f &up, value_type lookDistance=1.0f) const
META_Node(osgManipulator, Dragger) virtual void setParentDragger(Dragger *parent)
Definition: Dragger.h:199
const Dragger * getDragger(unsigned int i) const
Definition: Dragger.h:326
virtual CompositeDragger * getComposite()
Definition: Dragger.h:214
unsigned int _activationModKeyMask
Definition: Dragger.h:283
void OSGMANIPULATOR_EXPORT setMaterialColor(const osg::Vec4 &color, osg::Node &node)
IntersectionList _hitList
Definition: Dragger.h:175
const osg::MatrixTransform * getTransform() const
Definition: Dragger.h:70
Constraints _constraints
Definition: Dragger.h:295
std::list< NodePathIntersectionPair > IntersectionList
Definition: Dragger.h:115
void OSGMANIPULATOR_EXPORT setDrawableToAlwaysCull(osg::Drawable &drawable)
osg::Node::NodeMask getIntersectionMask() const
Definition: Dragger.h:270
const DraggerCallbacks & getDraggerCallbacks() const
Definition: Dragger.h:251
osg::MatrixTransform * getTransform()
Definition: Dragger.h:69
const Dragger * getParentDragger() const
Definition: Dragger.h:208
osg::Node::NodeMask _intersectionMask
Definition: Dragger.h:297
DraggerCallbacks _draggerCallbacks
Definition: Dragger.h:296
osgManipulator::PointerInfo _pointer
Definition: Dragger.h:290
Definition: Node.h:71
void setActivationMouseButtonMask(unsigned int mask)
Definition: Dragger.h:223
osg::observer_ptr< osg::MatrixTransform > _transform
Definition: Dragger.h:76
osg::Matrixd & getViewMatrix()
Definition: Camera.h:250
bool completed() const
Definition: Dragger.h:107
std::vector< osg::ref_ptr< Constraint > > Constraints
Definition: Dragger.h:236
void setActivationKeyEvent(int key)
Definition: Dragger.h:226
void setNearFarPoints(osg::Vec3d nearPoint, osg::Vec3d farPoint)
Definition: Dragger.h:122
unsigned int NodeMask
Definition: Node.h:352
virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask)
Definition: Dragger.h:269
Dragger * getParentDragger()
Definition: Dragger.h:207
std::vector< Node * > NodePath
Definition: Node.h:44
osg::Matrix _inverseMVPW
Definition: Dragger.h:184
unsigned int getActivationModKeyMask() const
Definition: Dragger.h:221
void addIntersection(const osg::NodePath &nodePath, const osg::Vec3d &intersectionPoint)
Definition: Dragger.h:159
osg::Vec3d getLocalIntersectPoint() const
Definition: Dragger.h:118
IntersectionList::const_iterator _hitIter
Definition: Dragger.h:176