src/GUIText.cpp

Go to the documentation of this file.
00001 #include "glwx.h"
00002 
00003 GUIText::GUIText(const std::string &text)
00004 {
00005   fontIndex =    -1;
00006   update    = true;
00007 
00008   color.set(1.0,1.0,1.0);
00009   scales.set(1.0f, 1.0f);
00010   setString(text);
00011 }
00012 
00013 GUIText::GUIText(const GUIText & copy)
00014 {
00015   this->operator=(copy);
00016 }
00017 
00018 GUIText &GUIText::operator =(const GUIText & copy)
00019 {
00020   if(this != &copy)
00021   {
00022     fontIndex = copy.fontIndex;
00023     scales    = copy.scales;
00024     update    = copy.update;
00025     color     = copy.color;
00026     size      = copy.size;
00027     text      = copy.text;
00028   }
00029   return *this;
00030 }
00031 
00032 GUIText &GUIText::operator =(const std::string & str)
00033 {
00034   setString(str);
00035   return *this;
00036 }
00037 
00038 bool GUIText::loadXMLSettings(const TiXmlElement *element)
00039 {
00040   if(!element || !element->Value() || strcmp(element->Value(),  "Text"))
00041     return Logger::writeErrorLog("Need a Text node in the xml file");
00042  
00043   for(const TiXmlElement *child = element->FirstChildElement(); 
00044       child;
00045           child = child->NextSiblingElement() )
00046   {
00047     const char * value = child->Value();
00048 
00049     if(value)
00050     {
00051       if(!strcmp(value, "Color"))
00052         XMLArbiter::fillComponents3f(child, color);
00053 
00054       if(!strcmp(value, "Font"))
00055         fontIndex = GUIFontManager::addFont(child);
00056     }
00057   }
00058 
00059   setHeightScale(XMLArbiter::fillComponents1f(element, "hScale", 1.0f));
00060   setWidthScale(XMLArbiter::fillComponents1f(element, "wScale", 1.0f));
00061   setFontIndex(XMLArbiter::fillComponents1i(element, "fontIndex", fontIndex));
00062   setString(element->Attribute("string"));
00063 
00064   return true;
00065 }
00066 
00067 const std::string& GUIText::getString(){  return text; }
00068 
00069 void GUIText::setString(const std::string &textArg)
00070 {
00071   if(textArg.size() && text != textArg)
00072   {
00073     text   = textArg;
00074     update = true;
00075   }
00076 }
00077 
00078 void GUIText::setString(const char *textArg)
00079 {
00080   if(textArg && text != textArg)
00081   {
00082     text   = textArg;
00083     update = true;
00084   }
00085 }
00086 
00087 void GUIText::clear()
00088 {
00089   text.clear();
00090   update = false;
00091   size.set(0,0);
00092 }
00093 
00094 int GUIText::getFontIndex()
00095 {
00096   return fontIndex;
00097 }
00098 
00099 void GUIText::setFontIndex(int index)
00100 {
00101   fontIndex = index;
00102 }
00103 
00104 void  GUIText::print(int x, int y, int startIndex, int endIndex)
00105 {
00106   if(!text.size())
00107     return;
00108 
00109   endIndex   = (endIndex  < 0) ? int(text.size()) : endIndex;
00110   startIndex = clamp(startIndex, 0, endIndex);
00111 
00112   GUIFontManager::setCurrentFont(fontIndex);
00113   GUIFont *currentFont = GUIFontManager::getCurrentFont();
00114   computeDimensions();
00115 
00116   if(!currentFont && !(currentFont = GUIFontManager::getDefaultFont()))
00117     return;
00118 
00119   currentFont->getFontObject()->printSubString(float(x), float(y), scales.x, scales.y, 
00120                                                color.x, color.y, color.z,
00121                                                startIndex, endIndex,
00122                                                text);
00123 }
00124 
00125 void  GUIText::printCenteredX (int x, int y, int startIndex, int endIndex)
00126 {
00127   if(!text.size())
00128     return;
00129 
00130   computeDimensions();
00131   print(x - size.x/2, y, startIndex, endIndex);
00132 }
00133 
00134 void  GUIText::printCenteredY (int x, int y, int startIndex, int endIndex)
00135 {
00136   if(!text.size())
00137     return;
00138 
00139   computeDimensions();
00140   print(x,  y - size.y/2, startIndex, endIndex);
00141 }
00142 
00143 void  GUIText::printCenteredXY(int x, int y, int startIndex, int endIndex)
00144 {
00145   if(!text.size())
00146     return;
00147 
00148   computeDimensions();
00149   print(x - size.x/2, y - size.y/2, startIndex, endIndex);
00150 }
00151 
00152 void GUIText::computeDimensions()
00153 {
00154   if(needUpdating())
00155   {
00156     GUIFontManager::setCurrentFont(fontIndex);
00157     GUIFont *currentFont = GUIFontManager::getCurrentFont();
00158 
00159     if(!currentFont && !(currentFont = GUIFontManager::getDefaultFont()))
00160       return;
00161 
00162     if(currentFont == GUIFontManager::getDefaultFont())
00163       fontIndex = GUIFontManager::findFontIndex(currentFont);
00164 
00165     if(text.size())
00166     {
00167       size = currentFont->getFontObject()->getStringDimensions(text);
00168       size.x = int(float(size.x)*scales.x);
00169     }
00170     size.y = int(float(currentFont->getFontObject()->getHeight())*scales.y);
00171     forceUpdate(false);
00172   }
00173 }
00174 
00175 void GUIText::setSize(const Tuple2i& sizeArg){ size = sizeArg; }
00176 void GUIText::setSize(int x, int y){ size.set(x, y); }
00177 
00178 int  GUIText::getHeight(){ return abs(size.y); }
00179 int  GUIText::getWidth() { return abs(size.x); }
00180 
00181 const Tuple2i& GUIText::getSize(){ return size; }
00182 bool  GUIText::needUpdating(){ return update; }
00183 
00184 void  GUIText::forceUpdate(bool updateArg){ update = updateArg; }
00185 void  GUIText::setColor(const Tuple3f &color){ setColor(color.x, color.y, color.z); }
00186 
00187 void GUIText::setColor(float r, float g, float b)
00188 {
00189   color.set(clamp(r, 0.0f, 255.0f),
00190             clamp(g, 0.0f, 255.0f),
00191             clamp(b, 0.0f, 255.0f));
00192 
00193   color.x /= (color.x > 1.0) ? 255.0f : 1.0f;
00194   color.y /= (color.y > 1.0) ? 255.0f : 1.0f;
00195   color.z /= (color.z > 1.0) ? 255.0f : 1.0f;
00196 }
00197 
00198 void   GUIText::setHeightScale(float hs)
00199 {
00200   scales.y = clamp(hs, 0.1f, 20.0f);
00201 }
00202 void   GUIText::setWidthScale(float ws)
00203 {
00204   scales.x = clamp(ws, 0.1f, 20.0f);
00205 }
00206 void   GUIText::setScales(Tuple2f scales)
00207 {
00208   setHeightScale(scales.y);
00209   setWidthScale(scales.x);
00210 }
00211  
00212 float  GUIText::getHeightScale()
00213 {
00214   return scales.y;
00215 }
00216 
00217 float  GUIText::getWidthScale()
00218 {
00219   return scales.x;
00220 } 
00221 
00222 const  Tuple2f &GUIText::getScales()
00223 {
00224   return scales;
00225 }
00226 const Tuple3f &GUIText::getColor()
00227 { 
00228   return color;
00229 }
00230 

Generated on Wed Dec 5 20:32:03 2007 for GLWX by  doxygen 1.5.3