2017-06-12 16:38:59 +00:00
|
|
|
#include "guis/GuiInfoPopup.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
|
|
|
#include "components/ComponentGrid.h"
|
|
|
|
#include "components/NinePatchComponent.h"
|
2017-06-12 16:38:59 +00:00
|
|
|
#include "components/TextComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "Renderer.h"
|
|
|
|
#include <SDL_timer.h>
|
2017-06-12 16:38:59 +00:00
|
|
|
|
|
|
|
GuiInfoPopup::GuiInfoPopup(Window* window, std::string message, int duration) :
|
|
|
|
GuiComponent(window), mMessage(message), mDuration(duration), running(true)
|
|
|
|
{
|
|
|
|
mFrame = new NinePatchComponent(window);
|
|
|
|
float maxWidth = Renderer::getScreenWidth() * 0.9f;
|
|
|
|
float maxHeight = Renderer::getScreenHeight() * 0.2f;
|
|
|
|
|
|
|
|
std::shared_ptr<TextComponent> s = std::make_shared<TextComponent>(mWindow,
|
|
|
|
"",
|
|
|
|
Font::get(FONT_SIZE_MINI),
|
|
|
|
0x444444FF,
|
|
|
|
ALIGN_CENTER);
|
|
|
|
|
|
|
|
// we do this to force the text container to resize and return an actual expected popup size
|
|
|
|
s->setSize(0,0);
|
|
|
|
s->setText(message);
|
|
|
|
mSize = s->getSize();
|
|
|
|
|
|
|
|
// confirm the size isn't larger than the screen width, otherwise cap it
|
|
|
|
if (mSize.x() > maxWidth) {
|
|
|
|
s->setSize(maxWidth, mSize[1]);
|
|
|
|
mSize[0] = maxWidth;
|
|
|
|
}
|
|
|
|
if (mSize.y() > maxHeight) {
|
|
|
|
s->setSize(mSize[0], maxHeight);
|
|
|
|
mSize[1] = maxHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a padding to the box
|
2017-10-18 14:19:32 +00:00
|
|
|
int paddingX = (int) (Renderer::getScreenWidth() * 0.03f);
|
|
|
|
int paddingY = (int) (Renderer::getScreenHeight() * 0.02f);
|
2017-06-12 16:38:59 +00:00
|
|
|
mSize[0] = mSize.x() + paddingX;
|
|
|
|
mSize[1] = mSize.y() + paddingY;
|
|
|
|
|
|
|
|
float posX = Renderer::getScreenWidth()*0.5f - mSize.x()*0.5f;
|
|
|
|
float posY = Renderer::getScreenHeight() * 0.02f;
|
|
|
|
|
|
|
|
setPosition(posX, posY, 0);
|
|
|
|
|
|
|
|
mFrame->setImagePath(":/frame.png");
|
2017-10-28 20:24:35 +00:00
|
|
|
mFrame->fitTo(mSize, Vector3f::Zero(), Vector2f(-32, -32));
|
2017-06-12 16:38:59 +00:00
|
|
|
addChild(mFrame);
|
|
|
|
|
|
|
|
// we only init the actual time when we first start to render
|
|
|
|
mStartTime = 0;
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
mGrid = new ComponentGrid(window, Vector2i(1, 3));
|
2017-06-12 16:38:59 +00:00
|
|
|
mGrid->setSize(mSize);
|
2017-10-28 20:24:35 +00:00
|
|
|
mGrid->setEntry(s, Vector2i(0, 1), false, true);
|
2017-06-12 16:38:59 +00:00
|
|
|
addChild(mGrid);
|
|
|
|
}
|
|
|
|
|
|
|
|
GuiInfoPopup::~GuiInfoPopup()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void GuiInfoPopup::render(const Transform4x4f& parentTrans)
|
2017-06-12 16:38:59 +00:00
|
|
|
{
|
|
|
|
// we use identity as we want to render on a specific window position, not on the view
|
2017-10-28 20:24:35 +00:00
|
|
|
Transform4x4f trans = getTransform() * Transform4x4f::Identity();
|
2017-06-12 16:38:59 +00:00
|
|
|
if(running && updateState())
|
|
|
|
{
|
|
|
|
// if we're still supposed to be rendering it
|
|
|
|
Renderer::setMatrix(trans);
|
|
|
|
renderChildren(trans);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiInfoPopup::updateState()
|
|
|
|
{
|
|
|
|
int curTime = SDL_GetTicks();
|
|
|
|
|
|
|
|
// we only init the actual time when we first start to render
|
|
|
|
if(mStartTime == 0)
|
|
|
|
{
|
|
|
|
mStartTime = curTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute fade in effect
|
|
|
|
if (curTime - mStartTime > mDuration)
|
|
|
|
{
|
|
|
|
// we're past the popup duration, no need to render
|
|
|
|
running = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (curTime < mStartTime) {
|
|
|
|
// if SDL reset
|
|
|
|
running = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (curTime - mStartTime <= 500) {
|
|
|
|
alpha = ((curTime - mStartTime)*255/500);
|
|
|
|
}
|
|
|
|
else if (curTime - mStartTime < mDuration - 500)
|
|
|
|
{
|
|
|
|
alpha = 255;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alpha = ((-(curTime - mStartTime - mDuration)*255)/500);
|
|
|
|
}
|
|
|
|
mGrid->setOpacity(alpha);
|
|
|
|
|
|
|
|
// apply fade in effect to popup frame
|
|
|
|
mFrame->setEdgeColor(0xFFFFFF00 | (unsigned char)(alpha));
|
|
|
|
mFrame->setCenterColor(0xFFFFFF00 | (unsigned char)(alpha));
|
|
|
|
return true;
|
|
|
|
}
|