2018-10-02 04:59:30 +00:00
|
|
|
#include "animationclipplayer.h"
|
|
|
|
|
|
|
|
AnimationClipPlayer::~AnimationClipPlayer()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2018-10-23 14:57:47 +00:00
|
|
|
void AnimationClipPlayer::setSpeedMode(SpeedMode speedMode)
|
|
|
|
{
|
|
|
|
m_speedMode = speedMode;
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:15:20 +00:00
|
|
|
void AnimationClipPlayer::updateFrameMeshes(std::vector<std::pair<float, Model *>> &frameMeshes)
|
2018-10-02 04:59:30 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
|
|
|
m_frameMeshes = frameMeshes;
|
|
|
|
frameMeshes.clear();
|
|
|
|
|
|
|
|
m_currentPlayIndex = 0;
|
|
|
|
m_countForFrame.restart();
|
|
|
|
|
|
|
|
if (!m_frameMeshes.empty())
|
|
|
|
m_timerForFrame.singleShot(0, this, &AnimationClipPlayer::frameReadyToShow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationClipPlayer::clear()
|
|
|
|
{
|
|
|
|
freeFrames();
|
|
|
|
delete m_lastFrameMesh;
|
|
|
|
m_lastFrameMesh = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationClipPlayer::freeFrames()
|
|
|
|
{
|
|
|
|
for (auto &it: m_frameMeshes) {
|
|
|
|
delete it.second;
|
|
|
|
}
|
|
|
|
m_frameMeshes.clear();
|
|
|
|
}
|
|
|
|
|
2018-10-23 14:57:47 +00:00
|
|
|
int AnimationClipPlayer::getFrameDurationMillis(int frame)
|
|
|
|
{
|
|
|
|
int millis = m_frameMeshes[frame].first * 1000;
|
|
|
|
if (SpeedMode::Slow == m_speedMode) {
|
|
|
|
millis *= 2;
|
|
|
|
} else if (SpeedMode::Fast == m_speedMode) {
|
|
|
|
millis /= 2;
|
|
|
|
}
|
|
|
|
return millis;
|
|
|
|
}
|
|
|
|
|
2020-04-07 23:15:20 +00:00
|
|
|
Model *AnimationClipPlayer::takeFrameMesh()
|
2018-10-02 04:59:30 +00:00
|
|
|
{
|
|
|
|
if (m_currentPlayIndex >= (int)m_frameMeshes.size()) {
|
|
|
|
if (nullptr != m_lastFrameMesh)
|
2020-04-07 23:15:20 +00:00
|
|
|
return new Model(*m_lastFrameMesh);
|
2018-10-02 04:59:30 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-23 14:57:47 +00:00
|
|
|
int millis = getFrameDurationMillis(m_currentPlayIndex) - m_countForFrame.elapsed();
|
2018-10-02 04:59:30 +00:00
|
|
|
if (millis > 0) {
|
|
|
|
m_timerForFrame.singleShot(millis, this, &AnimationClipPlayer::frameReadyToShow);
|
|
|
|
if (nullptr != m_lastFrameMesh)
|
2020-04-07 23:15:20 +00:00
|
|
|
return new Model(*m_lastFrameMesh);
|
2018-10-02 04:59:30 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
m_currentPlayIndex = (m_currentPlayIndex + 1) % m_frameMeshes.size();
|
|
|
|
m_countForFrame.restart();
|
|
|
|
|
2020-04-07 23:15:20 +00:00
|
|
|
Model *mesh = new Model(*m_frameMeshes[m_currentPlayIndex].second);
|
2018-10-23 14:57:47 +00:00
|
|
|
m_timerForFrame.singleShot(getFrameDurationMillis(m_currentPlayIndex), this, &AnimationClipPlayer::frameReadyToShow);
|
2018-10-02 04:59:30 +00:00
|
|
|
delete m_lastFrameMesh;
|
2020-04-07 23:15:20 +00:00
|
|
|
m_lastFrameMesh = new Model(*mesh);
|
2018-10-02 04:59:30 +00:00
|
|
|
return mesh;
|
|
|
|
}
|