openscenegraph
ComboBox
Go to the documentation of this file.
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
14#ifndef OSGUI_COMBOBOX
15#define OSGUI_COMBOBOX
16
17#include <osgUI/Popup>
18#include <osg/Switch>
19#include <osgText/Text>
20
21namespace osgUI
22{
23
24class OSGUI_EXPORT Item : public osg::Object
25{
26public:
27
28 Item() : _color(1.0f,1.0f,1.0f,0.0f) {}
29 Item(const std::string& str) : _text(str), _color(1.0f,1.0f,1.0f,0.0f) {}
30 Item(const std::string& str, const osg::Vec4& col) : _text(str), _color(col) {}
31 Item(const osg::Vec4& col) : _color(col) {}
32
33 Item(const Item& item, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osg::Object(item,copyop), _text(item._text), _color(item._color) {}
34
35 META_Object(osgUI, Item);
36
37 void setText(const std::string& text) { _text = text; }
38 std::string& getText() { return _text; }
39 const std::string& getText() const { return _text; }
40
41 void setColor(const osg::Vec4f& color) { _color = color; }
42 osg::Vec4f& getColor() { return _color; }
43 const osg::Vec4f& getColor() const { return _color; }
44
45protected:
46 virtual ~Item() {}
47
48 std::string _text;
49 osg::Vec4 _color;
50};
51
52class OSGUI_EXPORT ComboBox : public osgUI::Widget
53{
54public:
55 ComboBox();
56 ComboBox(const ComboBox& combobox, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
57 META_Node(osgUI, ComboBox);
58
59 void addItem(Item* item) { _items.push_back(item); dirty(); }
60
61 void setItem(unsigned int i, Item* item) { _items[i] = item; dirty(); }
62 Item* getItem(unsigned int i) { return _items[i].get(); }
63 const Item* getItem(unsigned int i) const { return _items[i].get(); }
64
65 void clear() { _items.clear(); dirty(); }
66 void removeItem(unsigned int i) { _items.erase(_items.begin()+i); dirty(); }
67 unsigned int getNumItems() { return static_cast<unsigned int>(_items.size()); }
68
69 typedef std::vector< osg::ref_ptr<Item> > Items;
70
71 void setItems(const Items& items) { _items = items; }
72 Items& getItems() { return _items; }
73 const Items& getItems() const { return _items; }
74
75 void setCurrentIndex(unsigned int i);
76 unsigned int getCurrentIndex() const { return _currentIndex; }
77
78 virtual void currrentIndexChanged(unsigned int i);
79 virtual void currentIndexChangedImplementation(unsigned int i);
80
81
82 virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event);
83 virtual void createGraphicsImplementation();
84 virtual void enterImplementation();
85 virtual void leaveImplementation();
86
87protected:
88 virtual ~ComboBox() {}
89
90 Items _items;
91 unsigned int _currentIndex;
92 osg::Vec3d _popupItemOrigin;
93 osg::Vec3d _popupItemSize;
94
95 osg::ref_ptr<osg::Switch> _buttonSwitch;
96 osg::ref_ptr<osg::Switch> _backgroundSwitch;
97 osg::ref_ptr<osgUI::Popup> _popup;
98};
99
100}
101
102#endif