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

« back to all changes in this revision

Viewing changes to src/sxmlgui/GUIClippedRectangle.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
GUIClippedRectangle::GUIClippedRectangle(int size)
 
4
{
 
5
  setTextureRectangle(0,0,1,1);
 
6
  setBordersColor(0.0f, 0.9f, 0.0f);
 
7
  setClipSize(size);
 
8
  setBGColor(0.413f, 0.7f, 0.52f, 0.75f);
 
9
 
 
10
  drawBackground = false;
 
11
  drawBounds     = false;
 
12
}
 
13
 
 
14
void  GUIClippedRectangle::setClipSize(int clipSizeArg)
 
15
{
 
16
  clipSize = clamp(clipSizeArg, 0, 100);
 
17
}
 
18
 
 
19
int GUIClippedRectangle::getClipSize()
 
20
{
 
21
  return clipSize;
 
22
}
 
23
 
 
24
void GUIClippedRectangle::setVisibleBounds(bool visibleArg)
 
25
{
 
26
  drawBounds = visibleArg;
 
27
}
 
28
 
 
29
bool  GUIClippedRectangle::boundsVisible()
 
30
{
 
31
  return drawBounds;
 
32
}
 
33
 
 
34
bool GUIClippedRectangle::loadXMLClippedRectangleInfo(const TiXmlElement *element)
 
35
{
 
36
  if(!element)
 
37
    return false;
 
38
 
 
39
  const TiXmlElement *absolutePriority = XMLArbiter::getChildElementByName(element, "Texture");
 
40
  setVisibleBounds(XMLArbiter::analyzeBooleanAttr(element,  "drawBounds",     drawBounds));
 
41
  enableBGColor(XMLArbiter::analyzeBooleanAttr(element,     "drawBackground", drawBackground));
 
42
  setClipSize(XMLArbiter::fillComponents1i(element,         "clipSize",       clipSize));
 
43
 
 
44
  if(absolutePriority)
 
45
    texture.loadXMLSettings(absolutePriority);
 
46
 
 
47
  for(const TiXmlElement *child = element->FirstChildElement(); 
 
48
      child;
 
49
          child = child->NextSiblingElement() )
 
50
  {
 
51
    const char * value = child->Value();
 
52
 
 
53
    if(!value)
 
54
      continue;
 
55
 
 
56
    if(!strcmp(value, "TextureRectangle"))
 
57
      setTextureRectangle(XMLArbiter::fillComponents4f(child, textureRectangle));
 
58
 
 
59
    if(!strcmp(value, "BordersColor"))
 
60
      setBordersColor(XMLArbiter::fillComponents3f(child, bordersColor));
 
61
 
 
62
    if(!strcmp(value, "BGColor"))
 
63
      setBGColor(XMLArbiter::fillComponents4f(child, bgColor));
 
64
  }
 
65
 
 
66
  return true;
 
67
}
 
68
 
 
69
void  GUIClippedRectangle::setBGColor(const Tuple4f& color)
 
70
{
 
71
  setBGColor(color.x, color.y, color.z, color.w);
 
72
}
 
73
 
 
74
void  GUIClippedRectangle::setBGColor(float r, float g, float b, float a)
 
75
{
 
76
  bgColor.set(clamp(r, 0.0f, 255.0f),
 
77
              clamp(g, 0.0f, 255.0f),
 
78
              clamp(b, 0.0f, 255.0f),
 
79
              clamp(a, 0.0f, 255.0f));
 
80
 
 
81
  bgColor.x /= (bgColor.x > 1.0) ? 255.0f : 1.0f;
 
82
  bgColor.y /= (bgColor.y > 1.0) ? 255.0f : 1.0f;
 
83
  bgColor.z /= (bgColor.z > 1.0) ? 255.0f : 1.0f;
 
84
  bgColor.w /= (bgColor.w > 1.0) ? 255.0f : 1.0f;
 
85
}
 
86
 
 
87
void  GUIClippedRectangle::setBordersColor(const Tuple3f& color)
 
88
{
 
89
  setBordersColor(color.x, color.y, color.z);
 
90
}
 
91
 
 
92
const Tuple3f &GUIClippedRectangle::getBordersColor()
 
93
{
 
94
  return bordersColor;
 
95
}
 
96
 
 
97
void  GUIClippedRectangle::setBordersColor(float r, float g, float b)
 
98
{
 
99
  bordersColor.set(clamp(r, 0.0f, 255.0f),
 
100
                   clamp(g, 0.0f, 255.0f),
 
101
                   clamp(b, 0.0f, 255.0f));
 
102
  bordersColor.x /= (bordersColor.x > 1.0) ? 255.0f : 1.0f;
 
103
  bordersColor.y /= (bordersColor.y > 1.0) ? 255.0f : 1.0f;
 
104
  bordersColor.z /= (bordersColor.z > 1.0) ? 255.0f : 1.0f;
 
105
}
 
106
 
 
107
void GUIClippedRectangle::renderClippedBounds()
 
