Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/graphics/text_engine/limited_width_text.cc @ 8538

Last change on this file since 8538 was 8538, checked in by bensch, 18 years ago

limited text-lenght

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