From 1e9722601c4b84109fd65aaf27662de392bbe70d Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 2 Sep 2014 17:01:44 +0000 Subject: [PATCH] Refactored the handling of ComboBox item picking to make it faster and more reliable git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14433 16af8721-9629-0410-8352-f15c8da7e697 --- include/osgUI/ComboBox | 8 +++-- src/osgUI/ComboBox.cpp | 75 ++++++++++++------------------------------ 2 files changed, 26 insertions(+), 57 deletions(-) diff --git a/include/osgUI/ComboBox b/include/osgUI/ComboBox index 834096d3e..3f18fbbfd 100644 --- a/include/osgUI/ComboBox +++ b/include/osgUI/ComboBox @@ -74,7 +74,7 @@ public: void setCurrentIndex(unsigned int i); unsigned int getCurrentIndex() const { return _currentIndex; } - + virtual void currrentIndexChanged(unsigned int i); virtual void currentIndexChangedImplementation(unsigned int i); @@ -87,8 +87,10 @@ public: protected: virtual ~ComboBox() {} - Items _items; - unsigned int _currentIndex; + Items _items; + unsigned int _currentIndex; + osg::Vec3d _popupItemOrigin; + osg::Vec3d _popupItemSize; osg::ref_ptr _buttonSwitch; osg::ref_ptr _backgroundSwitch; diff --git a/src/osgUI/ComboBox.cpp b/src/osgUI/ComboBox.cpp index c06cae7bb..9088f299e 100644 --- a/src/osgUI/ComboBox.cpp +++ b/src/osgUI/ComboBox.cpp @@ -86,67 +86,30 @@ bool ComboBox::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event case(osgGA::GUIEventAdapter::PUSH): { - OSG_NOTICE<<"Button pressed "<getActionAdapter() : 0; - osgGA::GUIEventAdapter* ea = event ? event->asGUIEventAdapter() : 0; - if ((aa && ea) && aa->computeIntersections(*ea, ev->getNodePath(), intersections)) -// if ((aa && ea) && computeIntersections(ev, ea, intersections)) + if (_popup->getVisible() && _popup->getHasEventFocus()) { - OSG_NOTICE<<"ComboBox intersections { "<computeExtentsPositionInLocalCoordinates(ev, ea, position)) { - const osgUtil::LineSegmentIntersector::Intersection& hit = *itr; - OSG_NOTICE<<" hit:drawable "<getName()<setVisible(!_popup->getVisible()); - } - - if (_popup->getVisible() && _popup->getExtents().contains(localPosition, 1e-6)) - { - OSG_NOTICE<<"In pop up"<(position.y()); + if (index>=0 && index(_items.size())) { - if ((*itr)==this) break; - if ((*itr)->getUserValue("index",index)) break; - } - - if (index<_items.size()) - { - OSG_NOTICE<<" index selected "<setVisible(false); } + _popup->setVisible(false); + } + else if (getHasEventFocus()) + { + _popup->setVisible(!_popup->getVisible()); } else { - OSG_NOTICE<<"Toggling off ComboBox popup"<setVisible(false); + return false; } break; } @@ -291,14 +254,19 @@ void ComboBox::createGraphicsImplementation() float itemHeight = (_extents.yMax()-_extents.yMin()) - 2.0f*frameWidth; float popupHeight = (itemHeight)* _items.size() + margin*static_cast(_items.size()-1) + 2.0f*frameWidth; float popupTop = _extents.yMin()-frameWidth-margin*1.0f; - float popupLeft = _extents.xMin()+30.0f; - float popupRight = _extents.xMax()+30.0f; + float popupLeft = _extents.xMin(); + float popupRight = _extents.xMax(); + + osg::BoundingBox popupExtents(popupLeft, popupTop-popupHeight, _extents.zMin(), popupRight, popupTop, _extents.zMax()); _popup->setExtents(popupExtents); osg::BoundingBox popupItemExtents(popupExtents.xMin()+frameWidth, popupTop-frameWidth-itemHeight, popupExtents.zMin(), popupExtents.xMax()-frameWidth, popupTop-frameWidth, popupExtents.zMax()); + _popupItemOrigin.set(popupItemExtents.xMin(), popupItemExtents.yMax(), popupExtents.zMin()); + _popupItemSize.set(popupItemExtents.xMax()-popupItemExtents.xMin(), -(itemHeight+margin), 0.0); + unsigned int index = 0; for(Items::iterator itr = _items.begin(); itr != _items.end(); @@ -318,7 +286,6 @@ void ComboBox::createGraphicsImplementation() // setup graphics for popup { osg::ref_ptr group = new osg::Group; - group->setUserValue("index",index); 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()) );