#include "CGUIImage.h"
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUISkin.h"
#include "IGUIEnvironment.h"
#include "IVideoDriver.h"
namespace irr
{
namespace gui
{
CGUIImage::CGUIImage(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
: IGUIImage(environment, parent, id, rectangle), Texture(0), Color(255,255,255,255),
UseAlphaChannel(false), ScaleImage(false)
{
#ifdef _DEBUG
setDebugName("CGUIImage");
#endif
}
CGUIImage::~CGUIImage()
{
if (Texture)
Texture->drop();
}
void CGUIImage::setImage(video::ITexture* image)
{
if (image == Texture)
return;
if (Texture)
Texture->drop();
Texture = image;
if (Texture)
Texture->grab();
}
video::ITexture* CGUIImage::getImage() const
{
return Texture;
}
void CGUIImage::setColor(video::SColor color)
{
Color = color;
}
video::SColor CGUIImage::getColor() const
{
return Color;
}
void CGUIImage::draw()
{
if (!IsVisible)
return;
IGUISkin* skin = Environment->getSkin();
video::IVideoDriver* driver = Environment->getVideoDriver();
core::rect<s32> rect = AbsoluteRect;
if (Texture)
{
if (ScaleImage)
{
const video::SColor Colors[] = {Color,Color,Color,Color};
driver->draw2DImage(Texture, AbsoluteRect,
core::rect<s32>(core::position2d<s32>(0,0), core::dimension2di(Texture->getOriginalSize())),
&AbsoluteClippingRect, Colors, UseAlphaChannel);
}
else
{
driver->draw2DImage(Texture, AbsoluteRect.UpperLeftCorner,
core::rect<s32>(core::position2d<s32>(0,0), core::dimension2di(Texture->getOriginalSize())),
&AbsoluteClippingRect, Color, UseAlphaChannel);
}
}
else
{
skin->draw2DRectangle(this, skin->getColor(EGDC_3D_DARK_SHADOW), AbsoluteRect, &AbsoluteClippingRect);
}
IGUIElement::draw();
}
void CGUIImage::setUseAlphaChannel(bool use)
{
UseAlphaChannel = use;
}
void CGUIImage::setScaleImage(bool scale)
{
ScaleImage = scale;
}
bool CGUIImage::isImageScaled() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return ScaleImage;
}
bool CGUIImage::isAlphaChannelUsed() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return UseAlphaChannel;
}
void CGUIImage::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
{
IGUIImage::serializeAttributes(out,options);
out->addTexture ("Texture", Texture);
out->addBool ("UseAlphaChannel", UseAlphaChannel);
out->addColor ("Color", Color);
out->addBool ("ScaleImage", ScaleImage);
}
void CGUIImage::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{
IGUIImage::deserializeAttributes(in,options);
setImage(in->getAttributeAsTexture("Texture"));
setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel"));
setColor(in->getAttributeAsColor("Color"));
setScaleImage(in->getAttributeAsBool("ScaleImage"));
}
}
}
#endif