From 20fd80de3a16f8c35f601eb778b8940637af1273 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 21 May 2014 09:45:11 +0000 Subject: [PATCH] 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. --- include/osgUI/Widget | 12 ++++++- src/osgUI/Widget.cpp | 79 +++++++++----------------------------------- 2 files changed, 27 insertions(+), 64 deletions(-) diff --git a/include/osgUI/Widget b/include/osgUI/Widget index 365366fa5..ca2235d8f 100644 --- a/include/osgUI/Widget +++ b/include/osgUI/Widget @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -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; osg::ref_ptr _frameSettings; osg::ref_ptr _textSettings; - }; } diff --git a/src/osgUI/Widget.cpp b/src/osgUI/Widget.cpp index 293a7bff2..090e0311e 100644 --- a/src/osgUI/Widget.cpp +++ b/src/osgUI/Widget.cpp @@ -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(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()