~ubuntu-branches/ubuntu/edgy/pouetchess/edgy-updates

« back to all changes in this revision

Viewing changes to src/sxmlgui/GUIText.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc (mr_pouit)
  • Date: 2006-07-15 15:45:57 UTC
  • Revision ID: james.westby@ubuntu.com-20060715154557-3paxq02hx4od0wm4
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "EasyGL.h"
 
2
 
 
3
GUIText::GUIText(const std::string &text)
 
4
{
 
5
  fontIndex =    -1;
 
6
  update    = true;
 
7
 
 
8
  color.set(1.0,1.0,1.0);
 
9
  scales.set(1.0f, 1.0f);
 
10
  setString(text);
 
11
}
 
12
 
 
13
GUIText::GUIText(const GUIText & copy)
 
14
{
 
15
  this->operator=(copy);
 
16
}
 
17
 
 
18
GUIText &GUIText::operator =(const GUIText & copy)
 
19
{
 
20
  if(this != &copy)
 
21
  {
 
22
    fontIndex = copy.fontIndex;
 
23
    scales    = copy.scales;
 
24
    update    = copy.update;
 
25
    color     = copy.color;
 
26
    size      = copy.size;
 
27
    text      = copy.text;
 
28
  }
 
29
  return *this;
 
30
}
 
31
 
 
32
GUIText &GUIText::operator =(const std::string & str)
 
33
{
 
34
  setString(str);
 
35
  return *this;
 
36
}
 
37
 
 
38
bool GUIText::loadXMLSettings(const TiXmlElement *element)
 
39
{
 
40
  if(!element || !element->Value() || strcmp(element->Value(),  "Text"))
 
41
    return Logger::writeErrorLog("Need a Text node in the xml file");
 
42
 
 
43
  for(const TiXmlElement *child = element->FirstChildElement(); 
 
44
      child;
 
45
          child = child->NextSiblingElement() )
 
46
  {
 
47
    const char * value = child->Value();
 
48
 
 
49
    if(value)
 
50
    {
 
51
      if(!strcmp(value, "Color"))
 
52
        XMLArbiter::fillComponents3f(child, color);
 
53
 
 
54
      if(!strcmp(value, "Font"))
 
55
        fontIndex = GUIFontManager::addFont(child);
 
56
    }
 
57
  }
 
58
 
 
59
  setHeightScale(XMLArbiter::fillComponents1f(element, "hScale", 1.0f));
 
60
  setWidthScale(XMLArbiter::fillComponents1f(element, "wScale", 1.0f));
 
61
  setFontIndex(XMLArbiter::fillComponents1i(element, "fontIndex", fontIndex));
 
62
  setString(element->Attribute("string"));
 
63
 
 
64
  return true;
 
65
}
 
66
 
 
67
const std::string& GUIText::getString(){  return text; }
 
68
 
 
69
void GUIText::setString(const std::string &textArg)
 
70
{
 
71
  if(textArg.size() && text != textArg)
 
72
  {
 
73
    text   = textArg;
 
74
    update = true;
 
75
  }
 
76
}
 
77
 
 
78
void GUIText::setString(const char *textArg)
 
79
{
 
80
  if(textArg && text != textArg)
 
81
  {
 
82
    text   = textArg;
 
83
    update = true;
 
84
  }
 
85
}
 
86
 
 
87
void GUIText::clear()
 
88
{
 
89
  text.clear();
 
90
  update = false;
 
91
  size.set(0,0);
 
92
}
 
93
 
 
94
int GUIText::getFontIndex()
 
95
{
 
96
  return fontIndex;
 
97
}
 
98
 
 
99
void GUIText::setFontIndex(int index)
 
100
{
 
101
  fontIndex = index;
 
102
}
 
103
 
 
104
void  GUIText::print(int x, int y, int startIndex, int endIndex)
 
105
{
 
106
  if(!text.size())
 
107
    return;
 
108
 
 
109
  endIndex   = (endIndex  < 0) ? int(text.size()) : endIndex;
 
110
  startIndex = clamp(startIndex, 0, endIndex);
 
111
 
 
112
  GUIFontManager::setCurrentFont(fontIndex);
 
113
  GUIFont *currentFont = GUIFontManager::getCurrentFont();
 
114
  computeDimensions();
 
115
 
 
116
  if(!currentFont && !(currentFont = GUIFontManager::getDefaultFont()))
 
117
    return;
 
118
 
 
119
  currentFont->getFontObject()->printSubString(float(x), float(y), scales.x, scales.y, 
 
120
                                               color.x, color.y, color.z,
 
121
                                               startIndex, endIndex,
 
122
                                               text);
 
123
}
 
