[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 | { |
---|
[7450] | 63 | if (unlikely(this->getText().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. |
---|
| 75 | // setting the Blending effects |
---|
[8059] | 76 | glActiveTexture(GL_TEXTURE0); |
---|
| 77 | |
---|
[7450] | 78 | glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending()); |
---|
[5343] | 79 | glEnable(GL_BLEND); |
---|
| 80 | glEnable(GL_TEXTURE_2D); |
---|
| 81 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 82 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); |
---|
| 83 | |
---|
[7450] | 84 | glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture()); |
---|
| 85 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
[7448] | 86 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
| 87 | |
---|
| 88 | Glyph* tmpGlyph; |
---|
| 89 | float posX = 0.0f; |
---|
[7450] | 90 | float posY = 0.0f; |
---|
| 91 | unsigned int lineNumber = 0; |
---|
| 92 | |
---|
[7448] | 93 | glBegin(GL_QUADS); |
---|
[7451] | 94 | for (unsigned int i = 0; i < this->getText().size(); ++i) |
---|
[5343] | 95 | { |
---|
[7757] | 96 | if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber])) |
---|
[5343] | 97 | { |
---|
[7451] | 98 | // go to the next Line. |
---|
| 99 | ++lineNumber; |
---|
[7758] | 100 | posX = 0.0f; |
---|
[7738] | 101 | posY += this->lineSpacing + this->getSize(); //this->getFont()->getMaxHeight(); |
---|
[7450] | 102 | } |
---|
| 103 | |
---|
| 104 | if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL)) |
---|
| 105 | { |
---|
[7448] | 106 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
[7450] | 107 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY); |
---|
[5419] | 108 | |
---|
[7448] | 109 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
[7450] | 110 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize()); |
---|
[5419] | 111 | |
---|
[7448] | 112 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
[7450] | 113 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize()); |
---|
[5419] | 114 | |
---|
[7448] | 115 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
[7450] | 116 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY); |
---|
[5419] | 117 | |
---|
[7450] | 118 | posX += tmpGlyph->advance * this->getSize(); |
---|
[5343] | 119 | } |
---|
| 120 | } |
---|
[7448] | 121 | glEnd(); |
---|
[7919] | 122 | glPopAttrib(); |
---|
[5343] | 123 | glPopMatrix(); |
---|
| 124 | } |
---|
| 125 | |
---|
[7754] | 126 | |
---|
[5343] | 127 | /** |
---|
[7450] | 128 | * @brief setting up the Text-Width if DYNAMIC |
---|
[5343] | 129 | */ |
---|
[7450] | 130 | void MultiLineText::setupTextWidth() |
---|
[5343] | 131 | { |
---|
[7450] | 132 | this->lineEnds.clear(); |
---|
| 133 | float width = 0.0f; |
---|
| 134 | float maxWidth = this->lineWidth / this->getSize(); |
---|
[5343] | 135 | |
---|
[7450] | 136 | for (unsigned int i = 0; i < this->getText().size(); i++) |
---|
[5343] | 137 | { |
---|
[7450] | 138 | if (width > maxWidth || this->getText()[i] == '\n') |
---|
| 139 | { |
---|
[7756] | 140 | if (likely(i > 0)) |
---|
| 141 | { |
---|
| 142 | this->lineEnds.push_back( i -1 ); |
---|
| 143 | width = this->getFont()->getGlyphArray()[this->getText()[i-1]]->advance; |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | width = 0.0f; |
---|
[7450] | 147 | } |
---|
[7756] | 148 | |
---|
[7450] | 149 | // Advance the Text. |
---|
| 150 | if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL) |
---|
| 151 | width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance; |
---|
[5343] | 152 | } |
---|
[7757] | 153 | this->lineCount = lineEnds.size() + 1; |
---|
[7457] | 154 | this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->getFont()->getMaxHeight())); |
---|
[5343] | 155 | } |
---|
[7757] | 156 | |
---|
[7758] | 157 | /** |
---|
| 158 | * @brief print out some nice debug output |
---|
| 159 | */ |
---|
[7757] | 160 | void MultiLineText::debug() const |
---|
| 161 | { |
---|
| 162 | printf("Debug %s::%s: %d lines\n", this->getClassName(), this->getName(), this->getLineCount()); |
---|
| 163 | |
---|
| 164 | std::string tmpText = this->getText(); |
---|
| 165 | std::vector<unsigned int> ends = this->lineEnds; |
---|
| 166 | ends.push_back(tmpText.size()); |
---|
| 167 | |
---|
| 168 | unsigned int prev = 0; |
---|
| 169 | for (unsigned int i = 0; i < ends.size(); i++) |
---|
| 170 | { |
---|
| 171 | printf("Line %d: %s\n", i, tmpText.substr(prev, ends[i] - prev).c_str()); |
---|
| 172 | prev = ends[i]; |
---|
| 173 | } |
---|
| 174 | } |
---|