108
{
 
109
  if(drawBackground || drawBounds)
 
110
  {
 
111
    glVertexPointer(2, GL_INT, 0, vertices[0]);
 
112
    glEnableClientState(GL_VERTEX_ARRAY);
 
113
 
 
114
    if(texture.getID() && drawBackground)
 
115
    {
 
116
      glTexCoordPointer(2, GL_FLOAT, 0, texCoords[0]) ;
 
117
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
118
      texture.activate();
 
119
    }
 
120
  }
 
121
 
 
122
  if(drawBackground)
 
123
  {
 
124
    glEnable(GL_BLEND);
 
125
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
126
    glColor4fv(bgColor);
 
127
    glDrawArrays(GL_TRIANGLE_FAN, 0, 7);
 
128
    glDisable(GL_BLEND);
 
129
 
 
130
    if(texture.getID())
 
131
    {
 
132
      texture.deactivate();
 
133
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
134
    }
 
135
  }
 
136
 
 
137
  if(drawBounds)
 
138
  {
 
139
    glColor3fv(bordersColor);
 
140
    glDrawArrays(GL_LINE_STRIP, 0, 7);
 
141
  }
 
142
 
 
143
  glColor3f(1,1,1);
 
144
  glDisableClientState(GL_VERTEX_ARRAY);
 
145
}
 
146
 
 
147
void  GUIClippedRectangle::computeClippedBounds(const Tuple4i &windowBounds)
 
148
{
 
149
  float yTexOffset    = float(clipSize)/(windowBounds.w - windowBounds.y),
 
150
        xTexOffset    = float(clipSize)/(windowBounds.z - windowBounds.x);
 
151
 
 
152
  xTexOffset *= textureRectangle.z - textureRectangle.x;
 
153
  yTexOffset *= textureRectangle.w - textureRectangle.y;
 
154
 
 
155
  vertices[0].set(windowBounds.x, windowBounds.y + clipSize);
 
156
  vertices[1].set(windowBounds.x, windowBounds.w);
 
157
  vertices[2].set(windowBounds.z - clipSize, windowBounds.w);
 
158
  vertices[3].set(windowBounds.z, windowBounds.w - clipSize);
 
159
  vertices[4].set(windowBounds.z, windowBounds.y);
 
160
  vertices[5].set(windowBounds.x + clipSize, windowBounds.y);
 
161
  vertices[6].set(windowBounds.x, windowBounds.y + clipSize);
 
162
 
 
163
  texCoords[0].set(textureRectangle.x             , textureRectangle.w - yTexOffset);
 
164
  texCoords[1].set(textureRectangle.x             , textureRectangle.y);
 
165
  texCoords[2].set(textureRectangle.z - xTexOffset, textureRectangle.y             );
 
166
  texCoords[3].set(textureRectangle.z             , textureRectangle.y + yTexOffset);
 
167
  texCoords[4].set(textureRectangle.z             , textureRectangle.w);
 
168
  texCoords[5].set(textureRectangle.x + xTexOffset, textureRectangle.w             );
 
169
  texCoords[6].set(textureRectangle.x             , textureRectangle.w - yTexOffset);
 
170
}
 
171
 
 
172
void  GUIClippedRectangle::enableBGColor(bool enable)
 
173
{
 
174
  drawBackground = enable;
 
175
}
 
176
 
 
177
void GUIClippedRectangle::setTextureRectangle(const Tuple4f &tr)
 
178
{
 
179
  setTextureRectangle(tr.x, tr.y, tr.z, tr.w);
 
180
}
 
181
 
 
182
void GUIClippedRectangle::setTextureRectangle(float x, float y, float z, float w)
 
183
{
 
184
  if(x > 1.0f || y > 1.0f || z > 1.0f || w > 1.0f)
 
185
  if(texture.getID())
 
186
  {
 
187
    x = clamp(x, 0.0f, float(texture.getWidth() ));
 
188
    y = clamp(y, 0.0f, float(texture.getHeight()));
 
189
    z = clamp(z, 0.0f, float(texture.getWidth() ));
 
190
    w = clamp(w, 0.0f, float(texture.getHeight()));
 
191
    
 
192
    x /= texture.getWidth();
 
193
    z /= texture.getWidth();
 
194
 
 
195
    w /= texture.getHeight();
 
196
    y /= texture.getHeight();
 
197
 
 
198
  }
 
199
 
 
200
  textureRectangle.set(clamp(x, 0.0f, 1.0f),
 
201
                       clamp(y, 0.0f, 1.0f),
 
202
                       clamp(z, 0.0f, 1.0f),
 
203
                       clamp(w, 0.0f, 1.0f));
 
204
}
 
205
 
 
206
const  Tuple4f &GUIClippedRectangle::getTextureRectangle()
 
207
{
 
208
  return textureRectangle;
 
209
 
210
 
 
211
Texture &GUIClippedRectangle::getTexture()
 
212
{
 
213
  return texture;
 
214
 
215
 
 
216
void  GUIClippedRectangle::setTexture(const Texture & textureArg)
 
217
{
 
218
  texture = textureArg;
 
219
}
 
220
 
 
221
bool  GUIClippedRectangle::isBGColorOn()
 
222
{
 
223
  return drawBackground;
 
224
}
 
225
 
 
226
const Tuple4f &GUIClippedRectangle::getBGColor()
 
227
{
 
228
  return bgColor;
 
229
}
 
230
 
 
231
const Tuple2i *GUIClippedRectangle::getVertices() const
 
232
{
 
233
  return vertices;
 
234
}
 
235
 
 
236
const Tuple2f *GUIClippedRectangle::getTexCoords() const
 
237
{
 
238
  return texCoords;
 
239
}