Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/text_engine/text.cc @ 9202

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

orxonox/trunk: Font in Button-Widget used

File size: 6.5 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 "text.h"
19#include "font.h"
20
21#include "util/loading/resource_manager.h"
22#include "debug.h"
23
24/**
25 * @brief creates a new Text Element
26 * @param fontFile the Font to render this text in
27 * @param type The renderType to display this font in
28 */
29Text::Text(const std::string& fontFile, unsigned int textSize)
30    : _font(fontFile, FONT_DEFAULT_RENDER_SIZE)
31{
32  this->setClassID(CL_TEXT, "Text");
33
34  // initialize this Text
35  this->_size = textSize;
36  this->setSizeY2D(textSize);
37  this->setColor(TEXT_DEFAULT_COLOR);
38
39  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
40}
41
42Text::Text(const Text& text)
43    : _font()
44{
45  this->setClassID(CL_TEXT, "Text");
46
47  *this = text;
48}
49
50
51/**
52 * @brief deletes a Text out of memory
53 */
54Text::~Text()
55{
56  /*  if (this->_font != NULL && this->_font != Font::getDefaultFont())
57      ResourceManager::getInstance()->unload(this->_font);*/
58}
59
60/**
61 * @brief compare the Text with another Text.
62 * @param text the Text to compare.
63 * @returns true if all the properties Match.
64 */
65bool Text::operator==(const Text& text) const
66{
67  return (this->_text == text._text &&
68          this->_size == text._size &&
69          this->_font == text._font );
70}
71
72/**
73 * @brief compare this Text's internal String with the text.
74 * @param text the Comparator Text.
75 * @returns true on a match.
76 */
77bool Text::operator==(const std::string& text) const
78{
79  return (this->_text == text);
80}
81
82/**
83 * @brief Copies the properties of one text onto the other one.
84 * @param text: the Text to apply to this one.
85 * @returns This-reference.
86 */
87Text& Text::operator=(const Text& text)
88{
89  this->_size = text._size;
90  this->setAlignment(text.getAlignment());
91
92  this->_font = text._font;
93
94  this->_text = text._text;
95  return *this;
96}
97
98/**
99 * @brief Sets a new Text to the font
100 * @param text the new text to set
101 */
102void Text::setText(const std::string& text)
103{
104  this->_text = text;
105  this->setupTextWidth();
106}
107
108/**
109 * @brief append some text to the already existing Text.
110 * @param appendText The text to append to this Text.
111 */
112void Text::append(const std::string& appendText)
113{
114  this->_text += appendText;
115  this->setupTextWidth();
116}
117
118/**
119 * @brief appends one Character to the String.
120 */
121void Text::appendCharacter(char character)
122{
123  this->_text += character;
124  this->setupTextWidth();
125}
126
127
128/**
129 * @brief append some text to the already existing Text.
130 * @param appendText The text to append to this Text.
131 */
132const std::string& Text::operator <<(const std::string& appendText)
133{
134  this->append(appendText);
135  return this->_text;
136}
137
138/**
139 * @brief removes char characters from the Text.
140 *
141 * @note this function checks, if the count can be removed, and if so does it.
142 * Otherwise the maximum count of characters will be removed.
143 */
144void Text::removeCharacters(unsigned int chars)
145{
146  if (this->_text.size() > chars)
147    this->_text.resize(this->_text.size()-chars);
148  else if (!this->_text.empty())
149    this->_text.clear();
150  this->setupTextWidth();
151}
152
153
154/**
155 * @brief clears the Text Line (empies it).
156 */
157void Text::clear()
158{
159  this->_text.clear();
160  this->setupTextWidth();
161}
162
163/**
164 * @brief sets the Font of this Text to font from fontFile
165 * @param fontFile the File to load the Font from.
166 * @param fontSize the Size of the Font
167 */
168void Text::setFont(const std::string& fontFile, unsigned int fontSize)
169{
170  Font* newFont = NULL;
171  //  Font* oldFont = this->_font;
172
173  // load a new Font
174  if (!fontFile.empty())
175  {
176    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
177    if (newFont == NULL)
178    {
179      //      newFont = &Font::();
180      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
181    }
182  }
183
184  if (newFont == NULL)
185    this->_font = Font();
186  else
187    this->_font = *newFont;
188
189  this->setupTextWidth();
190}
191
192/**
193 * @brief set a new Font to this Text.
194 * @param font the Font to set.
195 */
196void Text::setFont(const Font& font)
197{
198  this->_font = font;
199  this->setupTextWidth();
200}
201
202/**
203 * @brief sets the Size of the Font
204 * @param size :the size of the Text
205 */
206void Text::setSize(float size)
207{
208  this->_size = size;
209  this->setSizeY2D(size);
210  this->setupTextWidth();
211}
212
213
214/**
215 * @brief draws the Text
216 */
217void Text::draw() const
218{
219  if (unlikely(this->_text.empty()))
220    return;
221  glPushMatrix();
222  glPushAttrib(GL_ENABLE_BIT);
223  // transform for alignment.
224  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
225    glTranslatef(-this->getSizeX2D(), 0, 0);
226  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
227    glTranslatef(-this->getSizeX2D()/2, 0, 0);
228
229
230  this->font().select();
231  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
232  glRotatef(this->getAbsDir2D(), 0, 0, 1);
233
234  Glyph* tmpGlyph;
235  float posX = 0.0f;
236  glBegin(GL_QUADS);
237  for (unsigned int i = 0; i < this->_text.size(); i++)
238  {
239    if(likely((tmpGlyph = this->font().getGlyphArray()[this->_text[i]]) != NULL))
240    {
241      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
242      glVertex2d(posX+tmpGlyph->maxX*this->size(), 0);
243
244      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
245      glVertex2d(posX+tmpGlyph->maxX*this->size(), this->size());
246
247      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
248      glVertex2d(posX+tmpGlyph->minX*this->size(), this->size());
249
250      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
251      glVertex2d(posX+tmpGlyph->minX*this->size(), 0);
252
253      posX += tmpGlyph->advance * this->size();
254    }
255  }
256  glEnd();
257  glPopAttrib();
258  glPopMatrix();
259}
260
261
262/**
263 * @brief setting up the Text-Width.
264 */
265void Text::setupTextWidth()
266{
267  float width = 0;
268  for (unsigned int i = 0; i < this->_text.size(); i++)
269    if(this->_font.getGlyphArray()[this->_text[i]] != NULL)
270      width += this->_font.getGlyphArray()[this->_text[i]]->advance;
271  this->setSizeX2D(width * this->size());
272}
273
274
275/**
276 * @brief prints out some nice debug information about this text
277 */
278void Text::debug() const
279{
280  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->_font.getName(), this->_text.c_str());
281  //  PRINT(0)("Color: r=%0.2f g=%0.2f b=%0.2f a=%0.2f\n", this->_color.r(), this->_color.g(), this->_color.b(), this->_color.a());
282}
283
Note: See TracBrowser for help on using the repository browser.