2003-03-15 04:35:45 +08:00
|
|
|
#include "TestManipulator.h"
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
using namespace osgGA;
|
|
|
|
|
|
|
|
TestManipulator::TestManipulator()
|
|
|
|
{
|
|
|
|
_modelScale = 0.01f;
|
|
|
|
_minimumZoomScale = 0.05f;
|
|
|
|
_thrown = false;
|
|
|
|
|
|
|
|
_distance = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TestManipulator::~TestManipulator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TestManipulator::setNode(osg::Node* node)
|
|
|
|
{
|
|
|
|
_node = node;
|
|
|
|
if (_node.get())
|
|
|
|
{
|
|
|
|
const osg::BoundingSphere& boundingSphere=_node->getBound();
|
|
|
|
_modelScale = boundingSphere._radius;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const osg::Node* TestManipulator::getNode() const
|
|
|
|
{
|
|
|
|
return _node.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* TestManipulator::getNode()
|
|
|
|
{
|
|
|
|
return _node.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*ea*/
|
|
|
|
void TestManipulator::home(const GUIEventAdapter& ,GUIActionAdapter& us)
|
|
|
|
{
|
2003-05-19 23:15:17 +08:00
|
|
|
if(_node.get())
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
const osg::BoundingSphere& boundingSphere=_node->getBound();
|
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
computePosition(boundingSphere.center()+osg::Vec3(0.0f, 0.0f, 20.0f),
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::Vec3(0.0f, 1.0f, 0.0f),
|
|
|
|
osg::Vec3(0.0f, 0.0f, 1.0f));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
us.requestRedraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TestManipulator::init(const GUIEventAdapter& ,GUIActionAdapter& )
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us)
|
|
|
|
{
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(GUIEventAdapter::PUSH):
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
addMouseEvent(ea);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
_thrown = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::RELEASE):
|
|
|
|
{
|
|
|
|
if (ea.getButtonMask()==0)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (isMouseMoving())
|
|
|
|
{
|
|
|
|
if (calcMovement())
|
|
|
|
{
|
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(true);
|
|
|
|
_thrown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
addMouseEvent(ea);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
_thrown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
addMouseEvent(ea);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
_thrown = false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::DRAG):
|
|
|
|
{
|
|
|
|
addMouseEvent(ea);
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
_thrown = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::MOVE):
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case(GUIEventAdapter::KEYDOWN):
|
|
|
|
if (ea.getKey()==' ')
|
|
|
|
{
|
|
|
|
flushMouseEventStack();
|
|
|
|
_thrown = false;
|
|
|
|
home(ea,us);
|
|
|
|
us.requestRedraw();
|
|
|
|
us.requestContinuousUpdate(false);
|
|
|
|
return true;
|
2003-05-19 23:15:17 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
return false;
|
|
|
|
case(GUIEventAdapter::FRAME):
|
|
|
|
if (_thrown)
|
|
|
|
{
|
|
|
|
if (calcMovement()) us.requestRedraw();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TestManipulator::isMouseMoving()
|
|
|
|
{
|
|
|
|
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
|
|
|
|
|
2003-04-06 04:02:42 +08:00
|
|
|
static const float velocity = 0.1f;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2003-04-06 04:02:42 +08:00
|
|
|
float dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized();
|
|
|
|
float dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized();
|
2003-03-15 04:35:45 +08:00
|
|
|
float len = sqrtf(dx*dx+dy*dy);
|
2006-03-08 22:09:47 +08:00
|
|
|
float dt = _ga_t0->getTime()-_ga_t1->getTime();
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
return (len>dt*velocity);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TestManipulator::flushMouseEventStack()
|
|
|
|
{
|
|
|
|
_ga_t1 = NULL;
|
|
|
|
_ga_t0 = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TestManipulator::addMouseEvent(const GUIEventAdapter& ea)
|
|
|
|
{
|
|
|
|
_ga_t1 = _ga_t0;
|
|
|
|
_ga_t0 = &ea;
|
|
|
|
}
|
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
void TestManipulator::setByMatrix(const osg::Matrixd& matrix)
|
2003-05-19 23:15:17 +08:00
|
|
|
{
|
|
|
|
_center = matrix.getTrans();
|
2003-09-06 04:48:42 +08:00
|
|
|
matrix.get(_rotation);
|
2003-05-19 23:15:17 +08:00
|
|
|
_distance = 1.0f;
|
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
osg::Matrixd TestManipulator::getMatrix() const
|
2003-05-19 23:15:17 +08:00
|
|
|
{
|
2003-09-06 06:35:34 +08:00
|
|
|
return osg::Matrixd::rotate(_rotation)*osg::Matrixd::translate(_center);
|
2003-05-19 23:15:17 +08:00
|
|
|
}
|
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
osg::Matrixd TestManipulator::getInverseMatrix() const
|
2003-05-19 23:15:17 +08:00
|
|
|
{
|
2003-09-06 06:35:34 +08:00
|
|
|
return osg::Matrixd::translate(-_center)*osg::Matrixd::rotate(_rotation.inverse());
|
2003-05-19 23:15:17 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
void TestManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv,const osg::Vec3& up)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
2003-05-19 23:15:17 +08:00
|
|
|
osg::Vec3 f(lv);
|
2003-03-15 04:35:45 +08:00
|
|
|
f.normalize();
|
2003-05-19 23:15:17 +08:00
|
|
|
osg::Vec3 s(f^up);
|
2003-03-15 04:35:45 +08:00
|
|
|
s.normalize();
|
|
|
|
osg::Vec3 u(s^f);
|
|
|
|
u.normalize();
|
2003-05-19 23:15:17 +08:00
|
|
|
|
2003-09-06 06:35:34 +08:00
|
|
|
osg::Matrixd rotation_matrix(s[0], u[0], -f[0], 0.0f,
|
2003-03-15 04:35:45 +08:00
|
|
|
s[1], u[1], -f[1], 0.0f,
|
|
|
|
s[2], u[2], -f[2], 0.0f,
|
|
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
_center = eye+lv;
|
|
|
|
_distance = lv.length();
|
2003-09-06 04:48:42 +08:00
|
|
|
rotation_matrix.get(_rotation);
|
2003-03-15 04:35:45 +08:00
|
|
|
_rotation = _rotation.inverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TestManipulator::calcMovement()
|
|
|
|
{
|
|
|
|
|
|
|
|
// return if less then two events have been added.
|
|
|
|
if (_ga_t0.get()==NULL || _ga_t1.get()==NULL) return false;
|
|
|
|
|
2003-04-06 04:02:42 +08:00
|
|
|
float dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized();
|
|
|
|
float dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized();
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
// return if there is no movement.
|
|
|
|
if (dx==0 && dy==0) return false;
|
|
|
|
|
|
|
|
unsigned int buttonMask = _ga_t1->getButtonMask();
|
|
|
|
if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON)
|
|
|
|
{
|
|
|
|
|
|
|
|
// rotate camera.
|
|
|
|
|
|
|
|
osg::Quat new_rotate;
|
2005-11-18 04:22:55 +08:00
|
|
|
new_rotate.makeRotate(dx / 3.0f, osg::Vec3(0.0f, 0.0f, 1.0f));
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
_rotation = _rotation*new_rotate;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON)
|
|
|
|
{
|
|
|
|
|
|
|
|
// pan model.
|
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
osg::Vec3 dv = osg::Vec3(0.0f, 0.0f, -500.0f) * dy;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
_center += dv;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON)
|
|
|
|
{
|
2003-09-06 06:35:34 +08:00
|
|
|
osg::Matrixd rotation_matrix(_rotation);
|
2003-05-19 23:15:17 +08:00
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
|
2003-05-19 23:15:17 +08:00
|
|
|
osg::Vec3 uv = osg::Vec3(0.0f,1.0f,0.0f)*rotation_matrix;
|
|
|
|
osg::Vec3 sv = osg::Vec3(1.0f,0.0f,0.0f)*rotation_matrix;
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3 fv = uv ^ sv;
|
2003-04-15 02:49:41 +08:00
|
|
|
osg::Vec3 dv = fv*(dy*-500.0f)-sv*(dx*500.0f);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
_center += dv;
|
|
|
|
|
2005-11-18 04:22:55 +08:00
|
|
|
return true;
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|