Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10293 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
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"
[9869]19#include "resource_font.h"
[5343]20#include "font.h"
21#include "debug.h"
22
[9869]23ObjectListDefinition(Text);
24
[5343]25/**
[7355]26 * @brief creates a new Text Element
[5343]27 * @param fontFile the Font to render this text in
28 * @param type The renderType to display this font in
29 */
[7221]30Text::Text(const std::string& fontFile, unsigned int textSize)
[9869]31// : _font(fontFile, FONT_DEFAULT_RENDER_SIZE)
[5343]32{
[9869]33  this->registerObject(this, Text::_objectList);
[1856]34
[7355]35  // initialize this Text
[9869]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);
[8619]40  this->_size = textSize;
41  this->setSizeY2D(textSize);
[8761]42  this->setColor(TEXT_DEFAULT_COLOR);
[7455]43
[7753]44  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
45}
[7455]46
[7753]47Text::Text(const Text& text)
[8761]48    : _font()
[7753]49{
[9869]50  this->registerObject(this, Text::_objectList);
[7753]51
52  *this = text;
[5343]53}
54
[7753]55
[3245]56/**
[7355]57 * @brief deletes a Text out of memory
[5343]58 */
59Text::~Text()
60{
[8761]61  /*  if (this->_font != NULL && this->_font != Font::getDefaultFont())
62      ResourceManager::getInstance()->unload(this->_font);*/
[3365]63}
[1853]64
[7753]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{
[8619]72  return (this->_text == text._text &&
73          this->_size == text._size &&
[8761]74          this->_font == text._font );
[7753]75}
[1853]76
[3245]77/**
[7753]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{
[8619]84  return (this->_text == text);
[7753]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{
[8619]94  this->_size = text._size;
[7753]95  this->setAlignment(text.getAlignment());
96
[8761]97  this->_font = text._font;
[7753]98
[8619]99  this->_text = text._text;
[7753]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{
[8619]109  this->_text = text;
[7753]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{
[8619]119  this->_text += appendText;
[7753]120  this->setupTextWidth();
121}
122
123/**
[7919]124 * @brief appends one Character to the String.
125 */
126void Text::appendCharacter(char character)
127{
[8619]128  this->_text += character;
[7919]129  this->setupTextWidth();
130}
131
132
133/**
[7753]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);
[8619]140  return this->_text;
[7753]141}
142
143/**
[7919]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{
[8619]151  if (this->_text.size() > chars)
152    this->_text.resize(this->_text.size()-chars);
153  else if (!this->_text.empty())
154    this->_text.clear();
[7919]155  this->setupTextWidth();
156}
157
158
159/**
[8518]160 * @brief clears the Text Line (empies it).
161 */
162void Text::clear()
163{
[8619]164  this->_text.clear();
[8518]165  this->setupTextWidth();
166}
167
168/**
[7355]169 * @brief sets the Font of this Text to font from fontFile
[5343]170 * @param fontFile the File to load the Font from.
171 * @param fontSize the Size of the Font
172 */
[7221]173void Text::setFont(const std::string& fontFile, unsigned int fontSize)
[5343]174{
[9869]175  this->_font = ResourceFont(fontFile, fontSize);
[5343]176
[7450]177  this->setupTextWidth();
[5343]178}
179
180/**
[8764]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;
[8769]187  this->setupTextWidth();
[8764]188}
189
190/**
[7453]191 * @brief sets the Size of the Font
192 * @param size :the size of the Text
193 */
194void Text::setSize(float size)
195{
[8619]196  this->_size = size;
[7453]197  this->setSizeY2D(size);
198  this->setupTextWidth();
199}
200
201
202/**
[7355]203 * @brief draws the Text
[5343]204 */
205void Text::draw() const
206{
[8619]207  if (unlikely(this->_text.empty()))
[7448]208    return;
[5343]209  glPushMatrix();
[7919]210  glPushAttrib(GL_ENABLE_BIT);
[5343]211  // transform for alignment.
212  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
[5767]213    glTranslatef(-this->getSizeX2D(), 0, 0);
[5343]214  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
[5767]215    glTranslatef(-this->getSizeX2D()/2, 0, 0);
[5343]216
[8768]217
[8761]218  this->font().select();
[7448]219  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
220  glRotatef(this->getAbsDir2D(), 0, 0, 1);
221
[9869]222  const Font::Glyph* tmpGlyph;
[7448]223  float posX = 0.0f;
224  glBegin(GL_QUADS);
[8619]225  for (unsigned int i = 0; i < this->_text.size(); i++)
[5343]226  {
[8761]227    if(likely((tmpGlyph = this->font().getGlyphArray()[this->_text[i]]) != NULL))
[5343]228    {
[7448]229      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
[8619]230      glVertex2d(posX+tmpGlyph->maxX*this->size(), 0);
[5419]231
[7448]232      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
[8619]233      glVertex2d(posX+tmpGlyph->maxX*this->size(), this->size());
[5419]234
[7448]235      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
[8619]236      glVertex2d(posX+tmpGlyph->minX*this->size(), this->size());
[5419]237
[7448]238      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
[8619]239      glVertex2d(posX+tmpGlyph->minX*this->size(), 0);
[5419]240
[8619]241      posX += tmpGlyph->advance * this->size();
[5343]242    }
243  }
[7448]244  glEnd();
[7919]245  glPopAttrib();
[5343]246  glPopMatrix();
247}
248
[7450]249
[5343]250/**
[7450]251 * @brief setting up the Text-Width.
[5343]252 */
[7450]253void Text::setupTextWidth()
[5343]254{
[7450]255  float width = 0;
[8619]256  for (unsigned int i = 0; i < this->_text.size(); i++)
[8761]257    if(this->_font.getGlyphArray()[this->_text[i]] != NULL)
258      width += this->_font.getGlyphArray()[this->_text[i]]->advance;
[8619]259  this->setSizeX2D(width * this->size());
[5343]260}
261
262
263/**
[7450]264 * @brief prints out some nice debug information about this text
[5343]265 */
[7450]266void Text::debug() const
[5343]267{
[9406]268  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getCName(), this->_font.getCName(), this->_text.c_str());
[8761]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());
[5343]270}
271
Note: See TracBrowser for help on using the repository browser.