Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10771 was 10114, checked in by patrick, 18 years ago

merged network back to trunk

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