124
 
 
125
void  GUIText::printCenteredX (int x, int y, int startIndex, int endIndex)
 
126
{
 
127
  if(!text.size())
 
128
    return;
 
129
 
 
130
  computeDimensions();
 
131
  print(x - size.x/2, y, startIndex, endIndex);
 
132
}
 
133
 
 
134
void  GUIText::printCenteredY (int x, int y, int startIndex, int endIndex)
 
135
{
 
136
  if(!text.size())
 
137
    return;
 
138
 
 
139
  computeDimensions();
 
140
  print(x,  y - size.y/2, startIndex, endIndex);
 
141
}
 
142
 
 
143
void  GUIText::printCenteredXY(int x, int y, int startIndex, int endIndex)
 
144
{
 
145
  if(!text.size())
 
146
    return;
 
147
 
 
148
  computeDimensions();
 
149
  print(x - size.x/2, y - size.y/2, startIndex, endIndex);
 
150
}
 
151
 
 
152
void GUIText::computeDimensions()
 
153
{
 
154
  if(needUpdating())
 
155
  {
 
156
    GUIFontManager::setCurrentFont(fontIndex);
 
157
    GUIFont *currentFont = GUIFontManager::getCurrentFont();
 
158
 
 
159
    if(!currentFont && !(currentFont = GUIFontManager::getDefaultFont()))
 
160
      return;
 
161
 
 
162
    if(currentFont == GUIFontManager::getDefaultFont())
 
163
      fontIndex = GUIFontManager::findFontIndex(currentFont);
 
164
 
 
165
    if(text.size())
 
166
    {
 
167
      size = currentFont->getFontObject()->getStringDimensions(text);
 
168
      size.x = int(float(size.x)*scales.x);
 
169
    }
 
170
    size.y = int(float(currentFont->getFontObject()->getHeight())*scales.y);
 
171
    forceUpdate(false);
 
172
  }
 
173
}
 
174
 
 
175
void GUIText::setSize(const Tuple2i& sizeArg){ size = sizeArg; }
 
176
void GUIText::setSize(int x, int y){ size.set(x, y); }
 
177
 
 
178
int  GUIText::getHeight(){ return abs(size.y); }
 
179
int  GUIText::getWidth() { return abs(size.x); }
 
180
 
 
181
const Tuple2i& GUIText::getSize(){ return size; }
 
182
bool  GUIText::needUpdating(){ return update; }
 
183
 
 
184
void  GUIText::forceUpdate(bool updateArg){ update = updateArg; }
 
185
void  GUIText::setColor(const Tuple3f &color){ setColor(color.x, color.y, color.z); }
 
186
 
 
187
void GUIText::setColor(float r, float g, float b)
 
188
{
 
189
  color.set(clamp(r, 0.0f, 255.0f),
 
190
            clamp(g, 0.0f, 255.0f),
 
191
            clamp(b, 0.0f, 255.0f));
 
192
 
 
193
  color.x /= (color.x > 1.0) ? 255.0f : 1.0f;
 
194
  color.y /= (color.y > 1.0) ? 255.0f : 1.0f;
 
195
  color.z /= (color.z > 1.0) ? 255.0f : 1.0f;
 
196
}
 
197
 
 
198
void   GUIText::setHeightScale(float hs)
 
199
{
 
200
  scales.y = clamp(hs, 0.1f, 20.0f);
 
201
}
 
202
void   GUIText::setWidthScale(float ws)
 
203
{
 
204
  scales.x = clamp(ws, 0.1f, 20.0f);
 
205
}
 
206
void   GUIText::setScales(Tuple2f scales)
 
207
{
 
208
  setHeightScale(scales.y);
 
209
  setWidthScale(scales.x);
 
210
}
 
211
 
 
212
float  GUIText::getHeightScale()
 
213
{
 
214
  return scales.y;
 
215
}
 
216
 
 
217
float  GUIText::getWidthScale()
 
218
{
 
219
  return scales.x;
 
220
 
221
 
 
222
const  Tuple2f &GUIText::getScales()
 
223
{
 
224
  return scales;
 
225
}
 
226
const Tuple3f &GUIText::getColor()
 
227
 
228
  return color;
 
229
}
 
230