Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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