Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CATGUI/CATProgress.cpp Source File

CATProgress.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATProgress.cpp
00003 /// \brief Progress bar for GUI
00004 /// \ingroup CATGUI
00005 ///
00006 /// Copyright (c) 2003-2008 by Michael Ellison.
00007 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00008 ///
00009 // $Author: mikeellison $
00010 // $Date: 2008-01-30 06:11:10 -0600 (Wed, 30 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //
00014 //
00015 //---------------------------------------------------------------------------
00016 #include "CATProgress.h"
00017 #include "CATApp.h"
00018 #include "CATWindow.h"
00019 
00020 //---------------------------------------------------------------------------
00021 // CATProgress constructor (inherited from CATControl->CATXMLObject)
00022 // \param element - Type name ("Progress")
00023 // \param attribs - attribute information for the window
00024 // \param parent - parent XML object (should be a "Window" element)
00025 // \param rootDir - root directory of skin for bin/png loads
00026 //---------------------------------------------------------------------------
00027 CATProgress::CATProgress(  const CATString&             element, 
00028                          const CATString&             rootDir)
00029                          : CATControl(element,  rootDir)
00030 {
00031     fImageOn = 0;
00032     fProgressStyle = CATPROGRESS_HORIZONTAL;
00033 }
00034 
00035 /// IsFocusable() returns true if the control can receive
00036 /// focus, and false otherwise.
00037 bool CATProgress::IsFocusable() const
00038 {
00039     return false;
00040 }
00041 //---------------------------------------------------------------------------
00042 /// CATProgress destructor
00043 //---------------------------------------------------------------------------
00044 CATProgress::~CATProgress()
00045 {
00046     if (fImageOn)
00047     {
00048         CATImage::ReleaseImage(fImageOn);
00049     }
00050 }
00051 
00052 /// ParseAttributes() parses the known attributes for an object.
00053 CATResult CATProgress::ParseAttributes()
00054 {
00055     CATResult result = CATControl::ParseAttributes();
00056     CATResult tmpResult;
00057     CATString attrib;
00058 
00059     attrib = GetAttribute(L"ImageOn");
00060     if (!attrib.IsEmpty())
00061     {
00062         tmpResult = LoadSkinImage(attrib,fImageOn);
00063         if (CATFAILED(tmpResult))
00064             result = tmpResult;
00065     }
00066 
00067     attrib = GetAttribute(L"ProgressStyle");
00068     if (!attrib.IsEmpty())
00069     {
00070         if ( (attrib.GetWChar(0) | (char)0x20) == 'v')
00071         {
00072             fProgressStyle = CATPROGRESS_VERTICAL;
00073         }
00074         else
00075         {
00076             fProgressStyle = CATPROGRESS_HORIZONTAL;
00077         }
00078     }
00079 
00080     return result;
00081 }
00082 
00083 //---------------------------------------------------------------------------
00084 // Draw() draws the control into the parent window
00085 // \param dirtyRect - portion of control (in window coordinates)
00086 //        that requires redrawing.
00087 //---------------------------------------------------------------------------
00088 void CATProgress::Draw(CATImage* image, const CATRect& dirtyRect)
00089 {
00090     if (this->IsVisible() == false)
00091     {
00092         return;
00093     }
00094 
00095     // sanity check parent image / dirty rectangle
00096     CATRect imgRect(0,0,image->Width(), image->Height());
00097     CATASSERT(imgRect.Inside(dirtyRect), "Update rect is outside of img rect!");
00098 
00099     // Find intersection between dirty rect and us
00100     CATRect drawRect;
00101     bool   drawn = false;
00102 
00103     // Gracefully degrade depending on flags and whether the images are
00104     // available.
00105     if (this->fRect.Intersect(dirtyRect, &drawRect))
00106     {  
00107         CATRect ourRect;
00108         if ( (this->IsEnabled() == false) && (this->fImageDisabled))
00109         {
00110             if (drawRect.Intersect(CATRect(fRect.left, 
00111                 fRect.top, 
00112                 fRect.left + this->fImageDisabled->Width(),
00113                 fRect.top  + this->fImageDisabled->Height()),
00114                 &ourRect))
00115             {
00116                 ourRect.Offset(-fRect.left, -fRect.top);
00117 
00118                 image->Overlay(   this->fImageDisabled,
00119                     drawRect.left, 
00120                     drawRect.top, 
00121                     ourRect.left,
00122                     ourRect.top,
00123                     ourRect.Width(),
00124                     ourRect.Height());
00125                 drawn = true;
00126             }        
00127         }
00128 
00129         if (!drawn)
00130         {
00131             CATRect normalRect = fRect;
00132             CATRect onRect = fRect;
00133 
00134             if (this->fImage != 0)
00135             {
00136                 if (drawRect.Intersect(normalRect,&ourRect))
00137                 {
00138                     ourRect.Offset(-fRect.left, -fRect.top);
00139 
00140                     image->Overlay(   this->fImage,
00141                         drawRect.left,
00142                         drawRect.top,
00143                         ourRect.left,
00144                         ourRect.top,
00145                         ourRect.Width(),
00146                         ourRect.Height());
00147                     drawn = true;
00148                 }
00149             }
00150 
00151 
00152             if (this->fImageOn != 0)
00153             {
00154                 CATRect tmpRect = drawRect;
00155                 CATFloat32 drawPercent = this->GetValPercent();
00156 
00157 
00158                 if (this->fProgressStyle == CATPROGRESS_HORIZONTAL)
00159                 {
00160                     onRect.right = (CATInt32)(onRect.left + 
00161                         ( CATMin(onRect.Width(), fImageOn->Width()) * drawPercent));
00162                 }
00163                 else
00164                 {
00165                     CATInt32 offset = (CATInt32)(fImageOn->Height() - (fImageOn->Height() * drawPercent));
00166                     onRect.top += offset;          
00167                     tmpRect.top += offset;
00168                     if (tmpRect.top > tmpRect.bottom)
00169                         tmpRect.top = tmpRect.bottom;
00170                 }
00171 
00172                 if (tmpRect.Intersect(onRect,&ourRect))
00173                 {
00174                     ourRect.Offset(-fRect.left, -fRect.top);
00175 
00176                     image->Overlay(   this->fImageOn,
00177                         tmpRect.left,
00178                         tmpRect.top,
00179                         ourRect.left,
00180                         ourRect.top,
00181                         ourRect.Width(),
00182                         ourRect.Height());
00183                     drawn = true;
00184                 }
00185             }
00186         }
00187 
00188         if (!drawn)
00189         {
00190 
00191             // Right now, just make a big red rectangle where we should go.
00192             image->FillRect(drawRect, fBackgroundColor);
00193         }
00194     }
00195 }
00196 //---------------------------------------------------------------------------
00197 CATResult CATProgress::Load(    CATPROGRESSCB               progressCB,
00198                             void*                           progressParam,
00199                             CATFloat32                      progMin,
00200                             CATFloat32                      progMax)
00201 {
00202     CATResult result = CATControl::Load(progressCB, progressParam, progMin, progMax);
00203 
00204     if (CATFAILED(result))
00205     {
00206         return result;
00207     }
00208 
00209     // Sanity check images
00210     if (fImage != 0)
00211     {
00212         CATResult testResult = CAT_SUCCESS;
00213 
00214         if (fImageOn)
00215         {
00216             testResult = CheckImageSize(fImageOn);
00217             if (testResult != CAT_SUCCESS)
00218                 result = testResult;
00219         }
00220     }
00221 
00222     return result;
00223 }
00224 
00225 /// SetImages() sets the images and resets the control
00226 void CATProgress::SetImages( CATImage* imageOn, CATImage* imageOff, CATImage* imageDisabled)
00227 {
00228     if (fImage)
00229     {
00230         CATImage::ReleaseImage(fImage);     
00231     }
00232 
00233     if (fImageOn)
00234     {
00235         CATImage::ReleaseImage(fImageOn);
00236     }
00237 
00238     if (fImageDisabled)
00239     {
00240         CATImage::ReleaseImage(fImageDisabled);
00241     }
00242 
00243     fImage = imageOff;
00244     fImageOn = imageOn;
00245     fImageDisabled = imageDisabled;
00246 
00247     this->RectFromAttribs();
00248     this->MarkDirty();
00249 }
00250 

Generated on Mon Feb 11 04:09:55 2008 for Game Accessibility Suite by doxygen 1.5.4