Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIRenderableImage.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIRenderableImage.cpp
00003         created:        17/4/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implementation of RenderableImage UI entity
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIRenderableImage.h"
00027 #include "CEGUIImage.h"
00028 #include "CEGUIExceptions.h"
00029 
00030 // Start of CEGUI namespace section
00031 namespace CEGUI
00032 {
00033 
00034 /*************************************************************************
00035         Constructor             
00036 *************************************************************************/
00037 RenderableImage::RenderableImage(void) :
00038         d_horzFormat(LeftAligned),
00039         d_vertFormat(TopAligned),
00040         d_quadSplitMode(TopLeftToBottomRight),
00041         d_image(NULL)
00042 {
00043 }
00044 
00045 
00046 /*************************************************************************
00047         Destructor
00048 *************************************************************************/
00049 RenderableImage::~RenderableImage(void)
00050 {
00051 }
00052 
00053 
00054 /*************************************************************************
00055         Renders the imagery for a RenderableImage element.
00056 *************************************************************************/
00057 void RenderableImage::draw_impl(const Vector3& position, const Rect& clip_rect) const
00058 {
00059         ColourRect final_colours;
00060 
00061         // do not draw anything if image is not set.
00062         if (d_image == NULL)
00063                 return;
00064 
00065         // calculate final clipping rect which is intersection of RenderableImage area and supplied clipping area
00066         Rect final_clipper(position.d_x, position.d_y, 0, 0);
00067         final_clipper.setSize(d_area.getSize());
00068         final_clipper = clip_rect.getIntersection(final_clipper);
00069 
00070         Size imgSize(d_image->getSize());
00071 
00072         // calculate number of times to tile image based of formatting options
00073         uint horzTiles = (d_horzFormat == HorzTiled) ? (uint)((d_area.getWidth() + (imgSize.d_width - 1)) / imgSize.d_width) : 1;
00074         uint vertTiles = (d_vertFormat == VertTiled) ? (uint)((d_area.getHeight() + (imgSize.d_height - 1)) / imgSize.d_height) : 1;
00075 
00076         // calculate 'base' X co-ordinate, depending upon formatting
00077         float baseX;
00078 
00079         switch (d_horzFormat)
00080         {
00081                 case HorzStretched:
00082                         imgSize.d_width = d_area.getWidth();
00083                         // intentional fall-through
00084 
00085                 case HorzTiled:
00086                 case LeftAligned:
00087                         baseX = position.d_x;
00088                         break;
00089 
00090                 case HorzCentred:
00091                         baseX = position.d_x + PixelAligned((d_area.getWidth() - imgSize.d_width) / 2);
00092                         break;
00093 
00094                 case RightAligned:
00095                         baseX = position.d_x + d_area.getWidth() - imgSize.d_width;
00096                         break;
00097 
00098                 default:
00099                         throw InvalidRequestException((utf8*)"An unknown horizontal formatting value was specified in a RenderableImage object.");
00100         }
00101 
00102         // calculate 'base' Y co-ordinate, depending upon formatting
00103         float baseY;
00104 
00105         switch (d_vertFormat)
00106         {
00107                 case VertStretched:
00108                         imgSize.d_height = d_area.getHeight();
00109                         // intentional fall-through
00110 
00111                 case VertTiled:
00112                 case TopAligned:
00113                         baseY = position.d_y;
00114                         break;
00115 
00116                 case VertCentred:
00117                         baseY = position.d_y + PixelAligned((d_area.getHeight() - imgSize.d_height) / 2);
00118                         break;
00119 
00120                 case BottomAligned:
00121                         baseY = position.d_y + d_area.getHeight() - imgSize.d_height;
00122                         break;
00123 
00124                 default:
00125                         throw InvalidRequestException((utf8*)"An unknown vertical formatting value was specified in a RenderableImage object.");
00126         }
00127 
00128         Vector3 drawpos(0,baseY, position.d_z);
00129 
00130         // perform actual rendering
00131         for (uint row = 0; row < vertTiles; ++row)
00132         {
00133                 drawpos.d_x = baseX;
00134 
00135                 for (uint col = 0; col < horzTiles; ++col)
00136                 {
00137                         if (d_useColoursPerImage)
00138                         {
00139                                 final_colours = d_colours;
00140                         }
00141                         else
00142                         {
00143                                 float leftfactor = (drawpos.d_x - baseX) / d_area.getWidth();
00144                                 float rightfactor = (drawpos.d_x + imgSize.d_width - baseX) / d_area.getWidth();
00145                                 float topfactor = (drawpos.d_y - baseY) / d_area.getHeight();
00146                                 float bottomfactor = (drawpos.d_y + imgSize.d_height - baseY) / d_area.getHeight();
00147                                 if( rightfactor > 1 ) rightfactor = 1;
00148                                 if( bottomfactor > 1 ) bottomfactor = 1;
00149 
00150                                 final_colours = d_colours.getSubRectangle( leftfactor, rightfactor, topfactor, bottomfactor);
00151                         }
00152 
00153                         d_image->draw(drawpos, imgSize, final_clipper, final_colours, d_quadSplitMode);
00154                         drawpos.d_x += imgSize.d_width;
00155                 }
00156 
00157                 drawpos.d_y += imgSize.d_height;
00158         }
00159 
00160 }
00161 
00162 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:07 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1