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

CATSwitch.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATSwitch.cpp
00003 /// \brief On/Off switch 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 "CATSwitch.h"
00017 #include "CATApp.h"
00018 #include "CATWindow.h"
00019 //---------------------------------------------------------------------------
00020 // CATSwitch constructor (inherited from CATControl->CATXMLObject)
00021 // \param element - Type name ("Switch")
00022 // \param attribs - attribute information for the window
00023 // \param parent - parent XML object (should be a "Window" element)
00024 // \param rootDir - root directory of skin for bin/png loads
00025 //---------------------------------------------------------------------------
00026 CATSwitch::CATSwitch(  const CATString&             element, 
00027                        const CATString&             rootDir)
00028 : CATControl(element,  rootDir)
00029 {
00030     fValue               = 0.0f;  
00031     fImageOn             = 0;
00032     fImageDisabledOn     = 0;
00033     fImagePressedOn      = 0;
00034     fImageFocusOn        = 0;
00035     fImageActiveOn       = 0;
00036 }
00037 //---------------------------------------------------------------------------
00038 /// CATSwitch destructor
00039 //---------------------------------------------------------------------------
00040 CATSwitch::~CATSwitch()
00041 {
00042     if (fImageOn)
00043         CATImage::ReleaseImage(fImageOn);
00044     if (fImageDisabledOn)
00045         CATImage::ReleaseImage(fImageDisabledOn);
00046     if (fImagePressedOn)
00047         CATImage::ReleaseImage(fImagePressedOn);
00048     if (fImageFocusOn)
00049         CATImage::ReleaseImage(fImageFocusOn);
00050     if (fImageActiveOn)
00051         CATImage::ReleaseImage(fImageActiveOn);
00052 }
00053 
00054 void CATSwitch::OnMouseClick()
00055 {
00056     if (fValue < 0.5f)
00057     {
00058         fValue = 1.0f;
00059     }
00060     else
00061     {
00062         fValue = 0.0f;
00063     }
00064 
00065     CATControl::OnMouseClick();
00066 }
00067 
00068 /// ParseAttributes() parses the known attributes for an object.
00069 CATResult CATSwitch::ParseAttributes()
00070 {
00071     CATResult result = CATControl::ParseAttributes();
00072     CATString attrib;
00073     CATResult tmpResult;
00074 
00075     attrib = GetAttribute(L"ImageOn");
00076     if (!attrib.IsEmpty())
00077     {
00078         tmpResult = LoadSkinImage(attrib,fImageOn);
00079         if (CATFAILED(tmpResult))
00080             result = tmpResult;
00081     }
00082 
00083     attrib = GetAttribute(L"ImageDisabled");
00084     if (!attrib.IsEmpty())
00085     {
00086         tmpResult = LoadSkinImage(attrib, fImageDisabledOn);
00087         if (CATFAILED(tmpResult))
00088             result = tmpResult;
00089     }   
00090 
00091     attrib = GetAttribute(L"ImageFocusOn");
00092     if (!attrib.IsEmpty())
00093     {
00094         tmpResult = LoadSkinImage(attrib, fImageFocusOn);
00095         if (CATFAILED(tmpResult))
00096             result = tmpResult;
00097     }   
00098 
00099     attrib = GetAttribute(L"ImageActiveOn");
00100     if (!attrib.IsEmpty())
00101     {
00102         tmpResult = LoadSkinImage(attrib, fImageActiveOn);
00103         if (CATFAILED(tmpResult))
00104             result = tmpResult;
00105     }   
00106 
00107     attrib = GetAttribute(L"ImagePressedOn");
00108     if (!attrib.IsEmpty())
00109     {
00110         tmpResult = LoadSkinImage(attrib, fImagePressedOn);
00111         if (CATFAILED(tmpResult))
00112             result = tmpResult;
00113     }   
00114 
00115     return result;
00116 }
00117 
00118 //---------------------------------------------------------------------------
00119 // Draw() draws the control into the parent window
00120 // \param dirtyRect - portion of control (in window coordinates)
00121 //        that requires redrawing.
00122 //---------------------------------------------------------------------------
00123 void CATSwitch::Draw(CATImage* image, const CATRect& dirtyRect)
00124 {
00125     if (this->IsVisible() == false)
00126     {
00127         return;
00128     }
00129 
00130     // sanity check parent image / dirty rectangle
00131     CATRect imgRect(0,0,image->Width(), image->Height());
00132     CATASSERT(imgRect.Inside(dirtyRect), "Update rect is outside of img rect!");
00133 
00134     // Find intersection between dirty rect and us
00135     CATRect drawRect;
00136     bool   drawn = false;
00137 
00138     CATImage* disabled = fImageDisabled;
00139     CATImage* normal   = fImage;
00140     CATImage* pressed  = fImagePressed;
00141     CATImage* focus    = fImageFocus;
00142     CATImage* active   = fImageActive;
00143 
00144     if (fValue > 0.5)
00145     {
00146         // All on...
00147         if (fImageDisabledOn)
00148             disabled    = fImageDisabledOn;
00149 
00150         if (fImageOn)
00151             normal      = fImageOn;
00152 
00153         if (fImagePressedOn)
00154             pressed     = fImagePressedOn;
00155 
00156         if (fImageFocusOn)
00157             focus       = fImageFocusOn;
00158 
00159         if (fImageActiveOn)
00160             active      = fImageActiveOn;
00161     }
00162 
00163 
00164     // Gracefully degrade depending on flags and whether the images are
00165     // available.
00166     if (this->fRect.Intersect(dirtyRect, &drawRect))
00167     {  
00168         CATRect ourRect;
00169         if ( (this->IsEnabled() == false) && (disabled))
00170         {
00171             if (drawRect.Intersect(CATRect(fRect.left, 
00172                 fRect.top, 
00173                 fRect.left + disabled->Width(),
00174                 fRect.top  + disabled->Height()),
00175                 &ourRect))
00176             {
00177                 ourRect.Offset(-fRect.left, -fRect.top);
00178 
00179                 image->Overlay(   disabled,
00180                     drawRect.left, 
00181                     drawRect.top, 
00182                     ourRect.left,
00183                     ourRect.top,
00184                     ourRect.Width(),
00185                     ourRect.Height());
00186                 drawn = true;
00187             }        
00188         }
00189         else 
00190         {
00191             if (this->IsPressed() && (pressed))
00192             {
00193                 if (drawRect.Intersect(CATRect(fRect.left, 
00194                                                fRect.top, 
00195                                                fRect.left + pressed->Width(),
00196                                                fRect.top  + pressed->Height()),
00197                                        &ourRect))
00198                 {
00199                     ourRect.Offset(-fRect.left, -fRect.top);
00200 
00201                     image->Overlay( pressed,
00202                                     drawRect.left,
00203                                     drawRect.top,
00204                                     ourRect.left,
00205                                     ourRect.top,
00206                                     ourRect.Width(),
00207                                     ourRect.Height());
00208                     drawn = true;
00209                 }
00210             }
00211 
00212             if ((!drawn) && (IsFocused() || IsPressed()) && (focus))
00213             {
00214                 if (drawRect.Intersect(CATRect( fRect.left, 
00215                                                 fRect.top, 
00216                                                 fRect.left + focus->Width(),
00217                                                 fRect.top  + focus->Height()),
00218                                        &ourRect))
00219                 {
00220                     ourRect.Offset(-fRect.left, -fRect.top);
00221 
00222                     image->Overlay( focus,
00223                                     drawRect.left,
00224                                     drawRect.top,
00225                                     ourRect.left,
00226                                     ourRect.top,
00227                                     ourRect.Width(),
00228                                     ourRect.Height());
00229                     drawn = true;
00230                 }
00231             }
00232 
00233             if ((!drawn) && (IsActive()) && (active))
00234             {
00235                 if (drawRect.Intersect(CATRect( fRect.left, 
00236                                                 fRect.top, 
00237                                                 fRect.left + active->Width(),
00238                                                 fRect.top  + active->Height()),
00239                                        &ourRect))
00240                 {
00241                     ourRect.Offset(-fRect.left, -fRect.top);
00242 
00243                     image->Overlay( active,
00244                                     drawRect.left,
00245                                     drawRect.top,
00246                                     ourRect.left,
00247                                     ourRect.top,
00248                                     ourRect.Width(),
00249                                     ourRect.Height());
00250                     drawn = true;
00251                 }
00252             }
00253         }
00254 
00255         if ((!drawn) && (normal != 0))
00256         {
00257             if (drawRect.Intersect(CATRect(fRect.left, 
00258                 fRect.top, 
00259                 fRect.left + normal->Width(),
00260                 fRect.top  + normal->Height()),
00261                 &ourRect))
00262             {
00263                 ourRect.Offset(-fRect.left, -fRect.top);
00264 
00265                 image->Overlay(   normal,
00266                     drawRect.left,
00267                     drawRect.top,
00268                     ourRect.left,
00269                     ourRect.top,
00270                     ourRect.Width(),
00271                     ourRect.Height());
00272                 drawn = true;
00273             }
00274         }
00275 
00276         if (!drawn)
00277         {
00278 
00279             // Right now, just make a big red rectangle where we should go.
00280             image->FillRect(drawRect, fBackgroundColor);
00281         }
00282     }
00283 }
00284 
00285 CATResult CATSwitch::Load(CATPROGRESSCB             progressCB ,
00286                           void*             progressParam,
00287                           CATFloat32                progMin,
00288                           CATFloat32                progMax)
00289 {
00290     CATResult result = CATControl::Load(progressCB, progressParam,progMin,progMax);
00291 
00292     if (CATFAILED(result))
00293     {
00294         return result;
00295     }
00296 
00297     // Sanity check images
00298     if (fImage != 0)
00299     {
00300         CATResult testResult = CAT_SUCCESS;
00301 
00302         if (fImageOn)
00303         {
00304             testResult = CheckImageSize(fImageOn);
00305             if (testResult != CAT_SUCCESS)
00306                 result = testResult;
00307         }
00308 
00309         if (fImageDisabledOn)
00310         {
00311             testResult = CheckImageSize(fImageDisabledOn);
00312             if (testResult != CAT_SUCCESS)
00313                 result = testResult;
00314         }
00315 
00316         if (fImagePressedOn)
00317         {
00318             testResult = CheckImageSize(fImagePressedOn);
00319             if (testResult != CAT_SUCCESS)
00320                 result = testResult;
00321         }
00322 
00323         if (fImageFocusOn)
00324         {
00325             testResult = CheckImageSize(fImageFocusOn);
00326             if (testResult != CAT_SUCCESS)
00327                 result = testResult;
00328         }
00329 
00330         if (fImageActiveOn)
00331         {
00332             testResult = CheckImageSize(fImageActiveOn);
00333             if (testResult != CAT_SUCCESS)
00334                 result = testResult;
00335         }
00336 
00337     }
00338 
00339     return result;
00340 }
00341 
00342 CATString CATSwitch::GetHint() const
00343 {
00344     CATString retString;
00345     retString = CATControl::GetHint();
00346     if (fShowHintValue)
00347     {
00348         if (this->fValue >= 0.5f)
00349         {
00350             retString << " ( On )";
00351         }
00352         else
00353         {
00354             retString << " ( Off )";
00355         }
00356     }
00357     return retString;
00358 }
00359 

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