2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-28 16:39:18 +00:00
|
|
|
// AnimatedImageComponent.cpp
|
|
|
|
//
|
|
|
|
// Creates animation from multiple images files.
|
|
|
|
//
|
|
|
|
|
2014-06-20 01:30:09 +00:00
|
|
|
#include "components/AnimatedImageComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "Log.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "components/ImageComponent.h"
|
|
|
|
#include "resources/ResourceManager.h"
|
2014-04-19 00:00:49 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
AnimatedImageComponent::AnimatedImageComponent(Window* window)
|
2021-07-07 18:31:46 +00:00
|
|
|
: GuiComponent(window)
|
|
|
|
, mEnabled(false)
|
2014-04-19 00:00:49 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedImageComponent::load(const AnimationDef* def)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mFrames.clear();
|
2014-04-19 00:00:49 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
assert(def->frameCount >= 1);
|
2014-04-19 00:00:49 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
for (size_t i = 0; i < def->frameCount; i++) {
|
2020-12-16 22:59:00 +00:00
|
|
|
if (def->frames[i].path != "" &&
|
2021-07-07 18:31:46 +00:00
|
|
|
!ResourceManager::getInstance()->fileExists(def->frames[i].path)) {
|
|
|
|
LOG(LogError) << "Missing animation frame " << i << " (\"" << def->frames[i].path
|
|
|
|
<< "\")";
|
2020-06-28 16:39:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-19 00:00:49 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
auto img = std::unique_ptr<ImageComponent>(new ImageComponent(mWindow));
|
2021-08-16 16:25:01 +00:00
|
|
|
img->setResize(mSize.x, mSize.y);
|
2020-06-28 16:39:18 +00:00
|
|
|
img->setImage(std::string(def->frames[i].path), false);
|
2019-08-25 15:23:02 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mFrames.push_back(ImageFrame(std::move(img), def->frames[i].time));
|
|
|
|
}
|
2014-04-19 00:00:49 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mLoop = def->loop;
|
2014-04-19 00:00:49 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mCurrentFrame = 0;
|
|
|
|
mFrameAccumulator = 0;
|
|
|
|
mEnabled = true;
|
2014-04-19 00:00:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:46:55 +00:00
|
|
|
void AnimatedImageComponent::reset()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
mCurrentFrame = 0;
|
|
|
|
mFrameAccumulator = 0;
|
2014-04-19 00:46:55 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 00:00:49 +00:00
|
|
|
void AnimatedImageComponent::onSizeChanged()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
for (auto it = mFrames.cbegin(); it != mFrames.cend(); it++) {
|
2021-08-16 16:25:01 +00:00
|
|
|
it->first->setResize(mSize.x, mSize.y);
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2014-04-19 00:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedImageComponent::update(int deltaTime)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!mEnabled || mFrames.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mFrameAccumulator += deltaTime;
|
|
|
|
|
|
|
|
while (mFrames.at(mCurrentFrame).second <= mFrameAccumulator) {
|
|
|
|
mCurrentFrame++;
|
|
|
|
|
2020-11-17 22:06:54 +00:00
|
|
|
if (mCurrentFrame == static_cast<int>(mFrames.size())) {
|
2020-06-28 16:39:18 +00:00
|
|
|
if (mLoop) {
|
|
|
|
// Restart.
|
|
|
|
mCurrentFrame = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Done, stop at last frame.
|
|
|
|
mCurrentFrame--;
|
|
|
|
mEnabled = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mFrameAccumulator -= mFrames.at(mCurrentFrame).second;
|
|
|
|
}
|
2014-04-19 00:00:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void AnimatedImageComponent::render(const glm::mat4& trans)
|
2014-04-19 00:00:49 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (mFrames.size())
|
|
|
|
mFrames.at(mCurrentFrame).first->render(getTransform() * trans);
|
2014-04-19 00:00:49 +00:00
|
|
|
}
|