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), mAlphaColor(SColor(255, 255, 255, 255)), mClipRect(NULL),
00025 mColor(SColor(255, 255, 255, 255)), mPosition(vector2di(0, 0)),
00026 mSourceRect(rect<s32>(0, 0, 0, 0)), mVisible(true), mUseAlphaColor(false)
00027 {
00028
00029 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00030
00031
00032 mName = "ImageComponent";
00033
00034
00035 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onRender",
00036 this, &ImageComponent::onRender);
00037 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPause",
00038 this, &ImageComponent::onPause);
00039 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onUnPause",
00040 this, &ImageComponent::onUnPause);
00041 }
00042
00043
00044 ImageComponent::ImageComponent(Entity *parent, const std::string &fileName,
00045 const vector2di &position, const rect<s32> &sourceRect,
00046 rect<s32> *clipRect, const SColor &color,
00047 const SColor &alphaColor, bool useAlphaColor)
00048 : EntityComponent(parent), mAlphaColor(alphaColor), mClipRect(clipRect), mColor(color),
00049 mPosition(position), mSourceRect(sourceRect), mVisible(true), mUseAlphaColor(useAlphaColor)
00050 {
00051
00052 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00053
00054
00055 mTexture = GameManager::Instance()->getDriver()->getTexture(fileName.c_str());
00056
00057
00058 mName = "ImageComponent";
00059
00060
00061 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onRender",
00062 this, &ImageComponent::onRender);
00063 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPause",
00064 this, &ImageComponent::onPause);
00065 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onUnPause",
00066 this, &ImageComponent::onUnPause);
00067 }
00068
00069
00070 ImageComponent::ImageComponent(Entity *parent, ITexture *texture,
00071 const vector2di &position, const rect<s32> &sourceRect,
00072 rect<s32> *clipRect, const SColor &color,
00073 const SColor &alphaColor, bool useAlphaColor)
00074 : EntityComponent(parent), mAlphaColor(alphaColor), mClipRect(clipRect), mColor(color),
00075 mPosition(position), mSourceRect(sourceRect), mVisible(true), mUseAlphaColor(useAlphaColor)
00076 {
00077
00078 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00079
00080
00081 mTexture = texture;
00082
00083
00084 mName = "ImageComponent";
00085
00086
00087 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onRender",
00088 this, &ImageComponent::onRender);
00089 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPause",
00090 this, &ImageComponent::onPause);
00091 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onUnPause",
00092 this, &ImageComponent::onUnPause);
00093 }
00094
00095
00096
00097 ImageComponent::~ImageComponent()
00098 {
00099 }
00100
00101
00102 ImageComponent* ImageComponent::refFactory(Entity *parent)
00103 {
00104 return new ImageComponent(parent);
00105 }
00106
00107
00108 const SColor& ImageComponent::getAlphaColor() const
00109 {
00110 return mAlphaColor;
00111 }
00112
00113
00114 const rect<s32>* ImageComponent::getClipRect() const
00115 {
00116 return mClipRect;
00117 }
00118
00119
00120 const SColor& ImageComponent::getColor() const
00121 {
00122 return mColor;
00123 }
00124
00125
00126 const vector2di& ImageComponent::getPosition() const
00127 {
00128 return mPosition;
00129 }
00130
00131
00132 const rect<s32>& ImageComponent::getSourceRect() const
00133 {
00134 return mSourceRect;
00135 }
00136
00137
00138 const ITexture* ImageComponent::getTexture() const
00139 {
00140 return mTexture;
00141 }
00142
00143
00144 bool ImageComponent::getUseAlphaColor() const
00145 {
00146 return mUseAlphaColor;
00147 }
00148
00149
00150 void ImageComponent::setAlphaColor(const SColor &color)
00151 {
00152 if(mTexture)
00153 GameManager::Instance()->getDriver()->makeColorKeyTexture(mTexture, color);
00154
00155 mAlphaColor = color;
00156 }
00157
00158
00159 void ImageComponent::setClipRect(rect<s32> *rectangle)
00160 {
00161 if(mClipRect != NULL)
00162 {
00163 delete mClipRect;
00164 mClipRect = NULL;
00165 }
00166
00167 if(rectangle != NULL)
00168 mClipRect = new rect<s32>(*rectangle);
00169 }
00170
00171
00172 void ImageComponent::setColor(const SColor &color)
00173 {
00174 mColor = color;
00175 }
00176
00177
00178 void ImageComponent::setPosition(const vector2di &position)
00179 {
00180 mPosition = position;
00181 }
00182
00183
00184 void ImageComponent::setSourceRect(const rect<s32> &rectangle)
00185 {
00186 mSourceRect = rectangle;
00187 }
00188
00189
00190 void ImageComponent::setTexture(const std::string &fileName)
00191 {
00192 mTexture = GameManager::Instance()->getDriver()->getTexture(fileName.c_str());
00193
00194 if(mTexture)
00195 GameManager::Instance()->getDriver()->makeColorKeyTexture(mTexture, mAlphaColor);
00196 }
00197
00198
00199 void ImageComponent::setTexture(ITexture *texture)
00200 {
00201 mTexture = texture;
00202
00203 if(mTexture)
00204 GameManager::Instance()->getDriver()->makeColorKeyTexture(mTexture, mAlphaColor);
00205 }
00206
00207
00208 void ImageComponent::setUseAlphaColor(bool value)
00209 {
00210 mUseAlphaColor = value;
00211 }
00212
00213
00214
00215
00216 void ImageComponent::onRender(void *p)
00217 {
00218 if(mTexture && mVisible)
00219 {
00220 IVideoDriver *driver = GameManager::Instance()->getDriver();
00221 driver->draw2DImage(mTexture, mPosition, mSourceRect, mClipRect, mColor, mUseAlphaColor);
00222 }
00223 }
00224
00225
00226 void ImageComponent::onPause(void *p)
00227 {
00228 mWasVisible = mVisible;
00229 }
00230
00231
00232 void ImageComponent::onUnPause(void *p)
00233 {
00234 mVisible = mWasVisible;
00235 }
00236
00237
00238
00239 void bindImageComponent(asIScriptEngine *engine)
00240 {
00241
00242 int r;
00243
00244
00245 r = engine->RegisterObjectType("ImageComponent", sizeof(ImageComponent), asOBJ_REF); assert(r >= 0);
00246
00247
00248 bindEntityComponentBase<ImageComponent>(engine, "ImageComponent");
00249
00250
00251 r = engine->RegisterGlobalBehaviour(asBEHAVE_REF_CAST, "ImageComponent@ f(EntityComponent @)",
00252 asFUNCTION((asRefCast<EntityComponent,ImageComponent>)), asCALL_CDECL); assert( r >= 0 );
00253 r = engine->RegisterGlobalBehaviour(asBEHAVE_IMPLICIT_REF_CAST, "EntityComponent@ f(ImageComponent @)",
00254 asFUNCTION((asRefCast<ImageComponent,EntityComponent>)), asCALL_CDECL); assert( r >= 0 );
00255
00256 r = engine->RegisterObjectBehaviour("ImageComponent", asBEHAVE_FACTORY, "ImageComponent@ f(Entity @)",
00257 asFUNCTION(ImageComponent::refFactory), asCALL_CDECL); assert(r >= 0);
00258
00259
00260 r = engine->RegisterObjectMethod("ImageComponent", "void setAlphaColor(const SColor &in)",
00261 asMETHOD(ImageComponent, setAlphaColor), asCALL_THISCALL); assert(r >= 0);
00262 r = engine->RegisterObjectMethod("ImageComponent", "void setClipRect(const recti &in)",
00263 asMETHOD(ImageComponent, setClipRect), asCALL_THISCALL); assert(r >= 0);
00264 r = engine->RegisterObjectMethod("ImageComponent", "void setColor(const SColor &in)",
00265 asMETHOD(ImageComponent, setColor), asCALL_THISCALL); assert(r >= 0);
00266 r = engine->RegisterObjectMethod("ImageComponent", "void setPosition(vector2di &in)",
00267 asMETHOD(ImageComponent, setPosition), asCALL_THISCALL); assert(r >= 0);
00268 r = engine->RegisterObjectMethod("ImageComponent", "void setSourceRect(const recti &in)",
00269 asMETHOD(ImageComponent, setSourceRect), asCALL_THISCALL); assert(r >= 0);
00270 r = engine->RegisterObjectMethod("ImageComponent", "void setTexture(const string&)",
00271 asMETHODPR(ImageComponent, setTexture, (const std::string&), void),
00272 asCALL_THISCALL); assert(r >= 0);
00273 r = engine->RegisterObjectMethod("ImageComponent", "void setUseAlphaColor(bool)",
00274 asMETHOD(ImageComponent, setUseAlphaColor), asCALL_THISCALL); assert(r >= 0);
00275
00276 r = engine->RegisterObjectMethod("ImageComponent", "const SColor& getAlphaColor()",
00277 asMETHOD(ImageComponent, getAlphaColor), asCALL_THISCALL); assert(r >= 0);
00278 r = engine->RegisterObjectMethod("ImageComponent", "const recti& getClipRect()",
00279 asMETHOD(ImageComponent, getClipRect), asCALL_THISCALL); assert(r >= 0);
00280 r = engine->RegisterObjectMethod("ImageComponent", "const SColor& getColor()",
00281 asMETHOD(ImageComponent, getColor), asCALL_THISCALL); assert(r >= 0);
00282 r = engine->RegisterObjectMethod("ImageComponent", "const vector2di& getPosition()",
00283 asMETHOD(ImageComponent, getPosition), asCALL_THISCALL); assert(r >= 0);
00284 r = engine->RegisterObjectMethod("ImageComponent", "const recti& getSourceRect()",
00285 asMETHOD(ImageComponent, getSourceRect), asCALL_THISCALL); assert(r >= 0);
00286 r = engine->RegisterObjectMethod("ImageComponent", "bool getUseAlphaColor()",
00287 asMETHOD(ImageComponent, getUseAlphaColor), asCALL_THISCALL); assert(r >= 0);
00288 }
00289
00290