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