[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 | |
---|
[8538] | 18 | #include "limited_width_text.h" |
---|
[5343] | 19 | #include "font.h" |
---|
[1853] | 20 | |
---|
[5343] | 21 | /** |
---|
[7355] | 22 | * @brief creates a new Text Element |
---|
[5343] | 23 | * @param fontFile the Font to render this text in |
---|
| 24 | * @param type The renderType to display this font in |
---|
| 25 | */ |
---|
[8538] | 26 | LimitedWidthText::LimitedWidthText(const std::string& fontFile, unsigned int textSize, float lineWidth, DotsPosition dotsPosition) |
---|
| 27 | : Text(fontFile, textSize) |
---|
[5343] | 28 | { |
---|
[8538] | 29 | this->setClassID(CL_LIMITED_WIDTH_TEXT, "LimitedWidthText"); |
---|
[1856] | 30 | |
---|
[8538] | 31 | this->_dotsPosition = End; |
---|
[7754] | 32 | this->setLineWidth(lineWidth); |
---|
[5343] | 33 | } |
---|
| 34 | |
---|
[3245] | 35 | /** |
---|
[7450] | 36 | * @brief sets the maximum Line width |
---|
| 37 | * @param lineWidth the maximum lineWidth. |
---|
[5343] | 38 | */ |
---|
[8538] | 39 | void LimitedWidthText::setLineWidth(float lineWidth) |
---|
[5343] | 40 | { |
---|
[8538] | 41 | this->_lineWidth = lineWidth; |
---|
[7450] | 42 | this->setupTextWidth(); |
---|
[3365] | 43 | } |
---|
[1853] | 44 | |
---|
[8542] | 45 | /** |
---|
| 46 | * @brief sets the Dots Position |
---|
| 47 | * @param dotsPosition the Position of the Dots |
---|
| 48 | */ |
---|
| 49 | void LimitedWidthText::setDotsPosition(DotsPosition dotsPosition) |
---|
| 50 | { |
---|
| 51 | this->_dotsPosition = dotsPosition; |
---|
| 52 | this->setupTextWidth(); |
---|
| 53 | } |
---|
[7456] | 54 | |
---|
[8542] | 55 | |
---|
[7454] | 56 | /** |
---|
[7355] | 57 | * @brief draws the Text |
---|
[5343] | 58 | */ |
---|
[8538] | 59 | void LimitedWidthText::draw() const |
---|
[5343] | 60 | { |
---|
[8538] | 61 | if (unlikely(this->_dotedText.empty())) |
---|
[7448] | 62 | return; |
---|
[5343] | 63 | glPushMatrix(); |
---|
[7919] | 64 | glPushAttrib(GL_ENABLE_BIT); |
---|
[5343] | 65 | // transform for alignment. |
---|
| 66 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
[5767] | 67 | glTranslatef(-this->getSizeX2D(), 0, 0); |
---|
[5343] | 68 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
[5767] | 69 | glTranslatef(-this->getSizeX2D()/2, 0, 0); |
---|
[5343] | 70 | |
---|
| 71 | // drawing this Text. |
---|
[8761] | 72 | this->font().select(); |
---|
[5343] | 73 | |
---|
[8538] | 74 | glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); |
---|
[7448] | 75 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
| 76 | |
---|
| 77 | Glyph* tmpGlyph; |
---|
| 78 | float posX = 0.0f; |
---|
| 79 | glBegin(GL_QUADS); |
---|
[8538] | 80 | for (unsigned int i = 0; i < this->_dotedText.size(); i++) |
---|
[5343] | 81 | { |
---|
[8761] | 82 | if(likely((tmpGlyph = this->font().getGlyphArray()[this->_dotedText[i]]) != NULL)) |
---|
[5343] | 83 | { |
---|
[7448] | 84 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
[8539] | 85 | glVertex2d(posX+tmpGlyph->maxX*this->size(), 0); |
---|
[5419] | 86 | |
---|
[7448] | 87 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
[8539] | 88 | glVertex2d(posX+tmpGlyph->maxX*this->size(), this->size()); |
---|
[5419] | 89 | |
---|
[7448] | 90 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
[8539] | 91 | glVertex2d(posX+tmpGlyph->minX*this->size(), this->size()); |
---|
[5419] | 92 | |
---|
[7448] | 93 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
[8539] | 94 | glVertex2d(posX+tmpGlyph->minX*this->size(), 0); |
---|
[5419] | 95 | |
---|
[8539] | 96 | posX += tmpGlyph->advance * this->size(); |
---|
[5343] | 97 | } |
---|
| 98 | } |
---|
[7448] | 99 | glEnd(); |
---|
[7919] | 100 | glPopAttrib(); |
---|
[5343] | 101 | glPopMatrix(); |
---|
| 102 | } |
---|
| 103 | |
---|
[7754] | 104 | |
---|
[5343] | 105 | /** |
---|
[7450] | 106 | * @brief setting up the Text-Width if DYNAMIC |
---|
[5343] | 107 | */ |
---|
[8538] | 108 | void LimitedWidthText::setupTextWidth() |
---|
[5343] | 109 | { |
---|
[8761] | 110 | float dotsSize = this->font().getGlyphArray()[46]->advance * 3.0; |
---|
[8538] | 111 | |
---|
[7450] | 112 | float width = 0.0f; |
---|
[8539] | 113 | float maxWidth = this->_lineWidth / this->size(); |
---|
[5343] | 114 | |
---|
[8542] | 115 | this->_dotedText = this->text(); |
---|
| 116 | |
---|
[8538] | 117 | switch (this->_dotsPosition) |
---|
[5343] | 118 | { |
---|
[8538] | 119 | case End: |
---|
[8539] | 120 | for (unsigned int i = 0; i < this->text().size(); i++) |
---|
[7756] | 121 | { |
---|
[8538] | 122 | if (width + dotsSize > maxWidth ) |
---|
| 123 | { |
---|
[8539] | 124 | this->_dotedText = this->text().substr(0, i) + "..."; |
---|
[8613] | 125 | if (i > 0) |
---|
[8761] | 126 | width -= this->font().getGlyphArray()[this->text()[i-1]]->advance; |
---|
[8538] | 127 | width += dotsSize; |
---|
| 128 | break; |
---|
| 129 | } |
---|
| 130 | // Advance the Text. |
---|
[8761] | 131 | if(this->font().getGlyphArray()[this->text()[i]] != NULL) |
---|
| 132 | width += this->font().getGlyphArray()[this->text()[i]]->advance; |
---|
[7756] | 133 | } |
---|
[8538] | 134 | break; |
---|
[8543] | 135 | |
---|
[8538] | 136 | case Begin: |
---|
[8543] | 137 | int i = text().size() -1; |
---|
| 138 | for (; i >= 0; --i) |
---|
[8538] | 139 | { |
---|
| 140 | if (width + dotsSize > maxWidth ) |
---|
| 141 | { |
---|
[8539] | 142 | this->_dotedText = std::string("...") + this->text().substr(i); |
---|
[8613] | 143 | if (i + 1 < (int)text().size() ) |
---|
[8761] | 144 | width -= this->font().getGlyphArray()[this->text()[i+1]]->advance; |
---|
[8538] | 145 | width += dotsSize; |
---|
| 146 | break; |
---|
| 147 | } |
---|
| 148 | // Advance the Text. |
---|
[8761] | 149 | if(this->font().getGlyphArray()[this->text()[i]] != NULL) |
---|
| 150 | width += this->font().getGlyphArray()[this->text()[i]]->advance; |
---|
[8538] | 151 | } |
---|
| 152 | break; |
---|
[5343] | 153 | } |
---|
[8542] | 154 | |
---|
[8543] | 155 | printf("%f %s\n", width, _dotedText.c_str()); |
---|
[8542] | 156 | this->setSizeX2D(width * this->size()); |
---|
[5343] | 157 | } |
---|
[7757] | 158 | |
---|
[7758] | 159 | /** |
---|
| 160 | * @brief print out some nice debug output |
---|
| 161 | */ |
---|
[8538] | 162 | void LimitedWidthText::debug() const |
---|
[7757] | 163 | { |
---|
[8538] | 164 | printf("Debug %s::%s \n", this->getClassName(), this->getName() ); |
---|
[7757] | 165 | } |
---|