Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8433 was 8059, checked in by bensch, 18 years ago

multi-line-text-fix-hack

File size: 4.8 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
21/**
22 * @brief creates a new Text Element
23 * @param fontFile the Font to render this text in
24 * @param type The renderType to display this font in
25 */
26MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize, float lineWidth)
27  : Text(fontFile, textSize)
28{
29  this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText");
30
31  this->lineSpacing = 1.0;
32  this->lineCount = 0;
33  this->setLineWidth(lineWidth);
34}
35
36/**
37 * @brief sets the maximum Line width
38 * @param lineWidth the maximum lineWidth.
39 */
40void MultiLineText::setLineWidth(float lineWidth)
41{
42  this->lineWidth = lineWidth;
43  this->setSizeX2D(lineWidth);
44  this->setupTextWidth();
45}
46
47
48/**
49 * @param lineSpacing: the Spacing between the lines
50 */
51void MultiLineText::setLineSpacing(float lineSpacing)
52{
53   this->lineSpacing = lineSpacing;
54   this->setupTextWidth();
55}
56
57
58/**
59 * @brief draws the Text
60 */
61void MultiLineText::draw() const
62{
63  if (unlikely(this->getText().empty()))
64    return;
65  glPushMatrix();
66  glPushAttrib(GL_ENABLE_BIT);
67  // transform for alignment.
68  // TODO make the Stuff with the alignment
69  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
70    glTranslatef(-this->getSizeX2D(), 0, 0);
71  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
72    glTranslatef(-this->getSizeX2D()/2, 0, 0);
73
74  // drawing this Text.
75  // setting the Blending effects
76  glActiveTexture(GL_TEXTURE0);
77
78  glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending());
79  glEnable(GL_BLEND);
80  glEnable(GL_TEXTURE_2D);
81  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
82  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
83
84  glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture());
85  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
86  glRotatef(this->getAbsDir2D(), 0, 0, 1);
87
88  Glyph* tmpGlyph;
89  float posX = 0.0f;
90  float posY = 0.0f;
91  unsigned int lineNumber = 0;
92
93  glBegin(GL_QUADS);
94  for (unsigned int i = 0; i < this->getText().size(); ++i)
95  {
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))
105    {
106      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
107      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY);
108
109      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
110      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize());
111
112      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
113      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize());
114
115      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
116      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY);
117
118      posX += tmpGlyph->advance * this->getSize();
119    }
120  }
121  glEnd();
122  glPopAttrib();
123  glPopMatrix();
124}
125
126
127/**
128 * @brief setting up the Text-Width if DYNAMIC
129 */
130void MultiLineText::setupTextWidth()
131{
132  this->lineEnds.clear();
133  float width = 0.0f;
134  float maxWidth = this->lineWidth / this->getSize();
135
136  for (unsigned int i = 0; i < this->getText().size(); i++)
137  {
138    if (width > maxWidth || this->getText()[i] == '\n')
139    {
140      if (likely(i > 0))
141      {
142        this->lineEnds.push_back( i -1 );
143        width = this->getFont()->getGlyphArray()[this->getText()[i-1]]->advance;
144      }
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;
152  }
153  this->lineCount = lineEnds.size() + 1;
154  this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->getFont()->getMaxHeight()));
155}
156
157/**
158 * @brief print out some nice debug output
159 */
160void MultiLineText::debug() const
161{
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  }
174}
Note: See TracBrowser for help on using the repository browser.