2007-02-11 18:33:59 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
#ifndef OSGMANIPULATOR_DRAGGER
#define OSGMANIPULATOR_DRAGGER 1
2009-06-30 19:39:39 +08:00
#include <osgManipulator/Constraint>
#include <osgManipulator/Command>
2007-02-11 18:33:59 +08:00
#include <osg/BoundingSphere>
2009-07-01 22:01:09 +08:00
#include <osg/MatrixTransform>
2007-02-11 18:33:59 +08:00
#include <osgUtil/SceneView>
#include <osgUtil/IntersectVisitor>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIActionAdapter>
namespace osgManipulator
{
class CompositeDragger;
2009-07-01 22:01:09 +08:00
class MotionCommand;
class TranslateInLineCommand;
class TranslateInPlaneCommand;
class Scale1DCommand;
class Scale2DCommand;
class ScaleUniformCommand;
class Rotate3DCommand;
2015-06-01 21:40:20 +08:00
/** Computes the nodepath from the given node all the way up to the root. */
2009-07-01 22:01:09 +08:00
extern OSGMANIPULATOR_EXPORT void computeNodePathToRoot(osg::Node& node, osg::NodePath& np);
class OSGMANIPULATOR_EXPORT DraggerTransformCallback : public DraggerCallback
{
public:
2012-02-01 21:55:38 +08:00
enum HandleCommandMask
{
HANDLE_TRANSLATE_IN_LINE = 1<<0,
HANDLE_TRANSLATE_IN_PLANE = 1<<1,
HANDLE_SCALED_1D = 1<<2,
HANDLE_SCALED_2D = 1<<3,
HANDLE_SCALED_UNIFORM = 1<<4,
HANDLE_ROTATE_3D = 1<<5,
HANDLE_ALL = 0x8ffffff
};
DraggerTransformCallback(osg::MatrixTransform* transform, int handleCommandMask = HANDLE_ALL);
2009-07-01 22:01:09 +08:00
virtual bool receive(const MotionCommand&);
2012-02-01 21:55:38 +08:00
virtual bool receive(const TranslateInLineCommand& command);
virtual bool receive(const TranslateInPlaneCommand& command);
virtual bool receive(const Scale1DCommand& command);
virtual bool receive(const Scale2DCommand& command);
virtual bool receive(const ScaleUniformCommand& command);
virtual bool receive(const Rotate3DCommand& command);
2009-07-01 22:01:09 +08:00
osg::MatrixTransform* getTransform() { return _transform.get(); }
const osg::MatrixTransform* getTransform() const { return _transform.get(); }
protected:
2012-02-01 21:55:38 +08:00
unsigned int _handleCommandMask;
2012-03-22 01:36:20 +08:00
2009-07-01 22:01:09 +08:00
osg::observer_ptr<osg::MatrixTransform> _transform;
osg::Matrix _startMotionMatrix;
osg::Matrix _localToWorld;
osg::Matrix _worldToLocal;
};
2007-02-11 18:33:59 +08:00
2007-02-26 21:01:17 +08:00
class OSGMANIPULATOR_EXPORT PointerInfo
{
public:
PointerInfo();
PointerInfo(const PointerInfo& rhs):
2007-09-07 23:03:56 +08:00
_hitList(rhs._hitList),
2007-05-28 18:14:29 +08:00
_nearPoint(rhs._nearPoint),
_farPoint(rhs._farPoint),
2007-09-07 23:03:56 +08:00
_eyeDir(rhs._eyeDir)
2007-02-26 21:01:17 +08:00
{
_hitIter = _hitList.begin();
}
void reset()
{
_hitList.clear();
_hitIter = _hitList.begin();
2009-03-11 19:29:00 +08:00
setCamera(0);
2007-02-26 21:01:17 +08:00
}
2007-06-05 22:37:55 +08:00
bool completed() const { return _hitIter==_hitList.end(); }
2007-02-26 21:01:17 +08:00
2009-03-11 19:29:00 +08:00
void next()
2007-02-26 21:01:17 +08:00
{
if (!completed()) ++_hitIter;
}
2008-11-21 19:23:21 +08:00
typedef std::pair<osg::NodePath, osg::Vec3d> NodePathIntersectionPair;
2007-02-26 21:01:17 +08:00
typedef std::list< NodePathIntersectionPair> IntersectionList;
2008-11-21 19:23:21 +08:00
osg::Vec3d getLocalIntersectPoint() const { return _hitIter->second; }
2007-02-26 21:01:17 +08:00
2009-03-11 19:29:00 +08:00
2008-11-21 19:23:21 +08:00
void setNearFarPoints (osg::Vec3d nearPoint, osg::Vec3d farPoint) {
2007-05-28 18:14:29 +08:00
_nearPoint = nearPoint;
_farPoint=farPoint;
_eyeDir = farPoint - nearPoint;
}
2009-03-11 19:29:00 +08:00
2008-11-21 19:23:21 +08:00
const osg::Vec3d& getEyeDir() const {return _eyeDir;}
2009-03-11 19:29:00 +08:00
2008-11-21 19:23:21 +08:00
void getNearFarPoints( osg::Vec3d& nearPoint, osg::Vec3d& farPoint) const {
2007-05-28 18:14:29 +08:00
nearPoint = _nearPoint;
farPoint = _farPoint;
2007-02-26 21:01:17 +08:00
}
bool contains(const osg::Node* node) const;
void setCamera(osg::Camera* camera)
{
2009-03-11 19:29:00 +08:00
2007-05-28 18:14:29 +08:00
if (camera)
2007-02-26 21:01:17 +08:00
{
2007-05-28 18:14:29 +08:00
_MVPW = camera->getViewMatrix() * camera->getProjectionMatrix();
if (camera->getViewport()) _MVPW.postMult(camera->getViewport()->computeWindowMatrix());
2007-02-26 21:01:17 +08:00
_inverseMVPW.invert(_MVPW);
2008-11-21 19:23:21 +08:00
osg::Vec3d eye, center, up;
2007-05-28 18:14:29 +08:00
camera->getViewMatrix().getLookAt(eye, center, up);
_eyeDir = eye - center;
2007-02-26 21:01:17 +08:00
}
else
{
_MVPW.makeIdentity();
_inverseMVPW.makeIdentity();
2008-11-21 19:23:21 +08:00
_eyeDir = osg::Vec3d(0,0,1);
2007-02-26 21:01:17 +08:00
}
}
2008-11-21 19:23:21 +08:00
void addIntersection(const osg::NodePath& nodePath, const osg::Vec3d& intersectionPoint)
2007-02-26 21:01:17 +08:00
{
bool needToResetHitIter = _hitList.empty();
_hitList.push_back(NodePathIntersectionPair(nodePath, intersectionPoint));
if (needToResetHitIter) _hitIter = _hitList.begin();
}
2007-03-06 20:01:55 +08:00
void setMousePosition(float pixel_x, float pixel_y)
2009-03-11 19:29:00 +08:00
{
2008-11-21 19:23:21 +08:00
projectWindowXYIntoObject(osg::Vec2d(pixel_x, pixel_y), _nearPoint, _farPoint);
2007-02-26 21:01:17 +08:00
}
2009-06-30 19:39:39 +08:00
2007-05-28 18:14:29 +08:00
protected:
2008-11-21 19:23:21 +08:00
bool projectWindowXYIntoObject(const osg::Vec2d& windowCoord, osg::Vec3d& nearPoint, osg::Vec3d& farPoint) const;
2007-02-26 21:01:17 +08:00
public:
IntersectionList _hitList;
2007-06-05 22:37:55 +08:00
IntersectionList::const_iterator _hitIter;
2007-05-28 18:14:29 +08:00
protected:
2009-03-11 19:29:00 +08:00
2008-11-21 19:23:21 +08:00
osg::Vec3d _nearPoint,_farPoint;
osg::Vec3d _eyeDir;
2007-02-26 21:01:17 +08:00
osg::Matrix _MVPW;
osg::Matrix _inverseMVPW;
};
2007-02-11 18:33:59 +08:00
/**
* Base class for draggers. Concrete draggers implement the pick event handler
2009-03-11 19:29:00 +08:00
* and generate motion commands (translate, rotate, ...) and sends these
2009-07-01 22:01:09 +08:00
* command to all the DraggerCallbacks & Transforms that are connected to the Dragger that generates the
2007-02-11 18:33:59 +08:00
* commands.
*/
2009-07-01 22:01:09 +08:00
class OSGMANIPULATOR_EXPORT Dragger : public osg::MatrixTransform
2007-02-11 18:33:59 +08:00
{
public:
2009-07-01 22:01:09 +08:00
2016-06-01 21:20:14 +08:00
META_Node(osgManipulator,Dragger);
2009-03-11 19:29:00 +08:00
2007-02-11 18:33:59 +08:00
/**
2007-05-28 18:14:29 +08:00
* Set/Get parent dragger. For simple draggers parent points to itself.
* For composite draggers parent points to the parent dragger that uses
* this dragger.
*/
2007-02-11 18:33:59 +08:00
virtual void setParentDragger(Dragger* parent) { _parentDragger = parent; }
2015-10-22 21:42:19 +08:00
2007-02-11 18:33:59 +08:00
Dragger* getParentDragger() { return _parentDragger; }
const Dragger* getParentDragger() const { return _parentDragger; }
2007-05-28 18:14:29 +08:00
2007-02-11 18:33:59 +08:00
/** Returns 0 if this Dragger is not a CompositeDragger. */
virtual const CompositeDragger* getComposite() const { return 0; }
/** Returns 0 if this Dragger is not a CompositeDragger. */
virtual CompositeDragger* getComposite() { return 0; }
2007-02-12 05:12:34 +08:00
2009-06-30 05:32:10 +08:00
void setHandleEvents(bool flag);
bool getHandleEvents() const { return _handleEvents; }
2009-07-04 03:16:53 +08:00
void setActivationModKeyMask(unsigned int mask) { _activationModKeyMask = mask; }
unsigned int getActivationModKeyMask() const { return _activationModKeyMask; }
2012-05-25 23:32:51 +08:00
void setActivationMouseButtonMask(unsigned int mask) { _activationMouseButtonMask = mask; }
unsigned int getActivationMouseButtonMask() const { return _activationMouseButtonMask; }
2009-07-04 03:16:53 +08:00
void setActivationKeyEvent(int key) { _activationKeyEvent = key; }
int getActivationKeyEvent() const { return _activationKeyEvent; }
2009-06-30 05:32:10 +08:00
virtual void traverse(osg::NodeVisitor& nv);
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
2007-02-12 05:12:34 +08:00
virtual bool handle(const PointerInfo&, const osgGA::GUIEventAdapter&, osgGA::GUIActionAdapter&) { return false; }
2007-02-11 18:33:59 +08:00
2009-06-30 19:39:39 +08:00
typedef std::vector< osg::ref_ptr<Constraint> > Constraints;
void addConstraint(Constraint* constraint);
2015-10-22 21:42:19 +08:00
template<class T> void addConstraint(const osg::ref_ptr<T>& c) { addConstraint(c.get()); }
2009-06-30 19:39:39 +08:00
void removeConstraint(Constraint* constraint);
2015-10-22 21:42:19 +08:00
template<class T> void removeConstraint(const osg::ref_ptr<T>& c) { removeConstraint(c.get()); }
2009-06-30 19:39:39 +08:00
Constraints& getConstraints() { return _constraints; }
const Constraints& getConstraints() const { return _constraints; }
2009-07-01 22:01:09 +08:00
typedef std::vector< osg::ref_ptr<DraggerCallback> > DraggerCallbacks;
void addDraggerCallback(DraggerCallback* dc);
2015-10-22 21:42:19 +08:00
template<class T> void addDraggerCallback(const osg::ref_ptr<T>& dc) { addDraggerCallback(dc.get()); }
2009-07-01 22:01:09 +08:00
void removeDraggerCallback(DraggerCallback* dc);
2015-10-22 21:42:19 +08:00
template<class T> void removeDraggerCallback(const osg::ref_ptr<T>& dc) { removeDraggerCallback(dc.get()); }
2009-06-30 19:39:39 +08:00
2009-07-01 22:01:09 +08:00
DraggerCallbacks& getDraggerCallbacks() { return _draggerCallbacks; }
const DraggerCallbacks& getDraggerCallbacks() const { return _draggerCallbacks; }
2012-02-01 21:55:38 +08:00
void addTransformUpdating(MatrixTransform* transform, int handleCommandMask = DraggerTransformCallback::HANDLE_ALL);
2009-07-01 22:01:09 +08:00
void removeTransformUpdating(MatrixTransform* transform);
2009-06-30 19:39:39 +08:00
2009-07-03 13:52:52 +08:00
/** Setup default geometry for dragger. */
virtual void setupDefaultGeometry() {}
2010-01-13 22:16:33 +08:00
virtual bool receive(const MotionCommand& command);
2012-09-25 19:04:11 +08:00
virtual void dispatch(MotionCommand& command);
2010-01-13 22:16:33 +08:00
void setDraggerActive(bool active) { _draggerActive = active; }
bool getDraggerActive() const { return _draggerActive; }
2011-12-24 01:21:59 +08:00
/**
* Set/Get the traversal mask used by this dragger when looking for intersections during event handling.
2015-04-13 18:43:56 +08:00
* This is useful to "hide" some geometry during event handling.
2011-12-24 01:21:59 +08:00
*/
virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask) { _intersectionMask = intersectionMask; }
osg::Node::NodeMask getIntersectionMask() const { return _intersectionMask; }
2016-11-12 01:16:40 +08:00
/** Return true if the axis of the Locator are inverted requiring the faces of any cubes used from rendering to be flipped to ensure the correct front/back face is used.*/
bool inverted() const;
/** apply the appropriate FrontFace setting to provided StateSet to ensure that the rendering of hull of the volume is the correct orientation.*/
void applyAppropriateFrontFace(osg::StateSet* ss) const;
protected:
2007-02-11 18:33:59 +08:00
Dragger();
2009-07-01 22:01:09 +08:00
Dragger(const Dragger& rhs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
2007-02-11 18:33:59 +08:00
virtual ~Dragger();
2009-03-11 19:29:00 +08:00
2009-07-04 03:16:53 +08:00
2009-06-30 05:32:10 +08:00
bool _handleEvents;
bool _draggerActive;
2009-07-04 03:16:53 +08:00
unsigned int _activationModKeyMask;
2012-05-25 23:32:51 +08:00
unsigned int _activationMouseButtonMask;
2009-07-04 03:16:53 +08:00
int _activationKeyEvent;
bool _activationPermittedByModKeyMask;
2012-05-25 23:32:51 +08:00
bool _activationPermittedByMouseButtonMask;
2009-07-04 03:16:53 +08:00
bool _activationPermittedByKeyEvent;
2009-06-30 05:32:10 +08:00
osgManipulator::PointerInfo _pointer;
Dragger* _parentDragger;
2009-03-11 19:29:00 +08:00
2009-07-01 22:01:09 +08:00
osg::ref_ptr<DraggerCallback> _selfUpdater;
2009-06-30 19:39:39 +08:00
Constraints _constraints;
2009-07-01 22:01:09 +08:00
DraggerCallbacks _draggerCallbacks;
2011-12-24 01:21:59 +08:00
osg::Node::NodeMask _intersectionMask;
2009-06-30 19:39:39 +08:00
2007-02-11 18:33:59 +08:00
};
/**
2009-03-11 19:29:00 +08:00
* CompositeDragger allows to create complex draggers that are composed of a
2007-02-11 18:33:59 +08:00
* hierarchy of Draggers.
*/
class OSGMANIPULATOR_EXPORT CompositeDragger : public Dragger
{
public:
2016-06-01 21:20:14 +08:00
META_Node(osgManipulator,CompositeDragger);
2009-03-11 19:29:00 +08:00
2007-02-11 18:33:59 +08:00
typedef std::vector< osg::ref_ptr<Dragger> > DraggerList;
2009-03-11 19:29:00 +08:00
2007-02-11 18:33:59 +08:00
virtual const CompositeDragger* getComposite() const { return this; }
virtual CompositeDragger* getComposite() { return this; }
virtual void setParentDragger(Dragger* parent);
2007-02-12 05:12:34 +08:00
virtual bool handle(const PointerInfo& pi, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
2007-02-11 18:33:59 +08:00
// Composite-specific methods below
virtual bool addDragger(Dragger* dragger);
2015-10-22 21:42:19 +08:00
template<class T> bool addDragger(const osg::ref_ptr<T>& dc) { return addDragger(dc.get()); }
2007-02-11 18:33:59 +08:00
virtual bool removeDragger(Dragger* dragger);
2015-10-22 21:42:19 +08:00
template<class T> bool removeDragger(const osg::ref_ptr<T>& dc) { return removeDragger(dc.get()); }
2007-02-11 18:33:59 +08:00
unsigned int getNumDraggers() const { return _draggerList.size(); }
2015-10-22 21:42:19 +08:00
2007-02-11 18:33:59 +08:00
Dragger* getDragger(unsigned int i) { return _draggerList[i].get(); }
const Dragger* getDragger(unsigned int i) const { return _draggerList[i].get(); }
2015-10-22 21:42:19 +08:00
2007-02-11 18:33:59 +08:00
bool containsDragger(const Dragger* dragger) const;
2015-10-22 21:42:19 +08:00
template<class T> bool containsDragger(const osg::ref_ptr<T>& dc) const { return containsDragger(dc.get()); }
2007-02-11 18:33:59 +08:00
DraggerList::iterator findDragger(const Dragger* dragger);
2011-12-24 01:21:59 +08:00
virtual void setIntersectionMask(osg::Node::NodeMask intersectionMask);
2007-02-11 18:33:59 +08:00
protected:
CompositeDragger() {}
2009-07-01 22:01:09 +08:00
CompositeDragger(const CompositeDragger& rhs, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
2007-02-11 18:33:59 +08:00
virtual ~CompositeDragger() {}
2009-03-11 19:29:00 +08:00
2007-02-11 18:33:59 +08:00
DraggerList _draggerList;
};
/**
* Culls the drawable all the time. Used by draggers to have invisible geometry
* around lines and points so that they can be picked. For example, a dragger
2009-03-11 19:29:00 +08:00
* could have a line with an invisible cylinder around it to enable picking on
2007-02-11 18:33:59 +08:00
* that line.
*/
2007-05-28 18:14:29 +08:00
void OSGMANIPULATOR_EXPORT setDrawableToAlwaysCull(osg::Drawable& drawable);
2007-02-11 18:33:59 +08:00
/**
* Convenience function for setting the material color on a node.
*/
2007-05-28 18:14:29 +08:00
void OSGMANIPULATOR_EXPORT setMaterialColor(const osg::Vec4& color, osg::Node& node);
2007-02-11 18:33:59 +08:00
}
#endif