1032 lines · 39.1 KB · C++
RawDownload
    }    obj.setProperty("x", quat.x);    obj.setProperty("y", quat.y);    obj.setProperty("z", quat.z);    obj.setProperty("w", quat.w);    return obj;} // V8TODO: add similar check to other conversionsbool quatFromScriptValue(const ScriptValue& object, glm::quat& quat) {    if (!object.isValid()) {        return false;    }    if (!object.isObject()) {        return false;    }    QVariant x = object.property("x").toVariant();    QVariant y = object.property("y").toVariant();    QVariant z = object.property("z").toVariant();    QVariant w = object.property("w").toVariant();    if (!x.isValid() || !y.isValid() || !z.isValid() || !w.isValid()) {        return false;    }    quat.x = object.property("x").toVariant().toFloat();    quat.y = object.property("y").toVariant().toFloat();    quat.z = object.property("z").toVariant().toFloat();    quat.w = object.property("w").toVariant().toFloat();     // enforce normalized quaternion    float length = glm::length(quat);    if (length > FLT_EPSILON) {        quat /= length;    } else {        quat = glm::quat();    }    return true;} ScriptValue qVectorQuatToScriptValue(ScriptEngine* engine, const QVector<glm::quat>& vector) {    ScriptValue array = engine->newArray();    for (int i = 0; i < vector.size(); i++) {        array.setProperty(i, quatToScriptValue(engine, vector.at(i)));    }    return array;} ScriptValue qVectorBoolToScriptValue(ScriptEngine* engine, const QVector<bool>& vector) {    ScriptValue array = engine->newArray();    for (int i = 0; i < vector.size(); i++) {        array.setProperty(i, vector.at(i));    }    return array;} QVector<float> qVectorFloatFromScriptValue(const ScriptValue& array) {    if (!array.isArray()) {        return QVector<float>();    }    QVector<float> newVector;    int length = array.property("length").toInteger();    newVector.reserve(length);    for (int i = 0; i < length; i++) {        if (array.property(i).isNumber()) {            newVector << array.property(i).toNumber();        }    }     return newVector;} ScriptValue qVectorQUuidToScriptValue(ScriptEngine* engine, const QVector<QUuid>& vector) {    ScriptValue array = engine->newArray();    for (int i = 0; i < vector.size(); i++) {        array.setProperty(i, quuidToScriptValue(engine, vector.at(i)));    }    return array;} bool qVectorQUuidFromScriptValue(const ScriptValue& array, QVector<QUuid>& vector) {    int length = array.property("length").toInteger();     for (int i = 0; i < length; i++) {        vector << array.property(i).toVariant().toUuid();    }    return true;} QVector<QUuid> qVectorQUuidFromScriptValue(const ScriptValue& array) {    if (!array.isArray()) {        return QVector<QUuid>();    }    QVector<QUuid> newVector;    int length = array.property("length").toInteger();    newVector.reserve(length);    for (int i = 0; i < length; i++) {        QString uuidAsString = array.property(i).toString();        QUuid fromString(uuidAsString);        newVector << fromString;    }    return newVector;} ScriptValue qVectorFloatToScriptValue(ScriptEngine* engine, const QVector<float>& vector) {    ScriptValue array = engine->newArray();    for (int i = 0; i < vector.size(); i++) {        float num = vector.at(i);        array.setProperty(i, engine->newValue(num));    }    return array;} ScriptValue qVectorIntToScriptValue(ScriptEngine* engine, const QVector<uint32_t>& vector) {    ScriptValue array = engine->newArray();    for (int i = 0; i < vector.size(); i++) {        int num = vector.at(i);        array.setProperty(i, engine->newValue(num));    }    return array;} bool qVectorFloatFromScriptValue(const ScriptValue& array, QVector<float>& vector) {    int length = array.property("length").toInteger();     for (int i = 0; i < length; i++) {        vector << array.property(i).toVariant().toFloat();    }    return true;} bool qVectorIntFromScriptValue(const ScriptValue& array, QVector<uint32_t>& vector) {    int length = array.property("length").toInteger();     for (int i = 0; i < length; i++) {        vector << array.property(i).toVariant().toInt();    }    return true;} QVector<glm::quat> qVectorQuatFromScriptValue(const ScriptValue& array) {    QVector<glm::quat> newVector;    int length = array.property("length").toInteger();     for (int i = 0; i < length; i++) {        glm::quat newQuat = glm::quat();        quatFromScriptValue(array.property(i), newQuat);        newVector << newQuat;    }    return newVector;} bool qVectorQuatFromScriptValue(const ScriptValue& array, QVector<glm::quat>& vector) {    int length = array.property("length").toInteger();     for (int i = 0; i < length; i++) {        glm::quat newQuat = glm::quat();        quatFromScriptValue(array.property(i), newQuat);        vector << newQuat;    }    return true;} QVector<bool> qVectorBoolFromScriptValue(const ScriptValue& array) {    QVector<bool> newVector;    int length = array.property("length").toInteger();     for (int i = 0; i < length; i++) {        newVector << array.property(i).toBool();    }    return newVector;} bool qVectorBoolFromScriptValue(const ScriptValue& array, QVector<bool>& vector) {    int length = array.property("length").toInteger();     for (int i = 0; i < length; i++) {        vector << array.property(i).toBool();    }    return true;} ScriptValue qRectToScriptValue(ScriptEngine* engine, const QRect& rect) {    ScriptValue obj = engine->newObject();    obj.setProperty("x", rect.x());    obj.setProperty("y", rect.y());    obj.setProperty("width", rect.width());    obj.setProperty("height", rect.height());    return obj;} bool qRectFromScriptValue(const ScriptValue& object, QRect& rect) {    rect.setX(object.property("x").toVariant().toInt());    rect.setY(object.property("y").toVariant().toInt());    rect.setWidth(object.property("width").toVariant().toInt());    rect.setHeight(object.property("height").toVariant().toInt());    return true;} ScriptValue qRectFToScriptValue(ScriptEngine* engine, const QRectF& rect) {    ScriptValue obj = engine->newObject();    obj.setProperty("x", rect.x());    obj.setProperty("y", rect.y());    obj.setProperty("width", rect.width());    obj.setProperty("height", rect.height());    return obj;} bool qRectFFromScriptValue(const ScriptValue& object, QRectF& rect) {    rect.setX(object.property("x").toVariant().toFloat());    rect.setY(object.property("y").toVariant().toFloat());    rect.setWidth(object.property("width").toVariant().toFloat());    rect.setHeight(object.property("height").toVariant().toFloat());    return true;} ScriptValue qColorToScriptValue(ScriptEngine* engine, const QColor& color) {    ScriptValue object = engine->newObject();    object.setProperty("red", color.red());    object.setProperty("green", color.green());    object.setProperty("blue", color.blue());    object.setProperty("alpha", color.alpha());    return object;} /*@jsdoc * An axis-aligned cube, defined as the bottom right near (minimum axes values) corner of the cube plus the dimension of its * sides. * @typedef {object} AACube * @property {number} x - X coordinate of the brn corner of the cube. * @property {number} y - Y coordinate of the brn corner of the cube. * @property {number} z - Z coordinate of the brn corner of the cube. * @property {number} scale - The dimensions of each side of the cube. */ScriptValue aaCubeToScriptValue(ScriptEngine* engine, const AACube& aaCube) {    ScriptValue obj = engine->newObject();    const glm::vec3& corner = aaCube.getCorner();    obj.setProperty("x", corner.x);    obj.setProperty("y", corner.y);    obj.setProperty("z", corner.z);    obj.setProperty("scale", aaCube.getScale());    return obj;} bool aaCubeFromScriptValue(const ScriptValue& object, AACube& aaCube) {    glm::vec3 corner;    corner.x = object.property("x").toVariant().toFloat();    corner.y = object.property("y").toVariant().toFloat();    corner.z = object.property("z").toVariant().toFloat();    float scale = object.property("scale").toVariant().toFloat();     aaCube.setBox(corner, scale);    return true;} ScriptValue qTimerToScriptValue(ScriptEngine* engine, QTimer* const &in) {    return engine->newQObject(in, ScriptEngine::QtOwnership);} bool qTimerFromScriptValue(const ScriptValue& object, QTimer* &out) {    return (out = qobject_cast<QTimer*>(object.toQObject())) != nullptr;} bool qColorFromScriptValue(const ScriptValue& object, QColor& color) {    if (object.isNumber()) {        color.setRgb(object.toUInt32());     } else if (object.isString()) {        color.setNamedColor(object.toString());     } else {        ScriptValue alphaValue = object.property("alpha");        color.setRgb(object.property("red").toInt32(), object.property("green").toInt32(), object.property("blue").toInt32(),                     alphaValue.isNumber() ? alphaValue.toInt32() : 255);    }    return true;} ScriptValue qURLToScriptValue(ScriptEngine* engine, const QUrl& url) {    return engine->newValue(url.toString());} bool qURLFromScriptValue(const ScriptValue& object, QUrl& url) {    url = object.toString();    return true;} ScriptValue pickRayToScriptValue(ScriptEngine* engine, const PickRay& pickRay) {    ScriptValue obj = engine->newObject();    ScriptValue origin = vec3ToScriptValue(engine, pickRay.origin);    obj.setProperty("origin", origin);    ScriptValue direction = vec3ToScriptValue(engine, pickRay.direction);    obj.setProperty("direction", direction);    return obj;} bool pickRayFromScriptValue(const ScriptValue& object, PickRay& pickRay) {    ScriptValue originValue = object.property("origin");    if (originValue.isValid()) {        auto x = originValue.property("x");        auto y = originValue.property("y");        auto z = originValue.property("z");        if (x.isValid() && y.isValid() && z.isValid()) {            pickRay.origin.x = x.toVariant().toFloat();            pickRay.origin.y = y.toVariant().toFloat();            pickRay.origin.z = z.toVariant().toFloat();        }    }    ScriptValue directionValue = object.property("direction");    if (directionValue.isValid()) {        auto x = directionValue.property("x");        auto y = directionValue.property("y");        auto z = directionValue.property("z");        if (x.isValid() && y.isValid() && z.isValid()) {            pickRay.direction.x = x.toVariant().toFloat();            pickRay.direction.y = y.toVariant().toFloat();            pickRay.direction.z = z.toVariant().toFloat();        }    }    return true;} /*@jsdoc * Details of a collision between avatars and entities. * @typedef {object} Collision * @property {ContactEventType} type - The contact type of the collision event. * @property {Uuid} idA - The ID of one of the avatars or entities in the collision. * @property {Uuid} idB - The ID of the other of the avatars or entities in the collision. * @property {Vec3} penetration - The amount of penetration between the two items. * @property {Vec3} contactPoint - The point of contact. * @property {Vec3} velocityChange - The change in relative velocity of the two items, in m/s. */ScriptValue collisionToScriptValue(ScriptEngine* engine, const Collision& collision) {    ScriptValue obj = engine->newObject();    obj.setProperty("type", collision.type);    obj.setProperty("idA", quuidToScriptValue(engine, collision.idA));    obj.setProperty("idB", quuidToScriptValue(engine, collision.idB));    obj.setProperty("penetration", vec3ToScriptValue(engine, collision.penetration));    obj.setProperty("contactPoint", vec3ToScriptValue(engine, collision.contactPoint));    obj.setProperty("velocityChange", vec3ToScriptValue(engine, collision.velocityChange));    return obj;} bool collisionFromScriptValue(const ScriptValue& object, Collision& collision) {    // TODO: implement this when we know what it means to accept collision events from JS    return false;} ScriptValue quuidToScriptValue(ScriptEngine* engine, const QUuid& uuid) {    if (uuid.isNull()) {        return engine->nullValue();    }    ScriptValue obj(engine->newValue(uuid.toString()));    return obj;} bool quuidFromScriptValue(const ScriptValue& object, QUuid& uuid) {    if (object.isNull()) {        uuid = QUuid();        return true;    }    QString uuidAsString = object.toVariant().toString();    QUuid fromString(uuidAsString);    uuid = fromString;    return true;} /*@jsdoc * A 2D size value. * @typedef {object} Size * @property {number} height - The height value. * @property {number} width - The width value. */ScriptValue qSizeFToScriptValue(ScriptEngine* engine, const QSizeF& qSizeF) {    ScriptValue obj = engine->newObject();    obj.setProperty("width", qSizeF.width());    obj.setProperty("height", qSizeF.height());    return obj;} bool qSizeFFromScriptValue(const ScriptValue& object, QSizeF& qSizeF) {    qSizeF.setWidth(object.property("width").toVariant().toFloat());    qSizeF.setHeight(object.property("height").toVariant().toFloat());    return true;} /*@jsdoc * The details of an animation that is playing. * @typedef {object} Avatar.AnimationDetails * @property {string} role - <em>Not used.</em> * @property {string} url - The URL to the animation file. Animation files need to be in glTF or FBX format but only need to *     contain the avatar skeleton and animation data. glTF models may be in JSON or binary format (".gltf" or ".glb" URLs *     respectively). *     <p><strong>Warning:</strong> glTF animations currently do not always animate correctly.</p> * @property {number} fps - The frames per second(FPS) rate for the animation playback. 30 FPS is normal speed. * @property {number} priority - <em>Not used.</em> * @property {boolean} loop - <code>true</code> if the animation should loop, <code>false</code> if it shouldn't. * @property {boolean} hold - <em>Not used.</em> * @property {number} firstFrame - The frame the animation should start at. * @property {number} lastFrame - The frame the animation should stop at. * @property {boolean} running - <em>Not used.</em> * @property {number} currentFrame - The current frame being played. * @property {boolean} startAutomatically - <em>Not used.</em> * @property {boolean} allowTranslation - <em>Not used.</em> */ScriptValue animationDetailsToScriptValue(ScriptEngine* engine, const AnimationDetails& details) {    ScriptValue obj = engine->newObject();    obj.setProperty("role", details.role);    obj.setProperty("url", details.url.toString());    obj.setProperty("fps", details.fps);    obj.setProperty("priority", details.priority);    obj.setProperty("loop", details.loop);    obj.setProperty("hold", details.hold);    obj.setProperty("startAutomatically", details.startAutomatically);    obj.setProperty("firstFrame", details.firstFrame);    obj.setProperty("lastFrame", details.lastFrame);    obj.setProperty("running", details.running);    obj.setProperty("currentFrame", details.currentFrame);    obj.setProperty("allowTranslation", details.allowTranslation);    return obj;} bool animationDetailsFromScriptValue(const ScriptValue& object, AnimationDetails& details) {    // nothing for now...    return false;} ScriptValue meshToScriptValue(ScriptEngine* engine, MeshProxy* const& in) {    return engine->newQObject(in, ScriptEngine::QtOwnership);} bool meshFromScriptValue(const ScriptValue& value, MeshProxy*& out) {    return (out = qobject_cast<MeshProxy*>(value.toQObject())) != nullptr;} ScriptValue meshesToScriptValue(ScriptEngine* engine, const MeshProxyList& in) {    // ScriptValueList result;    ScriptValue result = engine->newArray();    int i = 0;    foreach (MeshProxy* const meshProxy, in) { result.setProperty(i++, meshToScriptValue(engine, meshProxy)); }    return result;} bool meshesFromScriptValue(const ScriptValue& value, MeshProxyList& out) {    ScriptValueIteratorPointer itr(value.newIterator());     qCDebug(scriptengine) << "in meshesFromScriptValue, value.length =" << value.property("length").toInt32();     while (itr->hasNext()) {        itr->next();        MeshProxy* meshProxy = scriptvalue_cast<MeshProxyList::value_type>(itr->value());        if (meshProxy) {            out.append(meshProxy);        } else {            qCDebug(scriptengine) << "null meshProxy";        }    }    return true;} /*@jsdoc * A triangle in a mesh. * @typedef {object} MeshFace * @property {number[]} vertices - The indexes of the three vertices that make up the face. */ScriptValue meshFaceToScriptValue(ScriptEngine* engine, const MeshFace& meshFace) {    ScriptValue obj = engine->newObject();    obj.setProperty("vertices", qVectorIntToScriptValue(engine, meshFace.vertexIndices));    return obj;} bool meshFaceFromScriptValue(const ScriptValue& object, MeshFace& meshFaceResult) {    return qVectorIntFromScriptValue(object.property("vertices"), meshFaceResult.vertexIndices);} ScriptValue qVectorMeshFaceToScriptValue(ScriptEngine* engine, const QVector<MeshFace>& vector) {    ScriptValue array = engine->newArray();    for (int i = 0; i < vector.size(); i++) {        array.setProperty(i, meshFaceToScriptValue(engine, vector.at(i)));    }    return array;} bool qVectorMeshFaceFromScriptValue(const ScriptValue& array, QVector<MeshFace>& result) {    int length = array.property("length").toInteger();    result.clear();     for (int i = 0; i < length; i++) {        MeshFace meshFace = MeshFace();        meshFaceFromScriptValue(array.property(i), meshFace);        result << meshFace;    }    return true;} ScriptValue stencilMaskModeToScriptValue(ScriptEngine* engine, const StencilMaskMode& stencilMode) {    return engine->newValue((int)stencilMode);} bool stencilMaskModeFromScriptValue(const ScriptValue& object, StencilMaskMode& stencilMode) {    stencilMode = StencilMaskMode(object.toVariant().toInt());    return true;

Lines 501–1000 of 1032. Previous lines