root/OpenSceneGraph/trunk/include/osgManipulator/Dragger @ 13041

Revision 13041, 11.8 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
28namespace osgManipulator
29{
30
31class CompositeDragger;
32class MotionCommand;
33class TranslateInLineCommand;
34class TranslateInPlaneCommand;
35class Scale1DCommand;
36class Scale2DCommand;
37class ScaleUniformCommand;
38class Rotate3DCommand;
39
40/** Computes the nodepath from the given node all the way upto the root. */
41extern OSGMANIPULATOR_EXPORT void computeNodePathToRoot(osg::Node& node, osg::NodePath& np);
42
43
44class OSGMANIPULATOR_EXPORT DraggerTransformCallback : public DraggerCallback
45{
46    public:
47
48        enum HandleCommandMask
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
76        osg::observer_ptr<osg::MatrixTransform> _transform;
77        osg::Matrix _startMotionMatrix;
78
79        osg::Matrix _localToWorld;
80        osg::Matrix _worldToLocal;
81};
82
83
84class OSGMANIPULATOR_EXPORT PointerInfo
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
180        osg::Vec3d _nearPoint,_farPoint;
181        osg::Vec3d _eyeDir;
182
183        osg::Matrix _MVPW;
184        osg::Matrix _inverseMVPW;
185
186};
187
188/**
189 * Base class for draggers. Concrete draggers implement the pick event handler
190 * and generate motion commands (translate, rotate, ...) and sends these
191 * command to all the DraggerCallbacks & Transforms that are connected to the Dragger that generates the
192 * commands.
193 */
194class OSGMANIPULATOR_EXPORT Dragger : public osg::MatrixTransform
195{
196    public:
197
198
199        META_Node(osgManipulator,Dragger)
200
201        /**
202         * Set/Get parent dragger. For simple draggers parent points to itself.
203         * For composite draggers parent points to the parent dragger that uses
204         * this dragger.
205         */
206        virtual void setParentDragger(Dragger* parent) { _parentDragger = parent; }
207        Dragger* getParentDragger() { return _parentDragger; }
208        const Dragger* getParentDragger() const { return _parentDragger; }
209
210        /** Returns 0 if this Dragger is not a CompositeDragger. */
211        virtual const CompositeDragger* getComposite() const { return 0; }
212
213        /** Returns 0 if this Dragger is not a CompositeDragger. */
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 setActivationKeyEvent(int key) { _activationKeyEvent = key; }
224        int getActivationKeyEvent() const { return _activationKeyEvent; }
225
226
227        virtual void traverse(osg::NodeVisitor& nv);
228
229        virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
230        virtual bool handle(const PointerInfo&, const osgGA::GUIEventAdapter&, osgGA::GUIActionAdapter&) { return false; }
231
232
233        typedef std::vector< osg::ref_ptr<Constraint> > Constraints;
234
235        void addConstraint(Constraint* constraint);
236        void removeConstraint(Constraint* constraint);
237
238        Constraints& getConstraints() { return _constraints; }
239        const Constraints& getConstraints() const { return _constraints; }
240
241
242        typedef std::vector< osg::ref_ptr<DraggerCallback> > DraggerCallbacks;
243
244        void addDraggerCallback(DraggerCallback* dc);
245        void removeDraggerCallback(DraggerCallback* dc);
246
247        DraggerCallbacks& getDraggerCallbacks() { return _draggerCallbacks; }
248        const DraggerCallbacks& getDraggerCallbacks() const { return _draggerCallbacks; }
249
250        void addTransformUpdating(MatrixTransform* transform, int handleCommandMask = DraggerTransformCallback::HANDLE_ALL);
251        void removeTransformUpdating(MatrixTransform* transform);
252
253        /** Setup default geometry for dragger. */
254        virtual void setupDefaultGeometry() {}
255
256        virtual bool receive(const MotionCommand& command);
257        void dispatch(MotionCommand& command);
258
259        void setDraggerActive(bool active) { _draggerActive = active; }
260        bool getDraggerActive() const { return _draggerActive; }
261
262        /**
263         * Set/Get the traversal mask used by this dragger when looking for intersections during event handling.
264         * This is usefull to "hide" some geometry during event handling.
265         */
266        virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask) { _intersectionMask = intersectionMask; }
267        osg::Node::NodeMask getIntersectionMask() const { return _intersectionMask; }
268
269    protected:
270
271        Dragger();
272        Dragger(const Dragger& rhs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
273
274        virtual ~Dragger();
275
276
277        bool                            _handleEvents;
278        bool                            _draggerActive;
279
280        unsigned int                    _activationModKeyMask;
281        int                             _activationKeyEvent;
282        bool                            _activationPermittedByModKeyMask;
283        bool                            _activationPermittedByKeyEvent;
284
285        osgManipulator::PointerInfo     _pointer;
286
287        Dragger*                        _parentDragger;
288
289        osg::ref_ptr<DraggerCallback>   _selfUpdater;
290        Constraints                     _constraints;
291        DraggerCallbacks                _draggerCallbacks;
292        osg::Node::NodeMask             _intersectionMask;
293
294};
295
296
297/**
298 * CompositeDragger allows to create complex draggers that are composed of a
299 * hierarchy of Draggers.
300 */
301class OSGMANIPULATOR_EXPORT CompositeDragger : public Dragger
302{
303    public:
304
305        META_Node(osgManipulator,CompositeDragger)
306
307        typedef std::vector< osg::ref_ptr<Dragger> > DraggerList;
308
309        virtual const CompositeDragger* getComposite() const { return this; }
310        virtual CompositeDragger* getComposite() { return this; }
311
312        virtual void setParentDragger(Dragger* parent);
313
314        virtual bool handle(const PointerInfo& pi, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
315
316        // Composite-specific methods below
317        virtual bool addDragger(Dragger* dragger);
318        virtual bool removeDragger(Dragger* dragger);
319        unsigned int getNumDraggers() const { return _draggerList.size(); }
320        Dragger* getDragger(unsigned int i) { return _draggerList[i].get(); }
321        const Dragger* getDragger(unsigned int i) const { return _draggerList[i].get(); }
322        bool containsDragger(const Dragger* dragger) const;
323        DraggerList::iterator findDragger(const Dragger* dragger);
324
325        virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask);
326
327    protected:
328
329        CompositeDragger() {}
330        CompositeDragger(const CompositeDragger& rhs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
331
332        virtual ~CompositeDragger() {}
333
334        DraggerList _draggerList;
335};
336
337/**
338 * Culls the drawable all the time. Used by draggers to have invisible geometry
339 * around lines and points so that they can be picked. For example, a dragger
340 * could have a line with an invisible cylinder around it to enable picking on
341 * that line.
342 */
343void OSGMANIPULATOR_EXPORT setDrawableToAlwaysCull(osg::Drawable& drawable);
344
345/**
346 * Convenience function for setting the material color on a node.
347 */
348void OSGMANIPULATOR_EXPORT setMaterialColor(const osg::Vec4& color, osg::Node& node);
349
350}
351
352#endif
Note: See TracBrowser for help on using the browser.