GLWLabel.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <GLW/GLWLabel.h>
00022 #include <GLW/GLWFont.h>
00023 
00024 REGISTER_CLASS_SOURCE(GLWLabel);
00025 
00026 GLWLabel::GLWLabel(float x, float y, const LangString &labelText, 
00027         float size, unsigned int flags) : 
00028         GLWidget(x, y, 0.0f, 20.0f), size_(size),
00029         color_(GLWFont::widgetFontColor), flags_(flags)
00030 {
00031         setText(labelText);
00032         tooltipTransparent_ = true;
00033 }
00034 
00035 GLWLabel::~GLWLabel()
00036 {
00037 
00038 }
00039 
00040 void GLWLabel::setColor(const Vector &color)
00041 {
00042         color_ = color;
00043 }
00044 
00045 void GLWLabel::setSize(float size)
00046 {
00047         size_ = size;
00048         w_ = 0.0f;
00049 }
00050 
00051 void GLWLabel::setText(const LangString &text)
00052 { 
00053         labelText_ = text.c_str(); 
00054         if (flags_ & eMultiLine)
00055         {
00056                 labelTexts_.clear();
00057                 LangString current;
00058                 for (unsigned int i=0; i<text.size(); i++)
00059                 {
00060                         if (text[i] != '\n') current.push_back(text[i]);
00061                         else 
00062                         {
00063                                 labelTexts_.push_back(current);
00064                                 current.clear();
00065                         }
00066                 }
00067                 if (!current.empty())
00068                 {
00069                         labelTexts_.push_back(current);
00070                 }
00071         }
00072 
00073         w_ = 0.0f;
00074 }
00075 
00076 void GLWLabel::calcWidth()
00077 {
00078         if (w_ == 0.0f)
00079         {
00080                 if (flags_ & eMultiLine)
00081                 {
00082                         for (int i=0; i<(int) labelTexts_.size(); i++)
00083                         {
00084                                 w_ = MAX(w_, GLWFont::instance()->getGameFont()->getWidth(
00085                                         size_, labelTexts_[i]));
00086                         }
00087                         h_ = float(labelTexts_.size()) * 20.0f / 14.0f * size_;
00088                 }
00089                 else
00090                 {
00091                         w_ = GLWFont::instance()->getGameFont()->getWidth(
00092                                 size_, labelText_);
00093                         h_ = 20.0f / 14.0f * size_;
00094                 }
00095         }
00096 }
00097 
00098 void GLWLabel::draw()
00099 {
00100         GLWidget::draw();
00101 
00102         glColor3f(1.0f, 0.0f, 0.0f);
00103         calcWidth();
00104 
00105         if (flags_ & eMultiLine)
00106         {
00107                 for (int i=0; i<(int) labelTexts_.size(); i++)
00108                 {
00109                         const LangString &text = labelTexts_[labelTexts_.size() - (i + 1)];
00110                         GLWFont::instance()->getGameFont()->draw(
00111                                 color_, size_,
00112                                 x_, y_ + 6.0f + float(i) * 20.0f / 14.0f * size_, 0.0f, text);
00113                 }
00114         }
00115         else
00116         {
00117                 GLWFont::instance()->getGameFont()->draw(
00118                         color_, size_, x_, y_ + 6.0f, 0.0f, labelText_);
00119         }
00120 }

Generated on Mon Feb 16 15:14:45 2009 for Scorched3D by  doxygen 1.5.3