canvas::Text: get maximum width (if displayed on a single line).

This commit is contained in:
Thomas Geymayer 2014-06-17 16:12:29 +02:00
parent 9ffc0ae8bc
commit 2c41c25fcd
2 changed files with 14 additions and 7 deletions

View File

@ -40,7 +40,7 @@ namespace canvas
void setFill(const std::string& fill); void setFill(const std::string& fill);
void setBackgroundColor(const std::string& fill); void setBackgroundColor(const std::string& fill);
int heightForWidth(int w) const; SGVec2i sizeForWidth(int w) const;
osg::Vec2 handleHit(const osg::Vec2f& pos); osg::Vec2 handleHit(const osg::Vec2f& pos);
virtual osg::BoundingBox computeBound() const; virtual osg::BoundingBox computeBound() const;
@ -98,16 +98,16 @@ namespace canvas
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// simplified version of osgText::Text::computeGlyphRepresentation() to // simplified version of osgText::Text::computeGlyphRepresentation() to
// just calculate the height for a given weight. Glpyh calculations/creating // just calculate the size for a given weight. Glpyh calculations/creating
// is not necessary for this... // is not necessary for this...
int Text::TextOSG::heightForWidth(int w) const SGVec2i Text::TextOSG::sizeForWidth(int w) const
{ {
if( _text.empty() ) if( _text.empty() )
return 0; return SGVec2i(0, 0);
osgText::Font* activefont = const_cast<osgText::Font*>(getActiveFont()); osgText::Font* activefont = const_cast<osgText::Font*>(getActiveFont());
if( !activefont ) if( !activefont )
return -1; return SGVec2i(-1, -1);
float max_width_safe = _maximumWidth; float max_width_safe = _maximumWidth;
const_cast<TextOSG*>(this)->_maximumWidth = w; const_cast<TextOSG*>(this)->_maximumWidth = w;
@ -368,7 +368,7 @@ namespace canvas
const_cast<TextOSG*>(this)->_maximumWidth = max_width_safe; const_cast<TextOSG*>(this)->_maximumWidth = max_width_safe;
return bb.height(); return bb.size();
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -648,7 +648,13 @@ namespace canvas
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int Text::heightForWidth(int w) const int Text::heightForWidth(int w) const
{ {
return _text->heightForWidth(w); return _text->sizeForWidth(w).y();
}
//----------------------------------------------------------------------------
int Text::maxWidth() const
{
return _text->sizeForWidth(INT_MAX).x();
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------

View File

@ -47,6 +47,7 @@ namespace canvas
void setAlignment(const char* align); void setAlignment(const char* align);
int heightForWidth(int w) const; int heightForWidth(int w) const;
int maxWidth() const;
osg::Vec2 getNearestCursor(const osg::Vec2& pos) const; osg::Vec2 getNearestCursor(const osg::Vec2& pos) const;
protected: protected: