Added scripting support for JumpData, KeyPosition, HomePosition and parts of SlideEventHandler that enable dispatching of user created events.

This commit is contained in:
Robert Osfield 2014-03-10 19:08:46 +00:00
parent 9b299dc4b9
commit ee266255eb
2 changed files with 145 additions and 21 deletions

View File

@ -41,7 +41,7 @@ enum Operation
FORWARD_TOUCH_EVENT
};
struct JumpData
struct JumpData : public osg::Object
{
JumpData():
relativeJump(true),
@ -60,7 +60,8 @@ struct JumpData
slideName(in_slideName),
layerName(in_layerName) {}
JumpData(const JumpData& rhs):
JumpData(const JumpData& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
osg::Object(rhs, copyop),
relativeJump(rhs.relativeJump),
slideNum(rhs.slideNum),
layerNum(rhs.layerNum),
@ -78,6 +79,8 @@ struct JumpData
return *this;
}
META_Object(osgPresentation, JumpData);
bool requiresJump() const
{
if (!slideName.empty() || !layerName.empty()) return true;
@ -86,6 +89,21 @@ struct JumpData
bool jump(SlideEventHandler* seh) const;
void setRelativeJump(bool flag) { relativeJump = flag; }
bool getRelativeJump() const { return relativeJump; }
void setSlideNum(int num) { slideNum = num; }
int getSlideNum() const { return slideNum; }
void setLayerNum(int num) { layerNum = num; }
int getLayerNum() const { return layerNum; }
void setSlideName(const std::string& name) { slideName = name; }
const std::string& getSlideName() const { return slideName; }
void setLayerName(const std::string& name) { layerName = name; }
const std::string& getLayerName() const { return layerName; }
bool relativeJump;
int slideNum;
int layerNum;
@ -95,21 +113,50 @@ struct JumpData
};
struct HomePosition : public virtual osg::Referenced
struct HomePosition : public osg::Object
{
HomePosition() {}
HomePosition():
eye(0.0, -1.0, 0.0),
center(0.0, 0.0, 0.0),
up(0.0, 0.0, 1.0) {}
HomePosition(const osg::Vec3& in_eye, const osg::Vec3& in_center, const osg::Vec3& in_up):
eye(in_eye),
center(in_center),
up(in_up) {}
osg::Vec3 eye;
osg::Vec3 center;
osg::Vec3 up;
HomePosition(const HomePosition& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
osg::Object(rhs, copyop),
eye(rhs.eye),
center(rhs.center),
up(rhs.up) {}
HomePosition& operator = (const HomePosition& rhs)
{
if (&rhs==this) return *this;
eye = rhs.eye;
center = rhs.center;
up = rhs.up;
return *this;
}
META_Object(osgPresentation, HomePosition);
void setEye(const osg::Vec3d& e) { eye = e; }
const osg::Vec3d& getEye() const { return eye; }
void setCenter(const osg::Vec3d& c) { center = c; }
const osg::Vec3d& getCenter() const { return center; }
void setUp(const osg::Vec3d& u) { up = u; }
const osg::Vec3d& getUp() const { return up; }
osg::Vec3d eye;
osg::Vec3d center;
osg::Vec3d up;
};
struct KeyPosition
struct KeyPosition : public osg::Object
{
KeyPosition(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX, bool forward_to_devices = false):
_key((osgGA::GUIEventAdapter::KeySymbol)key),
@ -117,6 +164,24 @@ struct KeyPosition
_y(y),
_forwardToDevices(forward_to_devices) {}
KeyPosition(const KeyPosition& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
osg::Object(rhs, copyop),
_key(rhs._key),
_x(rhs._x),
_y(rhs._y),
_forwardToDevices(rhs._forwardToDevices) {}
META_Object(osgPresentation, KeyPosition);
KeyPosition& operator = (const KeyPosition& rhs)
{
if (&rhs==this) return *this;
_key = rhs._key;
_x = rhs._x;
_y = rhs._y;
_forwardToDevices = rhs._forwardToDevices;
return *this;
}
void set(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX, bool forward_to_devices = false)
{
@ -126,10 +191,23 @@ struct KeyPosition
_forwardToDevices = forward_to_devices;
}
void setKey(int key) { _key = static_cast<osgGA::GUIEventAdapter::KeySymbol>(key); }
int getKey() const { return _key; }
void setX(float x) { _x = x; }
float getX() const { return _x; }
void setY(float y) { _y = y; }
float getY() const { return _y; }
void setForwardToDevices(bool flag) { _forwardToDevices = flag; }
bool getForwardToDevices() const { return _forwardToDevices; }
osgGA::GUIEventAdapter::KeySymbol _key;
float _x;
float _y;
bool _forwardToDevices;
float _x;
float _y;
bool _forwardToDevices;
};
struct LayerCallback : public virtual osg::Referenced
@ -254,7 +332,7 @@ public:
static SlideEventHandler* instance();
META_Object(osgslideshowApp,SlideEventHandler);
META_Object(osgPresentation,SlideEventHandler);
void set(osg::Node* model);
@ -316,7 +394,10 @@ public:
void setLoopPresentation(bool loop) { _loopPresentation = loop; }
bool getLoopPresentation() const { return _loopPresentation; }
void dispatchEvent(const KeyPosition& keyPosition);
void dispatchEvent(osgGA::Event* event);
void forwardEventToDevices(osgGA::Event* event);
void setRequestReload(bool flag);

View File

@ -39,7 +39,7 @@ SlideEventHandler* SlideEventHandler::instance() { return s_seh.get(); }
bool JumpData::jump(SlideEventHandler* seh) const
{
OSG_INFO<<"Requires jump "<<relativeJump<<", "<<slideNum<<", "<<layerNum<<", "<<slideName<<", "<<layerName<<std::endl;
OSG_NOTICE<<"Requires jump"<<seh<<", "<<relativeJump<<", "<<slideNum<<", "<<layerNum<<", "<<slideName<<", "<<layerName<<std::endl;
int slideNumToUse = slideNum;
int layerNumToUse = layerNum;
@ -47,15 +47,17 @@ bool JumpData::jump(SlideEventHandler* seh) const
if (!slideName.empty())
{
osg::Switch* presentation = seh->getPresentationSwitch();
for(unsigned int i=0; i<presentation->getNumChildren(); ++i)
if (presentation)
{
osg::Node* node = seh->getSlide(i);
std::string name;
if (node->getUserValue("name",name) && slideName==name)
for(unsigned int i=0; i<presentation->getNumChildren(); ++i)
{
slideNumToUse = i;
break;
osg::Node* node = seh->getSlide(i);
std::string name;
if (node->getUserValue("name",name) && slideName==name)
{
slideNumToUse = i;
break;
}
}
}
}
@ -1552,7 +1554,15 @@ void SlideEventHandler::releaseSlide(unsigned int slideNum)
void SlideEventHandler::forwardEventToDevices(osgGA::Event* event)
{
if (!event) return;
// dispatch cloned event to devices
if (!_viewer)
{
OSG_NOTICE<<"Warning: SlideEventHandler::forwardEventToDevices(Event*) error, no Viewer to dispatch to."<<std::endl;
return;
}
osgViewer::View::Devices& devices = _viewer->getDevices();
for(osgViewer::View::Devices::iterator i = devices.begin(); i != devices.end(); ++i)
{
@ -1563,8 +1573,36 @@ void SlideEventHandler::forwardEventToDevices(osgGA::Event* event)
}
}
void SlideEventHandler::dispatchEvent(osgGA::Event* event)
{
if (!event) return;
// dispatch cloned event to devices
if (!_viewer)
{
OSG_NOTICE<<"Warning: SlideEventHandler::forwardEventToDevices(Event*) error, no Viewer to dispatch to."<<std::endl;
return;
}
osgGA::EventQueue* eq = _viewer!=0 ? _viewer->getEventQueue() : 0;
if (!eq)
{
OSG_NOTICE<<"Warning: SlideEventHandler::dispatchEvent(KeyPosition&) error, no EventQueue to dispatch to."<<std::endl;
return;
}
eq->addEvent(event);
}
void SlideEventHandler::dispatchEvent(const KeyPosition& keyPosition)
{
if (!_viewer)
{
OSG_NOTICE<<"Warning: SlideEventHandler::dispatchEvent(KeyPosition*) error, no Viewer to dispatch to."<<std::endl;
return;
}
if (keyPosition._forwardToDevices)
{
osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter();
@ -1583,7 +1621,12 @@ void SlideEventHandler::dispatchEvent(const KeyPosition& keyPosition)
return;
}
osgGA::EventQueue* eq = _viewer->getEventQueue();
osgGA::EventQueue* eq = _viewer!=0 ? _viewer->getEventQueue() : 0;
if (!eq)
{
OSG_NOTICE<<"Warning: SlideEventHandler::dispatchEvent(KeyPosition&) error, no EventQueue to dispatch to."<<std::endl;
return;
}
// reset the time of the last key press to ensure that the event is disgarded as a key repeat.
_timeLastKeyPresses = -1.0;