From a45ba05bddf82b3bdf189cd33c20514d7bc15e30 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 29 May 2014 10:51:26 +0000 Subject: [PATCH] Added beginnings of ComboBox popup functionality. --- include/osgUI/ComboBox | 8 +++---- src/osgUI/ComboBox.cpp | 51 +++++++++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/include/osgUI/ComboBox b/include/osgUI/ComboBox index 63ccf48f8..719c27ab0 100644 --- a/include/osgUI/ComboBox +++ b/include/osgUI/ComboBox @@ -14,7 +14,7 @@ #ifndef OSGUI_COMBOBOX #define OSGUI_COMBOBOX -#include +#include #include #include @@ -72,7 +72,6 @@ public: Items& getItems() { return _items; } const Items& getItems() const { return _items; } - void setCurrentItem(unsigned int i); unsigned int getCurrentItem() const { return _currentItem; } @@ -84,11 +83,12 @@ public: protected: virtual ~ComboBox() {} - Items _items; unsigned int _currentItem; - osg::ref_ptr _switch; + osg::ref_ptr _buttonSwitch; + + osg::ref_ptr _popup; }; } diff --git a/src/osgUI/ComboBox.cpp b/src/osgUI/ComboBox.cpp index 92dbefb5c..237f3c115 100644 --- a/src/osgUI/ComboBox.cpp +++ b/src/osgUI/ComboBox.cpp @@ -94,37 +94,68 @@ void ComboBox::setCurrentItem(unsigned int i) { OSG_NOTICE << "ComboBox::setCurrentItem("<setSingleChildOn(_currentItem); + if (_buttonSwitch.valid()) _buttonSwitch->setSingleChildOn(_currentItem); } - void ComboBox::createGraphicsImplementation() { Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get(); - _switch = new osg::Switch; + _buttonSwitch = new osg::Switch; + _popup = new osgUI::Popup; + _popup->setVisible(true); if (!_items.empty()) { + + float itemWidth = (_extents.xMax()-_extents.xMin()); + float itemHeight = (_extents.yMax()-_extents.yMin()); + float popupHeight = itemHeight * _items.size(); + float gap = (_extents.yMax()-_extents.yMin())*0.1f; + float popupTop = _extents.yMin()-gap; + + osg::BoundingBox popupExtents(_extents.xMin(), popupTop-popupHeight, _extents.zMin(), _extents.xMin()+itemWidth, popupTop, _extents.zMax()); + _popup->setExtents(popupExtents); + + osg::BoundingBox popupItemExtents(_extents.xMin(), popupTop-itemHeight, _extents.zMin(), _extents.xMin()+itemWidth, popupTop, _extents.zMax()); + for(Items::iterator itr = _items.begin(); itr != _items.end(); ++itr) { Item* item = itr->get(); OSG_NOTICE<<"Creating item "<getText()<<", "<getColor()< group = new osg::Group; - if (item->getColor().a()!=0.0f) group->addChild( style->createPanel(_extents, item->getColor()) ); - if (!item->getText().empty()) group->addChild( style->createText(_extents, getAlignmentSettings(), getTextSettings(), item->getText()) ); - _switch->addChild(group.get()); + + // setup graphics for button + { + osg::ref_ptr group = new osg::Group; + if (item->getColor().a()!=0.0f) group->addChild( style->createPanel(_extents, item->getColor()) ); + if (!item->getText().empty()) group->addChild( style->createText(_extents, getAlignmentSettings(), getTextSettings(), item->getText()) ); + _buttonSwitch->addChild(group.get()); + } + + // setup graphics for popup + { + osg::ref_ptr group = new osg::Group; + if (item->getColor().a()!=0.0f) group->addChild( style->createPanel(popupItemExtents, item->getColor()) ); + if (!item->getText().empty()) group->addChild( style->createText(popupItemExtents, getAlignmentSettings(), getTextSettings(), item->getText()) ); + _popup->addChild(group.get()); + + popupItemExtents.yMin() -= itemHeight; + popupItemExtents.yMax() -= itemHeight; + } + } + } else { - _switch->addChild( style->createPanel(_extents, osg::Vec4(1.0f,1.0f,1.0f,1.0f)) ); + _buttonSwitch->addChild( style->createPanel(_extents, osg::Vec4(1.0f,1.0f,1.0f,1.0f)) ); } - _switch->setSingleChildOn(_currentItem); + _buttonSwitch->setSingleChildOn(_currentItem); style->setupClipStateSet(_extents, getOrCreateStateSet()); - setGraphicsSubgraph(0, _switch.get()); + setGraphicsSubgraph(0, _buttonSwitch.get()); + addChild(_popup.get()); }