/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS #include "multi_line_text.h" #include "font.h" using namespace std; /** * @brief creates a new Text Element * @param fontFile the Font to render this text in * @param type The renderType to display this font in */ MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize) : Text(fontFile, textSize) { this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText"); this->lineSpacing = 1.0f; this->lineWidth = 100.0f; this->setupTextWidth(); } /** * @brief sets the maximum Line width * @param lineWidth the maximum lineWidth. */ void MultiLineText::setLineWidth(float lineWidth) { this->lineWidth = lineWidth; this->setupTextWidth(); } /** * @brief draws the Text */ void MultiLineText::draw() const { if (unlikely(this->getText().empty())) return; glPushMatrix(); // transform for alignment. // TODO make the Stuff with the alignment if (this->getAlignment() == TEXT_ALIGN_RIGHT) glTranslatef(-this->getSizeX2D(), 0, 0); else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) glTranslatef(-this->getSizeX2D()/2, 0, 0); // drawing this Text. // setting the Blending effects glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending()); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture()); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); glRotatef(this->getAbsDir2D(), 0, 0, 1); Glyph* tmpGlyph; float posX = 0.0f; float posY = 0.0f; unsigned int lineNumber = 0; glBegin(GL_QUADS); for (unsigned int i = 0; i < this->getText().size(); ++i) { if (unlikely(this->getText()[i] == '\n' || i == this->lineEnds[lineNumber])) { // go to the next Line. ++lineNumber; posX = 0.0f; posY += this->lineSpacing + this->getFont()->getMaxHeight(); } if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL)) { glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY); glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize()); glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize()); glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY); posX += tmpGlyph->advance * this->getSize(); } } glEnd(); glPopMatrix(); } /** * @brief setting up the Text-Width if DYNAMIC */ void MultiLineText::setupTextWidth() { this->lineEnds.clear(); this->lineEnds.push_back(0); float width = 0.0f; // TODO make size local to this (not using the one from Element2D. float maxWidth = this->lineWidth / this->getSize(); for (unsigned int i = 0; i < this->getText().size(); i++) { if (width > maxWidth || this->getText()[i] == '\n') { this->lineEnds.push_back(i); width = 0.0f; } // Advance the Text. if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL) width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance; } }