Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/graphics/text_engine/limited_width_text.cc @ 9913

Last change on this file since 9913 was 9913, checked in by rennerc, 18 years ago

fixed bug

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