Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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