From f28d460caa22ee5309eded47aec782c7304e7da7 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 14 Aug 2014 19:00:37 +0000 Subject: [PATCH] Added support for ComboBox::currentIndexChanged*(uint) callback API. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14409 16af8721-9629-0410-8352-f15c8da7e697 --- include/osgUI/ComboBox | 10 ++-- src/osgUI/ComboBox.cpp | 48 ++++++++++++++----- .../serializers/osgUI/ComboBox.cpp | 25 +++++++++- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/include/osgUI/ComboBox b/include/osgUI/ComboBox index 82d159f78..834096d3e 100644 --- a/include/osgUI/ComboBox +++ b/include/osgUI/ComboBox @@ -72,8 +72,12 @@ public: Items& getItems() { return _items; } const Items& getItems() const { return _items; } - void setCurrentItem(unsigned int i); - unsigned int getCurrentItem() const { return _currentItem; } + void setCurrentIndex(unsigned int i); + unsigned int getCurrentIndex() const { return _currentIndex; } + + virtual void currrentIndexChanged(unsigned int i); + virtual void currentIndexChangedImplementation(unsigned int i); + virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event); virtual void createGraphicsImplementation(); @@ -84,7 +88,7 @@ protected: virtual ~ComboBox() {} Items _items; - unsigned int _currentItem; + unsigned int _currentIndex; osg::ref_ptr _buttonSwitch; osg::ref_ptr _backgroundSwitch; diff --git a/src/osgUI/ComboBox.cpp b/src/osgUI/ComboBox.cpp index 6a738584b..e2ec9217b 100644 --- a/src/osgUI/ComboBox.cpp +++ b/src/osgUI/ComboBox.cpp @@ -23,7 +23,7 @@ using namespace osgUI; ComboBox::ComboBox(): - _currentItem(0) + _currentIndex(0) { } @@ -56,12 +56,12 @@ bool ComboBox::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event case(osgGA::GUIEventAdapter::SCROLL): if (ea->getScrollingMotion()==osgGA::GUIEventAdapter::SCROLL_DOWN) { - if (getCurrentItem()getScrollingMotion()==osgGA::GUIEventAdapter::SCROLL_UP) { - if (getCurrentItem()>0) setCurrentItem(getCurrentItem()-1); + if (getCurrentIndex()>0) setCurrentIndex(getCurrentIndex()-1); return true; } break; @@ -69,12 +69,12 @@ bool ComboBox::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event case(osgGA::GUIEventAdapter::KEYDOWN): if (ea->getKey()==osgGA::GUIEventAdapter::KEY_Down) { - if (getCurrentItem()getKey()==osgGA::GUIEventAdapter::KEY_Up) { - if (getCurrentItem()>0) setCurrentItem(getCurrentItem()-1); + if (getCurrentIndex()>0) setCurrentIndex(getCurrentIndex()-1); return true; } @@ -127,7 +127,7 @@ bool ComboBox::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event if (index<_items.size()) { OSG_NOTICE<<" index selected "<setSingleChildOn(0); } -void ComboBox::setCurrentItem(unsigned int i) +void ComboBox::setCurrentIndex(unsigned int i) { - OSG_NOTICE << "ComboBox::setCurrentItem("<setSingleChildOn(_currentItem); + // OSG_NOTICE << "ComboBox::setCurrentIndex("<setSingleChildOn(_currentIndex); + + currrentIndexChanged(_currentIndex); } +void ComboBox::currrentIndexChanged(unsigned int i) +{ + osg::CallbackObject* co = getCallbackObject(this, "currentIndexChanged"); + if (co) + { + osg::Parameters inputParameters, outputParameters; + inputParameters.push_back(new osg::UIntValueObject("index",i)); + if (co->run(this, inputParameters, outputParameters)) + { + return; + } + } + currentIndexChangedImplementation(i); +} + +void ComboBox::currentIndexChangedImplementation(unsigned int i) +{ + OSG_NOTICE<<"ComboBox::currentIndexChangedImplementation("<addChild( style->createPanel(_extents, frameColor) ); } - _buttonSwitch->setSingleChildOn(_currentItem); + _buttonSwitch->setSingleChildOn(_currentIndex); style->setupClipStateSet(_extents, getOrCreateStateSet()); diff --git a/src/osgWrappers/serializers/osgUI/ComboBox.cpp b/src/osgWrappers/serializers/osgUI/ComboBox.cpp index a2744ce3e..38a936b6d 100644 --- a/src/osgWrappers/serializers/osgUI/ComboBox.cpp +++ b/src/osgWrappers/serializers/osgUI/ComboBox.cpp @@ -4,12 +4,35 @@ #include #include +struct ComboBoxCurrentIndexChangedImplementation : public osgDB::MethodObject +{ + virtual bool run(void* objectPtr, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const + { + if (inputParameters.empty()) return false; + + osg::Object* indexObject = inputParameters[0].get(); + + unsigned int index = 0; + osg::DoubleValueObject* dvo = dynamic_cast(indexObject); + if (dvo) index = static_cast(dvo->getValue()); + else + { + osg::UIntValueObject* uivo = dynamic_cast(indexObject); + if (uivo) index = uivo->getValue(); + } + osgUI::ComboBox* cb = reinterpret_cast(objectPtr); + cb->currentIndexChangedImplementation(index); + + return true; + } +}; REGISTER_OBJECT_WRAPPER( ComboBox, new osgUI::ComboBox, osgUI::ComboBox, "osg::Object osg::Node osg::Group osgUI::Widget osgUI::ComboBox" ) { - ADD_UINT_SERIALIZER(CurrentItem, 0); + ADD_UINT_SERIALIZER(CurrentIndex, 0); ADD_VECTOR_SERIALIZER( Items, osgUI::ComboBox::Items, osgDB::BaseSerializer::RW_OBJECT, 0 ); + ADD_METHOD_OBJECT( "currentIndexChangedImplementation", ComboBoxCurrentIndexChangedImplementation ); }