Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/text_engine/limited_width_text.cc @ 8697

Last change on this file since 8697 was 8619, checked in by bensch, 19 years ago

trunk: merged the gui-branche back.
merged with command:
svn merge -r8520:HEAD https://svn.orxonox.net/orxonox/branches/gui
no conflicts

File size: 4.7 KB
Line 
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 "limited_width_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 */
26LimitedWidthText::LimitedWidthText(const std::string& fontFile, unsigned int textSize, float lineWidth, DotsPosition dotsPosition)
27    : Text(fontFile, textSize)
28{
29  this->setClassID(CL_LIMITED_WIDTH_TEXT, "LimitedWidthText");
30
31  this->_dotsPosition = End;
32  this->setLineWidth(lineWidth);
33}
34
35/**
36 * @brief sets the maximum Line width
37 * @param lineWidth the maximum lineWidth.
38 */
39void LimitedWidthText::setLineWidth(float lineWidth)
40{
41  this->_lineWidth = lineWidth;
42  this->setupTextWidth();
43}
44
45/**
46 * @brief sets the Dots Position
47 * @param dotsPosition the Position of the Dots
48 */
49void LimitedWidthText::setDotsPosition(DotsPosition dotsPosition)
50{
51  this->_dotsPosition = dotsPosition;
52  this->setupTextWidth();
53}
54
55
56/**
57 * @brief draws the Text
58 */
59void LimitedWidthText::draw() const
60{
61  if (unlikely(this->_dotedText.empty()))
62    return;
63  glPushMatrix();
64  glPushAttrib(GL_ENABLE_BIT);
65  // transform for alignment.
66  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
67    glTranslatef(-this->getSizeX2D(), 0, 0);
68  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
69    glTranslatef(-this->getSizeX2D()/2, 0, 0);
70
71  // drawing this Text.
72  // setting the Blending effects
73  glColor4fv(&this->color()[0]);
74
75
76  glActiveTexture(GL_TEXTURE0);
77
78  glEnable(GL_BLEND);
79  glEnable(GL_TEXTURE_2D);
80  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
81  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
82
83  glBindTexture(GL_TEXTURE_2D, this->font()->getTexture());
84  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
85  glRotatef(this->getAbsDir2D(), 0, 0, 1);
86
87  Glyph* tmpGlyph;
88  float posX = 0.0f;
89  glBegin(GL_QUADS);
90  for (unsigned int i = 0; i < this->_dotedText.size(); i++)
91  {
92    if(likely((tmpGlyph = this->font()->getGlyphArray()[this->_dotedText[i]]) != NULL))
93    {
94      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
95      glVertex2d(posX+tmpGlyph->maxX*this->size(), 0);
96
97      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
98      glVertex2d(posX+tmpGlyph->maxX*this->size(), this->size());
99
100      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
101      glVertex2d(posX+tmpGlyph->minX*this->size(), this->size());
102
103      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
104      glVertex2d(posX+tmpGlyph->minX*this->size(), 0);
105
106      posX += tmpGlyph->advance * this->size();
107    }
108  }
109  glEnd();
110  glPopAttrib();
111  glPopMatrix();
112}
113
114
115/**
116 * @brief setting up the Text-Width if DYNAMIC
117 */
118void LimitedWidthText::setupTextWidth()
119{
120  float dotsSize = this->font()->getGlyphArray()[46]->advance * 3.0;
121
122  float width = 0.0f;
123  float maxWidth = this->_lineWidth / this->size();
124
125  this->_dotedText = this->text();
126
127  switch (this->_dotsPosition)
128  {
129    case End:
130      for (unsigned int i = 0; i < this->text().size(); i++)
131      {
132        if (width + dotsSize > maxWidth )
133        {
134          this->_dotedText = this->text().substr(0, i) + "...";
135          if (i > 0)
136            width -= this->font()->getGlyphArray()[this->text()[i-1]]->advance;
137          width += dotsSize;
138          break;
139        }
140        // Advance the Text.
141        if(this->font()->getGlyphArray()[this->text()[i]] != NULL)
142          width += this->font()->getGlyphArray()[this->text()[i]]->advance;
143      }
144      break;
145
146    case Begin:
147      int i = text().size() -1;
148      for (; i >= 0; --i)
149      {
150        if (width + dotsSize > maxWidth )
151        {
152          this->_dotedText = std::string("...") + this->text().substr(i);
153          if (i + 1 < (int)text().size() )
154            width -= this->font()->getGlyphArray()[this->text()[i+1]]->advance;
155          width += dotsSize;
156          break;
157        }
158        // Advance the Text.
159        if(this->font()->getGlyphArray()[this->text()[i]] != NULL)
160          width += this->font()->getGlyphArray()[this->text()[i]]->advance;
161      }
162      break;
163  }
164
165  printf("%f %s\n", width, _dotedText.c_str());
166  this->setSizeX2D(width * this->size());
167}
168
169/**
170 * @brief print out some nice debug output
171 */
172void LimitedWidthText::debug() const
173{
174  printf("Debug %s::%s \n", this->getClassName(), this->getName() );
175}
Note: See TracBrowser for help on using the repository browser.