00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "ImageComponent.h"
00018 #include "../../core/GameManager.h"
00019
00020
00021
00022
00023 ImageComponent::ImageComponent(Entity *parent)
00024 : EntityComponent(parent), mTexture(NULL), mPosition(vector2di(0, 0)),
00025 mSourceRect(rect<s32>(0, 0, 0, 0)), mClipRect(rect<s32>(0, 0, 0, 0)),
00026 mColor(SColor(255, 255, 255, 255)), mAlphaColor(SColor(255, 255, 255, 255)), mVisible(true),
00027 mUseAlphaColor(false)
00028 {
00029 init();
00030 }
00031
00032
00033 ImageComponent::ImageComponent(Entity *parent, const std::string &fileName,
00034 const vector2di &position, const rect<s32> &sourceRect,
00035 const rect<s32> &clipRect, const SColor &color,
00036 const SColor &alphaColor, bool useAlphaColor)
00037 : EntityComponent(parent), mPosition(position), mSourceRect(sourceRect), mClipRect(clipRect),
00038 mColor(color), mAlphaColor(alphaColor), mVisible(true), mUseAlphaColor(useAlphaColor)
00039 {
00040 init();
00041 setTexture(fileName);
00042 }
00043
00044
00045 ImageComponent::ImageComponent(Entity *parent, ITexture *texture,
00046 const vector2di &position, const rect<s32> &sourceRect,
00047 const rect<s32> &clipRect, const SColor &color,
00048 const SColor &alphaColor, bool useAlphaColor)
00049 : EntityComponent(parent), mPosition(position), mSourceRect(sourceRect), mClipRect(clipRect),
00050 mColor(color), mAlphaColor(alphaColor), mVisible(true), mUseAlphaColor(useAlphaColor)
00051 {
00052 init();
00053 setTexture(texture);
00054 }
00055
00056
00057
00058 ImageComponent::~ImageComponent()
00059 {
00060 if(mTexture)
00061 mTexture->drop();
00062 }
00063
00064
00065 void ImageComponent::init()
00066 {
00067
00068 mName = "ImageComponent";
00069
00070
00071 pParent->connectEventSignal("onRender", this, &ImageComponent::onRender);
00072 pParent->connectEventSignal("onPause", this, &ImageComponent::onPause);
00073 pParent->connectEventSignal("onUnPause", this, &ImageComponent::onUnPause);
00074 }
00075
00076
00077 const SColor& ImageComponent::getAlphaColor() const
00078 {
00079 return mAlphaColor;
00080 }
00081
00082
00083 const rect<s32>& ImageComponent::getClipRect() const
00084 {
00085 return mClipRect;
00086 }
00087
00088
00089 const SColor& ImageComponent::getColor() const
00090 {
00091 return mColor;
00092 }
00093
00094
00095 const vector2di& ImageComponent::getPosition() const
00096 {
00097 return mPosition;
00098 }
00099
00100
00101 const rect<s32>& ImageComponent::getSourceRect() const
00102 {
00103 return mSourceRect;
00104 }
00105
00106
00107 const ITexture* ImageComponent::getTexture() const
00108 {
00109 return mTexture;
00110 }
00111
00112
00113 bool ImageComponent::getUseAlphaColor() const
00114 {
00115 return mUseAlphaColor;
00116 }
00117
00118
00119 void ImageComponent::setAlphaColor(const SColor &color)
00120 {
00121 if(mTexture)
00122 GameManager::Instance()->getDriver()->makeColorKeyTexture(mTexture, color);
00123
00124 mAlphaColor = color;
00125 }
00126
00127
00128 void ImageComponent::setClipRect(const rect<s32> &rectangle)
00129 {
00130 mClipRect = rectangle;
00131 }
00132
00133
00134 void ImageComponent::setColor(const SColor &color)
00135 {
00136 mColor = color;
00137 }
00138
00139
00140 void ImageComponent::setPosition(const vector2di &position)
00141 {
00142 mPosition = position;
00143 }
00144
00145
00146 void ImageComponent::setSourceRect(const rect<s32> &rectangle)
00147 {
00148 mSourceRect = rectangle;
00149 }
00150
00151
00152 void ImageComponent::setTexture(const std::string &fileName)
00153 {
00154
00155 if(mTexture)
00156 mTexture->drop();
00157
00158
00159 AssetGroup *assets = pParent->getAssetGroup();
00160 ITexture *texture = 0;
00161
00162
00163 if(assets != NULL)
00164 {
00165
00166 assets->disconnectEventSignal(std::string("textures/") + mTextureFileName, this);
00167
00168
00169 TextureProcessor *processor = static_cast<TextureProcessor*>
00170 (assets->getAssetProcessor("textures"));
00171 texture = processor->getTexture(fileName);
00172
00173 if(texture)
00174 {
00175 assets->connectEventSignal(std::string("textures/") + fileName, this,
00176 &ImageComponent::onTexture);
00177 mTextureFileName = fileName;
00178 }
00179 }
00180
00181 else
00182 texture = GameManager::Instance()->getDriver()->getTexture(fileName.c_str());
00183
00184
00185 mTexture = texture;
00186
00187 if(mTexture)
00188 {
00189 GameManager::Instance()->getDriver()->makeColorKeyTexture(mTexture, mAlphaColor);
00190 mTexture->grab();
00191 }
00192 }
00193
00194
00195 void ImageComponent::setTexture(ITexture *texture)
00196 {
00197
00198 if(mTexture)
00199 mTexture->drop();
00200
00201
00202 AssetGroup *assets = pParent->getAssetGroup();
00203
00204 if(assets != NULL)
00205 {
00206 assets->disconnectEventSignal(std::string("textures/") + mTextureFileName, this);
00207 mTextureFileName = "";
00208 }
00209
00210
00211 mTexture = texture;
00212
00213 if(mTexture)
00214 {
00215 GameManager::Instance()->getDriver()->makeColorKeyTexture(mTexture, mAlphaColor);
00216 mTexture->grab();
00217 }
00218 }
00219
00220
00221 void ImageComponent::setUseAlphaColor(bool value)
00222 {
00223 mUseAlphaColor = value;
00224 }
00225
00226
00227
00228
00229 void ImageComponent::onRender(void *p)
00230 {
00231 if(mTexture && mVisible)
00232 {
00233 IVideoDriver *driver = GameManager::Instance()->getDriver();
00234 driver->draw2DImage(mTexture, mPosition, mSourceRect, &mClipRect, mColor, mUseAlphaColor);
00235 }
00236 }
00237
00238
00239 void ImageComponent::onPause(void *p)
00240 {
00241 mWasVisible = mVisible;
00242 }
00243
00244
00245 void ImageComponent::onUnPause(void *p)
00246 {
00247 mVisible = mWasVisible;
00248 }
00249
00250
00251 void ImageComponent::onTexture(void *p)
00252 {
00253
00254 ITexture *texture = reinterpret_cast<ITexture*>(p);
00255
00256
00257 if(mTexture)
00258 mTexture->drop();
00259
00260
00261 mTexture = texture;
00262
00263
00264 if(mTexture)
00265 mTexture->grab();
00266 }
00267
00268
00269 bool ImageComponent::parseXML(IXMLReader *file, Entity *entity)
00270 {
00271
00272 ImageComponent *component = NULL;
00273
00274
00275 if(file->getNodeType() == io::EXN_ELEMENT)
00276 {
00277 if(stringw("ImageComponent") == file->getNodeName())
00278 {
00279
00280 component = new ImageComponent(entity);
00281 }
00282 }
00283
00284 if(component == NULL)
00285 return false;
00286
00287
00288 while(file->read())
00289 {
00290 switch(file->getNodeType())
00291 {
00292 case io::EXN_ELEMENT:
00293
00294
00295 if(stringw("alphaColor") == file->getNodeName())
00296 {
00297 component->setAlphaColor( SColor(file->getAttributeValueAsInt(L"a"),
00298 file->getAttributeValueAsInt(L"r"),
00299 file->getAttributeValueAsInt(L"g"),
00300 file->getAttributeValueAsInt(L"b")) );
00301 }
00302
00303
00304 else if(stringw("clipRect") == file->getNodeName())
00305 {
00306 rect<s32> clipRect = rect<s32>( file->getAttributeValueAsInt(L"x1"),
00307 file->getAttributeValueAsInt(L"y1"),
00308 file->getAttributeValueAsInt(L"x2"),
00309 file->getAttributeValueAsInt(L"y2") );
00310
00311 component->setClipRect(clipRect);
00312 }
00313
00314
00315 else if(stringw("color") == file->getNodeName())
00316 {
00317 component->setColor( SColor(file->getAttributeValueAsInt(L"a"),
00318 file->getAttributeValueAsInt(L"r"),
00319 file->getAttributeValueAsInt(L"g"),
00320 file->getAttributeValueAsInt(L"b")) );
00321 }
00322
00323
00324 else if(stringw("position") == file->getNodeName())
00325 {
00326 component->setPosition(vector2di(file->getAttributeValueAsInt(L"x"),
00327 file->getAttributeValueAsInt(L"y")) );
00328 }
00329
00330
00331 else if(stringw("sourceRect") == file->getNodeName())
00332 {
00333 rect<s32> sourceRect = rect<s32>( file->getAttributeValueAsInt(L"x1"),
00334 file->getAttributeValueAsInt(L"y1"),
00335 file->getAttributeValueAsInt(L"x2"),
00336 file->getAttributeValueAsInt(L"y2") );
00337
00338 component->setSourceRect(sourceRect);
00339 }
00340
00341
00342 else if(stringw("texture") == file->getNodeName())
00343 {
00344 stringc fileName = file->getAttributeValue(L"fileName");
00345 component->setTexture(fileName.c_str());
00346 }
00347
00348
00349 else if(stringw("useAlphaColor") == file->getNodeName())
00350 {
00351 stringc value = file->getAttributeValue(L"value");
00352 component->setUseAlphaColor( (value == "true") ? true : false );
00353 }
00354
00355
00356 break;
00357
00358 case io::EXN_ELEMENT_END:
00359
00360
00361 if(stringw("ImageComponent") == file->getNodeName())
00362 return true;
00363
00364 break;
00365
00366 default:
00367
00368 break;
00369 }
00370
00371 }
00372
00373
00374 return false;
00375 }
00376
00377