AaronCameron.net
I care not for your petty politics.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 12:34am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cGuiSEColourTrans.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  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 
00026 #include "cGuiSEColourTrans.h"
00027 
00028 #include "gui/cGuiElement.h"
00029 
00030 #include "n2l/vfs.h"
00031 
00032 namespace n2l
00033 {
00034     /**************************************************************************/
00035     cGuiSEColourTrans::cGuiSEColourTrans()
00036     {
00037         init();
00038     }
00039 
00040     /**************************************************************************/
00041     cGuiSEColourTrans::cGuiSEColourTrans( const cVfsNodeInterface &iNode )
00042     {
00043         init();
00044         load( iNode );
00045     }
00046 
00047     /**************************************************************************/
00048     cGuiSEColourTrans::cGuiSEColourTrans( const cDynVar &iDef )
00049     {
00050         init();
00051         load( iDef );
00052     }
00053 
00054     /**************************************************************************/
00055     cGuiSEColourTrans::~cGuiSEColourTrans()
00056     {
00057     }
00058 
00059     /**************************************************************************/
00060     void cGuiSEColourTrans::load( const cVfsNodeInterface &iNode )
00061     {
00062         const tUint DataOffset =
00063             vfsNodeFileWithHeader( iNode, "n2l::cGuiSEColourTrans");
00064 
00065         cDynVar def;
00066         def.unserialize( iNode.buffer().c_str()+DataOffset );
00067         load( def );
00068     }
00069 
00070     /**************************************************************************/
00071     void cGuiSEColourTrans::load( const cDynVar &iDef )
00072     {
00073         cGuiSEInt::load( iDef );
00074 
00075         if (iDef["colourCorners"]) {
00076             for (tUint i=0; i!=4; ++i) {
00077                 if (iDef.keyExists("fromColour")) {
00078                     mC1[i] = iDef["fromColour"][i];
00079                     mInheritC1 = false;
00080                 }
00081                 mC2[i] = iDef["toColour"][i];
00082             }
00083             mColourCorners = true;
00084             // Reset the default property.  It may still be overridden later
00085             mInnerKey = "colourCorners";
00086         } else {
00087             if (iDef.keyExists("fromColour")) {
00088                 mC1[0] = iDef["fromColour"];
00089                 mInheritC1 = false;
00090             }
00091             mC2[0] = iDef["toColour"];
00092         }
00093 
00094         mLife = iDef["time"];
00095 
00096         mProp = iDef.keyValueOr( "prop", mProp );
00097         mInnerKey = iDef.keyValueOr( "innerKey", mInnerKey );
00098     }
00099 
00100     /**************************************************************************/
00101     const tBool cGuiSEColourTrans::actOn( cGuiElement *const i_ioElement,
00102         const tUint iTimePassed )
00103     {
00104         if (!alive()) return false;
00105         mAge += iTimePassed;
00106         mAge = n2lMin(mAge,mLife);
00107 
00108         if (mFirstUpdate) {
00109             mFirstUpdate = false;
00110             // This is temporary, I would think.
00111             // It removes ambiguity in the prop call, but is really,
00112             // really unobvious.
00113             const cGuiElement *const tmpElement = i_ioElement;
00114             if (mInheritC1) {
00115                 if (mColourCorners) {
00116                     cDynVar tmp = tmpElement->prop( mProp, mInnerKey );
00117                     for (tUint i=0; i!=4; ++i)
00118                         mC1[i] = tmp[i];
00119                 } else
00120                     mC1[0] = tmpElement->prop( mProp, mInnerKey );
00121             }
00122         }
00123 
00124         if (mColourCorners) {
00125             const tFloat PCent = mAge/tFloat(mLife);
00126             cColour tmpCol[4] = { mC2[0], mC2[1], mC2[2], mC2[3] };
00127 
00128             cDynVar tmpDef = cDynVar::Array;
00129             for (tUint i=0; i!=4; ++i) {
00130                 tmpCol[i] -= mC1[i];
00131                 tmpCol[i] *= PCent;
00132                 tmpCol[i] += mC1[i];
00133 
00134                 // Don't hate me.  I'm just lazy.
00135                 tmpDef[i] = cDynVar::Array;
00136                 tmpDef[i][0] = tmpCol[i].r();
00137                 tmpDef[i][1] = tmpCol[i].g();
00138                 tmpDef[i][2] = tmpCol[i].b();
00139                 tmpDef[i][3] = tmpCol[i].a();
00140             }
00141             
00142             i_ioElement->prop( mProp, tmpDef, mInnerKey );
00143 
00144         } else {
00145             cColour tmpCol = mC2[0];
00146             tmpCol -= mC1[0];
00147             tmpCol *= mAge/tFloat(mLife);
00148             tmpCol += mC1[0];
00149 
00150             // No.  I'm not proud of this.
00151             cDynVar tmpDef = cDynVar::Array;
00152             tmpDef[0] = tmpCol.r();
00153             tmpDef[1] = tmpCol.g();
00154             tmpDef[2] = tmpCol.b();
00155             tmpDef[3] = tmpCol.a();
00156 
00157             i_ioElement->prop( mProp, tmpDef, mInnerKey );
00158         }
00159         
00160         return alive(mAge!=mLife);
00161     }
00162 
00163 
00164     /**************************************************************************/
00165     const cAutoPtr<cGuiSEInt> 
00166         cGuiSEColourTrans::clone() const
00167     {
00168         cAutoPtr<cGuiSEColourTrans> tmp = new cGuiSEColourTrans;
00169         cloneInto( tmp );
00170         return tmp;
00171     }
00172 
00173     /**************************************************************************/
00174     void cGuiSEColourTrans::cloneInto(
00175         const cAutoPtr<cGuiSEColourTrans> &ioElement ) const
00176     {
00177         cGuiSEInt::cloneInto( ioElement );
00178         
00179         ioElement->mC1 = mC1;
00180         ioElement->mC2 = mC2;
00181         ioElement->mColourCorners = mColourCorners;
00182         ioElement->mInheritC1 = mInheritC1;
00183         ioElement->mLife = mLife;
00184         ioElement->mAge = mAge;
00185         ioElement->mProp = mProp;
00186         ioElement->mInnerKey = mInnerKey;
00187 
00188         ioElement->mFirstUpdate = true;
00189     }
00190 
00191     /**************************************************************************/
00192     void cGuiSEColourTrans::init()
00193     {
00194         mLife = 1000;
00195         mAge = 0;
00196         mProp = "colour";
00197         mColourCorners = false;
00198         mFirstUpdate = true;
00199         mInheritC1 = true;
00200         mC1.reserve(4);
00201         mC2.reserve(4);
00202         for (tUint i=0; i!=4; ++i) {
00203             mC1.push_back( cColour() );
00204             mC2.push_back( cColour() );
00205         }
00206     }
00207 }
©2012 Aaron Cameron