Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Font in Button-Widget used

File size: 6.5 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)
[8761]30    : _font(fontFile, FONT_DEFAULT_RENDER_SIZE)
[5343]31{
[7355]32  this->setClassID(CL_TEXT, "Text");
[1856]33
[7355]34  // initialize this Text
[8619]35  this->_size = textSize;
36  this->setSizeY2D(textSize);
[8761]37  this->setColor(TEXT_DEFAULT_COLOR);
[7455]38
[7753]39  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
40}
[7455]41
[7753]42Text::Text(const Text& text)
[8761]43    : _font()
[7753]44{
45  this->setClassID(CL_TEXT, "Text");
46
47  *this = text;
[5343]48}
49
[7753]50
[3245]51/**
[7355]52 * @brief deletes a Text out of memory
[5343]53 */
54Text::~Text()
55{
[8761]56  /*  if (this->_font != NULL && this->_font != Font::getDefaultFont())
57      ResourceManager::getInstance()->unload(this->_font);*/
[3365]58}
[1853]59
[7753]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{
[8619]67  return (this->_text == text._text &&
68          this->_size == text._size &&
[8761]69          this->_font == text._font );
[7753]70}
[1853]71
[3245]72/**
[7753]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{
[8619]79  return (this->_text == text);
[7753]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{
[8619]89  this->_size = text._size;
[7753]90  this->setAlignment(text.getAlignment());
91
[8761]92  this->_font = text._font;
[7753]93
[8619]94  this->_text = text._text;
[7753]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{
[8619]104  this->_text = text;
[7753]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{
[8619]114  this->_text += appendText;
[7753]115  this->setupTextWidth();
116}
117
118/**
[7919]119 * @brief appends one Character to the String.
120 */
121void Text::appendCharacter(char character)
122{
[8619]123  this->_text += character;
[7919]124  this->setupTextWidth();
125}
126
127
128/**
[7753]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);
[8619]135  return this->_text;
[7753]136}
137
138/**
[7919]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{
[8619]146  if (this->_text.size() > chars)
147    this->_text.resize(this->_text.size()-chars);
148  else if (!this->_text.empty())
149    this->_text.clear();
[7919]150  this->setupTextWidth();
151}
152
153
154/**
[8518]155 * @brief clears the Text Line (empies it).
156 */
157void Text::clear()
158{
[8619]159  this->_text.clear();
[8518]160  this->setupTextWidth();
161}
162
163/**
[7355]164 * @brief sets the Font of this Text to font from fontFile
[5343]165 * @param fontFile the File to load the Font from.
166 * @param fontSize the Size of the Font
167 */
[7221]168void Text::setFont(const std::string& fontFile, unsigned int fontSize)
[5343]169{
[7455]170  Font* newFont = NULL;
[8761]171  //  Font* oldFont = this->_font;
[5343]172
[5345]173  // load a new Font
[7221]174  if (!fontFile.empty())
[5344]175  {
[7426]176    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
177    if (newFont == NULL)
178    {
[8761]179      //      newFont = &Font::();
[7221]180      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
[7426]181    }
[5343]182  }
[8761]183
[7455]184  if (newFont == NULL)
[8761]185    this->_font = Font();
186  else
187    this->_font = *newFont;
[7426]188
[7450]189  this->setupTextWidth();
[5343]190}
191
192/**
[8764]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;
[8769]199  this->setupTextWidth();
[8764]200}
201
202/**
[7453]203 * @brief sets the Size of the Font
204 * @param size :the size of the Text
205 */
206void Text::setSize(float size)
207{
[8619]208  this->_size = size;
[7453]209  this->setSizeY2D(size);
210  this->setupTextWidth();
211}
212
213
214/**
[7355]215 * @brief draws the Text
[5343]216 */
217void Text::draw() const
218{
[8619]219  if (unlikely(this->_text.empty()))
[7448]220    return;
[5343]221  glPushMatrix();
[7919]222  glPushAttrib(GL_ENABLE_BIT);
[5343]223  // transform for alignment.
224  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
[5767]225    glTranslatef(-this->getSizeX2D(), 0, 0);
[5343]226  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
[5767]227    glTranslatef(-this->getSizeX2D()/2, 0, 0);
[5343]228
[8768]229
[8761]230  this->font().select();
[7448]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);
[8619]237  for (unsigned int i = 0; i < this->_text.size(); i++)
[5343]238  {
[8761]239    if(likely((tmpGlyph = this->font().getGlyphArray()[this->_text[i]]) != NULL))
[5343]240    {
[7448]241      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
[8619]242      glVertex2d(posX+tmpGlyph->maxX*this->size(), 0);
[5419]243
[7448]244      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
[8619]245      glVertex2d(posX+tmpGlyph->maxX*this->size(), this->size());
[5419]246
[7448]247      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
[8619]248      glVertex2d(posX+tmpGlyph->minX*this->size(), this->size());
[5419]249
[7448]250      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
[8619]251      glVertex2d(posX+tmpGlyph->minX*this->size(), 0);
[5419]252
[8619]253      posX += tmpGlyph->advance * this->size();
[5343]254    }
255  }
[7448]256  glEnd();
[7919]257  glPopAttrib();
[5343]258  glPopMatrix();
259}
260
[7450]261
[5343]262/**
[7450]263 * @brief setting up the Text-Width.
[5343]264 */
[7450]265void Text::setupTextWidth()
[5343]266{
[7450]267  float width = 0;
[8619]268  for (unsigned int i = 0; i < this->_text.size(); i++)
[8761]269    if(this->_font.getGlyphArray()[this->_text[i]] != NULL)
270      width += this->_font.getGlyphArray()[this->_text[i]]->advance;
[8619]271  this->setSizeX2D(width * this->size());
[5343]272}
273
274
275/**
[7450]276 * @brief prints out some nice debug information about this text
[5343]277 */
[7450]278void Text::debug() const
[5343]279{
[8761]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());
[5343]282}
283
Note: See TracBrowser for help on using the repository browser.