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