Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/text_engine/multi_line_text.cc

Last change on this file was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 4.4 KB
RevLine 
[4744]1/*
[1853]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[1855]10
11   ### File Specific:
[5343]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]17
[7450]18#include "multi_line_text.h"
[5343]19#include "font.h"
[1853]20
[9869]21ObjectListDefinition(MultiLineText);
22
[5343]23/**
[7355]24 * @brief creates a new Text Element
[5343]25 * @param fontFile the Font to render this text in
26 * @param type The renderType to display this font in
27 */
[7757]28MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize, float lineWidth)
[7450]29  : Text(fontFile, textSize)
[5343]30{
[9869]31  this->registerObject(this, MultiLineText::_objectList);
[1856]32
[7757]33  this->lineSpacing = 1.0;
[7737]34  this->lineCount = 0;
[7754]35  this->setLineWidth(lineWidth);
[5343]36}
37
[3245]38/**
[7450]39 * @brief sets the maximum Line width
40 * @param lineWidth the maximum lineWidth.
[5343]41 */
[7450]42void MultiLineText::setLineWidth(float lineWidth)
[5343]43{
[7450]44  this->lineWidth = lineWidth;
[7457]45  this->setSizeX2D(lineWidth);
[7450]46  this->setupTextWidth();
[3365]47}
[1853]48
[7456]49
[7454]50/**
51 * @param lineSpacing: the Spacing between the lines
52 */
53void MultiLineText::setLineSpacing(float lineSpacing)
54{
55   this->lineSpacing = lineSpacing;
56   this->setupTextWidth();
57}
[1853]58
[7454]59
[3245]60/**
[7355]61 * @brief draws the Text
[5343]62 */
[7450]63void MultiLineText::draw() const
[5343]64{
[8619]65  if (unlikely(this->text().empty()))
[7448]66    return;
[5343]67  glPushMatrix();
[7919]68  glPushAttrib(GL_ENABLE_BIT);
[5343]69  // transform for alignment.
[7450]70  // TODO make the Stuff with the alignment
[5343]71  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
[5767]72    glTranslatef(-this->getSizeX2D(), 0, 0);
[5343]73  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
[5767]74    glTranslatef(-this->getSizeX2D()/2, 0, 0);
[5343]75
76  // drawing this Text.
[8768]77  this->font().select();
[8059]78
[7450]79  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
[7448]80  glRotatef(this->getAbsDir2D(), 0, 0, 1);
81
[9869]82  const Font::Glyph* tmpGlyph;
[7448]83  float posX = 0.0f;
[7450]84  float posY = 0.0f;
85  unsigned int lineNumber = 0;
86
[7448]87  glBegin(GL_QUADS);
[8619]88  for (unsigned int i = 0; i < this->text().size(); ++i)
[5343]89  {
[7757]90    if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber]))
[5343]91    {
[7451]92      // go to the next Line.
93      ++lineNumber;
[7758]94      posX = 0.0f;
[8761]95      posY += this->lineSpacing + this->size(); //this->font().getMaxHeight();
[7450]96    }
97
[8761]98    if(likely((tmpGlyph = this->font().getGlyphArray()[this->text()[i]]) != NULL))
[7450]99    {
[7448]100      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
[8619]101      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY);
[5419]102
[7448]103      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
[8619]104      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY + this->size());
[5419]105
[7448]106      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
[8619]107      glVertex2d(posX+tmpGlyph->minX*this->size(), posY+ this->size());
[5419]108
[7448]109      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
[8619]110      glVertex2d(posX+tmpGlyph->minX*this->size(), posY);
[5419]111
[8619]112      posX += tmpGlyph->advance * this->size();
[5343]113    }
114  }
[7448]115  glEnd();
[7919]116  glPopAttrib();
[5343]117  glPopMatrix();
118}
119
[7754]120
[5343]121/**
[7450]122 * @brief setting up the Text-Width if DYNAMIC
[5343]123 */
[7450]124void MultiLineText::setupTextWidth()
[5343]125{
[7450]126  this->lineEnds.clear();
127  float width = 0.0f;
[8619]128  float maxWidth = this->lineWidth / this->size();
[5343]129
[8619]130  for (unsigned int i = 0; i < this->text().size(); i++)
[5343]131  {
[8619]132    if (width > maxWidth || this->text()[i] == '\n')
[7450]133    {
[7756]134      if (likely(i > 0))
135      {
136        this->lineEnds.push_back( i -1 );
[8761]137        width = this->font().getGlyphArray()[this->text()[i-1]]->advance;
[7756]138      }
139      else
140        width = 0.0f;
[7450]141    }
[7756]142
[7450]143    // Advance the Text.
[8761]144    if(this->font().getGlyphArray()[this->text()[i]] != NULL)
145      width += this->font().getGlyphArray()[this->text()[i]]->advance;
[5343]146  }
[7757]147  this->lineCount = lineEnds.size() + 1;
[8761]148  this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->font().getMaxHeight()));
[5343]149}
[7757]150
[7758]151/**
152 * @brief print out some nice debug output
153 */
[7757]154void MultiLineText::debug() const
155{
[9406]156 printf("Debug %s::%s: %d lines\n", this->getClassCName(), this->getCName(), this->getLineCount());
[7757]157
[8619]158 std::string tmpText = this->text();
[7757]159 std::vector<unsigned int> ends = this->lineEnds;
160 ends.push_back(tmpText.size());
161
162 unsigned int prev = 0;
163  for (unsigned int i = 0; i < ends.size(); i++)
164  {
165    printf("Line %d: %s\n", i, tmpText.substr(prev, ends[i] - prev).c_str());
166    prev = ends[i];
167  }
168}
Note: See TracBrowser for help on using the repository browser.