AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Wednesday the 23rd of May 2012 @ 09:37am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cGuiFill.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "gui/cGuiFill.h"
00026 
00027 #include "n2l/video.h"
00028 #include "n2l/dynVars.h"
00029 #include "n2l/resourceManagement.h"
00030 #include "n2l/materials.h"
00031 
00032 namespace n2l
00033 {
00034     /**************************************************************************/
00035     cGuiFill::cGuiFill() :
00036         mHRepeat(false),
00037         mVRepeat(false),
00038         mHInvert(false),
00039         mVInvert(false),
00040         mColourInherit(false),
00041         mBlending(Blending_Disable),
00042         mColour(1.0f,1.0f,1.0f,1.0f),
00043         mCornerColours(0),
00044         mTexture(0),
00045         mTexSize(1.0f, 1.0f),
00046         mTLPad(.0f, .0f),
00047         mBRPad(.0f, .0f)
00048     {
00049     }
00050 
00051     /**************************************************************************/
00052     cGuiFill::cGuiFill( const cGuiFill &iFill ) :
00053         mHRepeat( iFill.mHRepeat ),
00054         mVRepeat( iFill.mVRepeat ),
00055         mHInvert( iFill.mHInvert ),
00056         mVInvert( iFill.mVInvert ),
00057         mColourInherit( iFill.mColourInherit ),
00058         mBlending( iFill.mBlending ),
00059         mColour( iFill.mColour ),
00060         mCornerColours(0),
00061         mTexture( iFill.mTexture ),
00062         mTexSize(1.0f, 1.0f),
00063         mTLPad( iFill.mTLPad ),
00064         mBRPad( iFill.mBRPad )
00065     {
00066         if (iFill.mCornerColours)
00067             colour( iFill.mCornerColours[0],
00068                     iFill.mCornerColours[1],
00069                     iFill.mCornerColours[2],
00070                     iFill.mCornerColours[3] );
00071     }
00072 
00073 
00074     /**************************************************************************/
00075     cGuiFill::cGuiFill( const cVfsNodeInterface & iNode ) :
00076         mHRepeat(false),
00077         mVRepeat(false),
00078         mHInvert(false),
00079         mVInvert(false),
00080         mColourInherit(false),
00081         mBlending(Blending_Disable),
00082         mColour(1.0f,1.0f,1.0f,1.0f),
00083         mCornerColours(0),
00084         mTexture(0),
00085         mTexSize(1.0f, 1.0f),
00086         mTLPad(.0f, .0f),
00087         mBRPad(.0f, .0f)
00088     {
00089         load( iNode );
00090     }
00091 
00092 
00093     /**************************************************************************/
00094     cGuiFill::cGuiFill( const cDynVar & iDefinition ) :
00095         mHRepeat(false),
00096         mVRepeat(false),
00097         mColourInherit(false),
00098         mBlending(Blending_Disable),
00099         mColour(1.0f,1.0f,1.0f,1.0f),
00100         mCornerColours(0),
00101         mTexture(0),
00102         mTexSize(1.0f, 1.0f),
00103         mTLPad(.0f, .0f),
00104         mBRPad(.0f, .0f)
00105     {
00106         load( iDefinition );
00107     }
00108 
00109 
00110     /**************************************************************************/
00111     void cGuiFill::load( const cVfsNodeInterface &iNode )
00112     {
00113         if (!iNode.likeFile())
00114             throw cBadDataUseException( "cGuiFill::load",
00115                                         "Provided node isn\'t like a file" );
00116         // Load it.
00117         if (iNode.firstLine()!="n2l::cGuiFill")
00118             throw cParsingException("cGuiFill::load",
00119                                     "This file isn\'t a cGuiFill" );
00120 
00121         cDynVar def;
00122         if (iNode.buffer().size()<=(iNode.firstLine().size()+1))
00123             throw cParsingException("cGuiFill::load",
00124                                     "No data after header!" );
00125         def.unserialize( iNode.buffer().c_str() + iNode.firstLine().size()+1 );
00126 
00127         load( def );
00128     }
00129 
00130 
00131     /**************************************************************************/
00132     void cGuiFill::load( const cDynVar &iDef )
00133     {
00134         if (iDef["cornerColours"]) {
00135             colour( cColour( iDef["cornerColours"][0][0],
00136                 iDef["cornerColours"][0][1], iDef["cornerColours"][0][2],
00137                 iDef["cornerColours"][0][3]),
00138                 cColour( iDef["cornerColours"][1][0],
00139                     iDef["cornerColours"][1][1], iDef["cornerColours"][1][2],
00140                     iDef["cornerColours"][1][3]),
00141                 cColour( iDef["cornerColours"][2][0],
00142                     iDef["cornerColours"][2][1], iDef["cornerColours"][2][2],
00143                     iDef["cornerColours"][2][3]),
00144                 cColour( iDef["cornerColours"][3][0],
00145                     iDef["cornerColours"][3][1], iDef["cornerColours"][3][2],
00146                     iDef["cornerColours"][3][3]) );
00147         } else {
00148             if (iDef["colour"])
00149                 colour( cColour(iDef["colour"][0], iDef["colour"][1],
00150                     iDef["colour"][2], iDef["colour"][3]) );
00151         }
00152         if (iDef["texture"]) {
00153             if (iDef["textureSize"])
00154                 texture( cResourceManager::get<cGLTexture>(
00155                     iDef["texture"]), tGuiPos(iDef["textureSize"][0],
00156                     iDef["textureSize"][1] ) );
00157             else texture( cResourceManager::get<cGLTexture>(
00158                 iDef["texture"]) );
00159         }
00160         colourInherit( iDef["colourInherit"] );
00161         hRepeat( iDef["hRepeat"]==1 );
00162         vRepeat( iDef["vRepeat"]==1 );
00163 
00164         hInvert( iDef["hInvert"]==1 );
00165         vInvert( iDef["vInvert"]==1 );
00166 
00167         if (iDef.keyExistsAsArray("padding") && iDef["padding"].size()==2) {
00168             padding( tGuiPos( iDef["padding"][0][0],
00169                 iDef["padding"][0][1] ), tGuiPos( iDef["padding"][1][0],
00170                 iDef["padding"][1][1] ) );
00171 
00172         } else if (iDef.keyExistsAsArray("padding")) {
00173             padding( tGuiPos(iDef["padding"][0],iDef["padding"][1] ),
00174                 tGuiPos(iDef["padding"][2],iDef["padding"][3] ) );
00175 
00176         } else if (iDef.keyExists("padding")) {
00177             const tFloat Padding = iDef["padding"];
00178             padding( tGuiPos(Padding,Padding), tGuiPos(Padding,Padding) );
00179         }
00180     }
00181 
00182 
00183 
00184     /**************************************************************************/
00185     cGuiFill & cGuiFill::operator =( const cGuiFill & iFill )
00186     {
00187         mHRepeat = iFill.mHRepeat;
00188         mVRepeat = iFill.mVRepeat;
00189         mHInvert = iFill.mHInvert;
00190         mVInvert = iFill.mVInvert;
00191         mColourInherit = iFill.mColourInherit;
00192         mBlending = iFill.mBlending;
00193         mColour = iFill.mColour;
00194         if (mCornerColours)
00195             delete []mCornerColours;
00196         mTexture = iFill.mTexture;
00197         mTexSize = iFill.mTexSize;
00198         if (iFill.mCornerColours)
00199             colour( iFill.mCornerColours[0],
00200                     iFill.mCornerColours[1],
00201                     iFill.mCornerColours[2],
00202                     iFill.mCornerColours[3] );
00203 
00204         mTLPad = iFill.mTLPad;
00205         mBRPad = iFill.mBRPad;
00206 
00207         return *this;
00208     }
00209 
00210     /**************************************************************************/
00211     void cGuiFill::prop( const tString &iName, const cDynVar &iVal )
00212     {
00213         if (iName == "texture") {
00214             if (!iVal) texture(0);
00215             else texture( cResourceManager::get<cGLTexture>(iVal) );
00216 
00217         } else if (iName == "textureSize") {
00218             if (!iVal) textureSize( tGuiPos(1.0f,1.0f) );
00219             textureSize( tGuiPos(iVal[0],iVal[1]) );
00220 
00221         } else if (iName == "colour") {
00222             colour( iVal );
00223 
00224         } else if (iName == "cornerColours")
00225             colour( iVal[0], iVal[1], iVal[2], iVal[3] );
00226         
00227     }
00228 
00229     /**************************************************************************/
00230     const cDynVar cGuiFill::prop( const tString &iName ) const
00231     {
00232         cDynVar oVal = cDynVar::Null;
00233         if (iName == "colour") {
00234             oVal = cDynVar::Array;
00235             oVal.insert(mColour.r());
00236             oVal.insert(mColour.g());
00237             oVal.insert(mColour.b());
00238             oVal.insert(mColour.a());
00239 
00240         } else if (iName == "cornerColours" && mCornerColours) {
00241             // I hate my life.
00242             oVal = cDynVar::Array;
00243             for (tUint i=0; i!=4; ++i) {
00244                 oVal[i] = cDynVar::Array;
00245                 cDynVar &tmp = oVal[i];
00246                 tmp.insert(mCornerColours[i].r());
00247                 tmp.insert(mCornerColours[i].g());
00248                 tmp.insert(mCornerColours[i].b());
00249                 tmp.insert(mCornerColours[i].a());
00250             }
00251         }
00252         return oVal;
00253     }
00254 
00255     /**************************************************************************/
00256     cGuiFill::~cGuiFill()
00257     {
00258         if (mCornerColours)
00259             delete []mCornerColours;
00260     }
00261 
00262     /**************************************************************************/
00263     void cGuiFill::texture( const cAutoPtr<const cGLTexture> & i_iTexture,
00264                             const tGuiPos & iTexSize )
00265     {
00266         mTexture = i_iTexture;
00267         mTexSize = iTexSize;
00268         testBlending();
00269     }
00270 
00271     /**************************************************************************/
00272     void cGuiFill::textureSize( const tGuiPos &iTexSize )
00273     {
00274         mTexSize = iTexSize;
00275     }
00276 
00277     /**************************************************************************/
00278     void cGuiFill::colour( const tGuiColour &iColour )
00279     {
00280         if (mCornerColours) {
00281             delete []mCornerColours;
00282             mCornerColours = 0;
00283         }
00284         mColour = iColour;
00285         testBlending();
00286     }
00287 
00288 
00289     /**************************************************************************/
00290     void cGuiFill::colour(  const tGuiColour & iTLColour,
00291                             const tGuiColour & iTRColour,
00292                             const tGuiColour & iBRColour,
00293                             const tGuiColour & iBLColour )
00294     {
00295         if (!mCornerColours)
00296             mCornerColours = new tGuiColour[4];
00297         mCornerColours[0] = iTLColour;
00298         mCornerColours[1] = iTRColour;
00299         mCornerColours[2] = iBRColour;
00300         mCornerColours[3] = iBLColour;
00301         testBlending();
00302     }
00303 
00304     /**************************************************************************/
00305     void cGuiFill::colourInherit( const tBool iValue )
00306     {
00307         mColourInherit = iValue;
00308         testBlending();
00309     }
00310 
00311 
00312     /**************************************************************************/
00313     void cGuiFill::hRepeat( const tBool iRepeat )
00314     {
00315         mHRepeat = iRepeat;
00316     }
00317 
00318 
00319     /**************************************************************************/
00320     void cGuiFill::vRepeat( const tBool iRepeat )
00321     {
00322         mVRepeat = iRepeat;
00323     }
00324 
00325 
00326     /**************************************************************************/
00327     void cGuiFill::hInvert( const tBool iInvert )
00328     {
00329         mHInvert = iInvert;
00330     }
00331 
00332 
00333     /**************************************************************************/
00334     void cGuiFill::vInvert( const tBool iInvert )
00335     {
00336         mVInvert = iInvert;
00337     }
00338 
00339     /**************************************************************************/
00340     void cGuiFill::padding( const tGuiPos &iTLPad, const tGuiPos &iBRPad )
00341     {
00342         mTLPad = iTLPad;
00343         mBRPad = iBRPad;
00344     }
00345 
00346     /**************************************************************************/
00347     void cGuiFill::draw( const tGuiPos &iPos, const tGuiPos &iSize ) const
00348     {
00349         glPushAttrib( GL_ALL_ATTRIB_BITS );
00350 
00351         switch (mBlending) {
00352             case Blending_Enable:
00353                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00354                 glEnable( GL_BLEND );
00355                 break;
00356 
00357             case Blending_Disable:
00358                 glDisable( GL_BLEND );
00359                 break;
00360 
00361             default:
00362                 break;
00363         }
00364         tGuiPos adjSize(
00365             mHRepeat?iSize.x():iSize.x()*mTexSize.x(),
00366             mVRepeat?iSize.y():iSize.y()*mTexSize.y() );
00367 
00368         const tGuiPos Pos( iPos+mTLPad );
00369         const tGuiPos Pos2( iPos+adjSize-mBRPad );
00370 
00371         tGuiPos texCoordMin( 0.0f, 0.0f );
00372         tGuiPos texCoordMax( 1.0f, 1.0f );
00373 
00374         if (mHRepeat) texCoordMax.x( iSize.x()/mTexSize.x() );
00375         if (mVRepeat) texCoordMax.y( iSize.y()/mTexSize.y() );
00376         if (mHInvert) n2l_swap( texCoordMin.x(), texCoordMax.x() );
00377         if (mVInvert) n2l_swap( texCoordMin.y(), texCoordMax.y() );
00378 
00379         // Set the colour
00380         if (!mColourInherit) mColour.glColour4();
00381 
00382         // Turn on texturing if we need it.
00383         if (mTexture) {
00384             glEnable( GL_TEXTURE_2D );
00385             mTexture->bind();
00386         } else {
00387             glDisable( GL_TEXTURE_2D );
00388         }
00389 
00390         if (mCornerColours && !mColourInherit) {
00391             glBegin( GL_QUADS );
00392                 glTexCoord2f( texCoordMin.x(), texCoordMin.y() );
00393                 mCornerColours[0].glColour4();
00394                 Pos.glVertex3f();
00395 
00396                 glTexCoord2f( texCoordMin.x(), texCoordMax.y() );
00397                 mCornerColours[3].glColour4();
00398                 glVertex3f( Pos.x(), Pos2.y(), 0.0f );
00399 
00400                 glTexCoord2f( texCoordMax.x(),  texCoordMax.y() );
00401                 mCornerColours[2].glColour4();
00402                 Pos2.glVertex3f();
00403 
00404                 glTexCoord2f( texCoordMax.x(),  texCoordMin.y() );
00405                 mCornerColours[1].glColour4();
00406                 glVertex3f( Pos2.x(), Pos.y(), 0.0f );
00407             glEnd();
00408         } else {
00409             // Not corner colours or inheriting
00410             glBegin( GL_QUADS );
00411                 glTexCoord2f( texCoordMin.x(), texCoordMin.y() );
00412                 Pos.glVertex3f();
00413 
00414                 glTexCoord2f( texCoordMin.x(), texCoordMax.y() );
00415                 glVertex3f( Pos.x(), Pos2.y(), 0.0f );
00416 
00417                 glTexCoord2f( texCoordMax.x(),  texCoordMax.y() );
00418                 Pos2.glVertex3f();
00419                 
00420                 glTexCoord2f( texCoordMax.x(),  texCoordMin.y() );
00421                 glVertex3f( Pos2.x(), Pos.y(), 0.0f );
00422             glEnd();
00423         }
00424 
00425         glPopAttrib();
00426     }
00427 
00428 
00429     /**************************************************************************/
00430     void cGuiFill::testBlending()
00431     {
00432         if (mTexture && mTexture->hasAlpha()) {
00433             mBlending = Blending_Enable;
00434             return;
00435         }
00436         
00437         if (mColourInherit) {
00438             // There's no way to know so by not explicitly
00439             // enabling blending, we won't touch the value
00440             // and it'll inherit
00441             mBlending = Blending_Inherit;
00442             return;
00443         }
00444 
00445         // The colour is explicit, we have to check them.
00446         if (mCornerColours && (mCornerColours[0].a()<1.0f ||
00447             mCornerColours[1].a()<1.0f || mCornerColours[2].a()<1.0f ||
00448             mCornerColours[3].a()<1.0f) ) {
00449             mBlending = Blending_Enable;
00450             return;
00451         }
00452 
00453         if (mColour.a()<1.0f) {
00454             mBlending = Blending_Enable;
00455             return;
00456         }
00457 
00458         mBlending = Blending_Disable;
00459         return;
00460     }
00461 
00462 } // namespace n2l
00463 
©2012 Aaron Cameron