diff --git a/include/osgUI/ComboBox b/include/osgUI/ComboBox new file mode 100644 index 000000000..63ccf48f8 --- /dev/null +++ b/include/osgUI/ComboBox @@ -0,0 +1,96 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGUI_COMBOBOX +#define OSGUI_COMBOBOX + +#include +#include +#include + +namespace osgUI +{ + +class OSGUI_EXPORT Item : public osg::Object +{ +public: + + Item() : _color(1.0f,1.0f,1.0f,1.0f) {} + Item(const std::string& str) : _text(str), _color(1.0f,1.0f,1.0f,1.0f) {} + Item(const std::string& str, const osg::Vec4& col) : _text(str), _color(col) {} + Item(const osg::Vec4& col) : _color(col) {} + + Item(const Item& item, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : osg::Object(item,copyop), _text(item._text), _color(item._color) {} + + META_Object(osgUI, Item); + + void setText(const std::string& text) { _text = text; } + std::string& getText() { return _text; } + const std::string& getText() const { return _text; } + + void setColor(const osg::Vec4f& color) { _color = color; } + osg::Vec4f& getColor() { return _color; } + const osg::Vec4f& getColor() const { return _color; } + +protected: + virtual ~Item() {} + + std::string _text; + osg::Vec4 _color; +}; + +class OSGUI_EXPORT ComboBox : public osgUI::Widget +{ +public: + ComboBox(); + ComboBox(const ComboBox& combobox, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + META_Node(osgUI, ComboBox); + + void addItem(Item* item) { _items.push_back(item); dirty(); } + + void setItem(unsigned int i, Item* item) { _items[i] = item; dirty(); } + Item* getItem(unsigned int i) { return _items[i].get(); } + const Item* getItem(unsigned int i) const { return _items[i].get(); } + + void clear() { _items.clear(); dirty(); } + void removeItem(unsigned int i) { _items.erase(_items.begin()+i); dirty(); } + unsigned int getNumItems() { return static_cast(_items.size()); } + + typedef std::vector< osg::ref_ptr > Items; + + void setItems(const Items& items) { _items = items; } + Items& getItems() { return _items; } + const Items& getItems() const { return _items; } + + + void setCurrentItem(unsigned int i); + unsigned int getCurrentItem() const { return _currentItem; } + + virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event); + virtual void createGraphicsImplementation(); + virtual void enterImplementation(); + virtual void leaveImplementation(); + +protected: + virtual ~ComboBox() {} + + + Items _items; + unsigned int _currentItem; + + osg::ref_ptr _switch; +}; + +} + +#endif diff --git a/include/osgUI/PushButton b/include/osgUI/PushButton index 758698058..966ba2092 100644 --- a/include/osgUI/PushButton +++ b/include/osgUI/PushButton @@ -25,7 +25,7 @@ class OSGUI_EXPORT PushButton : public osgUI::Widget { public: PushButton(); - PushButton(const PushButton& label, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + PushButton(const PushButton& pb, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); META_Node(osgUI, PushButton); void setText(const std::string& text) { _text = text; dirty(); } diff --git a/src/osgUI/CMakeLists.txt b/src/osgUI/CMakeLists.txt index cb8a2dcda..bbbc1e38c 100644 --- a/src/osgUI/CMakeLists.txt +++ b/src/osgUI/CMakeLists.txt @@ -13,6 +13,7 @@ SET(TARGET_H ${HEADER_PATH}/Label ${HEADER_PATH}/LineEdit ${HEADER_PATH}/PushButton + ${HEADER_PATH}/ComboBox ${HEADER_PATH}/Style ${HEADER_PATH}/AlignmentSettings ${HEADER_PATH}/FrameSettings @@ -24,6 +25,7 @@ SET(TARGET_SRC Label.cpp LineEdit.cpp PushButton.cpp + ComboBox.cpp Style.cpp AlignmentSettings.cpp FrameSettings.cpp diff --git a/src/osgUI/ComboBox.cpp b/src/osgUI/ComboBox.cpp new file mode 100644 index 000000000..fa99ce8c0 --- /dev/null +++ b/src/osgUI/ComboBox.cpp @@ -0,0 +1,128 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + + +#include +#include +#include +#include +#include +#include + +using namespace osgUI; + +ComboBox::ComboBox() +{ +} + +ComboBox::ComboBox(const osgUI::ComboBox& combobox, const osg::CopyOp& copyop): + Widget(combobox, copyop), + _items(combobox._items) +{ +} + +bool ComboBox::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event) +{ + // OSG_NOTICE<<"ComboBox::handleImplementation"<asGUIEventAdapter(); + if (!ea) return false; + + switch(ea->getEventType()) + { + case(osgGA::GUIEventAdapter::SCROLL): + OSG_NOTICE<<"Scroll "<getScrollingMotion()==osgGA::GUIEventAdapter::SCROLL_DOWN) + { + OSG_NOTICE<<"Scroll Down`"<getScrollingMotion()==osgGA::GUIEventAdapter::SCROLL_UP) + { + OSG_NOTICE<<"Scroll Up`"<0) setCurrentItem(getCurrentItem()-1); + } + break; + case(osgGA::GUIEventAdapter::PUSH): + OSG_NOTICE<<"Button pressed "<setSingleChildOn(_currentItem); +} + + +void ComboBox::createGraphicsImplementation() +{ + + if (_switch.valid() && _switch->getNumChildren()==_items.size()) + { + OSG_NOTICE<<"Need to update existing scene graph"<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()); + } + } + else + { + _switch->addChild( style->createPanel(_extents, osg::Vec4(1.0f,1.0f,1.0f,1.0f)) ); + } + + _switch->setSingleChildOn(_currentItem); + } +} diff --git a/src/osgUI/PushButton.cpp b/src/osgUI/PushButton.cpp index c7f23cdad..5b8e9e0b6 100644 --- a/src/osgUI/PushButton.cpp +++ b/src/osgUI/PushButton.cpp @@ -24,9 +24,9 @@ PushButton::PushButton() { } -PushButton::PushButton(const osgUI::PushButton& label, const osg::CopyOp& copyop): - Widget(label, copyop), - _text(label._text) +PushButton::PushButton(const osgUI::PushButton& pb, const osg::CopyOp& copyop): + Widget(pb, copyop), + _text(pb._text) { } diff --git a/src/osgWrappers/serializers/osgUI/ComboBox.cpp b/src/osgWrappers/serializers/osgUI/ComboBox.cpp new file mode 100644 index 000000000..a2744ce3e --- /dev/null +++ b/src/osgWrappers/serializers/osgUI/ComboBox.cpp @@ -0,0 +1,15 @@ +#include +#include +#include +#include +#include + + +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_VECTOR_SERIALIZER( Items, osgUI::ComboBox::Items, osgDB::BaseSerializer::RW_OBJECT, 0 ); +}