ComboBox 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGUI_COMBOBOX
  14. #define OSGUI_COMBOBOX
  15. #include <osgUI/Popup>
  16. #include <osg/Switch>
  17. #include <osgText/Text>
  18. namespace osgUI
  19. {
  20. class OSGUI_EXPORT Item : public osg::Object
  21. {
  22. public:
  23. Item() : _color(1.0f,1.0f,1.0f,0.0f) {}
  24. Item(const std::string& str) : _text(str), _color(1.0f,1.0f,1.0f,0.0f) {}
  25. Item(const std::string& str, const osg::Vec4& col) : _text(str), _color(col) {}
  26. Item(const osg::Vec4& col) : _color(col) {}
  27. Item(const Item& item, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osg::Object(item,copyop), _text(item._text), _color(item._color) {}
  28. META_Object(osgUI, Item);
  29. void setText(const std::string& text) { _text = text; }
  30. std::string& getText() { return _text; }
  31. const std::string& getText() const { return _text; }
  32. void setColor(const osg::Vec4f& color) { _color = color; }
  33. osg::Vec4f& getColor() { return _color; }
  34. const osg::Vec4f& getColor() const { return _color; }
  35. protected:
  36. virtual ~Item() {}
  37. std::string _text;
  38. osg::Vec4 _color;
  39. };
  40. class OSGUI_EXPORT ComboBox : public osgUI::Widget
  41. {
  42. public:
  43. ComboBox();
  44. ComboBox(const ComboBox& combobox, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  45. META_Node(osgUI, ComboBox);
  46. void addItem(Item* item) { _items.push_back(item); dirty(); }
  47. void setItem(unsigned int i, Item* item) { _items[i] = item; dirty(); }
  48. Item* getItem(unsigned int i) { return _items[i].get(); }
  49. const Item* getItem(unsigned int i) const { return _items[i].get(); }
  50. void clear() { _items.clear(); dirty(); }
  51. void removeItem(unsigned int i) { _items.erase(_items.begin()+i); dirty(); }
  52. unsigned int getNumItems() { return static_cast<unsigned int>(_items.size()); }
  53. typedef std::vector< osg::ref_ptr<Item> > Items;
  54. void setItems(const Items& items) { _items = items; }
  55. Items& getItems() { return _items; }
  56. const Items& getItems() const { return _items; }
  57. void setCurrentIndex(unsigned int i);
  58. unsigned int getCurrentIndex() const { return _currentIndex; }
  59. virtual void currrentIndexChanged(unsigned int i);
  60. virtual void currentIndexChangedImplementation(unsigned int i);
  61. virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event);
  62. virtual void createGraphicsImplementation();
  63. virtual void enterImplementation();
  64. virtual void leaveImplementation();
  65. protected:
  66. virtual ~ComboBox() {}
  67. Items _items;
  68. unsigned int _currentIndex;
  69. osg::Vec3d _popupItemOrigin;
  70. osg::Vec3d _popupItemSize;
  71. osg::ref_ptr<osg::Switch> _buttonSwitch;
  72. osg::ref_ptr<osg::Switch> _backgroundSwitch;
  73. osg::ref_ptr<osgUI::Popup> _popup;
  74. };
  75. }
  76. #endif