Put ScriptableResource state enum in prototype

This commit is contained in:
Zach Pomerantz 2016-04-27 18:27:41 -07:00
parent 0c78d5bdd1
commit 1e8d45aecb
4 changed files with 31 additions and 18 deletions

View file

@ -121,17 +121,7 @@ QSharedPointer<Resource> ResourceCacheSharedItems::getHighestPendingRequest() {
ScriptableResource::ScriptableResource(const QUrl& url) :
QObject(nullptr),
_url(url) {
// Expose enum State to JS/QML via properties
QObject* state = new QObject(this);
state->setObjectName("ResourceState");
setProperty("State", QVariant::fromValue(state));
auto metaEnum = QMetaEnum::fromType<State>();
for (int i = 0; i < metaEnum.keyCount(); ++i) {
state->setProperty(metaEnum.key(i), metaEnum.value(i));
}
}
_url(url) { }
void ScriptableResource::release() {
disconnectHelper();

View file

@ -24,9 +24,12 @@
#include <QtCore/QWeakPointer>
#include <QtCore/QReadWriteLock>
#include <QtCore/QQueue>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QScriptEngine>
#include <DependencyManager.h>
#include "ResourceManager.h"

View file

@ -285,6 +285,23 @@ static void scriptableResourceFromScriptValue(const QScriptValue& value, Scripta
resource = static_cast<ScriptableResourceRawPtr>(value.toQObject());
}
static QScriptValue createScriptableResourcePrototype(QScriptEngine* engine) {
auto prototype = engine->newObject();
// Expose enum State to JS/QML via properties
QObject* state = new QObject(engine);
state->setObjectName("ResourceState");
auto metaEnum = QMetaEnum::fromType<ScriptableResource::State>();
for (int i = 0; i < metaEnum.keyCount(); ++i) {
state->setProperty(metaEnum.key(i), metaEnum.value(i));
}
auto prototypeState = engine->newQObject(state, QScriptEngine::QtOwnership, QScriptEngine::ExcludeSlots | QScriptEngine::ExcludeSuperClassMethods);
prototype.setProperty("State", prototypeState);
return prototype;
}
void ScriptEngine::init() {
if (_isInitialized) {
return; // only initialize once
@ -342,11 +359,14 @@ void ScriptEngine::init() {
registerGlobalObject("Vec3", &_vec3Library);
registerGlobalObject("Mat4", &_mat4Library);
registerGlobalObject("Uuid", &_uuidLibrary);
registerGlobalObject("AnimationCache", DependencyManager::get<AnimationCache>().data());
registerGlobalObject("Messages", DependencyManager::get<MessagesClient>().data());
qScriptRegisterMetaType(this, animVarMapToScriptValue, animVarMapFromScriptValue);
qScriptRegisterMetaType(this, resultHandlerToScriptValue, resultHandlerFromScriptValue);
// Scriptable cache access
auto resourcePrototype = createScriptableResourcePrototype(this);
globalObject().setProperty("Resource", resourcePrototype);
setDefaultPrototype(qMetaTypeId<ScriptableResource*>(), resourcePrototype);
qScriptRegisterMetaType(this, scriptableResourceToScriptValue, scriptableResourceFromScriptValue);
// constants

View file

@ -19,15 +19,15 @@ function getFrame(callback) {
var FRAME_URL = "http://hifi-production.s3.amazonaws.com/tutorials/pictureFrame/finalFrame.fbx";
var model = ModelCache.prefetch(FRAME_URL);
if (model.loaded) {
makeFrame(true);
if (model.state = Resource.State.FINISHED) {
makeFrame(Resource.State.FINISHED);
} else {
model.stateChanged.connect(makeFrame);
}
function makeFrame(state) {
if (state == 4) { throw "Failed to load frame"; }
if (state != 3) { return; }
if (state == Resource.State.FAILED) { throw "Failed to load frame"; }
if (state != Resource.State.FINISHED) { return; }
var pictureFrameProperties = {
name: 'scriptableResourceTest Picture Frame',
@ -67,10 +67,10 @@ function prefetch(callback) {
var filepath = MOVIE_URL + padded + '.jpg';
var texture = TextureCache.prefetch(filepath);
frames.push(texture);
if (!texture.loaded) {
if (!texture.state == Resource.State.FINISHED) {
numLoading++;
texture.stateChanged.connect(function(state) {
if (state == 3 || state == 4) {
if (state == Resource.State.FAILED || state == Resource.State.FINISHED) {
--numLoading;
if (!numLoading) { callback(frames); }
}