Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the new Font-Implementation back here
merged with svn merge https://svn.orxonox.net/orxonox/branches/fontdata . -r8752:HEAD
no conflicts, naturally

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