[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 | |
---|
[1856] | 21 | using namespace std; |
---|
[1853] | 22 | |
---|
[5343] | 23 | /** |
---|
[7355] | 24 | * @brief creates a new Text Element |
---|
[5343] | 25 | * @param fontFile the Font to render this text in |
---|
| 26 | * @param type The renderType to display this font in |
---|
| 27 | */ |
---|
[7450] | 28 | MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize) |
---|
| 29 | : Text(fontFile, textSize) |
---|
[5343] | 30 | { |
---|
[7450] | 31 | this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText"); |
---|
[1856] | 32 | |
---|
[7450] | 33 | this->lineSpacing = 1.0f; |
---|
| 34 | this->lineWidth = 100.0f; |
---|
| 35 | this->setupTextWidth(); |
---|
[5343] | 36 | } |
---|
| 37 | |
---|
[3245] | 38 | /** |
---|
[7450] | 39 | * @brief sets the maximum Line width |
---|
| 40 | * @param lineWidth the maximum lineWidth. |
---|
[5343] | 41 | */ |
---|
[7450] | 42 | void MultiLineText::setLineWidth(float lineWidth) |
---|
[5343] | 43 | { |
---|
[7450] | 44 | this->lineWidth = lineWidth; |
---|
| 45 | this->setupTextWidth(); |
---|
[3365] | 46 | } |
---|
[1853] | 47 | |
---|
| 48 | |
---|
[3245] | 49 | /** |
---|
[7355] | 50 | * @brief draws the Text |
---|
[5343] | 51 | */ |
---|
[7450] | 52 | void MultiLineText::draw() const |
---|
[5343] | 53 | { |
---|
[7450] | 54 | if (unlikely(this->getText().empty())) |
---|
[7448] | 55 | return; |
---|
[5343] | 56 | glPushMatrix(); |
---|
| 57 | // transform for alignment. |
---|
[7450] | 58 | // TODO make the Stuff with the alignment |
---|
[5343] | 59 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
[5767] | 60 | glTranslatef(-this->getSizeX2D(), 0, 0); |
---|
[5343] | 61 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
[5767] | 62 | glTranslatef(-this->getSizeX2D()/2, 0, 0); |
---|
[5343] | 63 | |
---|
| 64 | // drawing this Text. |
---|
| 65 | // setting the Blending effects |
---|
[7450] | 66 | glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending()); |
---|
[5343] | 67 | glEnable(GL_BLEND); |
---|
| 68 | glEnable(GL_TEXTURE_2D); |
---|
| 69 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 70 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); |
---|
| 71 | |
---|
[7450] | 72 | glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture()); |
---|
| 73 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
[7448] | 74 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
| 75 | |
---|
| 76 | Glyph* tmpGlyph; |
---|
| 77 | float posX = 0.0f; |
---|
[7450] | 78 | float posY = 0.0f; |
---|
| 79 | unsigned int lineNumber = 0; |
---|
| 80 | |
---|
[7448] | 81 | glBegin(GL_QUADS); |
---|
[7450] | 82 | for (unsigned int i = 0; i < this->getText().size(); i++) |
---|
[5343] | 83 | { |
---|
[7450] | 84 | if (unlikely(tmpGlyph->character == '\n' || i == this->lineEnds[lineNumber])) |
---|
[5343] | 85 | { |
---|
[7450] | 86 | // go to the next Line. |
---|
| 87 | lineNumber++; |
---|
| 88 | posX = 0.0f; |
---|
| 89 | posY += this->lineSpacing + this->getFont()->getMaxHeight(); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL)) |
---|
| 93 | { |
---|
[7448] | 94 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
[7450] | 95 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY); |
---|
[5419] | 96 | |
---|
[7448] | 97 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
[7450] | 98 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize()); |
---|
[5419] | 99 | |
---|
[7448] | 100 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
[7450] | 101 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize()); |
---|
[5419] | 102 | |
---|
[7448] | 103 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
[7450] | 104 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY); |
---|
[5419] | 105 | |
---|
[7450] | 106 | posX += tmpGlyph->advance * this->getSize(); |
---|
[5343] | 107 | } |
---|
| 108 | } |
---|
[7448] | 109 | glEnd(); |
---|
[5343] | 110 | glPopMatrix(); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
[7450] | 114 | * @brief setting up the Text-Width if DYNAMIC |
---|
[5343] | 115 | */ |
---|
[7450] | 116 | void MultiLineText::setupTextWidth() |
---|
[5343] | 117 | { |
---|
[7450] | 118 | this->lineEnds.clear(); |
---|
| 119 | float width = 0.0f; |
---|
| 120 | // TODO make size local to this (not using the one from Element2D. |
---|
| 121 | float maxWidth = this->lineWidth / this->getSize(); |
---|
[5343] | 122 | |
---|
[7450] | 123 | for (unsigned int i = 0; i < this->getText().size(); i++) |
---|
[5343] | 124 | { |
---|
[7450] | 125 | if (width > maxWidth || this->getText()[i] == '\n') |
---|
| 126 | { |
---|
| 127 | this->lineEnds.push_back(i); |
---|
| 128 | width = 0.0f; |
---|
| 129 | } |
---|
| 130 | // Advance the Text. |
---|
| 131 | if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL) |
---|
| 132 | width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance; |
---|
[5343] | 133 | } |
---|
| 134 | } |
---|