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 +++++++++++-- src/menu/text.hpp | 21 +++++++++- src/menu/vertical_text_alignment.cpp | 59 ++++++++++++++++++++++++++ src/menu/vertical_text_alignment.hpp | 81 ++++++++++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 src/menu/vertical_text_alignment.cpp create mode 100644 src/menu/vertical_text_alignment.hpp (limited to 'src/menu') 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 diff --git a/src/menu/text.hpp b/src/menu/text.hpp index ad5fcb9a..7ec0dade 100644 --- a/src/menu/text.hpp +++ b/src/menu/text.hpp @@ -35,6 +35,7 @@ #include "drawable_control.hpp" #include "container.hpp" #include "utils/point.hpp" +#include "vertical_text_alignment.hpp" namespace usdx { @@ -62,15 +63,33 @@ namespace usdx */ bool stretch; + /** + * Offset of the baseline of the text to the top left corner of the + * control. + */ + Point offset; + FTFont *font; + const VerticalTextAlignment *valign; + boost::mutex font_mutex; + /** + * This function is used to recalculate the offset of the drawn + * text. It should be called every time the text or the font size is + * changed. + * + * @see: offset + */ + void realign(void); protected: virtual void draw(void); public: - Text(Container* parent, const std::string text = "", const unsigned int size = 60); + Text(Container*, const std::string = "", const unsigned int = 60, + const VerticalTextAlignment* = new VerticalTextAlignmentTop()); + virtual ~Text(); void set_font_size(const unsigned int value); diff --git a/src/menu/vertical_text_alignment.cpp b/src/menu/vertical_text_alignment.cpp new file mode 100644 index 00000000..bdd61853 --- /dev/null +++ b/src/menu/vertical_text_alignment.cpp @@ -0,0 +1,59 @@ +/* + * UltraStar Deluxe - Karaoke Game + * + * UltraStar Deluxe is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#include "vertical_text_alignment.hpp" + +namespace usdx +{ + void VerticalTextAlignmentTop::align(Point& offset, + const Rectangle& font, + const Dimension& box) const + { + offset.set_y(font.get_bottom()); + } + + void VerticalTextAlignmentBottom::align(Point& offset, + const Rectangle& font, + const Dimension& box) const + { + offset.set_y(box.get_height() + font.get_top()); + } + + void VerticalTextAlignmentBaseline::align(Point& offset, + const Rectangle& font, + const Dimension& box) const + { + offset.set_y(box.get_height()); + } + + void VerticalTextAlignmentCenter::align(Point& offset, + const Rectangle& font, + const Dimension& box) const + { + offset.set_y(((box.get_height() - font.get_height()) / 2) + font.get_bottom()); + } +}; + diff --git a/src/menu/vertical_text_alignment.hpp b/src/menu/vertical_text_alignment.hpp new file mode 100644 index 00000000..4c34ef47 --- /dev/null +++ b/src/menu/vertical_text_alignment.hpp @@ -0,0 +1,81 @@ +/* + * UltraStar Deluxe - Karaoke Game + * + * UltraStar Deluxe is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#ifndef VERTICAL_TEXT_ALIGNMENT_HPP +#define VERTICAL_TEXT_ALIGNMENT_HPP + +#include "utils/rectangle.hpp" +#include "utils/dimension.hpp" + +namespace usdx +{ + class VerticalTextAlignment + { + public: + /** + * This function should be used to align a text vertical in a box. + * + * @param offset reference where the offset should be modified (only the + * vertical values are modified, so that it is possible to combine it + * with an other Alignment) + * @param font size of the text, that should be displayed + * @param box size of the box, the text should be aligned in + */ + virtual void align(Point& offset, const Rectangle& font, + const Dimension& box) const = 0; + }; + + class VerticalTextAlignmentTop : public VerticalTextAlignment + { + public: + virtual void align(Point&, const Rectangle&, + const Dimension&) const; + }; + + class VerticalTextAlignmentBottom : public VerticalTextAlignment + { + public: + virtual void align(Point&, const Rectangle&, + const Dimension&) const; + }; + + class VerticalTextAlignmentBaseline : public VerticalTextAlignment + { + public: + virtual void align(Point&, const Rectangle&, + const Dimension&) const; + }; + + class VerticalTextAlignmentCenter : public VerticalTextAlignment + { + public: + virtual void align(Point&, const Rectangle&, + const Dimension&) const; + }; +}; + + +#endif -- cgit v1.2.3