Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 16, 2006, 4:36:12 PM (19 years ago)
Author:
bensch
Message:

limited text-lenght

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/gui/src/lib/graphics/text_engine/limited_width_text.cc

    r8529 r8538  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
    1717
    18 #include "multi_line_text.h"
     18#include "limited_width_text.h"
    1919#include "font.h"
    2020
     
    2424 * @param type The renderType to display this font in
    2525 */
    26 MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize, float lineWidth)
    27   : Text(fontFile, textSize)
     26LimitedWidthText::LimitedWidthText(const std::string& fontFile, unsigned int textSize, float lineWidth, DotsPosition dotsPosition)
     27    : Text(fontFile, textSize)
    2828{
    29   this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText");
     29  this->setClassID(CL_LIMITED_WIDTH_TEXT, "LimitedWidthText");
    3030
    31   this->lineSpacing = 1.0;
    32   this->lineCount = 0;
     31  this->_dotsPosition = End;
    3332  this->setLineWidth(lineWidth);
    3433}
     
    3837 * @param lineWidth the maximum lineWidth.
    3938 */
    40 void MultiLineText::setLineWidth(float lineWidth)
     39void LimitedWidthText::setLineWidth(float lineWidth)
    4140{
    42   this->lineWidth = lineWidth;
     41  this->_lineWidth = lineWidth;
    4342  this->setSizeX2D(lineWidth);
    4443  this->setupTextWidth();
     
    4746
    4847/**
    49  * @param lineSpacing: the Spacing between the lines
    50  */
    51 void MultiLineText::setLineSpacing(float lineSpacing)
    52 {
    53    this->lineSpacing = lineSpacing;
    54    this->setupTextWidth();
    55 }
    56 
    57 
    58 /**
    5948 * @brief draws the Text
    6049 */
    61 void MultiLineText::draw() const
     50void LimitedWidthText::draw() const
    6251{
    63   if (unlikely(this->getText().empty()))
     52  if (unlikely(this->_dotedText.empty()))
    6453    return;
    6554  glPushMatrix();
    6655  glPushAttrib(GL_ENABLE_BIT);
    6756  // transform for alignment.
    68   // TODO make the Stuff with the alignment
    6957  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
    7058    glTranslatef(-this->getSizeX2D(), 0, 0);
     
    7462  // drawing this Text.
    7563  // setting the Blending effects
     64  glColor4fv(&this->getColor()[0]);
     65
     66
    7667  glActiveTexture(GL_TEXTURE0);
    7768
    78   glColor4fv(&this->getColor()[0]);
    7969  glEnable(GL_BLEND);
    8070  glEnable(GL_TEXTURE_2D);
     
    8373
    8474  glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture());
    85   glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
     75  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
    8676  glRotatef(this->getAbsDir2D(), 0, 0, 1);
    8777
    8878  Glyph* tmpGlyph;
    8979  float posX = 0.0f;
    90   float posY = 0.0f;
    91   unsigned int lineNumber = 0;
    92 
    9380  glBegin(GL_QUADS);
    94   for (unsigned int i = 0; i < this->getText().size(); ++i)
     81  for (unsigned int i = 0; i < this->_dotedText.size(); i++)
    9582  {
    96     if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber]))
    97     {
    98       // go to the next Line.
    99       ++lineNumber;
    100       posX = 0.0f;
    101       posY += this->lineSpacing + this->getSize(); //this->getFont()->getMaxHeight();
    102     }
    103 
    104     if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL))
     83    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->_dotedText[i]]) != NULL))
    10584    {
    10685      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
    107       glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY);
     86      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
    10887
    10988      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
    110       glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize());
     89      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
    11190
    11291      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
    113       glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize());
     92      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
    11493
    11594      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
    116       glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY);
     95      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
    11796
    11897      posX += tmpGlyph->advance * this->getSize();
     
    128107 * @brief setting up the Text-Width if DYNAMIC
    129108 */
    130 void MultiLineText::setupTextWidth()
     109void LimitedWidthText::setupTextWidth()
    131110{
    132   this->lineEnds.clear();
     111  float dotsSize = this->getFont()->getGlyphArray()[46]->advance * 3.0;
     112
    133113  float width = 0.0f;
    134   float maxWidth = this->lineWidth / this->getSize();
     114  float maxWidth = this->_lineWidth / this->getSize();
    135115
    136   for (unsigned int i = 0; i < this->getText().size(); i++)
     116  switch (this->_dotsPosition)
    137117  {
    138     if (width > maxWidth || this->getText()[i] == '\n')
    139     {
    140       if (likely(i > 0))
     118    case End:
     119      for (unsigned int i = 0; i < this->getText().size(); i++)
    141120      {
    142         this->lineEnds.push_back( i -1 );
    143         width = this->getFont()->getGlyphArray()[this->getText()[i-1]]->advance;
     121        if (width + dotsSize > maxWidth )
     122        {
     123          this->_dotedText = this->getText().substr(0, i) + "...";
     124          width += dotsSize;
     125          break;
     126        }
     127        // Advance the Text.
     128        if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL)
     129          width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance;
    144130      }
    145       else
    146         width = 0.0f;
    147     }
    148 
    149     // Advance the Text.
    150     if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL)
    151       width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance;
     131      this->setSizeX2D(width);
     132      break;
     133    case Begin:
     134      for (unsigned int i = this->getText().size() - 1; i < 0; i--)
     135      {
     136        if (width + dotsSize > maxWidth )
     137        {
     138          this->_dotedText = std::string("...") + this->getText().substr(i);
     139          width += dotsSize;
     140          break;
     141        }
     142        // Advance the Text.
     143        if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL)
     144          width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance;
     145      }
     146      this->setSizeX2D(width);
     147      break;
    152148  }
    153   this->lineCount = lineEnds.size() + 1;
    154   this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->getFont()->getMaxHeight()));
    155149}
    156150
     
    158152 * @brief print out some nice debug output
    159153 */
    160 void MultiLineText::debug() const
     154void LimitedWidthText::debug() const
    161155{
    162  printf("Debug %s::%s: %d lines\n", this->getClassName(), this->getName(), this->getLineCount());
    163 
    164  std::string tmpText = this->getText();
    165  std::vector<unsigned int> ends = this->lineEnds;
    166  ends.push_back(tmpText.size());
    167 
    168  unsigned int prev = 0;
    169   for (unsigned int i = 0; i < ends.size(); i++)
    170   {
    171     printf("Line %d: %s\n", i, tmpText.substr(prev, ends[i] - prev).c_str());
    172     prev = ends[i];
    173   }
     156  printf("Debug %s::%s \n", this->getClassName(), this->getName() );
    174157}
Note: See TracChangeset for help on using the changeset viewer.