Implemented clipping of widget to the widgets extents.

This commit is contained in:
Robert Osfield 2014-05-23 18:59:16 +00:00
parent cca18e82e8
commit f50ec0fe8e
7 changed files with 78 additions and 49 deletions

View File

@ -16,6 +16,7 @@
#include <osg/Object>
#include <osg/BoundingBox>
#include <osg/Texture2D>
#include <osg/Vec4>
#include <osgUI/AlignmentSettings>
@ -48,10 +49,13 @@ public:
virtual osg::Node* createText(const osg::BoundingBox& extents, const AlignmentSettings* as, const TextSettings* textSettings, const std::string& text);
virtual osg::Node* createIcon(const osg::BoundingBox& extents, const std::string& filename);
virtual void setupDialogStateSet(osg::StateSet* stateset);
virtual void setupClipStateSet(const osg::BoundingBox& extents, osg::StateSet* stateset);
protected:
virtual ~Style() {}
osg::ref_ptr<osg::Texture2D> _clipTexture;
osg::Vec4 _backgroundColor;
osg::Vec4 _textColor;
osg::Vec4 _disabledTextColor;

View File

@ -99,43 +99,31 @@ void ComboBox::setCurrentItem(unsigned int i)
void ComboBox::createGraphicsImplementation()
{
Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
if (_switch.valid() && _switch->getNumChildren()==_items.size())
_switch = new osg::Switch;
if (!_items.empty())
{
OSG_NOTICE<<"Need to update existing scene graph"<<std::endl;
_graphicsInitialized = true;
for(Items::iterator itr = _items.begin();
itr != _items.end();
++itr)
{
Item* item = itr->get();
OSG_NOTICE<<"Creating item "<<item->getText()<<", "<<item->getColor()<<std::endl;
osg::ref_ptr<osg::Group> 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
{
OSG_NOTICE<<"ComboBox::createGraphicsImplementation()"<<std::endl;
Widget::createGraphicsImplementation();
Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
_switch = new osg::Switch;
if (!_items.empty())
{
for(Items::iterator itr = _items.begin();
itr != _items.end();
++itr)
{
Item* item = itr->get();
OSG_NOTICE<<"Creating item "<<item->getText()<<", "<<item->getColor()<<std::endl;
osg::ref_ptr<osg::Group> 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);
setGraphicsSubgraph(_switch.get());
_switch->addChild( style->createPanel(_extents, osg::Vec4(1.0f,1.0f,1.0f,1.0f)) );
}
_switch->setSingleChildOn(_currentItem);
style->setupClipStateSet(_extents, getOrCreateStateSet());
setGraphicsSubgraph(_switch.get());
}

View File

@ -69,28 +69,33 @@ void Dialog::open()
void Dialog::createGraphicsImplementation()
{
OSG_NOTICE<<"Dialog::createGraphicsImplementation()"<<std::endl;
_transform = new osg::PositionAttitudeTransform;
Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
float titleHeight = 1.0;
float titleHeight = 10.0;
osg::BoundingBox titleBarExents(_extents.xMin(), _extents.yMax(), _extents.zMin(), _extents.xMax(), _extents.yMax()+titleHeight, _extents.zMin());
osg::Vec4 dialogBackgroundColor(0.8,0.8,0.8,1.0);
osg::Vec4 dialogTitleBackgroundColor(0.5,0.5,1.0,1.0);
_transform->addChild( style->createPanel(_extents, dialogBackgroundColor) );
_transform->addChild( style->createPanel(titleBarExents, dialogTitleBackgroundColor) );
osg::BoundingBox dialogWithTileExtents(_extents);
dialogWithTileExtents.expandBy(titleBarExents);
OSG_NOTICE<<"Dialog::_extents ("<<_extents.xMin()<<", "<<_extents.yMin()<<", "<<_extents.zMin()<<"), ("<<_extents.xMax()<<", "<<_extents.yMax()<<", "<<_extents.zMax()<<")"<<std::endl;
OSG_NOTICE<<"Dialog::titleBarExents ("<<titleBarExents.xMin()<<", "<<titleBarExents.yMin()<<", "<<titleBarExents.zMin()<<"), ("<<titleBarExents.xMax()<<", "<<titleBarExents.yMax()<<", "<<titleBarExents.zMax()<<")"<<std::endl;
OSG_NOTICE<<"Dialog::dialogWithTileExtents ("<<dialogWithTileExtents.xMin()<<", "<<dialogWithTileExtents.yMin()<<", "<<dialogWithTileExtents.zMin()<<"), ("<<dialogWithTileExtents.xMax()<<", "<<dialogWithTileExtents.yMax()<<", "<<dialogWithTileExtents.zMax()<<")"<<std::endl;
osg::ref_ptr<Node> node = style->createText(titleBarExents, getAlignmentSettings(), getTextSettings(), _title);
_titleDrawable = dynamic_cast<osgText::Text*>(node.get());
_titleDrawable->setDataVariance(osg::Object::DYNAMIC);
_transform->addChild(_titleDrawable.get());
style->setupDialogStateSet(getOrCreateStateSet());
style->setupClipStateSet(dialogWithTileExtents, getOrCreateStateSet());
setGraphicsSubgraph(_transform.get());
}

View File

@ -32,8 +32,6 @@ LineEdit::LineEdit(const osgUI::LineEdit& label, const osg::CopyOp& copyop):
bool LineEdit::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event)
{
OSG_NOTICE<<"LineEdit::handleImplementation"<<std::endl;
osgGA::GUIEventAdapter* ea = event->asGUIEventAdapter();
if (!ea) return false;
@ -78,12 +76,12 @@ void LineEdit::setText(const std::string& text)
void LineEdit::createGraphicsImplementation()
{
OSG_NOTICE<<"LineEdit::createGraphicsImplementation()"<<std::endl;
Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
osg::ref_ptr<Node> node = style->createText(_extents, getAlignmentSettings(), getTextSettings(), _text);
_textDrawable = dynamic_cast<osgText::Text*>(node.get());
_textDrawable->setDataVariance(osg::Object::DYNAMIC);
style->setupClipStateSet(_extents, getOrCreateStateSet());
setGraphicsSubgraph(_textDrawable.get());
}

View File

@ -76,9 +76,7 @@ void Popup::leaveImplementation()
void Popup::createGraphicsImplementation()
{
OSG_NOTICE<<"Popup::createGraphicsImplementation()"<<std::endl;
_transform = new osg::PositionAttitudeTransform;
_transform = new osg::PositionAttitudeTransform;
Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
@ -87,6 +85,7 @@ void Popup::createGraphicsImplementation()
_transform->addChild( style->createPanel(_extents, dialogBackgroundColor) );
style->setupDialogStateSet(getOrCreateStateSet());
style->setupClipStateSet(_extents, getOrCreateStateSet());
setGraphicsSubgraph(_transform.get());
}

View File

@ -71,8 +71,6 @@ void PushButton::leaveImplementation()
void PushButton::createGraphicsImplementation()
{
OSG_NOTICE<<"PushButton::createGraphicsImplementation()"<<std::endl;
osg::ref_ptr<osg::Group> group = new osg::Group;
Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
@ -97,6 +95,8 @@ void PushButton::createGraphicsImplementation()
group->addChild(_textDrawable.get());
style->setupClipStateSet(_extents, getOrCreateStateSet());
setGraphicsSubgraph(group.get());
}

View File

@ -14,7 +14,10 @@
#include <osgUI/Style>
#include <osg/Geode>
#include <osg/Depth>
#include <osg/TexGen>
#include <osg/AlphaFunc>
#include <osgText/Text>
#include <osgDB/ReadFile>
using namespace osgUI;
@ -28,10 +31,25 @@ OSG_INIT_SINGLETON_PROXY(StyleSingletonProxy, Style::instance())
Style::Style()
{
osg::ref_ptr<osg::Image> image = new osg::Image;
image->allocateImage(1,1,1,GL_RGBA, GL_FLOAT);
*(reinterpret_cast<osg::Vec4f*>(image->data(0,0,0))) = osg::Vec4f(1.0f, 1.0f, 1.0f, 1.0f);
_clipTexture = new osg::Texture2D;
_clipTexture->setImage(image.get());
_clipTexture->setBorderColor(osg::Vec4f(1.0f,1.0f,0.5f,0.0f));
_clipTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
_clipTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
_clipTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
_clipTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
//image = osgDB::readImageFile("Images/lz.rgb");
//_clipTexture->setImage(image.get());
}
Style::Style(const Style& style, const osg::CopyOp& copyop):
osg::Object(style, copyop)
osg::Object(style, copyop),
_clipTexture(style._clipTexture)
{
}
@ -93,6 +111,23 @@ osg::Node* Style::createIcon(const osg::BoundingBox& extents, const std::string&
void Style::setupDialogStateSet(osg::StateSet* stateset)
{
stateset->setRenderBinDetails(5, "TraversalOrderBin", osg::StateSet::OVERRIDE_RENDERBIN_DETAILS);
stateset->setAttributeAndModes( new osg::Depth(osg::Depth::LESS,0.0, 1.0,false), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
stateset->setAttributeAndModes( new osg::Depth(osg::Depth::LESS,0.0, 1.0,false), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
}
void Style::setupClipStateSet(const osg::BoundingBox& extents, osg::StateSet* stateset)
{
unsigned int clipTextureUnit = 1;
stateset->setAttributeAndModes( new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.0f), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
stateset->setTextureAttributeAndModes( clipTextureUnit, _clipTexture.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
osg::Matrixd matrix = osg::Matrixd::translate(osg::Vec3(-extents.xMin(), -extents.yMin(), -extents.zMin()))*
osg::Matrixd::scale(osg::Vec3(1.0f/(extents.xMax()-extents.xMin()), 1.0f/(extents.yMax()-extents.yMin()), 1.0f));
osg::ref_ptr<osg::TexGen> texgen = new osg::TexGen;
texgen->setPlanesFromMatrix(matrix);
texgen->setMode(osg::TexGen::OBJECT_LINEAR);
stateset->setTextureAttributeAndModes( clipTextureUnit, texgen.get(), osg::StateAttribute::ON);
}