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/Group>
#include <osg/BoundingBox> #include <osg/BoundingBox>
#include <osg/ScriptEngine>
#include <osgGA/Event> #include <osgGA/Event>
#include <osgGA/EventVisitor> #include <osgGA/EventVisitor>
@ -44,6 +45,7 @@ public:
virtual void createGraphics(); virtual void createGraphics();
virtual void createGraphicsImplementation(); virtual void createGraphicsImplementation();
virtual void setExtents(const osg::BoundingBoxf& bb); virtual void setExtents(const osg::BoundingBoxf& bb);
const osg::BoundingBoxf& getExtents() const { return _extents; } const osg::BoundingBoxf& getExtents() const { return _extents; }
@ -83,6 +85,15 @@ public:
/** get whether the widget has focus or not.*/ /** get whether the widget has focus or not.*/
virtual bool getHasEventFocus() const; 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; virtual osg::BoundingSphere computeBound() const;
/** update any focus related graphics+state to the focused state.*/ /** update any focus related graphics+state to the focused state.*/
@ -108,7 +119,6 @@ protected:
osg::ref_ptr<AlignmentSettings> _alignmentSettings; osg::ref_ptr<AlignmentSettings> _alignmentSettings;
osg::ref_ptr<FrameSettings> _frameSettings; osg::ref_ptr<FrameSettings> _frameSettings;
osg::ref_ptr<TextSettings> _textSettings; osg::ref_ptr<TextSettings> _textSettings;
}; };
} }

View File

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