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

CATLayer.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATLayer.cpp
00003 /// \brief GUI Layer object - contains sub-controls
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-23 01:43:25 -0600 (Wed, 23 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //---------------------------------------------------------------------------
00014 #include "CATLayer.h"
00015 #include "CATApp.h"
00016 #include "CATFileSystem.h"
00017 #include "CATStream.h"
00018 #include "CATEvent.h"
00019 #include "CATWindow.h"
00020 #include "CATEventDefs.h"
00021 
00022 //---------------------------------------------------------------------------
00023 // CATLayer constructor (inherited from CATGuiObj)
00024 // \param element - Type name (e.g. "Button", "Label", etc.)
00025 // \param attribs - attribute information for the window
00026 // \param parent - parent XML object (should be a "Window" element)
00027 //---------------------------------------------------------------------------
00028 CATLayer::CATLayer(  const CATString&             element, 
00029                    const CATString&               rootDir)
00030                    : CATWidget(element,  rootDir)
00031 {
00032 }
00033 
00034 //---------------------------------------------------------------------------
00035 // CATLayer destructor
00036 //---------------------------------------------------------------------------
00037 CATLayer::~CATLayer()
00038 {
00039 }
00040 
00041 /// ParseAttributes() parses the known attributes for an object.
00042 CATResult CATLayer::ParseAttributes()
00043 {
00044     return CATWidget::ParseAttributes();
00045 }
00046 
00047 
00048 
00049 //---------------------------------------------------------------------------
00050 CATControl* CATLayer::HitTest(const CATPOINT& point)
00051 {
00052     if (!fRect.InRect(point))
00053     {
00054         return 0;
00055     }
00056 
00057     CATPOINT layerPt = point;
00058     layerPt.x -= fRect.left;
00059     layerPt.y -= fRect.top;
00060     CATControl* finalHit = 0;
00061 
00062     CATUInt32 numControls = this->GetNumChildren();
00063     for (CATUInt32 i = 0; i < numControls; i++)
00064     {
00065         CATXMLObject* curChild = 0;
00066         if (0 != (curChild = GetChild(i)))
00067         {
00068             CATWidget* curControl = (CATWidget*)curChild;
00069             if (curControl->IsEnabled())
00070             {
00071                 // In case of overlapped, always use the last in order.
00072                 CATControl* hitControl = curControl->HitTest(layerPt);
00073                 if (hitControl)
00074                 {
00075                     finalHit = hitControl;
00076                 }
00077             }         
00078         }
00079     }
00080 
00081     return finalHit;
00082 }
00083 
00084 //---------------------------------------------------------------------------
00085 void CATLayer::Draw(CATImage* image, const CATRect& dirtyRect)
00086 {
00087     if (this->IsVisible() == false)
00088     {
00089         return;
00090     }
00091     // Layer offset
00092     CATRect layerRect;
00093     if (!fRect.Intersect( dirtyRect, &layerRect))
00094         return;
00095 
00096     layerRect.Offset( -fRect.left, -fRect.top);
00097 
00098     CATImage* subImage = 0;
00099 
00100     if (CATSUCCEEDED(CATImage::CreateSub( image, 
00101         subImage, 
00102         fRect.left, 
00103         fRect.top, 
00104         CATMin(image->Width() - fRect.left, fRect.Width()), 
00105         CATMin(image->Height() - fRect.top, fRect.Height()) )))
00106     {
00107         // Draw children
00108         CATXMLObject* curChild = 0;
00109         CATUInt32 index;
00110         CATUInt32 numChildren = this->GetNumChildren();
00111         for (index = 0; index < numChildren; index++)
00112         {
00113             if (0 != (curChild = GetChild(index)))
00114             {
00115                 CATWidget* curControl = (CATWidget*)curChild;
00116                 curControl->Draw(subImage,layerRect);         
00117             }
00118         }
00119 
00120         CATImage::ReleaseImage(subImage);
00121     }
00122 }
00123 //---------------------------------------------------------------------------
00124 void CATLayer::PostDraw(CATDRAWCONTEXT context, const CATRect& dirtyRect)
00125 {
00126     if (this->IsVisible() == false)
00127     {
00128         return;
00129     }
00130     // Offset rectangle
00131     CATRect layerRect;
00132     if (!fRect.Intersect( dirtyRect, &layerRect))
00133         return;
00134     layerRect.Offset( -fRect.left, -fRect.top);
00135 
00136     // Call children
00137     CATXMLObject* curChild = 0;
00138     CATUInt32 numChildren = this->GetNumChildren();
00139 
00140     for (CATUInt32 i = 0; i < numChildren; i++)
00141     {
00142         if (0 != (curChild = GetChild(i)))
00143         {
00144             CATWidget* curControl = (CATWidget*)curChild;
00145             curControl->PostDraw(context,layerRect);
00146         }
00147     }   
00148 }
00149 
00150 // OnParentCreate() is called when the parent window is created.
00151 // 
00152 // Most controls won't need this, but any that create their own
00153 // windows should do so at this point.
00154 void CATLayer::OnParentCreate()
00155 {
00156     CATXMLObject* curChild = 0;
00157     CATUInt32 numChildren = this->GetNumChildren();
00158 
00159     for (CATUInt32 i = 0; i < numChildren; i++)
00160     {
00161         if (0 != (curChild = GetChild(i)))
00162         {
00163             CATWidget* curControl = (CATWidget*)curChild;
00164             curControl->OnParentCreate();
00165         }
00166     }   
00167 }
00168 
00169 // OnParentDestroy() is called as the parent window is destroyed.
00170 //
00171 // Controls that create their own windows during OnParentCreate()
00172 // should destroy them during OnParentDestroy()
00173 void CATLayer::OnParentDestroy()
00174 {
00175     CATXMLObject* curChild = 0;
00176     CATUInt32 numChildren = this->GetNumChildren();
00177 
00178     for (CATUInt32 i = 0; i < numChildren; i++)
00179     {
00180         if (0 != (curChild = GetChild(i)))
00181         {
00182             CATWidget* curControl = (CATWidget*)curChild;
00183             curControl->OnParentDestroy();
00184         }
00185     }   
00186 }
00187 
00188 void CATLayer::SetEnabled(bool enabled)
00189 {
00190     CATWidget::SetEnabled(enabled);
00191 
00192     CATXMLObject* curChild = 0;
00193     CATUInt32 numChildren = this->GetNumChildren();
00194 
00195     for (CATUInt32 i = 0; i < numChildren; i++)
00196     {
00197         if (0 != (curChild = GetChild(i)))
00198         {
00199             CATWidget* curControl = (CATWidget*)curChild;
00200             CATInt32 evRes = 0;
00201             (void)curControl->OnEvent(CATEvent(CATEVENT_ENABLE_CHANGE),evRes);
00202         }
00203     }     
00204 }

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