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

CEGUIFalImageryComponent.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002     filename:   CEGUIFalImageryComponent.cpp
00003     created:    Mon Jun 13 2005
00004     author:     Paul D Turner <paul@cegui.org.uk>
00005 *************************************************************************/
00006 /*************************************************************************
00007     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00008     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00009  
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2.1 of the License, or (at your option) any later version.
00014  
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019  
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 *************************************************************************/
00024 #include "falagard/CEGUIFalImageryComponent.h"
00025 #include "falagard/CEGUIFalXMLEnumHelper.h"
00026 #include "CEGUIImage.h"
00027 #include "CEGUIExceptions.h"
00028 #include "CEGUIImagesetManager.h"
00029 #include "CEGUIImageset.h"
00030 #include "CEGUIPropertyHelper.h"
00031 #include <iostream>
00032 
00033 // void draw(const Rect& dest_rect, float z, const Rect& clip_rect,const ColourRect& colours);
00034 
00035 // Start of CEGUI namespace section
00036 namespace CEGUI
00037 {
00038     ImageryComponent::ImageryComponent() :
00039         d_image(0),
00040         d_vertFormatting(VF_TOP_ALIGNED),
00041         d_horzFormatting(HF_LEFT_ALIGNED)
00042     {}
00043 
00044     const Image* ImageryComponent::getImage() const
00045     {
00046         return d_image;
00047     }
00048 
00049     void ImageryComponent::setImage(const Image* image)
00050     {
00051         d_image = image;
00052     }
00053 
00054     void ImageryComponent::setImage(const String& imageset, const String& image)
00055     {
00056         try
00057         {
00058             d_image = &ImagesetManager::getSingleton().getImageset(imageset)->getImage(image);
00059         }
00060         catch (UnknownObjectException)
00061         {
00062             d_image = 0;
00063         }
00064     }
00065 
00066     VerticalFormatting ImageryComponent::getVerticalFormatting() const
00067     {
00068         return d_vertFormatting;
00069     }
00070 
00071     void ImageryComponent::setVerticalFormatting(VerticalFormatting fmt)
00072     {
00073         d_vertFormatting = fmt;
00074     }
00075 
00076     HorizontalFormatting ImageryComponent::getHorizontalFormatting() const
00077     {
00078         return d_horzFormatting;
00079     }
00080 
00081     void ImageryComponent::setHorizontalFormatting(HorizontalFormatting fmt)
00082     {
00083         d_horzFormatting = fmt;
00084     }
00085 
00086     void ImageryComponent::render_impl(Window& srcWindow, Rect& destRect, float base_z, const CEGUI::ColourRect* modColours, const Rect* clipper, bool clipToDisplay) const
00087     {
00088         // get final image to use.
00089         const Image* img = isImageFetchedFromProperty() ?
00090             PropertyHelper::stringToImage(srcWindow.getProperty(d_imagePropertyName)) :
00091             d_image;
00092 
00093         // do not draw anything if image is not set.
00094         if (!img)
00095             return;
00096 
00097         HorizontalFormatting horzFormatting = d_horzFormatPropertyName.empty() ? d_horzFormatting :
00098             FalagardXMLHelper::stringToHorzFormat(srcWindow.getProperty(d_horzFormatPropertyName));
00099 
00100         VerticalFormatting vertFormatting = d_vertFormatPropertyName.empty() ? d_vertFormatting :
00101             FalagardXMLHelper::stringToVertFormat(srcWindow.getProperty(d_vertFormatPropertyName));
00102 
00103         uint horzTiles, vertTiles;
00104         float xpos, ypos;
00105 
00106         Size imgSz(img->getSize());
00107 
00108         // calculate final colours to be used
00109         ColourRect finalColours;
00110         initColoursRect(srcWindow, modColours, finalColours);
00111 
00112         // calculate initial x co-ordinate and horizontal tile count according to formatting options
00113         switch (horzFormatting)
00114         {
00115             case HF_STRETCHED:
00116                 imgSz.d_width = destRect.getWidth();
00117                 xpos = destRect.d_left;
00118                 horzTiles = 1;
00119                 break;
00120 
00121             case HF_TILED:
00122                 xpos = destRect.d_left;
00123                 horzTiles = (uint)((destRect.getWidth() + (imgSz.d_width - 1)) / imgSz.d_width);
00124                 break;
00125 
00126             case HF_LEFT_ALIGNED:
00127                 xpos = destRect.d_left;
00128                 horzTiles = 1;
00129                 break;
00130 
00131             case HF_CENTRE_ALIGNED:
00132                 xpos = destRect.d_left + PixelAligned((destRect.getWidth() - imgSz.d_width) * 0.5f);
00133                 horzTiles = 1;
00134                 break;
00135 
00136             case HF_RIGHT_ALIGNED:
00137                 xpos = destRect.d_right - imgSz.d_width;
00138                 horzTiles = 1;
00139                 break;
00140 
00141             default:
00142                 throw InvalidRequestException("ImageryComponent::render - An unknown HorizontalFormatting value was specified.");
00143         }
00144 
00145         // calculate initial y co-ordinate and vertical tile count according to formatting options
00146         switch (vertFormatting)
00147         {
00148             case VF_STRETCHED:
00149                 imgSz.d_height = destRect.getHeight();
00150                 ypos = destRect.d_top;
00151                 vertTiles = 1;
00152                 break;
00153 
00154             case VF_TILED:
00155                 ypos = destRect.d_top;
00156                 vertTiles = (uint)((destRect.getHeight() + (imgSz.d_height - 1)) / imgSz.d_height);
00157                 break;
00158 
00159             case VF_TOP_ALIGNED:
00160                 ypos = destRect.d_top;
00161                 vertTiles = 1;
00162                 break;
00163 
00164             case VF_CENTRE_ALIGNED:
00165                 ypos = destRect.d_top + PixelAligned((destRect.getHeight() - imgSz.d_height) * 0.5f);
00166                 vertTiles = 1;
00167                 break;
00168 
00169             case VF_BOTTOM_ALIGNED:
00170                 ypos = destRect.d_bottom - imgSz.d_height;
00171                 vertTiles = 1;
00172                 break;
00173 
00174             default:
00175                 throw InvalidRequestException("ImageryComponent::render - An unknown VerticalFormatting value was specified.");
00176         }
00177 
00178         // perform final rendering (actually is now a caching of the images which will be drawn)
00179         Rect finalRect;
00180         Rect finalClipper;
00181         const Rect* clippingRect;
00182         finalRect.d_top = ypos;
00183         finalRect.d_bottom = ypos + imgSz.d_height;
00184 
00185         for (uint row = 0; row < vertTiles; ++row)
00186         {
00187             finalRect.d_left = xpos;
00188             finalRect.d_right = xpos + imgSz.d_width;
00189 
00190             for (uint col = 0; col < horzTiles; ++col)
00191             {
00192                 // use custom clipping for right and bottom edges when tiling the imagery
00193                 if (((vertFormatting == VF_TILED) && row == vertTiles - 1) ||
00194                     ((horzFormatting == HF_TILED) && col == horzTiles - 1))
00195                 {
00196                     finalClipper = clipper ? clipper->getIntersection(destRect) : destRect;
00197                     clippingRect = &finalClipper;
00198                 }
00199                 // not tiliing, or not on far edges, just used passed in clipper (if any).
00200                 else
00201                 {
00202                     clippingRect = clipper;
00203                 }
00204 
00205                 // add image to the rendering cache for the target window.
00206                 srcWindow.getRenderCache().cacheImage(*img, finalRect, base_z, finalColours, clippingRect, clipToDisplay);
00207 
00208                 finalRect.d_left += imgSz.d_width;
00209                 finalRect.d_right += imgSz.d_width;
00210             }
00211 
00212             finalRect.d_top += imgSz.d_height;
00213             finalRect.d_bottom += imgSz.d_height;
00214         }
00215     }
00216 
00217     void ImageryComponent::writeXMLToStream(OutStream& out_stream) const
00218     {
00219         // opening tag
00220         out_stream << "<ImageryComponent>" << std::endl;
00221         // write out area
00222         d_area.writeXMLToStream(out_stream);
00223 
00224         // write image
00225         if (isImageFetchedFromProperty())
00226             out_stream << "<ImageProperty name=\"" << d_imagePropertyName << "\" />" << std::endl;
00227         else
00228             out_stream << "<Image imageset=\"" << d_image->getImagesetName() << "\" image=\"" << d_image->getName() << "\" />" << std::endl;
00229 
00230         // get base class to write colours
00231         writeColoursXML(out_stream);
00232 
00233         // write vert format, allowing base class to do this for us if a propety is in use
00234         if (!writeVertFormatXML(out_stream))
00235         {
00236             // was not a property, so write out explicit formatting in use
00237             out_stream << "<VertFormat type=\"" << FalagardXMLHelper::vertFormatToString(d_vertFormatting) << "\" />" << std::endl;
00238         }
00239 
00240         // write horz format, allowing base class to do this for us if a propety is in use
00241         if (!writeHorzFormatXML(out_stream))
00242         {
00243             // was not a property, so write out explicit formatting in use
00244             out_stream << "<HorzFormat type=\"" << FalagardXMLHelper::horzFormatToString(d_horzFormatting) << "\" />" << std::endl;
00245         }
00246 
00247         // closing tag
00248         out_stream << "</ImageryComponent>" << std::endl;
00249     }
00250 
00251     bool ImageryComponent::isImageFetchedFromProperty() const
00252     {
00253         return !d_imagePropertyName.empty();
00254     }
00255 
00256     const String& ImageryComponent::getImagePropertySource() const
00257     {
00258         return d_imagePropertyName;
00259     }
00260 
00261     void ImageryComponent::setImagePropertySource(const String& property)
00262     {
00263         d_imagePropertyName = property;
00264     }
00265 
00266 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:31 2005 for Crazy Eddies GUI System by  doxygen 1.4.3