src/GUIClippedRectangle.cpp

Go to the documentation of this file.
00001 #include "glwx.h"
00002 
00003 GUIClippedRectangle::GUIClippedRectangle(int size)
00004 {
00005   setTextureRectangle(0,0,1,1);
00006   setBordersColor(0.0f, 0.9f, 0.0f);
00007   setClipSize(size);
00008   setBGColor(0.413f, 0.7f, 0.52f, 0.75f);
00009 
00010   drawBackground = false;
00011   drawBounds     = false;
00012 }
00013 
00014 void  GUIClippedRectangle::setClipSize(int clipSizeArg)
00015 {
00016   clipSize = clamp(clipSizeArg, 0, 100);
00017 }
00018 
00019 int GUIClippedRectangle::getClipSize()
00020 {
00021   return clipSize;
00022 }
00023 
00024 void GUIClippedRectangle::setVisibleBounds(bool visibleArg)
00025 {
00026   drawBounds = visibleArg;
00027 }
00028 
00029 bool  GUIClippedRectangle::boundsVisible()
00030 {
00031   return drawBounds;
00032 }
00033 
00034 bool GUIClippedRectangle::loadXMLClippedRectangleInfo(const TiXmlElement *element)
00035 {
00036   if(!element)
00037     return false;
00038 
00039   const TiXmlElement *absolutePriority = XMLArbiter::getChildElementByName(element, "Texture");
00040   setVisibleBounds(XMLArbiter::analyzeBooleanAttr(element,  "drawBounds",     drawBounds));
00041   enableBGColor(XMLArbiter::analyzeBooleanAttr(element,     "drawBackground", drawBackground));
00042   setClipSize(XMLArbiter::fillComponents1i(element,         "clipSize",       clipSize));
00043  
00044   if(absolutePriority)
00045     texture.loadXMLSettings(absolutePriority);
00046 
00047   for(const TiXmlElement *child = element->FirstChildElement(); 
00048       child;
00049           child = child->NextSiblingElement() )
00050   {
00051     const char * value = child->Value();
00052 
00053     if(!value)
00054       continue;
00055 
00056     if(!strcmp(value, "TextureRectangle"))
00057       setTextureRectangle(XMLArbiter::fillComponents4f(child, textureRectangle));
00058 
00059     if(!strcmp(value, "BordersColor"))
00060       setBordersColor(XMLArbiter::fillComponents3f(child, bordersColor));
00061  
00062     if(!strcmp(value, "BGColor"))
00063       setBGColor(XMLArbiter::fillComponents4f(child, bgColor));
00064   }
00065 
00066   return true;
00067 }
00068 
00069 void  GUIClippedRectangle::setBGColor(const Tuple4f& color)
00070 {
00071   setBGColor(color.x, color.y, color.z, color.w);
00072 }
00073 
00074 void  GUIClippedRectangle::setBGColor(float r, float g, float b, float a)
00075 {
00076   bgColor.set(clamp(r, 0.0f, 255.0f),
00077               clamp(g, 0.0f, 255.0f),
00078               clamp(b, 0.0f, 255.0f),
00079               clamp(a, 0.0f, 255.0f));
00080 
00081   bgColor.x /= (bgColor.x > 1.0) ? 255.0f : 1.0f;
00082   bgColor.y /= (bgColor.y > 1.0) ? 255.0f : 1.0f;
00083   bgColor.z /= (bgColor.z > 1.0) ? 255.0f : 1.0f;
00084   bgColor.w /= (bgColor.w > 1.0) ? 255.0f : 1.0f;
00085 }
00086 
00087 void  GUIClippedRectangle::setBordersColor(const Tuple3f& color)
00088 {
00089   setBordersColor(color.x, color.y, color.z);
00090 }
00091 
00092 const Tuple3f &GUIClippedRectangle::getBordersColor()
00093 {
00094   return bordersColor;
00095 }
00096 
00097 void  GUIClippedRectangle::setBordersColor(float r, float g, float b)
00098 {
00099   bordersColor.set(clamp(r, 0.0f, 255.0f),
00100                    clamp(g, 0.0f, 255.0f),
00101                    clamp(b, 0.0f, 255.0f));
00102   bordersColor.x /= (bordersColor.x > 1.0) ? 255.0f : 1.0f;
00103   bordersColor.y /= (bordersColor.y > 1.0) ? 255.0f : 1.0f;
00104   bordersColor.z /= (bordersColor.z > 1.0) ? 255.0f : 1.0f;
00105 }
00106 
00107 void GUIClippedRectangle::renderClippedBounds()
00108 {
00109   if(drawBackground || drawBounds)
00110   {
00111     glVertexPointer(2, GL_INT, 0, vertices[0]);
00112     glEnableClientState(GL_VERTEX_ARRAY);
00113 
00114     if(texture.getID() && drawBackground)
00115     {
00116       glTexCoordPointer(2, GL_FLOAT, 0, texCoords[0]) ;
00117       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
00118       texture.activate();
00119     }
00120   }
00121 
00122   if(drawBackground)
00123   {
00124     glEnable(GL_BLEND);
00125     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00126     glColor4fv(bgColor);
00127     glDrawArrays(GL_TRIANGLE_FAN, 0, 7);
00128     glDisable(GL_BLEND);
00129 
00130     if(texture.getID())
00131     {
00132       texture.deactivate();
00133       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
00134     }
00135   }
00136 
00137   if(drawBounds)
00138   {
00139     glColor3fv(bordersColor);
00140     glDrawArrays(GL_LINE_STRIP, 0, 7);
00141   }
00142 
00143   glColor3f(1,1,1);
00144   glDisableClientState(GL_VERTEX_ARRAY);
00145 }
00146 
00147 void  GUIClippedRectangle::computeClippedBounds(const Tuple4i &windowBounds)
00148 {
00149   float yTexOffset    = float(clipSize)/(windowBounds.w - windowBounds.y),
00150         xTexOffset    = float(clipSize)/(windowBounds.z - windowBounds.x);
00151 
00152   xTexOffset *= textureRectangle.z - textureRectangle.x;
00153   yTexOffset *= textureRectangle.w - textureRectangle.y;
00154 
00155   vertices[0].set(windowBounds.x, windowBounds.y + clipSize);
00156   vertices[1].set(windowBounds.x, windowBounds.w);
00157   vertices[2].set(windowBounds.z - clipSize, windowBounds.w);
00158   vertices[3].set(windowBounds.z, windowBounds.w - clipSize);
00159   vertices[4].set(windowBounds.z, windowBounds.y);
00160   vertices[5].set(windowBounds.x + clipSize, windowBounds.y);
00161   vertices[6].set(windowBounds.x, windowBounds.y + clipSize);
00162 
00163   texCoords[0].set(textureRectangle.x             , textureRectangle.w - yTexOffset);
00164   texCoords[1].set(textureRectangle.x             , textureRectangle.y);
00165   texCoords[2].set(textureRectangle.z - xTexOffset, textureRectangle.y             );
00166   texCoords[3].set(textureRectangle.z             , textureRectangle.y + yTexOffset);
00167   texCoords[4].set(textureRectangle.z             , textureRectangle.w);
00168   texCoords[5].set(textureRectangle.x + xTexOffset, textureRectangle.w             );
00169   texCoords[6].set(textureRectangle.x             , textureRectangle.w - yTexOffset);
00170 }
00171 
00172 void  GUIClippedRectangle::enableBGColor(bool enable)
00173 {
00174   drawBackground = enable;
00175 }
00176 
00177 void GUIClippedRectangle::setTextureRectangle(const Tuple4f &tr)
00178 {
00179   setTextureRectangle(tr.x, tr.y, tr.z, tr.w);
00180 }
00181 
00182 void GUIClippedRectangle::setTextureRectangle(float x, float y, float z, float w)
00183 {
00184   if(x > 1.0f || y > 1.0f || z > 1.0f || w > 1.0f)
00185   if(texture.getID())
00186   {
00187     x = clamp(x, 0.0f, float(texture.getWidth() ));
00188     y = clamp(y, 0.0f, float(texture.getHeight()));
00189     z = clamp(z, 0.0f, float(texture.getWidth() ));
00190     w = clamp(w, 0.0f, float(texture.getHeight()));
00191     
00192     x /= texture.getWidth();
00193     z /= texture.getWidth();
00194 
00195     w /= texture.getHeight();
00196     y /= texture.getHeight();
00197 
00198   }
00199 
00200   textureRectangle.set(clamp(x, 0.0f, 1.0f),
00201                        clamp(y, 0.0f, 1.0f),
00202                        clamp(z, 0.0f, 1.0f),
00203                        clamp(w, 0.0f, 1.0f));
00204 }
00205 
00206 const  Tuple4f &GUIClippedRectangle::getTextureRectangle()
00207 {
00208   return textureRectangle;
00209 } 
00210 
00211 Texture &GUIClippedRectangle::getTexture()
00212 {
00213   return texture;
00214 } 
00215 
00216 void  GUIClippedRectangle::setTexture(const Texture & textureArg)
00217 {
00218   texture = textureArg;
00219 }
00220 
00221 bool  GUIClippedRectangle::isBGColorOn()
00222 {
00223   return drawBackground;
00224 }
00225 
00226 const Tuple4f &GUIClippedRectangle::getBGColor()
00227 {
00228   return bgColor;
00229 }
00230 
00231 const Tuple2i *GUIClippedRectangle::getVertices() const
00232 {
00233   return vertices;
00234 }
00235 
00236 const Tuple2f *GUIClippedRectangle::getTexCoords() const
00237 {
00238   return texCoords;
00239 }

Generated on Wed Dec 5 20:32:03 2007 for GLWX by  doxygen 1.5.3