Cleaned up event handler and namespaces in prep for integrating keystone functionality into osgViewer

This commit is contained in:
Robert Osfield 2013-05-07 14:43:36 +00:00
parent 8810d27f7c
commit 046c3fca7a

View File

@ -30,7 +30,10 @@
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/ViewerEventHandlers>
class Keystone : public osg::Referenced namespace osg
{
class Keystone : public osg::Object
{ {
public: public:
Keystone(): Keystone():
@ -41,6 +44,16 @@ public:
top_left(-1.0,1.0), top_left(-1.0,1.0),
top_right(1.0,1.0) {} top_right(1.0,1.0) {}
Keystone(const Keystone& rhs, const osg::CopyOp&):
keystoneEditingEnabled(rhs.keystoneEditingEnabled),
gridColour(rhs.gridColour),
bottom_left(rhs.bottom_left),
bottom_right(rhs.bottom_right),
top_left(rhs.top_left),
top_right(rhs.top_right) {}
META_Object(osg, Keystone)
void reset() void reset()
{ {
bottom_left.set(-1.0,-1.0); bottom_left.set(-1.0,-1.0);
@ -89,15 +102,22 @@ public:
br = osg::Vec3(screenWidth*0.5*bottom_right.x(), screenHeight*0.5*bottom_right.y(), -screenDistance)*r_right*r_bottom; br = osg::Vec3(screenWidth*0.5*bottom_right.x(), screenHeight*0.5*bottom_right.y(), -screenDistance)*r_right*r_bottom;
bl = osg::Vec3(screenWidth*0.5*bottom_left.x(), screenHeight*0.5*bottom_left.y(), -screenDistance)*r_left*r_bottom; bl = osg::Vec3(screenWidth*0.5*bottom_left.x(), screenHeight*0.5*bottom_left.y(), -screenDistance)*r_left*r_bottom;
} }
protected:
virtual ~Keystone() {}
}; };
}
class KeystoneHandler : public osgGA::GUIEventHandler class KeystoneHandler : public osgGA::GUIEventHandler
{ {
public: public:
KeystoneHandler(Keystone* keystone); KeystoneHandler(osg::Keystone* keystone);
~KeystoneHandler() {} ~KeystoneHandler() {}
@ -127,7 +147,7 @@ public:
protected: protected:
osg::ref_ptr<Keystone> _keystone; osg::ref_ptr<osg::Keystone> _keystone;
osg::Vec2d _defaultIncrement; osg::Vec2d _defaultIncrement;
osg::Vec2d _ctrlIncrement; osg::Vec2d _ctrlIncrement;
@ -135,14 +155,14 @@ protected:
osg::Vec2d _keyIncrement; osg::Vec2d _keyIncrement;
osg::Vec2d _startPosition; osg::Vec2d _startPosition;
osg::ref_ptr<Keystone> _startControlPoints; osg::ref_ptr<osg::Keystone> _startControlPoints;
Region _selectedRegion; Region _selectedRegion;
osg::ref_ptr<Keystone> _currentControlPoints; osg::ref_ptr<osg::Keystone> _currentControlPoints;
}; };
KeystoneHandler::KeystoneHandler(Keystone* keystone): KeystoneHandler::KeystoneHandler(osg::Keystone* keystone):
_keystone(keystone), _keystone(keystone),
_defaultIncrement(0.0,0.0), _defaultIncrement(0.0,0.0),
_ctrlIncrement(1.0,1.0), _ctrlIncrement(1.0,1.0),
@ -150,7 +170,7 @@ KeystoneHandler::KeystoneHandler(Keystone* keystone):
_keyIncrement(0.005, 0.005), _keyIncrement(0.005, 0.005),
_selectedRegion(NONE_SELECTED) _selectedRegion(NONE_SELECTED)
{ {
_startControlPoints = new Keystone; _startControlPoints = new osg::Keystone;
_currentControlPoints = keystone; //new Keystone; _currentControlPoints = keystone; //new Keystone;
} }
@ -239,6 +259,15 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
if (!viewport) return false; if (!viewport) return false;
if (ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN)
{
if (ea.getUnmodifiedKey()=='g' && (ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL || ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
{
setKeystoneEditingEnabled(!getKeystoneEditingEnabled());
return true;
}
}
bool haveCameraMatch = false; bool haveCameraMatch = false;
float x = ea.getXnormalized(); float x = ea.getXnormalized();
float y = ea.getYnormalized(); float y = ea.getYnormalized();
@ -254,14 +283,11 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
} }
} }
if (!haveCameraMatch || !getKeystoneEditingEnabled()) return false;
if (!haveCameraMatch) return false;
switch(ea.getEventType()) switch(ea.getEventType())
{ {
case(osgGA::GUIEventAdapter::PUSH): case(osgGA::GUIEventAdapter::PUSH):
{
if (getKeystoneEditingEnabled())
{ {
osg::Vec2d scale = incrementScale(ea); osg::Vec2d scale = incrementScale(ea);
if (scale.length2()!=0.0) if (scale.length2()!=0.0)
@ -274,12 +300,9 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
{ {
_selectedRegion = NONE_SELECTED; _selectedRegion = NONE_SELECTED;
} }
}
return false; return false;
} }
case(osgGA::GUIEventAdapter::DRAG): case(osgGA::GUIEventAdapter::DRAG):
{
if (getKeystoneEditingEnabled())
{ {
if (_selectedRegion!=NONE_SELECTED) if (_selectedRegion!=NONE_SELECTED)
{ {
@ -290,26 +313,16 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
move(_selectedRegion, osg::Vec2d(delta.x()*scale.x(), delta.y()*scale.y()) ); move(_selectedRegion, osg::Vec2d(delta.x()*scale.x(), delta.y()*scale.y()) );
return true; return true;
} }
}
return false; return false;
} }
case(osgGA::GUIEventAdapter::RELEASE): case(osgGA::GUIEventAdapter::RELEASE):
{
if (getKeystoneEditingEnabled())
{ {
_selectedRegion = NONE_SELECTED; _selectedRegion = NONE_SELECTED;
}
return false; return false;
} }
case(osgGA::GUIEventAdapter::KEYDOWN): case(osgGA::GUIEventAdapter::KEYDOWN):
{ {
if (getKeystoneEditingEnabled()) if (ea.getKey()=='r')
{
if (ea.getUnmodifiedKey()=='g' && (ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL || ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
{
setKeystoneEditingEnabled(false);
}
else if (ea.getKey()=='r')
{ {
_selectedRegion = NONE_SELECTED; _selectedRegion = NONE_SELECTED;
_startControlPoints->reset(); _startControlPoints->reset();
@ -347,11 +360,6 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
{ {
_currentControlPoints->bottom_left.set(x, y); _currentControlPoints->bottom_left.set(x, y);
} }
}
else if (ea.getUnmodifiedKey()=='g' && (ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL || ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
{
setKeystoneEditingEnabled(true);
}
return false; return false;
} }
default: default:
@ -360,7 +368,7 @@ bool KeystoneHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionA
} }
struct KeystoneCullCallback : public osg::Drawable::CullCallback struct KeystoneCullCallback : public osg::Drawable::CullCallback
{ {
KeystoneCullCallback(Keystone* keystone=0):_keystone(keystone) {} KeystoneCullCallback(osg::Keystone* keystone=0):_keystone(keystone) {}
KeystoneCullCallback(const KeystoneCullCallback&, const osg::CopyOp&) {} KeystoneCullCallback(const KeystoneCullCallback&, const osg::CopyOp&) {}
META_Object(osg,KeystoneCullCallback); META_Object(osg,KeystoneCullCallback);
@ -371,13 +379,13 @@ struct KeystoneCullCallback : public osg::Drawable::CullCallback
return _keystone.valid() ? !_keystone->keystoneEditingEnabled : true; return _keystone.valid() ? !_keystone->keystoneEditingEnabled : true;
} }
osg::ref_ptr<Keystone> _keystone; osg::ref_ptr<osg::Keystone> _keystone;
}; };
struct KeystoneUpdateCallback : public osg::Drawable::UpdateCallback struct KeystoneUpdateCallback : public osg::Drawable::UpdateCallback
{ {
KeystoneUpdateCallback(Keystone* keystone=0):_keystone(keystone) {} KeystoneUpdateCallback(osg::Keystone* keystone=0):_keystone(keystone) {}
KeystoneUpdateCallback(const KeystoneUpdateCallback&, const osg::CopyOp&) {} KeystoneUpdateCallback(const KeystoneUpdateCallback&, const osg::CopyOp&) {}
META_Object(osg,KeystoneUpdateCallback); META_Object(osg,KeystoneUpdateCallback);
@ -414,11 +422,11 @@ struct KeystoneUpdateCallback : public osg::Drawable::UpdateCallback
geometry->dirtyBound(); geometry->dirtyBound();
} }
osg::ref_ptr<Keystone> _keystone; osg::ref_ptr<osg::Keystone> _keystone;
}; };
osg::Geode* createKeystoneDistortionMesh(Keystone* keystone) osg::Geode* createKeystoneDistortionMesh(osg::Keystone* keystone)
{ {
osg::ref_ptr<osg::Geode> geode = new osg::Geode; osg::ref_ptr<osg::Geode> geode = new osg::Geode;
@ -483,7 +491,7 @@ osg::Geode* createKeystoneDistortionMesh(Keystone* keystone)
return geode.release(); return geode.release();
} }
osg::Node* createGrid(Keystone* keystone, const osg::Vec4& colour) osg::Node* createGrid(osg::Keystone* keystone, const osg::Vec4& colour)
{ {
osg::ref_ptr<osg::Geode> geode = new osg::Geode; osg::ref_ptr<osg::Geode> geode = new osg::Geode;
@ -623,7 +631,7 @@ osg::Camera* assignKeystoneRenderToTextureCamera(osgViewer::View* view, osg::Gra
return camera.release(); return camera.release();
} }
osg::Camera* assignKeystoneDistortionCamera(osgViewer::View* view, osg::DisplaySettings* ds, osg::GraphicsContext* gc, int x, int y, int width, int height, GLenum buffer, osg::Texture* texture, Keystone* keystone) osg::Camera* assignKeystoneDistortionCamera(osgViewer::View* view, osg::DisplaySettings* ds, osg::GraphicsContext* gc, int x, int y, int width, int height, GLenum buffer, osg::Texture* texture, osg::Keystone* keystone)
{ {
double screenDistance = ds->getScreenDistance(); double screenDistance = ds->getScreenDistance();
double screenWidth = ds->getScreenWidth(); double screenWidth = ds->getScreenWidth();
@ -845,7 +853,7 @@ void setUpViewForStereo(osgViewer::View* view, osg::DisplaySettings* ds)
ds->setUseSceneViewForStereoHint(false); ds->setUseSceneViewForStereoHint(false);
osg::ref_ptr<Keystone> keystone = new Keystone; osg::ref_ptr<osg::Keystone> keystone = new osg::Keystone;
// set up view's main camera // set up view's main camera
@ -1114,7 +1122,7 @@ void setUpViewForStereo(osgViewer::View* view, osg::DisplaySettings* ds)
left_keystone_camera->addEventCallback(new KeystoneHandler(keystone.get())); left_keystone_camera->addEventCallback(new KeystoneHandler(keystone.get()));
osg::ref_ptr<Keystone> right_keystone = new Keystone; osg::ref_ptr<osg::Keystone> right_keystone = new osg::Keystone;
right_keystone->gridColour.set(0.0f,1.0f,0.0,1.0); right_keystone->gridColour.set(0.0f,1.0f,0.0,1.0);
// create Keystone right distortion camera // create Keystone right distortion camera
@ -1207,7 +1215,7 @@ void setUpViewForStereo(osgViewer::View* view, osg::DisplaySettings* ds)
left_keystone_camera->addEventCallback(new KeystoneHandler(keystone.get())); left_keystone_camera->addEventCallback(new KeystoneHandler(keystone.get()));
osg::ref_ptr<Keystone> right_keystone = new Keystone; osg::ref_ptr<osg::Keystone> right_keystone = new osg::Keystone;
right_keystone->gridColour.set(0.0f,1.0f,0.0,1.0); right_keystone->gridColour.set(0.0f,1.0f,0.0,1.0);
// create Keystone right distortion camera // create Keystone right distortion camera
@ -1399,7 +1407,7 @@ void setUpViewForStereo(osgViewer::View* view, osg::DisplaySettings* ds)
} }
void setUpViewForKeystone(osgViewer::View* view, Keystone* keystone) void setUpViewForKeystone(osgViewer::View* view, osg::Keystone* keystone)
{ {
int screenNum = 0; int screenNum = 0;
@ -1501,7 +1509,7 @@ int main( int argc, char **argv )
} }
else else
{ {
setUpViewForKeystone(&viewer, new Keystone); setUpViewForKeystone(&viewer, new osg::Keystone);
} }
viewer.realize(); viewer.realize();