Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: less debug output

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
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  this->font().select();
73
74  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
75  glRotatef(this->getAbsDir2D(), 0, 0, 1);
76
77  Glyph* tmpGlyph;
78  float posX = 0.0f;
79  glBegin(GL_QUADS);
80  for (unsigned int i = 0; i < this->_dotedText.size(); i++)
81  {
82    if(likely((tmpGlyph = this->font().getGlyphArray()[this->_dotedText[i]]) != NULL))
83    {
84      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
85      glVertex2d(posX+tmpGlyph->maxX*this->size(), 0);
86
87      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
88      glVertex2d(posX+tmpGlyph->maxX*this->size(), this->size());
89
90      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
91      glVertex2d(posX+tmpGlyph->minX*this->size(), this->size());
92
93      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
94      glVertex2d(posX+tmpGlyph->minX*this->size(), 0);
95
96      posX += tmpGlyph->advance * this->size();
97    }
98  }
99  glEnd();
100  glPopAttrib();
101  glPopMatrix();
102}
103
104
105/**
106 * @brief setting up the Text-Width if DYNAMIC
107 */
108void LimitedWidthText::setupTextWidth()
109{
110  float dotsSize = this->font().getGlyphArray()[46]->advance * 3.0;
111
112  float width = 0.0f;
113  float maxWidth = this->_lineWidth / this->size();
114
115  this->_dotedText = this->text();
116
117  switch (this->_dotsPosition)
118  {
119    case End:
120      for (unsigned int i = 0; i < this->text().size(); i++)
121      {
122        if (width + dotsSize > maxWidth )
123        {
124          this->_dotedText = this->text().substr(0, i) + "...";
125          if (i > 0)
126            width -= this->font().getGlyphArray()[this->text()[i-1]]->advance;
127          width += dotsSize;
128          break;
129        }
130        // Advance the Text.
131        if(this->font().getGlyphArray()[this->text()[i]] != NULL)
132          width += this->font().getGlyphArray()[this->text()[i]]->advance;
133      }
134      break;
135
136    case Begin:
137      int i = text().size() -1;
138      for (; i >= 0; --i)
139      {
140        if (width + dotsSize > maxWidth )
141        {
142          this->_dotedText = std::string("...") + this->text().substr(i);
143          if (i + 1 < (int)text().size() )
144            width -= this->font().getGlyphArray()[this->text()[i+1]]->advance;
145          width += dotsSize;
146          break;
147        }
148        // Advance the Text.
149        if(this->font().getGlyphArray()[this->text()[i]] != NULL)
150          width += this->font().getGlyphArray()[this->text()[i]]->advance;
151      }
152      break;
153  }
154  this->setSizeX2D(width * this->size());
155}
156
157/**
158 * @brief print out some nice debug output
159 */
160void LimitedWidthText::debug() const
161{
162  printf("Debug %s::%s \n", this->getClassName(), this->getName() );
163}
Note: See TracBrowser for help on using the repository browser.