Added event and update callbacks to pass up changes to the mouse position to the ImageSequence::seek() to control which images is selected based on mouse x position

This commit is contained in:
Robert Osfield
2012-10-31 16:07:23 +00:00
parent fa2fb07609
commit a6f3e0af78
5 changed files with 124 additions and 16 deletions

View File

@@ -2179,7 +2179,7 @@ osgDB::ReaderWriter::ReadResult ReaderWriterP3DXML::readNode(osgDB::XmlNode::Inp
{
osgPresentation::SlideEventHandler* seh = new osgPresentation::SlideEventHandler;
seh->set(presentation_node.get());
presentation_node->setEventCallback(seh);
presentation_node->addEventCallback(seh);
}
}
return presentation_node.release();

View File

@@ -57,6 +57,40 @@ void LayerAttributes::callLeaveCallbacks(osg::Node* node)
}
}
void ImageSequenceUpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
float x;
if (_propertyManager->getProperty(_propertyName,x))
{
double xMin = -1.0;
double xMax = 1.0;
double position = ((double)x-xMin)/(xMax-xMin)*_imageSequence->getLength();
_imageSequence->seek(position);
}
else
{
OSG_INFO<<"ImageSequenceUpdateCallback::operator() Could not find property : "<<_propertyName<<std::endl;
}
// note, callback is responsible for scenegraph traversal so
// they must call traverse(node,nv) to ensure that the
// scene graph subtree (and associated callbacks) are traversed.
traverse(node,nv);
}
bool PropertyEventCallback::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
_propertyManager->setProperty("mouse.x",ea.getX());
_propertyManager->setProperty("mouse.x_normalized",ea.getXnormalized());
_propertyManager->setProperty("mouse.y",ea.getX());
_propertyManager->setProperty("mouse.y_normalized",ea.getYnormalized());
return false;
}
struct InteractiveImageSequenceOperator : public ObjectOperator
{
@@ -90,10 +124,7 @@ struct InteractiveImageSequenceOperator : public ObjectOperator
void set(SlideEventHandler* seh)
{
OSG_NOTICE<<"Mouse x position is : "<<seh->getNormalizedMousePosition().x()<<std::endl;
double position = (static_cast<double>(seh->getNormalizedMousePosition().x())+1.0)*0.5*_imageSequence->getLength();
_imageSequence->seek(position);
OSG_NOTICE<<"InteractiveImageSequenceOperator::set(..)"<<std::endl;
}
osg::ref_ptr<osg::ImageSequence> _imageSequence;
@@ -694,8 +725,7 @@ SlideEventHandler::SlideEventHandler(osgViewer::Viewer* viewer):
_timeDelayOnNewSlideWithMovies(0.25f),
_minimumTimeBetweenKeyPresses(0.25),
_timeLastKeyPresses(-1.0),
_requestReload(false),
_normalizedMousePosition(0.0f,0.0f)
_requestReload(false)
{
s_seh = this;
}
@@ -789,7 +819,6 @@ double SlideEventHandler::getCurrentTimeDelayBetweenSlides() const
return _timePerSlide;
}
void SlideEventHandler::operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv);
@@ -821,8 +850,6 @@ bool SlideEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIAction
}
//else OSG_NOTICE<<"SlideEventHandler::handle() "<<ea.getTime()<<std::endl;
_normalizedMousePosition.set(ea.getXnormalized(), ea.getYnormalized());
if (ea.getHandled()) return false;
switch(ea.getEventType())

View File

@@ -156,6 +156,9 @@ SlideShowConstructor::SlideShowConstructor(osgDB::Options* options):
{
const osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
_propertyManager = new osgPresentation::PropertyManager;
_propertyEventCallback = new osgPresentation::PropertyEventCallback(_propertyManager.get());
_slideHeight = ds->getScreenHeight();
_slideWidth = ds->getScreenWidth();
_slideDistance = ds->getScreenDistance();
@@ -263,6 +266,10 @@ void SlideShowConstructor::createPresentation()
if (_loopPresentation) _root->addDescription("loop");
if (_autoSteppingActive) _root->addDescription("auto");
//_root->addEventCallback(_propertyEventCallback.get());
_presentationSwitch->setEventCallback(_propertyEventCallback.get());
}
LayerAttributes* SlideShowConstructor::getOrCreateLayerAttributes(osg::Node* node)
@@ -1101,6 +1108,12 @@ void SlideShowConstructor::addImage(const std::string& filename, const PositionD
pictureStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
}
osg::ImageSequence* imageSequence = dynamic_cast<osg::ImageSequence*>(image.get());
if (imageSequence && imageData.imageSequenceInteractionMode==ImageData::USE_MOUSE_X_POSITION)
{
subgraph->setUpdateCallback(new osgPresentation::ImageSequenceUpdateCallback(imageSequence, _propertyManager.get(), "mouse.x_normalized"));
}
// attached any rotation
if (positionData.rotation[0]!=0.0)
{
@@ -1116,7 +1129,6 @@ void SlideShowConstructor::addImage(const std::string& filename, const PositionD
subgraph = animation_transform;
}
// attached any animation
osg::AnimationPathCallback* animation = getAnimationPathCallback(positionData);
if (animation)
@@ -1283,6 +1295,12 @@ void SlideShowConstructor::addStereoImagePair(const std::string& filenameLeft, c
subgraph->addChild(pictureLeft);
subgraph->addChild(pictureRight);
osg::ImageSequence* imageSequence = dynamic_cast<osg::ImageSequence*>(imageLeft.get());
if (imageSequence && imageDataLeft.imageSequenceInteractionMode==ImageData::USE_MOUSE_X_POSITION)
{
subgraph->setUpdateCallback(new osgPresentation::ImageSequenceUpdateCallback(imageSequence, _propertyManager.get(), "mouse.x_normalized"));
}
// attach any meterial animation.
if (positionData.requiresMaterialAnimation())
subgraph = attachMaterialAnimation(subgraph,positionData)->asGroup();