sepearate looping logic from AnimationHandle

This commit is contained in:
ZappoMan 2014-11-12 14:47:10 -08:00
parent 68bd6c23d9
commit 6ff8abcbdd
3 changed files with 52 additions and 47 deletions

View file

@ -33,7 +33,7 @@ void AnimationHandle::setPriority(float priority) {
if (_priority == priority) { if (_priority == priority) {
return; return;
} }
if (_running) { if (isRunning()) {
_model->_runningAnimations.removeOne(_self); _model->_runningAnimations.removeOne(_self);
if (priority < _priority) { if (priority < _priority) {
replaceMatchingPriorities(priority); replaceMatchingPriorities(priority);
@ -47,7 +47,8 @@ void AnimationHandle::setPriority(float priority) {
} }
void AnimationHandle::setStartAutomatically(bool startAutomatically) { void AnimationHandle::setStartAutomatically(bool startAutomatically) {
if ((_startAutomatically = startAutomatically) && !_running) { _animationLoop.setStartAutomatically(startAutomatically);
if (getStartAutomatically() && !isRunning()) {
start(); start();
} }
} }
@ -58,42 +59,36 @@ void AnimationHandle::setMaskedJoints(const QStringList& maskedJoints) {
} }
void AnimationHandle::setRunning(bool running) { void AnimationHandle::setRunning(bool running) {
if (_running == running) { if (isRunning() == running) {
// if we're already running, this is the same as a restart
if (running) { if (running) {
// move back to the beginning // move back to the beginning
_frameIndex = _firstFrame; setFrameIndex(getFirstFrame());
} }
return; return;
} }
if ((_running = running)) { _animationLoop.setRunning(running);
if (isRunning()) {
if (!_model->_runningAnimations.contains(_self)) { if (!_model->_runningAnimations.contains(_self)) {
insertSorted(_model->_runningAnimations, _self); insertSorted(_model->_runningAnimations, _self);
} }
_frameIndex = _firstFrame;
} else { } else {
_model->_runningAnimations.removeOne(_self); _model->_runningAnimations.removeOne(_self);
replaceMatchingPriorities(0.0f); replaceMatchingPriorities(0.0f);
} }
emit runningChanged(_running); emit runningChanged(isRunning());
} }
AnimationHandle::AnimationHandle(Model* model) : AnimationHandle::AnimationHandle(Model* model) :
QObject(model), QObject(model),
_model(model), _model(model),
_fps(30.0f), _priority(1.0f)
_priority(1.0f), {
_loop(false),
_hold(false),
_startAutomatically(false),
_firstFrame(0.0f),
_lastFrame(FLT_MAX),
_running(false) {
} }
AnimationDetails AnimationHandle::getAnimationDetails() const { AnimationDetails AnimationHandle::getAnimationDetails() const {
AnimationDetails details(_role, _url, _fps, _priority, _loop, _hold, AnimationDetails details(_role, _url, getFPS(), _priority, getLoop(), getHold(),
_startAutomatically, _firstFrame, _lastFrame, _running, _frameIndex); getStartAutomatically(), getFirstFrame(), getLastFrame(), isRunning(), getFrameIndex());
return details; return details;
} }
@ -116,7 +111,7 @@ void AnimationHandle::setAnimationDetails(const AnimationDetails& details) {
void AnimationHandle::simulate(float deltaTime) { void AnimationHandle::simulate(float deltaTime) {
_frameIndex += deltaTime * _fps; _animationLoop.simulate(deltaTime);
// update the joint mappings if necessary/possible // update the joint mappings if necessary/possible
if (_jointMappings.isEmpty()) { if (_jointMappings.isEmpty()) {
@ -142,6 +137,8 @@ void AnimationHandle::simulate(float deltaTime) {
stop(); stop();
return; return;
} }
/*
float endFrameIndex = qMin(_lastFrame, animationGeometry.animationFrames.size() - (_loop ? 0.0f : 1.0f)); float endFrameIndex = qMin(_lastFrame, animationGeometry.animationFrames.size() - (_loop ? 0.0f : 1.0f));
float startFrameIndex = qMin(_firstFrame, endFrameIndex); float startFrameIndex = qMin(_firstFrame, endFrameIndex);
if ((!_loop && (_frameIndex < startFrameIndex || _frameIndex > endFrameIndex)) || startFrameIndex == endFrameIndex) { if ((!_loop && (_frameIndex < startFrameIndex || _frameIndex > endFrameIndex)) || startFrameIndex == endFrameIndex) {
@ -159,9 +156,10 @@ void AnimationHandle::simulate(float deltaTime) {
} else if (_frameIndex > endFrameIndex) { } else if (_frameIndex > endFrameIndex) {
_frameIndex = startFrameIndex + glm::mod(_frameIndex - startFrameIndex, endFrameIndex - startFrameIndex); _frameIndex = startFrameIndex + glm::mod(_frameIndex - startFrameIndex, endFrameIndex - startFrameIndex);
} }
*/
// blend between the closest two frames // blend between the closest two frames
applyFrame(_frameIndex); applyFrame(getFrameIndex());
} }
void AnimationHandle::applyFrame(float frameIndex) { void AnimationHandle::applyFrame(float frameIndex) {

View file

@ -19,6 +19,7 @@
#include <QVector> #include <QVector>
#include <AnimationCache.h> #include <AnimationCache.h>
#include <AnimationLoop.h>
class AnimationHandle; class AnimationHandle;
class Model; class Model;
@ -38,36 +39,37 @@ public:
void setURL(const QUrl& url); void setURL(const QUrl& url);
const QUrl& getURL() const { return _url; } const QUrl& getURL() const { return _url; }
void setFPS(float fps) { _fps = fps; }
float getFPS() const { return _fps; }
void setPriority(float priority); void setPriority(float priority);
float getPriority() const { return _priority; } float getPriority() const { return _priority; }
void setLoop(bool loop) { _loop = loop; }
bool getLoop() const { return _loop; }
void setHold(bool hold) { _hold = hold; }
bool getHold() const { return _hold; }
void setStartAutomatically(bool startAutomatically);
bool getStartAutomatically() const { return _startAutomatically; }
void setFirstFrame(float firstFrame) { _firstFrame = firstFrame; }
float getFirstFrame() const { return _firstFrame; }
void setLastFrame(float lastFrame) { _lastFrame = lastFrame; }
float getLastFrame() const { return _lastFrame; }
void setMaskedJoints(const QStringList& maskedJoints); void setMaskedJoints(const QStringList& maskedJoints);
const QStringList& getMaskedJoints() const { return _maskedJoints; } const QStringList& getMaskedJoints() const { return _maskedJoints; }
void setRunning(bool running);
bool isRunning() const { return _running; }
void setFrameIndex(float frameIndex) { _frameIndex = glm::clamp(_frameIndex, _firstFrame, _lastFrame); } void setFPS(float fps) { _animationLoop.setFPS(fps); }
float getFrameIndex() const { return _frameIndex; } float getFPS() const { return _animationLoop.getFPS(); }
void setLoop(bool loop) { _animationLoop.setLoop(loop); }
bool getLoop() const { return _animationLoop.getLoop(); }
void setHold(bool hold) { _animationLoop.setHold(hold); }
bool getHold() const { return _animationLoop.getHold(); }
void setStartAutomatically(bool startAutomatically);
bool getStartAutomatically() const { return _animationLoop.getStartAutomatically(); }
void setFirstFrame(float firstFrame) { _animationLoop.setFirstFrame(firstFrame); }
float getFirstFrame() const { return _animationLoop.getFirstFrame(); }
void setLastFrame(float lastFrame) { _animationLoop.setLastFrame(lastFrame); }
float getLastFrame() const { return _animationLoop.getLastFrame(); }
void setRunning(bool running);
bool isRunning() const { return _animationLoop.isRunning(); }
void setFrameIndex(float frameIndex) { _animationLoop.setFrameIndex(frameIndex); }
float getFrameIndex() const { return _animationLoop.getFrameIndex(); }
AnimationDetails getAnimationDetails() const; AnimationDetails getAnimationDetails() const;
void setAnimationDetails(const AnimationDetails& details); void setAnimationDetails(const AnimationDetails& details);
@ -96,17 +98,23 @@ private:
AnimationPointer _animation; AnimationPointer _animation;
QString _role; QString _role;
QUrl _url; QUrl _url;
float _fps;
float _priority; float _priority;
QStringList _maskedJoints;
QVector<int> _jointMappings;
AnimationLoop _animationLoop;
/*
float _fps;
bool _loop; bool _loop;
bool _hold; bool _hold;
bool _startAutomatically; bool _startAutomatically;
float _firstFrame; float _firstFrame;
float _lastFrame; float _lastFrame;
QStringList _maskedJoints;
bool _running; bool _running;
QVector<int> _jointMappings;
float _frameIndex; float _frameIndex;
*/
}; };

View file

@ -94,5 +94,4 @@ Q_DECLARE_METATYPE(AnimationDetails);
QScriptValue animationDetailsToScriptValue(QScriptEngine* engine, const AnimationDetails& event); QScriptValue animationDetailsToScriptValue(QScriptEngine* engine, const AnimationDetails& event);
void animationDetailsFromScriptValue(const QScriptValue& object, AnimationDetails& event); void animationDetailsFromScriptValue(const QScriptValue& object, AnimationDetails& event);
#endif // hifi_AnimationCache_h #endif // hifi_AnimationCache_h