// // ScriptValueUtils.h // libraries/shared/src // // Created by Anthony Thibault on 4/15/16. // Copyright 2016 High Fidelity, Inc. // Copyright 2023 Overte e.V. // // Utilities for working with QtScriptValues // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // SPDX-License-Identifier: Apache-2.0 // /// @addtogroup ScriptEngine /// @{ #ifndef hifi_ScriptValueUtils_h #define hifi_ScriptValueUtils_h #include #include #include #include "ScriptValue.h" bool isListOfStrings(const ScriptValue& value); void registerMetaTypes(ScriptEngine* engine); // Mat4 /*@jsdoc * A 4 x 4 matrix, typically containing a scale, rotation, and translation transform. See also the {@link Mat4(0)|Mat4} object. * * @typedef {object} Mat4 * @property {number} r0c0 - Row 0, column 0 value. * @property {number} r1c0 - Row 1, column 0 value. * @property {number} r2c0 - Row 2, column 0 value. * @property {number} r3c0 - Row 3, column 0 value. * @property {number} r0c1 - Row 0, column 1 value. * @property {number} r1c1 - Row 1, column 1 value. * @property {number} r2c1 - Row 2, column 1 value. * @property {number} r3c1 - Row 3, column 1 value. * @property {number} r0c2 - Row 0, column 2 value. * @property {number} r1c2 - Row 1, column 2 value. * @property {number} r2c2 - Row 2, column 2 value. * @property {number} r3c2 - Row 3, column 2 value. * @property {number} r0c3 - Row 0, column 3 value. * @property {number} r1c3 - Row 1, column 3 value. * @property {number} r2c3 - Row 2, column 3 value. * @property {number} r3c3 - Row 3, column 3 value. */ ScriptValue mat4toScriptValue(ScriptEngine* engine, const glm::mat4& mat4); bool mat4FromScriptValue(const ScriptValue& object, glm::mat4& mat4); /*@jsdoc * A 2-dimensional vector. * * @typedef {object} Vec2 * @property {number} x - X-coordinate of the vector. Synonyms: u. * @property {number} y - Y-coordinate of the vector. Synonyms: v. * @example Vec2s can be set in multiple ways and modified with their aliases, but still stringify in the same way * Entities.editEntity(, { materialMappingPos: { x: 0.1, y: 0.2 }}); // { x: 0.1, y: 0.2 } * Entities.editEntity(, { materialMappingPos: { u: 0.3, v: 0.4 }}); // { x: 0.3, y: 0.4 } * Entities.editEntity(, { materialMappingPos: [0.5, 0.6] }); // { x: 0.5, y: 0.6 } * Entities.editEntity(, { materialMappingPos: 0.7 }); // { x: 0.7, y: 0.7 } * var color = Entities.getEntityProperties().materialMappingPos; // { x: 0.7, y: 0.7 } * color.v = 0.8; // { x: 0.7, y: 0.8 } */ ScriptValue vec2ToScriptValue(ScriptEngine* engine, const glm::vec2& vec2); bool vec2FromScriptValue(const ScriptValue& object, glm::vec2& vec2); /*@jsdoc * A 3-dimensional vector. See also the {@link Vec3(0)|Vec3} object. * * @typedef {object} Vec3 * @property {number} x - X-coordinate of the vector. Synonyms: r, red. * @property {number} y - Y-coordinate of the vector. Synonyms: g, green. * @property {number} z - Z-coordinate of the vector. Synonyms: b, blue. * @example Vec3 values can be set in multiple ways and modified with their aliases, but still stringify in the same * way. * Entities.editEntity(, { position: { x: 1, y: 2, z: 3 }}); // { x: 1, y: 2, z: 3 } * Entities.editEntity(, { position: { r: 4, g: 5, b: 6 }}); // { x: 4, y: 5, z: 6 } * Entities.editEntity(, { position: { red: 7, green: 8, blue: 9 }}); // { x: 7, y: 8, z: 9 } * Entities.editEntity(, { position: [10, 11, 12] }); // { x: 10, y: 11, z: 12 } * Entities.editEntity(, { position: 13 }); // { x: 13, y: 13, z: 13 } * var position = Entities.getEntityProperties().position; // { x: 13, y: 13, z: 13 } * position.g = 14; // { x: 13, y: 14, z: 13 } * position.blue = 15; // { x: 13, y: 14, z: 15 } * Entities.editEntity(, { position: "red"}); // { x: 255, y: 0, z: 0 } * Entities.editEntity(, { position: "#00FF00"}); // { x: 0, y: 255, z: 0 } */ ScriptValue vec3ToScriptValue(ScriptEngine* engine, const glm::vec3& vec3); ScriptValue vec3ColorToScriptValue(ScriptEngine* engine, const glm::vec3& vec3); bool vec3FromScriptValue(const ScriptValue& object, glm::vec3& vec3); /*@jsdoc * A color vector. See also the {@link Vec3(0)|Vec3} object. * * @typedef {object} Color * @property {number} red - Red component value. Integer in the range 0 - 255. Synonyms: r, x. * @property {number} green - Green component value. Integer in the range 0 - 255. Synonyms: g, y. * @property {number} blue - Blue component value. Integer in the range 0 - 255. Synonyms: b, z. * @example Colors can be set in multiple ways and modified with their aliases, but still stringify in the same way * Entities.editEntity(, { color: { x: 1, y: 2, z: 3 }}); // { red: 1, green: 2, blue: 3 } * Entities.editEntity(, { color: { r: 4, g: 5, b: 6 }}); // { red: 4, green: 5, blue: 6 } * Entities.editEntity(, { color: { red: 7, green: 8, blue: 9 }}); // { red: 7, green: 8, blue: 9 } * Entities.editEntity(, { color: [10, 11, 12] }); // { red: 10, green: 11, blue: 12 } * Entities.editEntity(, { color: 13 }); // { red: 13, green: 13, blue: 13 } * var color = Entities.getEntityProperties().color; // { red: 13, green: 13, blue: 13 } * color.g = 14; // { red: 13, green: 14, blue: 13 } * color.blue = 15; // { red: 13, green: 14, blue: 15 } * Entities.editEntity(, { color: "red"}); // { red: 255, green: 0, blue: 0 } * Entities.editEntity(, { color: "#00FF00"}); // { red: 0, green: 255, blue: 0 } */ /*@jsdoc * A color vector with real values. Values may also be null. See also the {@link Vec3(0)|Vec3} object. * * @typedef {object} ColorFloat * @property {number} red - Red component value. Real in the range 0 - 255. Synonyms: r, x. * @property {number} green - Green component value. Real in the range 0 - 255. Synonyms: g, y. * @property {number} blue - Blue component value. Real in the range 0 - 255. Synonyms: b, z. * @example ColorFloats can be set in multiple ways and modified with their aliases, but still stringify in the same way * Entities.editEntity(, { color: { x: 1, y: 2, z: 3 }}); // { red: 1, green: 2, blue: 3 } * Entities.editEntity(, { color: { r: 4, g: 5, b: 6 }}); // { red: 4, green: 5, blue: 6 } * Entities.editEntity(, { color: { red: 7, green: 8, blue: 9 }}); // { red: 7, green: 8, blue: 9 } * Entities.editEntity(, { color: [10, 11, 12] }); // { red: 10, green: 11, blue: 12 } * Entities.editEntity(, { color: 13 }); // { red: 13, green: 13, blue: 13 } * var color = Entities.getEntityProperties().color; // { red: 13, green: 13, blue: 13 } * color.g = 14; // { red: 13, green: 14, blue: 13 } * color.blue = 15; // { red: 13, green: 14, blue: 15 } * Entities.editEntity(, { color: "red"}); // { red: 255, green: 0, blue: 0 } * Entities.editEntity(, { color: "#00FF00"}); // { red: 0, green: 255, blue: 0 } */ ScriptValue u8vec3ToScriptValue(ScriptEngine* engine, const glm::u8vec3& vec3); ScriptValue u8vec3ColorToScriptValue(ScriptEngine* engine, const glm::u8vec3& vec3); bool u8vec3FromScriptValue(const ScriptValue& object, glm::u8vec3& vec3); /*@jsdoc * A 4-dimensional vector. * * @typedef {object} Vec4 * @property {number} x - X-coordinate of the vector. * @property {number} y - Y-coordinate of the vector. * @property {number} z - Z-coordinate of the vector. * @property {number} w - W-coordinate of the vector. */ ScriptValue vec4toScriptValue(ScriptEngine* engine, const glm::vec4& vec4); bool vec4FromScriptValue(const ScriptValue& object, glm::vec4& vec4); // Quaternions ScriptValue quatToScriptValue(ScriptEngine* engine, const glm::quat& quat); bool quatFromScriptValue(const ScriptValue& object, glm::quat& quat); /*@jsdoc * Defines a rectangular portion of an image or screen, or similar. * @typedef {object} Rect * @property {number} x - Left, x-coordinate value. * @property {number} y - Top, y-coordinate value. * @property {number} width - Width of the rectangle. * @property {number} height - Height of the rectangle. */ class QVector2D; ScriptValue qVector2DToScriptValue(ScriptEngine* engine, const QVector2D& qVector2D); bool qVector2DFromScriptValue(const ScriptValue& object, QVector2D& qVector2D); class QVector3D; ScriptValue qVector3DToScriptValue(ScriptEngine* engine, const QVector3D& qVector3D); bool qVector3DFromScriptValue(const ScriptValue& object, QVector3D& qVector3D); class QRect; ScriptValue qRectToScriptValue(ScriptEngine* engine, const QRect& rect); bool qRectFromScriptValue(const ScriptValue& object, QRect& rect); class QRectF; ScriptValue qRectFToScriptValue(ScriptEngine* engine, const QRectF& rect); bool qRectFFromScriptValue(const ScriptValue& object, QRectF& rect); // QColor class QColor; ScriptValue qColorToScriptValue(ScriptEngine* engine, const QColor& color); bool qColorFromScriptValue(const ScriptValue& object, QColor& color); //QTimer class QTimer; ScriptValue qTimerToScriptValue(ScriptEngine* engine, QTimer* const &in); bool qTimerFromScriptValue(const ScriptValue& object, QTimer* &out); class QUrl; ScriptValue qURLToScriptValue(ScriptEngine* engine, const QUrl& url); bool qURLFromScriptValue(const ScriptValue& object, QUrl& url); // vector Q_DECLARE_METATYPE(QVector) ScriptValue qVectorVec3ToScriptValue(ScriptEngine* engine, const QVector& vector); ScriptValue qVectorVec3ColorToScriptValue(ScriptEngine* engine, const QVector& vector); bool qVectorVec3FromScriptValue(const ScriptValue& array, QVector& vector); QVector qVectorVec3FromScriptValue(const ScriptValue& array); // vector Q_DECLARE_METATYPE(QVector) ScriptValue qVectorQuatToScriptValue(ScriptEngine* engine, const QVector& vector); bool qVectorQuatFromScriptValue(const ScriptValue& array, QVector& vector); QVector qVectorQuatFromScriptValue(const ScriptValue& array); // vector ScriptValue qVectorBoolToScriptValue(ScriptEngine* engine, const QVector& vector); bool qVectorBoolFromScriptValue(const ScriptValue& array, QVector& vector); QVector qVectorBoolFromScriptValue(const ScriptValue& array); // vector ScriptValue qVectorFloatToScriptValue(ScriptEngine* engine, const QVector& vector); bool qVectorFloatFromScriptValue(const ScriptValue& array, QVector& vector); QVector qVectorFloatFromScriptValue(const ScriptValue& array); // vector ScriptValue qVectorIntToScriptValue(ScriptEngine* engine, const QVector& vector); bool qVectorIntFromScriptValue(const ScriptValue& array, QVector& vector); ScriptValue qVectorQUuidToScriptValue(ScriptEngine* engine, const QVector& vector); bool qVectorQUuidFromScriptValue(const ScriptValue& array, QVector& vector); QVector qVectorQUuidFromScriptValue(const ScriptValue& array); class AACube; ScriptValue aaCubeToScriptValue(ScriptEngine* engine, const AACube& aaCube); bool aaCubeFromScriptValue(const ScriptValue& object, AACube& aaCube); class PickRay; ScriptValue pickRayToScriptValue(ScriptEngine* engine, const PickRay& pickRay); bool pickRayFromScriptValue(const ScriptValue& object, PickRay& pickRay); class Collision; ScriptValue collisionToScriptValue(ScriptEngine* engine, const Collision& collision); bool collisionFromScriptValue(const ScriptValue& object, Collision& collision); /*@jsdoc * UUIDs (Universally Unique IDentifiers) are used to uniquely identify entities, avatars, and the like. They are represented * in JavaScript as strings in the format, "{nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn}", where the "n"s are * hexadecimal digits. * @typedef {string} Uuid */ //Q_DECLARE_METATYPE(QUuid) // don't need to do this for QUuid since it's already a meta type ScriptValue quuidToScriptValue(ScriptEngine* engine, const QUuid& uuid); bool quuidFromScriptValue(const ScriptValue& object, QUuid& uuid); //Q_DECLARE_METATYPE(QSizeF) // Don't need to to this becase it's arleady a meta type class QSizeF; ScriptValue qSizeFToScriptValue(ScriptEngine* engine, const QSizeF& qSizeF); bool qSizeFFromScriptValue(const ScriptValue& object, QSizeF& qSizeF); class AnimationDetails; ScriptValue animationDetailsToScriptValue(ScriptEngine* engine, const AnimationDetails& event); bool animationDetailsFromScriptValue(const ScriptValue& object, AnimationDetails& event); class MeshProxy; ScriptValue meshToScriptValue(ScriptEngine* engine, MeshProxy* const& in); bool meshFromScriptValue(const ScriptValue& value, MeshProxy*& out); class MeshProxyList; ScriptValue meshesToScriptValue(ScriptEngine* engine, const MeshProxyList& in); bool meshesFromScriptValue(const ScriptValue& value, MeshProxyList& out); class MeshFace; ScriptValue meshFaceToScriptValue(ScriptEngine* engine, const MeshFace& meshFace); bool meshFaceFromScriptValue(const ScriptValue& object, MeshFace& meshFaceResult); ScriptValue qVectorMeshFaceToScriptValue(ScriptEngine* engine, const QVector& vector); bool qVectorMeshFaceFromScriptValue(const ScriptValue& array, QVector& result); enum class StencilMaskMode; ScriptValue stencilMaskModeToScriptValue(ScriptEngine* engine, const StencilMaskMode& stencilMode); bool stencilMaskModeFromScriptValue(const ScriptValue& object, StencilMaskMode& stencilMode); class MiniPromise; bool promiseFromScriptValue(const ScriptValue& object, std::shared_ptr& promise); ScriptValue promiseToScriptValue(ScriptEngine* engine, const std::shared_ptr& promise); class EntityItemID; ScriptValue EntityItemIDtoScriptValue(ScriptEngine* engine, const EntityItemID& properties); bool EntityItemIDfromScriptValue(const ScriptValue& object, EntityItemID& properties); QVector qVectorEntityItemIDFromScriptValue(const ScriptValue& array); #endif // #define hifi_ScriptValueUtils_h /// @}