From Cesar L.B. Silveira, "I have written these few lines of code which allow setting the label

of an osgWidget::Label with an osgText::String. I had to do this on a
project I'm working on, because I needed UTF-8 strings on my labels,
and using setLabel with std::string was not working.
"
This commit is contained in:
Robert Osfield 2009-10-10 11:06:30 +00:00
parent 058d31d7b5
commit 3fce07e5e5
2 changed files with 18 additions and 10 deletions

View File

@ -1,13 +1,13 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
* *
* This library is open source and may be redistributed and/or modified under * 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 * 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 * (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website. * included with this distribution, and on the openscenegraph.org website.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details. * OpenSceneGraph Public License for more details.
*/ */
@ -36,6 +36,7 @@ class OSGWIDGET_EXPORT Label: public Widget
virtual void positioned (); virtual void positioned ();
void setLabel (const std::string&); void setLabel (const std::string&);
void setLabel (const osgText::String&);
void setFont (const std::string&); void setFont (const std::string&);
void setFontSize (unsigned int); void setFontSize (unsigned int);
void setFontColor (const Color&); void setFontColor (const Color&);

View File

@ -8,7 +8,7 @@ namespace osgWidget {
Label::Label(const std::string& name, const std::string& label): Label::Label(const std::string& name, const std::string& label):
Widget (name, 0, 0), Widget (name, 0, 0),
_text (new osgText::Text()), _text (new osgText::Text()),
_textIndex (0) { _textIndex (0) {
_text->setAlignment(osgText::Text::LEFT_BOTTOM); _text->setAlignment(osgText::Text::LEFT_BOTTOM);
_text->setDataVariance(osg::Object::DYNAMIC); _text->setDataVariance(osg::Object::DYNAMIC);
@ -43,7 +43,7 @@ void Label::_calculateSize(const XYCoord& size) {
// if(size.x() && size.y()) setMinimumSize(size.x(), size.y()); // if(size.x() && size.y()) setMinimumSize(size.x(), size.y());
if(getWidth() < size.x()) setWidth(size.x()); if(getWidth() < size.x()) setWidth(size.x());
if(getHeight() < size.y()) setHeight(size.y()); if(getHeight() < size.y()) setHeight(size.y());
} }
@ -56,7 +56,7 @@ void Label::parented(Window* parent) {
// a Window; it'll have a _textIndex, but that _textIndex won't apply to the // a Window; it'll have a _textIndex, but that _textIndex won't apply to the
// currently cloned object. In this case, we'll need to check to be SURE. // currently cloned object. In this case, we'll need to check to be SURE.
osgText::Text* text = dynamic_cast<osgText::Text*>(geode->getDrawable(_textIndex)); osgText::Text* text = dynamic_cast<osgText::Text*>(geode->getDrawable(_textIndex));
if(text) parent->getGeode()->setDrawable(_textIndex, _text.get()); if(text) parent->getGeode()->setDrawable(_textIndex, _text.get());
// Otherwise, add it as new. // Otherwise, add it as new.
@ -108,16 +108,23 @@ void Label::setLabel(const std::string& label) {
_calculateSize(getTextSize()); _calculateSize(getTextSize());
} }
void Label::setLabel(const osgText::String& label)
{
_text->setText(label);
_calculateSize(getTextSize());
}
void Label::setFont(const std::string& font) { void Label::setFont(const std::string& font) {
_text->setFont(font); _text->setFont(font);
_calculateSize(getTextSize()); _calculateSize(getTextSize());
} }
void Label::setFontSize(unsigned int size) { void Label::setFontSize(unsigned int size) {
_text->setCharacterSize(size); _text->setCharacterSize(size);
_text->setFontResolution(size, size); _text->setFontResolution(size, size);
_calculateSize(getTextSize()); _calculateSize(getTextSize());
} }