Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10549 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
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
17
18#include "multi_line_text.h"
19#include "font.h"
20
21ObjectListDefinition(MultiLineText);
22
23/**
24 * @brief creates a new Text Element
25 * @param fontFile the Font to render this text in
26 * @param type The renderType to display this font in
27 */
28MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize, float lineWidth)
29  : Text(fontFile, textSize)
30{
31  this->registerObject(this, MultiLineText::_objectList);
32
33  this->lineSpacing = 1.0;
34  this->lineCount = 0;
35  this->setLineWidth(lineWidth);
36}
37
38/**
39 * @brief sets the maximum Line width
40 * @param lineWidth the maximum lineWidth.
41 */
42void MultiLineText::setLineWidth(float lineWidth)
43{
44  this->lineWidth = lineWidth;
45  this->setSizeX2D(lineWidth);
46  this->setupTextWidth();
47}
48
49
50/**
51 * @param lineSpacing: the Spacing between the lines
52 */
53void MultiLineText::setLineSpacing(float lineSpacing)
54{
55   this->lineSpacing = lineSpacing;
56   this->setupTextWidth();
57}
58
59
60/**
61 * @brief draws the Text
62 */
63void MultiLineText::draw() const
64{
65  if (unlikely(this->text().empty()))
66    return;
67  glPushMatrix();
68  glPushAttrib(GL_ENABLE_BIT);
69  // transform for alignment.
70  // TODO make the Stuff with the alignment
71  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
72    glTranslatef(-this->getSizeX2D(), 0, 0);
73  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
74    glTranslatef(-this->getSizeX2D()/2, 0, 0);
75
76  // drawing this Text.
77  this->font().select();
78
79  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
80  glRotatef(this->getAbsDir2D(), 0, 0, 1);
81
82  const Font::Glyph* tmpGlyph;
83  float posX = 0.0f;
84  float posY = 0.0f;
85  unsigned int lineNumber = 0;
86
87  glBegin(GL_QUADS);
88  for (unsigned int i = 0; i < this->text().size(); ++i)
89  {
90    if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber]))
91    {
92      // go to the next Line.
93      ++lineNumber;
94      posX = 0.0f;
95      posY += this->lineSpacing + this->size(); //this->font().getMaxHeight();
96    }
97
98    if(likely((tmpGlyph = this->font().getGlyphArray()[this->text()[i]]) != NULL))
99    {
100      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
101      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY);
102
103      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
104      glVertex2d(posX+tmpGlyph->maxX*this->size(), posY + this->size());
105
106      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
107      glVertex2d(posX+tmpGlyph->minX*this->size(), posY+ this->size());
108
109      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
110      glVertex2d(posX+tmpGlyph->minX*this->size(), posY);
111
112      posX += tmpGlyph->advance * this->size();
113    }
114  }
115  glEnd();
116  glPopAttrib();
117  glPopMatrix();
118}
119
120
121/**
122 * @brief setting up the Text-Width if DYNAMIC
123 */
124void MultiLineText::setupTextWidth()
125{
126  this->lineEnds.clear();
127  float width = 0.0f;
128  float maxWidth = this->lineWidth / this->size();
129
130  for (unsigned int i = 0; i < this->text().size(); i++)
131  {
132    if (width > maxWidth || this->text()[i] == '\n')
133    {
134      if (likely(i > 0))
135      {
136        this->lineEnds.push_back( i -1 );
137        width = this->font().getGlyphArray()[this->text()[i-1]]->advance;
138      }
139      else
140        width = 0.0f;
141    }
142
143    // Advance the Text.
144    if(this->font().getGlyphArray()[this->text()[i]] != NULL)
145      width += this->font().getGlyphArray()[this->text()[i]]->advance;
146  }
147  this->lineCount = lineEnds.size() + 1;
148  this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->font().getMaxHeight()));
149}
150
151/**
152 * @brief print out some nice debug output
153 */
154void MultiLineText::debug() const
155{
156 printf("Debug %s::%s: %d lines\n", this->getClassCName(), this->getCName(), this->getLineCount());
157
158 std::string tmpText = this->text();
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.