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