// // AnimationCache.cpp // libraries/script-engine/src/ // // Created by Andrzej Kapolka on 4/14/14. // Copyright (c) 2014 High Fidelity, Inc. All rights reserved. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // #include #include #include "AnimationCache.h" static int animationPointerMetaTypeId = qRegisterMetaType(); AnimationCache::AnimationCache(QObject* parent) : ResourceCache(parent) { } AnimationPointer AnimationCache::getAnimation(const QUrl& url) { if (QThread::currentThread() != thread()) { AnimationPointer result; QMetaObject::invokeMethod(this, "getAnimation", Qt::BlockingQueuedConnection, Q_RETURN_ARG(AnimationPointer, result), Q_ARG(const QUrl&, url)); return result; } return getResource(url).staticCast(); } QSharedPointer AnimationCache::createResource(const QUrl& url, const QSharedPointer& fallback, bool delayLoad, const void* extra) { return QSharedPointer(new Animation(url), &Resource::allReferencesCleared); } Animation::Animation(const QUrl& url) : Resource(url) { } class AnimationReader : public QRunnable { public: AnimationReader(const QWeakPointer& animation, QNetworkReply* reply); virtual void run(); private: QWeakPointer _animation; QNetworkReply* _reply; }; AnimationReader::AnimationReader(const QWeakPointer& animation, QNetworkReply* reply) : _animation(animation), _reply(reply) { } void AnimationReader::run() { QSharedPointer animation = _animation.toStrongRef(); if (!animation.isNull()) { QMetaObject::invokeMethod(animation.data(), "setGeometry", Q_ARG(const FBXGeometry&, readFBX(_reply->readAll(), QVariantHash()))); } _reply->deleteLater(); } QStringList Animation::getJointNames() const { if (QThread::currentThread() != thread()) { QStringList result; QMetaObject::invokeMethod(const_cast(this), "getJointNames", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QStringList, result)); return result; } QStringList names; foreach (const FBXJoint& joint, _geometry.joints) { names.append(joint.name); } return names; } QVector Animation::getFrames() const { if (QThread::currentThread() != thread()) { QVector result; QMetaObject::invokeMethod(const_cast(this), "getFrames", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QVector, result)); return result; } return _geometry.animationFrames; } void Animation::setGeometry(const FBXGeometry& geometry) { _geometry = geometry; finishedLoading(true); } void Animation::downloadFinished(QNetworkReply* reply) { // send the reader off to the thread pool QThreadPool::globalInstance()->start(new AnimationReader(_self, reply)); }