Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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