2014-06-20 01:30:09 +00:00
|
|
|
#include "components/AnimatedImageComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
|
|
|
#include "components/ImageComponent.h"
|
|
|
|
#include "resources/ResourceManager.h"
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "Log.h"
|
2014-04-19 00:00:49 +00:00
|
|
|
|
2014-04-19 00:46:55 +00:00
|
|
|
AnimatedImageComponent::AnimatedImageComponent(Window* window) : GuiComponent(window), mEnabled(false)
|
2014-04-19 00:00:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedImageComponent::load(const AnimationDef* def)
|
|
|
|
{
|
|
|
|
mFrames.clear();
|
|
|
|
|
|
|
|
assert(def->frameCount >= 1);
|
|
|
|
|
|
|
|
for(size_t i = 0; i < def->frameCount; i++)
|
|
|
|
{
|
|
|
|
if(def->frames[i].path != NULL && !ResourceManager::getInstance()->fileExists(def->frames[i].path))
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Missing animation frame " << i << " (\"" << def->frames[i].path << "\")";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto img = std::unique_ptr<ImageComponent>(new ImageComponent(mWindow));
|
|
|
|
img->setResize(mSize.x(), mSize.y());
|
|
|
|
img->setImage(std::string(def->frames[i].path), false);
|
|
|
|
|
|
|
|
mFrames.push_back(ImageFrame(std::move(img), def->frames[i].time));
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoop = def->loop;
|
|
|
|
|
|
|
|
mCurrentFrame = 0;
|
|
|
|
mFrameAccumulator = 0;
|
|
|
|
mEnabled = true;
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:46:55 +00:00
|
|
|
void AnimatedImageComponent::reset()
|
|
|
|
{
|
|
|
|
mCurrentFrame = 0;
|
|
|
|
mFrameAccumulator = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:00:49 +00:00
|
|
|
void AnimatedImageComponent::onSizeChanged()
|
|
|
|
{
|
2017-11-11 14:56:22 +00:00
|
|
|
for(auto it = mFrames.cbegin(); it != mFrames.cend(); it++)
|
2014-04-19 00:00:49 +00:00
|
|
|
{
|
|
|
|
it->first->setResize(mSize.x(), mSize.y());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedImageComponent::update(int deltaTime)
|
|
|
|
{
|
|
|
|
if(!mEnabled || mFrames.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mFrameAccumulator += deltaTime;
|
|
|
|
|
|
|
|
while(mFrames.at(mCurrentFrame).second <= mFrameAccumulator)
|
|
|
|
{
|
|
|
|
mCurrentFrame++;
|
|
|
|
|
|
|
|
if(mCurrentFrame == mFrames.size())
|
|
|
|
{
|
|
|
|
if(mLoop)
|
|
|
|
{
|
|
|
|
// restart
|
|
|
|
mCurrentFrame = 0;
|
|
|
|
}else{
|
|
|
|
// done, stop at last frame
|
|
|
|
mCurrentFrame--;
|
|
|
|
mEnabled = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mFrameAccumulator -= mFrames.at(mCurrentFrame).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
void AnimatedImageComponent::render(const Transform4x4f& trans)
|
2014-04-19 00:00:49 +00:00
|
|
|
{
|
|
|
|
if(mFrames.size())
|
|
|
|
mFrames.at(mCurrentFrame).first->render(getTransform() * trans);
|
|
|
|
}
|