00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00034
00035
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
00089 const Image* img = isImageFetchedFromProperty() ?
00090 PropertyHelper::stringToImage(srcWindow.getProperty(d_imagePropertyName)) :
00091 d_image;
00092
00093
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
00109 ColourRect finalColours;
00110 initColoursRect(srcWindow, modColours, finalColours);
00111
00112
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
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
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
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
00200 else
00201 {
00202 clippingRect = clipper;
00203 }
00204
00205
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
00220 out_stream << "<ImageryComponent>" << std::endl;
00221
00222 d_area.writeXMLToStream(out_stream);
00223
00224
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
00231 writeColoursXML(out_stream);
00232
00233
00234 if (!writeVertFormatXML(out_stream))
00235 {
00236
00237 out_stream << "<VertFormat type=\"" << FalagardXMLHelper::vertFormatToString(d_vertFormatting) << "\" />" << std::endl;
00238 }
00239
00240
00241 if (!writeHorzFormatXML(out_stream))
00242 {
00243
00244 out_stream << "<HorzFormat type=\"" << FalagardXMLHelper::horzFormatToString(d_horzFormatting) << "\" />" << std::endl;
00245 }
00246
00247
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 }