Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: small properties

File size: 4.1 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
21using namespace std;
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, unsigned int lineWidth)
29  : Text(fontFile, textSize)
30{
31  this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText");
32
33  this->lineSpacing = 1.0f;
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->getText().empty()))
66    return;
67  glPushMatrix();
68  // transform for alignment.
69  // TODO make the Stuff with the alignment
70  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
71    glTranslatef(-this->getSizeX2D(), 0, 0);
72  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
73    glTranslatef(-this->getSizeX2D()/2, 0, 0);
74
75  // drawing this Text.
76  // setting the Blending effects
77  glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending());
78  glEnable(GL_BLEND);
79  glEnable(GL_TEXTURE_2D);
80  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
81  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
82
83  glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture());
84  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
85  glRotatef(this->getAbsDir2D(), 0, 0, 1);
86
87  Glyph* tmpGlyph;
88  float posX = 0.0f;
89  float posY = 0.0f;
90  unsigned int lineNumber = 0;
91
92  glBegin(GL_QUADS);
93  for (unsigned int i = 0; i < this->getText().size(); ++i)
94  {
95    if (unlikely(!this->lineEnds.empty() && i == this->lineEnds[lineNumber]))
96    {
97      // go to the next Line.
98      ++lineNumber;
99      posX = 0.0f;
100      posY += this->lineSpacing + this->getSize(); //this->getFont()->getMaxHeight();
101    }
102
103    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL))
104    {
105      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
106      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY);
107
108      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
109      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize());
110
111      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
112      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize());
113
114      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
115      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY);
116
117      posX += tmpGlyph->advance * this->getSize();
118    }
119  }
120  glEnd();
121  glPopMatrix();
122}
123
124
125/**
126 * @brief setting up the Text-Width if DYNAMIC
127 */
128void MultiLineText::setupTextWidth()
129{
130  this->lineEnds.clear();
131  float width = 0.0f;
132  float maxWidth = this->lineWidth / this->getSize();
133
134  for (unsigned int i = 0; i < this->getText().size(); i++)
135  {
136    if (width > maxWidth || this->getText()[i] == '\n')
137    {
138      this->lineEnds.push_back(i);
139      width = 0.0f;
140    }
141    // Advance the Text.
142    if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL)
143      width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance;
144  }
145
146  this->lineCount = lineEnds.size()+1;
147  this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->getFont()->getMaxHeight()));
148}
Note: See TracBrowser for help on using the repository browser.