Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the gui-branche back.
merged with command:
svn merge -r8520:HEAD https://svn.orxonox.net/orxonox/branches/gui
no conflicts

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