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