#include "CGUIInOutFader.h"
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUIEnvironment.h"
#include "IVideoDriver.h"
#include "os.h"
namespace irr
{
namespace gui
{
CGUIInOutFader::CGUIInOutFader(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
: IGUIInOutFader(environment, parent, id, rectangle)
{
#ifdef _DEBUG
setDebugName("CGUIInOutFader");
#endif
Action = EFA_NOTHING;
StartTime = 0;
EndTime = 0;
setColor(video::SColor(0,0,0,0));
}
void CGUIInOutFader::draw()
{
if (!IsVisible || !Action)
return;
u32 now = os::Timer::getTime();
if (now > EndTime && Action == EFA_FADE_IN)
{
Action = EFA_NOTHING;
return;
}
video::IVideoDriver* driver = Environment->getVideoDriver();
if (driver)
{
f32 d;
if (now > EndTime)
d = 0.0f;
else
d = (EndTime - now) / (f32)(EndTime - StartTime);
video::SColor newCol = FullColor.getInterpolated(TransColor, d);
driver->draw2DRectangle(newCol, AbsoluteRect, &AbsoluteClippingRect);
}
IGUIElement::draw();
}
video::SColor CGUIInOutFader::getColor() const
{
return Color[1];
}
void CGUIInOutFader::setColor(video::SColor color)
{
video::SColor s = color;
video::SColor d = color;
s.setAlpha ( 255 );
d.setAlpha ( 0 );
setColor ( s,d );
}
void CGUIInOutFader::setColor(video::SColor source, video::SColor dest)
{
Color[0] = source;
Color[1] = dest;
if (Action == EFA_FADE_OUT)
{
FullColor = Color[1];
TransColor = Color[0];
}
else
if (Action == EFA_FADE_IN)
{
FullColor = Color[0];
TransColor = Color[1];
}
}
bool CGUIInOutFader::isReady() const
{
u32 now = os::Timer::getTime();
bool ret = (now > EndTime);
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return ret;
}
void CGUIInOutFader::fadeIn(u32 time)
{
StartTime = os::Timer::getTime();
EndTime = StartTime + time;
Action = EFA_FADE_IN;
setColor(Color[0],Color[1]);
}
void CGUIInOutFader::fadeOut(u32 time)
{
StartTime = os::Timer::getTime();
EndTime = StartTime + time;
Action = EFA_FADE_OUT;
setColor(Color[0],Color[1]);
}
void CGUIInOutFader::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
{
IGUIInOutFader::serializeAttributes(out,options);
out->addColor ("FullColor", FullColor);
out->addColor ("TransColor", TransColor);
}
void CGUIInOutFader::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
{
IGUIInOutFader::deserializeAttributes(in,options);
FullColor = in->getAttributeAsColor("FullColor");
TransColor = in->getAttributeAsColor("TransColor");
}
}
}
#endif