1 | /* |
---|
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. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
17 | |
---|
18 | #include "multi_line_text.h" |
---|
19 | #include "font.h" |
---|
20 | |
---|
21 | /** |
---|
22 | * @brief creates a new Text Element |
---|
23 | * @param fontFile the Font to render this text in |
---|
24 | * @param type The renderType to display this font in |
---|
25 | */ |
---|
26 | MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize, float lineWidth) |
---|
27 | : Text(fontFile, textSize) |
---|
28 | { |
---|
29 | this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText"); |
---|
30 | |
---|
31 | this->lineSpacing = 1.0; |
---|
32 | this->lineCount = 0; |
---|
33 | this->setLineWidth(lineWidth); |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * @brief sets the maximum Line width |
---|
38 | * @param lineWidth the maximum lineWidth. |
---|
39 | */ |
---|
40 | void MultiLineText::setLineWidth(float lineWidth) |
---|
41 | { |
---|
42 | this->lineWidth = lineWidth; |
---|
43 | this->setSizeX2D(lineWidth); |
---|
44 | this->setupTextWidth(); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
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 | } |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | * @brief draws the Text |
---|
60 | */ |
---|
61 | void MultiLineText::draw() const |
---|
62 | { |
---|
63 | if (unlikely(this->getText().empty())) |
---|
64 | return; |
---|
65 | glPushMatrix(); |
---|
66 | glPushAttrib(GL_ENABLE_BIT); |
---|
67 | // transform for alignment. |
---|
68 | // TODO make the Stuff with the alignment |
---|
69 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
70 | glTranslatef(-this->getSizeX2D(), 0, 0); |
---|
71 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
72 | glTranslatef(-this->getSizeX2D()/2, 0, 0); |
---|
73 | |
---|
74 | // drawing this Text. |
---|
75 | // setting the Blending effects |
---|
76 | glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending()); |
---|
77 | glEnable(GL_BLEND); |
---|
78 | glEnable(GL_TEXTURE_2D); |
---|
79 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
80 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); |
---|
81 | |
---|
82 | glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture()); |
---|
83 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
84 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
85 | |
---|
86 | Glyph* tmpGlyph; |
---|
87 | float posX = 0.0f; |
---|
88 | float posY = 0.0f; |
---|
89 | unsigned int lineNumber = 0; |
---|
90 | |
---|
91 | glBegin(GL_QUADS); |
---|
92 | for (unsigned int i = 0; i < this->getText().size(); ++i) |
---|
93 | { |
---|
94 | if (unlikely(this->lineEnds.size() > lineNumber && i == this->lineEnds[lineNumber])) |
---|
95 | { |
---|
96 | // go to the next Line. |
---|
97 | ++lineNumber; |
---|
98 | posX = 0.0f; |
---|
99 | posY += this->lineSpacing + this->getSize(); //this->getFont()->getMaxHeight(); |
---|
100 | } |
---|
101 | |
---|
102 | if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL)) |
---|
103 | { |
---|
104 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
105 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY); |
---|
106 | |
---|
107 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
108 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize()); |
---|
109 | |
---|
110 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
111 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize()); |
---|
112 | |
---|
113 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
114 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY); |
---|
115 | |
---|
116 | posX += tmpGlyph->advance * this->getSize(); |
---|
117 | } |
---|
118 | } |
---|
119 | glEnd(); |
---|
120 | glPopAttrib(); |
---|
121 | glPopMatrix(); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * @brief setting up the Text-Width if DYNAMIC |
---|
127 | */ |
---|
128 | void MultiLineText::setupTextWidth() |
---|
129 | { |
---|
130 | this->lineEnds.clear(); |
---|
131 | float width = 0.0f; |
---|
132 | float maxWidth = this->lineWidth / this->getSize(); |
---|
133 | |
---|
134 | for (unsigned int i = 0; i < this->getText().size(); i++) |
---|
135 | { |
---|
136 | if (width > maxWidth || this->getText()[i] == '\n') |
---|
137 | { |
---|
138 | if (likely(i > 0)) |
---|
139 | { |
---|
140 | this->lineEnds.push_back( i -1 ); |
---|
141 | width = this->getFont()->getGlyphArray()[this->getText()[i-1]]->advance; |
---|
142 | } |
---|
143 | else |
---|
144 | width = 0.0f; |
---|
145 | } |
---|
146 | |
---|
147 | // Advance the Text. |
---|
148 | if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL) |
---|
149 | width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance; |
---|
150 | } |
---|
151 | this->lineCount = lineEnds.size() + 1; |
---|
152 | this->setSizeY2D((this->lineEnds.size()+1) * (this->lineSpacing + this->getFont()->getMaxHeight())); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * @brief print out some nice debug output |
---|
157 | */ |
---|
158 | void MultiLineText::debug() const |
---|
159 | { |
---|
160 | printf("Debug %s::%s: %d lines\n", this->getClassName(), this->getName(), this->getLineCount()); |
---|
161 | |
---|
162 | std::string tmpText = this->getText(); |
---|
163 | std::vector<unsigned int> ends = this->lineEnds; |
---|
164 | ends.push_back(tmpText.size()); |
---|
165 | |
---|
166 | unsigned int prev = 0; |
---|
167 | for (unsigned int i = 0; i < ends.size(); i++) |
---|
168 | { |
---|
169 | printf("Line %d: %s\n", i, tmpText.substr(prev, ends[i] - prev).c_str()); |
---|
170 | prev = ends[i]; |
---|
171 | } |
---|
172 | } |
---|