Improved handling of callback object to open the door to use of general osg::CallbackObject as mechnisms for something simialr to Qt's signal/slot mechanism.

This commit is contained in:
Robert Osfield 2014-05-21 09:45:11 +00:00
parent 28c36d615b
commit 20fd80de3a
2 changed files with 27 additions and 64 deletions

View File

@ -16,6 +16,7 @@
#include <osg/Group>
#include <osg/BoundingBox>
#include <osg/ScriptEngine>
#include <osgGA/Event>
#include <osgGA/EventVisitor>
@ -44,6 +45,7 @@ public:
virtual void createGraphics();
virtual void createGraphicsImplementation();
virtual void setExtents(const osg::BoundingBoxf& bb);
const osg::BoundingBoxf& getExtents() const { return _extents; }
@ -83,6 +85,15 @@ public:
/** get whether the widget has focus or not.*/
virtual bool getHasEventFocus() const;
/** invoke all callbacks with specified names providing input and output parameters.*/
bool runCallbacks(const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters) { return osg::runNamedCallbackObjects(this, name, inputParameters, outputParameters); }
/** invoke all callbacks with specified names without any specified input or output parameters.*/
bool runCallbacks(const std::string& name) { osg::Parameters inputParameters, outputParameters; return osg::runNamedCallbackObjects(this, name, inputParameters, outputParameters); }
/** Compute the bounding sphere of the widget.*/
virtual osg::BoundingSphere computeBound() const;
/** update any focus related graphics+state to the focused state.*/
@ -108,7 +119,6 @@ protected:
osg::ref_ptr<AlignmentSettings> _alignmentSettings;
osg::ref_ptr<FrameSettings> _frameSettings;
osg::ref_ptr<TextSettings> _textSettings;
};
}

View File

@ -156,15 +156,7 @@ bool Widget::getHasEventFocus() const
void Widget::enter()
{
osg::CallbackObject* co = osg::getCallbackObject(this, "enter");
if (co)
{
co->run(this);
}
else
{
enterImplementation();
}
if (!runCallbacks("enter")) enterImplementation();
}
void Widget::enterImplementation()
@ -174,15 +166,7 @@ void Widget::enterImplementation()
void Widget::leave()
{
osg::CallbackObject* co = osg::getCallbackObject(this, "leave");
if (co)
{
co->run(this);
}
else
{
leaveImplementation();
}
if (!runCallbacks("leave")) leaveImplementation();
}
void Widget::leaveImplementation()
@ -192,25 +176,16 @@ void Widget::leaveImplementation()
void Widget::traverse(osg::NodeVisitor& nv)
{
osg::CallbackObject* co = osg::getCallbackObject(this, "traverse");
if (co)
if (nv.referenceCount()!=0)
{
// currently lua scripting takes a ref count so messes up handling of NodeVisitor's created on stack,
// so don't attempt to call the sctipt.
if (nv.referenceCount()==0)
{
traverseImplementation(nv);
return;
}
osg::Parameters inputParameters, outputParameters;
inputParameters.push_back(&nv);
co->run(this, inputParameters, outputParameters);
}
else
{
traverseImplementation(nv);
runCallbacks("traverse",inputParameters, outputParameters);
return;
}
traverseImplementation(nv);
}
void Widget::traverseImplementation(osg::NodeVisitor& nv)
@ -250,37 +225,24 @@ void Widget::traverseImplementation(osg::NodeVisitor& nv)
bool Widget::handle(osgGA::EventVisitor* ev, osgGA::Event* event)
{
osg::CallbackObject* co = osg::getCallbackObject(this, "handle");
if (co)
// currently lua scripting takes a ref count so messes up handling of NodeVisitor's created on stack,
// so don't attempt to call the sctipt.
if (ev->referenceCount()!=0)
{
// currently lua scripting takes a ref count so messes up handling of NodeVisitor's created on stack,
// so don't attempt to call the sctipt.
if (ev->referenceCount()==0)
{
return handleImplementation(ev, event);
}
osg::Parameters inputParameters, outputParameters;
inputParameters.push_back(ev);
inputParameters.push_back(event);
if (co->run(this, inputParameters, outputParameters))
if (runCallbacks("handle",inputParameters, outputParameters))
{
if (outputParameters.size()>=1)
{
osg::BoolValueObject* bvo = dynamic_cast<osg::BoolValueObject*>(outputParameters[0].get());
if (bvo)
{
return bvo->getValue();
}
return false;
return bvo ? bvo->getValue() : false;
}
}
return false;
}
else
{
return handleImplementation(ev, event);
}
return handleImplementation(ev, event);
}
bool Widget::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event)
@ -311,16 +273,7 @@ void Widget::dirty()
void Widget::createGraphics()
{
osg::CallbackObject* co = osg::getCallbackObject(this, "createGraphics");
if (co)
{
co->run(this);
}
else
{
createGraphicsImplementation();
}
if (!runCallbacks("createGraphics")) createGraphicsImplementation();
}
void Widget::createGraphicsImplementation()