[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 | |
---|
[7450] | 18 | #include "multi_line_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 | */ |
---|
[7757] | 26 | MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize, float lineWidth) |
---|
[7450] | 27 | : Text(fontFile, textSize) |
---|
[5343] | 28 | { |
---|
[7450] | 29 | this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText"); |
---|
[1856] | 30 | |
---|
[7757] | 31 | this->lineSpacing = 1.0; |
---|
[7737] | 32 | this->lineCount = 0; |
---|
[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 | */ |
---|
[7450] | 40 | void MultiLineText::setLineWidth(float lineWidth) |
---|
[5343] | 41 | { |
---|
[7450] | 42 | this->lineWidth = lineWidth; |
---|
[7457] | 43 | this->setSizeX2D(lineWidth); |
---|
[7450] | 44 | this->setupTextWidth(); |
---|
[3365] | 45 | } |
---|
[1853] | 46 | |
---|
[7456] | 47 | |
---|
[7454] | 48 | /** |
---|
| 49 | * @param lineSpacing: the Spacing between the lines |
---|
| 50 | */ |
---|
| 51 | void MultiLineText::setLineSpacing(float lineSpacing) |
---|
| 52 | { |
---|
| 53 | this->lineSpacing = lineSpacing; |
---|
| 54 | this->setupTextWidth(); |
---|
| 55 | } |
---|
[1853] | 56 | |
---|
[7454] | 57 | |
---|
[3245] | 58 | /** |
---|
[7355] | 59 | * @brief draws the Text |
---|
[5343] | 60 | */ |
---|
[7450] | 61 | void MultiLineText::draw() const |
---|
[5343] | 62 | { |
---|
[8619] | 63 | if (unlikely(this->text().empty())) |
---|
[7448] | 64 | return; |
---|
[5343] | 65 | glPushMatrix(); |
---|
[7919] | 66 | glPushAttrib(GL_ENABLE_BIT); |
---|
[5343] | 67 | // transform for alignment. |
---|
[7450] | 68 | // TODO make the Stuff with the alignment |
---|
[5343] | 69 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
[5767] | 70 | glTranslatef(-this->getSizeX2D(), 0, 0); |
---|
[5343] | 71 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
[5767] | 72 | glTranslatef(-this->getSizeX2D()/2, 0, 0); |
---|
[5343] | 73 | |
---|
| 74 | // drawing this Text. |
---|
[8768] | 75 | this->font().select(); |
---|
[8059] | 76 | |
---|
[7450] | 77 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
[7448] | 78 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
| 79 | |
---|
| 80 | Glyph* tmpGlyph; |
---|
| 81 | float posX = 0.0f; |
---|
[7450] | 82 | float posY = 0.0f; |
---|
| 83 | unsigned int lineNumber = 0; |
---|
| 84 | |
---|
[7448] | 85 | glBegin(GL_QUADS); |
---|
[8619] | 86 | for (unsigned int i = 0; i < this->text().size(); ++i) |
---|
[5343] | 87 | { |
---|
[7757] | 88 | if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber])) |
---|
[5343] | 89 | { |
---|
[7451] | 90 | // go to the next Line. |
---|
| 91 | ++lineNumber; |
---|
[7758] | 92 | posX = 0.0f; |
---|
[8761] | 93 | posY += this->lineSpacing + this->size(); //this->font().getMaxHeight(); |
---|
[7450] | 94 | } |
---|
| 95 | |
---|
[8761] | 96 | if(likely((tmpGlyph = this->font().getGlyphArray()[this->text()[i]]) != NULL)) |
---|
[7450] | 97 | { |
---|
[7448] | 98 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
[8619] | 99 | glVertex2d(posX+tmpGlyph->maxX*this->size(), posY); |
---|
[5419] | 100 | |
---|
[7448] | 101 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
[8619] | 102 | glVertex2d(posX+tmpGlyph->maxX*this->size(), posY + this->size()); |
---|
[5419] | 103 | |
---|
[7448] | 104 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
[8619] | 105 | glVertex2d(posX+tmpGlyph->minX*this->size(), posY+ this->size()); |
---|
[5419] | 106 | |
---|
[7448] | 107 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
[8619] | 108 | glVertex2d(posX+tmpGlyph->minX*this->size(), posY); |
---|
[5419] | 109 | |
---|
[8619] | 110 | posX += tmpGlyph->advance * this->size(); |
---|
[5343] | 111 | } |
---|
| 112 | } |
---|
[7448] | 113 | glEnd(); |
---|
[7919] | 114 | glPopAttrib(); |
---|
[5343] | 115 | glPopMatrix(); |
---|
| 116 | } |
---|
| 117 | |
---|
[7754] | 118 | |
---|
[5343] | 119 | /** |
---|
[7450] | 120 | * @brief setting up the Text-Width if DYNAMIC |
---|
[5343] | 121 | */ |
---|
[7450] | 122 | void MultiLineText::setupTextWidth() |
---|
[5343] | 123 | { |
---|
[7450] | 124 | this->lineEnds.clear(); |
---|
| 125 | float width = 0.0f; |
---|
[8619] | 126 | float maxWidth = this->lineWidth / this->size(); |
---|
[5343] | 127 | |
---|
[8619] | 128 | for (unsigned int i = 0; i < this->text().size(); i++) |
---|
[5343] | 129 | { |
---|
[8619] | 130 | if (width > maxWidth || this->text()[i] == '\n') |
---|
[7450] | 131 | { |
---|
[7756] | 132 | if (likely(i > 0)) |
---|
| 133 | { |
---|
| 134 | this->lineEnds.push_back( i -1 ); |
---|
[8761] | 135 | width = this->font().getGlyphArray()[this->text()[i-1]]->advance; |
---|
[7756] | 136 | } |
---|
| 137 | else |
---|
| 138 | width = 0.0f; |
---|
[7450] | 139 | } |
---|
[7756] | 140 | |
---|
[7450] | 141 | // Advance the Text. |
---|
[8761] | 142 | if(this->font().getGlyphArray()[this->text()[i]] != NULL) |
---|
| 143 | width += this->font().getGlyphArray()[this->text()[i]]->advance; |
---|
[5343] | 144 | } |
---|
[7757] | 145 | this->lineCount = lineEnds.size() + 1; |
---|
[8761] | 146 | this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->font().getMaxHeight())); |
---|
[5343] | 147 | } |
---|
[7757] | 148 | |
---|
[7758] | 149 | /** |
---|
| 150 | * @brief print out some nice debug output |
---|
| 151 | */ |
---|
[7757] | 152 | void MultiLineText::debug() const |
---|
| 153 | { |
---|
| 154 | printf("Debug %s::%s: %d lines\n", this->getClassName(), this->getName(), this->getLineCount()); |
---|
| 155 | |
---|
[8619] | 156 | std::string tmpText = this->text(); |
---|
[7757] | 157 | std::vector<unsigned int> ends = this->lineEnds; |
---|
| 158 | ends.push_back(tmpText.size()); |
---|
| 159 | |
---|
| 160 | unsigned int prev = 0; |
---|
| 161 | for (unsigned int i = 0; i < ends.size(); i++) |
---|
| 162 | { |
---|
| 163 | printf("Line %d: %s\n", i, tmpText.substr(prev, ends[i] - prev).c_str()); |
---|
| 164 | prev = ends[i]; |
---|
| 165 | } |
---|
| 166 | } |
---|