From 231a5c0d8dfc6d5b0fd2cbbdbd10cea582562f85 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Sun, 25 Mar 2012 21:40:45 +0200 Subject: menu/text: added variable vertical alignment in the box of the control --- src/menu/text.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'src/menu/text.cpp') diff --git a/src/menu/text.cpp b/src/menu/text.cpp index d5e658e1..301cc62b 100644 --- a/src/menu/text.cpp +++ b/src/menu/text.cpp @@ -34,8 +34,10 @@ namespace usdx log4cpp::Category& Text::log = log4cpp::Category::getInstance("usdx.menu.text"); - Text::Text(Container *parent, const std::string text, const unsigned int size) : - DrawableControl(parent), text(text), auto_size(true), stretch(false) + Text::Text(Container *parent, const std::string text, + const unsigned int size, const VerticalTextAlignment* valign) : + DrawableControl(parent), text(text), auto_size(true), + stretch(false), offset(Point(0, 0)), valign(valign) { font = new FTBufferFont("DejaVuSans.ttf"); font->FaceSize(size); @@ -44,22 +46,39 @@ namespace usdx throw new std::exception(); } + realign(); } Text::~Text() { + if (valign) { + delete valign; + valign = NULL; + } + if (font) { delete font; font = NULL; } } - void Text::draw(void) + void Text::realign(void) { - glScalef(1, -1, 1); + Rectangle bbox(font->BBox(text.c_str())); + bbox.get_point1().set_y(font->Ascender()); + bbox.get_point2().set_y(font->Descender()); + valign->align(offset, bbox, get_size()); + } + void Text::draw(void) + { { boost::mutex::scoped_lock lock(font_mutex); + glTranslatef(offset.get_x(), offset.get_y(), 0.0f); + + // invert y axis, text is drawn using window orientation (origin is + // bottom left) + glScalef(1, -1, 1); font->Render(text.c_str()); } } @@ -68,6 +87,7 @@ namespace usdx { boost::mutex::scoped_lock lock(font_mutex); font->FaceSize(value); + realign(); } unsigned int Text::get_font_size(void) const @@ -79,6 +99,7 @@ namespace usdx { boost::mutex::scoped_lock lock(font_mutex); text = value; + realign(); } std::string Text::get_text(void) const -- cgit v1.2.3