Keep list of animations in Rig, not MyAvatar.

This commit is contained in:
Howard Stearns 2015-07-24 22:02:39 -07:00
parent 05dda1220e
commit 8b5f24e4df
4 changed files with 118 additions and 82 deletions

View file

@ -491,17 +491,6 @@ void MyAvatar::loadLastRecording() {
_player->loadRecording(_recorder->getRecording()); _player->loadRecording(_recorder->getRecording());
} }
AnimationHandlePointer MyAvatar::addAnimationHandle() {
AnimationHandlePointer handle = _rig->createAnimationHandle();
_animationHandles.append(handle);
return handle;
}
void MyAvatar::removeAnimationHandle(const AnimationHandlePointer& handle) {
handle->stop();
_animationHandles.removeOne(handle);
}
void MyAvatar::startAnimation(const QString& url, float fps, float priority, void MyAvatar::startAnimation(const QString& url, float fps, float priority,
bool loop, bool hold, float firstFrame, float lastFrame, const QStringList& maskedJoints) { bool loop, bool hold, float firstFrame, float lastFrame, const QStringList& maskedJoints) {
if (QThread::currentThread() != thread()) { if (QThread::currentThread() != thread()) {
@ -510,16 +499,7 @@ void MyAvatar::startAnimation(const QString& url, float fps, float priority,
Q_ARG(float, lastFrame), Q_ARG(const QStringList&, maskedJoints)); Q_ARG(float, lastFrame), Q_ARG(const QStringList&, maskedJoints));
return; return;
} }
AnimationHandlePointer handle = _rig->createAnimationHandle(); _rig->startAnimation(url, fps, priority, loop, hold, firstFrame, lastFrame, maskedJoints);
handle->setURL(url);
handle->setFPS(fps);
handle->setPriority(priority);
handle->setLoop(loop);
handle->setHold(hold);
handle->setFirstFrame(firstFrame);
handle->setLastFrame(lastFrame);
handle->setMaskedJoints(maskedJoints);
handle->start();
} }
void MyAvatar::startAnimationByRole(const QString& role, const QString& url, float fps, float priority, void MyAvatar::startAnimationByRole(const QString& role, const QString& url, float fps, float priority,
@ -530,25 +510,7 @@ void MyAvatar::startAnimationByRole(const QString& role, const QString& url, flo
Q_ARG(float, lastFrame), Q_ARG(const QStringList&, maskedJoints)); Q_ARG(float, lastFrame), Q_ARG(const QStringList&, maskedJoints));
return; return;
} }
// check for a configured animation for the role _rig->startAnimationByRole(role, url, fps, priority, loop, hold, firstFrame, lastFrame, maskedJoints);
foreach (const AnimationHandlePointer& handle, _animationHandles) {
if (handle->getRole() == role) {
handle->start();
return;
}
}
// no joy; use the parameters provided
AnimationHandlePointer handle = _rig->createAnimationHandle();
handle->setRole(role);
handle->setURL(url);
handle->setFPS(fps);
handle->setPriority(priority);
handle->setLoop(loop);
handle->setHold(hold);
handle->setFirstFrame(firstFrame);
handle->setLastFrame(lastFrame);
handle->setMaskedJoints(maskedJoints);
handle->start();
} }
void MyAvatar::stopAnimationByRole(const QString& role) { void MyAvatar::stopAnimationByRole(const QString& role) {
@ -556,11 +518,7 @@ void MyAvatar::stopAnimationByRole(const QString& role) {
QMetaObject::invokeMethod(this, "stopAnimationByRole", Q_ARG(const QString&, role)); QMetaObject::invokeMethod(this, "stopAnimationByRole", Q_ARG(const QString&, role));
return; return;
} }
foreach (const AnimationHandlePointer& handle, _skeletonModel.getRunningAnimations()) { _rig->stopAnimationByRole(role);
if (handle->getRole() == role) {
handle->stop();
}
}
} }
void MyAvatar::stopAnimation(const QString& url) { void MyAvatar::stopAnimation(const QString& url) {
@ -568,11 +526,7 @@ void MyAvatar::stopAnimation(const QString& url) {
QMetaObject::invokeMethod(this, "stopAnimation", Q_ARG(const QString&, url)); QMetaObject::invokeMethod(this, "stopAnimation", Q_ARG(const QString&, url));
return; return;
} }
foreach (const AnimationHandlePointer& handle, _skeletonModel.getRunningAnimations()) { _rig->stopAnimation(url);
if (handle->getURL() == url) {
handle->stop();
}
}
} }
AnimationDetails MyAvatar::getAnimationDetailsByRole(const QString& role) { AnimationDetails MyAvatar::getAnimationDetailsByRole(const QString& role) {
@ -583,7 +537,7 @@ AnimationDetails MyAvatar::getAnimationDetailsByRole(const QString& role) {
Q_ARG(const QString&, role)); Q_ARG(const QString&, role));
return result; return result;
} }
foreach (const AnimationHandlePointer& handle, _skeletonModel.getRunningAnimations()) { foreach (const AnimationHandlePointer& handle, _rig->getRunningAnimations()) {
if (handle->getRole() == role) { if (handle->getRole() == role) {
result = handle->getAnimationDetails(); result = handle->getAnimationDetails();
break; break;
@ -600,7 +554,7 @@ AnimationDetails MyAvatar::getAnimationDetails(const QString& url) {
Q_ARG(const QString&, url)); Q_ARG(const QString&, url));
return result; return result;
} }
foreach (const AnimationHandlePointer& handle, _skeletonModel.getRunningAnimations()) { foreach (const AnimationHandlePointer& handle, _rig->getRunningAnimations()) {
if (handle->getURL() == url) { if (handle->getURL() == url) {
result = handle->getAnimationDetails(); result = handle->getAnimationDetails();
break; break;
@ -646,9 +600,11 @@ void MyAvatar::saveData() {
settings.endArray(); settings.endArray();
settings.beginWriteArray("animationHandles"); settings.beginWriteArray("animationHandles");
for (int i = 0; i < _animationHandles.size(); i++) { auto animationHandles = _rig->getAnimationHandles();
for (int i = 0; i < animationHandles.size(); i++) {
settings.setArrayIndex(i); settings.setArrayIndex(i);
const AnimationHandlePointer& pointer = _animationHandles.at(i); const AnimationHandlePointer& pointer = animationHandles.at(i);
qCDebug(interfaceapp) << "Save animation" << pointer->getURL().toString();
settings.setValue("role", pointer->getRole()); settings.setValue("role", pointer->getRole());
settings.setValue("url", pointer->getURL()); settings.setValue("url", pointer->getURL());
settings.setValue("fps", pointer->getFPS()); settings.setValue("fps", pointer->getFPS());
@ -766,25 +722,19 @@ void MyAvatar::loadData() {
setAttachmentData(attachmentData); setAttachmentData(attachmentData);
int animationCount = settings.beginReadArray("animationHandles"); int animationCount = settings.beginReadArray("animationHandles");
while (_animationHandles.size() > animationCount) { _rig->deleteAnimations();
_animationHandles.takeLast()->stop();
}
while (_animationHandles.size() < animationCount) {
addAnimationHandle();
}
for (int i = 0; i < animationCount; i++) { for (int i = 0; i < animationCount; i++) {
settings.setArrayIndex(i); settings.setArrayIndex(i);
const AnimationHandlePointer& handle = _animationHandles.at(i); _rig->addAnimationByRole(settings.value("role", "idle").toString(),
handle->setRole(settings.value("role", "idle").toString()); settings.value("url").toString(),
handle->setURL(settings.value("url").toUrl()); loadSetting(settings, "fps", 30.0f),
handle->setFPS(loadSetting(settings, "fps", 30.0f)); loadSetting(settings, "priority", 1.0f),
handle->setPriority(loadSetting(settings, "priority", 1.0f)); settings.value("loop", true).toBool(),
handle->setLoop(settings.value("loop", true).toBool()); settings.value("hold", false).toBool(),
handle->setHold(settings.value("hold", false).toBool()); settings.value("firstFrame", 0.0f).toFloat(),
handle->setFirstFrame(settings.value("firstFrame", 0.0f).toFloat()); settings.value("lastFrame", INT_MAX).toFloat(),
handle->setLastFrame(settings.value("lastFrame", INT_MAX).toFloat()); settings.value("maskedJoints").toStringList(),
handle->setMaskedJoints(settings.value("maskedJoints").toStringList()); settings.value("startAutomatically", true).toBool());
handle->setStartAutomatically(settings.value("startAutomatically", true).toBool());
} }
settings.endArray(); settings.endArray();

View file

@ -61,9 +61,9 @@ public:
bool getShouldRenderLocally() const { return _shouldRender; } bool getShouldRenderLocally() const { return _shouldRender; }
float getRealWorldFieldOfView() { return _realWorldFieldOfView.get(); } float getRealWorldFieldOfView() { return _realWorldFieldOfView.get(); }
const QList<AnimationHandlePointer>& getAnimationHandles() const { return _animationHandles; } const QList<AnimationHandlePointer>& getAnimationHandles() const { return _rig->getAnimationHandles(); }
AnimationHandlePointer addAnimationHandle(); AnimationHandlePointer addAnimationHandle() { return _rig->createAnimationHandle(); }
void removeAnimationHandle(const AnimationHandlePointer& handle); void removeAnimationHandle(const AnimationHandlePointer& handle) { _rig->removeAnimationHandle(handle); }
/// Allows scripts to run animations. /// Allows scripts to run animations.
Q_INVOKABLE void startAnimation(const QString& url, float fps = 30.0f, float priority = 1.0f, bool loop = false, Q_INVOKABLE void startAnimation(const QString& url, float fps = 30.0f, float priority = 1.0f, bool loop = false,
@ -256,8 +256,6 @@ private:
bool _shouldRender; bool _shouldRender;
bool _billboardValid; bool _billboardValid;
float _oculusYawOffset; float _oculusYawOffset;
QList<AnimationHandlePointer> _animationHandles;
bool _feetTouchFloor; bool _feetTouchFloor;
eyeContactTarget _eyeContactTarget; eyeContactTarget _eyeContactTarget;

View file

@ -12,7 +12,7 @@
#include <queue> #include <queue>
#include "AnimationHandle.h" #include "AnimationHandle.h"
#include "AnimationLogging.h"
#include "Rig.h" #include "Rig.h"
void insertSorted(QList<AnimationHandlePointer>& handles, const AnimationHandlePointer& handle) { void insertSorted(QList<AnimationHandlePointer>& handles, const AnimationHandlePointer& handle) {
@ -27,9 +27,86 @@ void insertSorted(QList<AnimationHandlePointer>& handles, const AnimationHandleP
AnimationHandlePointer Rig::createAnimationHandle() { AnimationHandlePointer Rig::createAnimationHandle() {
AnimationHandlePointer handle(new AnimationHandle(getRigPointer())); AnimationHandlePointer handle(new AnimationHandle(getRigPointer()));
_animationHandles.insert(handle); _animationHandles.append(handle);
return handle; return handle;
} }
void Rig::removeAnimationHandle(const AnimationHandlePointer& handle) {
handle->stop();
// FIXME? Do we need to also animationHandle->clearJoints()? deleteAnimations(), below, was first written to do so, but did not first stop it.
_animationHandles.removeOne(handle);
}
void Rig::startAnimation(const QString& url, float fps, float priority,
bool loop, bool hold, float firstFrame, float lastFrame, const QStringList& maskedJoints) {
qCDebug(animation) << "startAnimation" << url << fps << priority << loop << hold << firstFrame << lastFrame << maskedJoints;
AnimationHandlePointer handle = nullptr;
foreach (const AnimationHandlePointer& candidate, _animationHandles) {
if (candidate->getURL() == url) {
handle = candidate;
break;
}
}
if (!handle) {
handle = createAnimationHandle();
handle->setURL(url);
}
handle->setFPS(fps);
handle->setPriority(priority);
handle->setLoop(loop);
handle->setHold(hold);
handle->setFirstFrame(firstFrame);
handle->setLastFrame(lastFrame);
handle->setMaskedJoints(maskedJoints);
handle->start();
}
void Rig::addAnimationByRole(const QString& role, const QString& url, float fps, float priority,
bool loop, bool hold, float firstFrame, float lastFrame, const QStringList& maskedJoints, bool startAutomatically) {
// check for a configured animation for the role
qCDebug(animation) << "addAnimationByRole" << role << url << fps << priority << loop << hold << firstFrame << lastFrame << maskedJoints << startAutomatically;
AnimationHandlePointer handle = nullptr;
foreach (const AnimationHandlePointer& candidate, _animationHandles) {
if (candidate->getRole() == role) {
handle = candidate;
break;
}
}
if (!handle) {
handle = createAnimationHandle();
handle->setRole(role);
}
handle->setURL(url);
handle->setFPS(fps);
handle->setPriority(priority);
handle->setLoop(loop);
handle->setHold(hold);
handle->setFirstFrame(firstFrame);
handle->setLastFrame(lastFrame);
handle->setMaskedJoints(maskedJoints);
if (startAutomatically) {
handle->start();
}
}
void Rig::startAnimationByRole(const QString& role, const QString& url, float fps, float priority,
bool loop, bool hold, float firstFrame, float lastFrame, const QStringList& maskedJoints) {
addAnimationByRole(role, url, fps, priority, loop, hold, firstFrame, lastFrame, maskedJoints, true);
}
void Rig::stopAnimationByRole(const QString& role) {
foreach (const AnimationHandlePointer& handle, getRunningAnimations()) {
if (handle->getRole() == role) {
handle->stop();
}
}
}
void Rig::stopAnimation(const QString& url) {
foreach (const AnimationHandlePointer& handle, getRunningAnimations()) {
if (handle->getURL() == url) {
handle->stop();
}
}
}
bool Rig::removeRunningAnimation(AnimationHandlePointer animationHandle) { bool Rig::removeRunningAnimation(AnimationHandlePointer animationHandle) {
return _runningAnimations.removeOne(animationHandle); return _runningAnimations.removeOne(animationHandle);
@ -44,10 +121,10 @@ bool Rig::isRunningAnimation(AnimationHandlePointer animationHandle) {
} }
void Rig::deleteAnimations() { void Rig::deleteAnimations() {
for (QSet<AnimationHandlePointer>::iterator it = _animationHandles.begin(); it != _animationHandles.end(); ) { for (auto animation : _animationHandles) {
(*it)->clearJoints(); removeAnimationHandle(animation);
it = _animationHandles.erase(it);
} }
_animationHandles.clear();
} }
float Rig::initJointStates(QVector<JointState> states, glm::mat4 parentTransform, int neckJointIndex) { float Rig::initJointStates(QVector<JointState> states, glm::mat4 parentTransform, int neckJointIndex) {

View file

@ -57,11 +57,22 @@ public:
RigPointer getRigPointer() { return shared_from_this(); } RigPointer getRigPointer() { return shared_from_this(); }
AnimationHandlePointer createAnimationHandle(); AnimationHandlePointer createAnimationHandle();
void removeAnimationHandle(const AnimationHandlePointer& handle);
bool removeRunningAnimation(AnimationHandlePointer animationHandle); bool removeRunningAnimation(AnimationHandlePointer animationHandle);
void addRunningAnimation(AnimationHandlePointer animationHandle); void addRunningAnimation(AnimationHandlePointer animationHandle);
bool isRunningAnimation(AnimationHandlePointer animationHandle); bool isRunningAnimation(AnimationHandlePointer animationHandle);
const QList<AnimationHandlePointer>& getRunningAnimations() const { return _runningAnimations; } const QList<AnimationHandlePointer>& getRunningAnimations() const { return _runningAnimations; }
void deleteAnimations(); void deleteAnimations();
const QList<AnimationHandlePointer>& getAnimationHandles() const { return _animationHandles; }
void startAnimation(const QString& url, float fps = 30.0f, float priority = 1.0f, bool loop = false,
bool hold = false, float firstFrame = 0.0f, float lastFrame = FLT_MAX, const QStringList& maskedJoints = QStringList());
void stopAnimation(const QString& url);
void startAnimationByRole(const QString& role, const QString& url = QString(), float fps = 30.0f,
float priority = 1.0f, bool loop = false, bool hold = false, float firstFrame = 0.0f,
float lastFrame = FLT_MAX, const QStringList& maskedJoints = QStringList());
void stopAnimationByRole(const QString& role);
void addAnimationByRole(const QString& role, const QString& url, float fps, float priority,
bool loop, bool hold, float firstFrame, float lastFrame, const QStringList& maskedJoints, bool startAutomatically);
float initJointStates(QVector<JointState> states, glm::mat4 parentTransform, int neckJointIndex); float initJointStates(QVector<JointState> states, glm::mat4 parentTransform, int neckJointIndex);
bool jointStatesEmpty() { return _jointStates.isEmpty(); }; bool jointStatesEmpty() { return _jointStates.isEmpty(); };
@ -124,7 +135,7 @@ public:
protected: protected:
QVector<JointState> _jointStates; QVector<JointState> _jointStates;
QSet<AnimationHandlePointer> _animationHandles; QList<AnimationHandlePointer> _animationHandles;
QList<AnimationHandlePointer> _runningAnimations; QList<AnimationHandlePointer> _runningAnimations;
JointState maybeCauterizeHead(int jointIndex) const; JointState maybeCauterizeHead(int jointIndex) const;