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