diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index e5b2d32df1..2b41c95d97 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -2701,17 +2701,60 @@ void EntityScriptingInterface::canvasPushImage(const QUuid& entityID, const Canv auto canvas = std::dynamic_pointer_cast(entity); if (image.buffer.length() != (int)(4 * image.width * image.height)) { - qCWarning(entities) << "canvasPushImage: \"image\" has invalid buffer size, expected " << (4 * image.width * image.height) << ", got " << image.buffer.length(); + qCCritical(entities) << "canvasPushImage: \"image\" has invalid buffer size, expected " << (4 * image.width * image.height) << ", got " << image.buffer.length(); return; } if (image.width != canvas->getWidth() || image.height != canvas->getHeight()) { - qCWarning(entities) << "canvasPushImage: \"image\" dimensions don't match canvas, expected " << canvas->getWidth() << "x" << canvas->getHeight() << ", got " << image.width << "x" << image.height; + qCCritical(entities) << "canvasPushImage: \"image\" dimensions don't match canvas, expected " << canvas->getWidth() << "x" << canvas->getHeight() << ", got " << image.width << "x" << image.height; return; } canvas->setImageData(image); } else { - qCWarning(entities) << "canvasSubmitImage called on a non-canvas entity " << entityID; + qCWarning(entities) << "canvasPushImage called on a non-canvas entity " << entityID; + } +} + +void EntityScriptingInterface::canvasPushCommands(const QUuid& entityID, const QVector& commands) { + EntityItemPointer entity = _entityTree->findEntityByEntityItemID(EntityItemID(entityID)); + if (!entity) { + return; + } + + if (entity->getType() == EntityTypes::Canvas) { + auto canvas = std::dynamic_pointer_cast(entity); + canvas->pushCommands(commands); + } else { + qCWarning(entities) << "canvasPushCommands called on a non-canvas entity " << entityID; + } +} + +CanvasImage EntityScriptingInterface::canvasGetImage(const QUuid& entityID) { + EntityItemPointer entity = _entityTree->findEntityByEntityItemID(EntityItemID(entityID)); + if (!entity) { + return CanvasImage(); + } + + if (entity->getType() == EntityTypes::Canvas) { + auto canvas = std::dynamic_pointer_cast(entity); + return CanvasImage { canvas->getImageData(), canvas->getWidth(), canvas->getHeight() }; + } else { + qCWarning(entities) << "canvasCommit called on a non-canvas entity " << entityID; + return CanvasImage(); + } +} + +void EntityScriptingInterface::canvasCommit(const QUuid& entityID) { + EntityItemPointer entity = _entityTree->findEntityByEntityItemID(EntityItemID(entityID)); + if (!entity) { + return; + } + + if (entity->getType() == EntityTypes::Canvas) { + auto canvas = std::dynamic_pointer_cast(entity); + canvas->commit(); + } else { + qCWarning(entities) << "canvasCommit called on a non-canvas entity " << entityID; } } diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index 530e40cbed..c0576f9051 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -2228,7 +2228,7 @@ public slots: * @function Entities.canvasCommit * @param {Uuid} entityID - The canvas entity to update the texture of. */ - Q_INVOKABLE void canvasCommit(const QUuid* entityID); + Q_INVOKABLE void canvasCommit(const QUuid& entityID); signals: /*@jsdoc diff --git a/libraries/script-engine/src/CanvasCommand.cpp b/libraries/script-engine/src/CanvasCommand.cpp index ca837acc10..8902e82e9c 100644 --- a/libraries/script-engine/src/CanvasCommand.cpp +++ b/libraries/script-engine/src/CanvasCommand.cpp @@ -347,6 +347,16 @@ bool qVectorCanvasCommandFromScriptValue(const ScriptValue& array, QVector& list) { + auto array = engine->newArray(list.length()); + + for (int i = 0; i < list.length(); i++) { + array.setProperty(i, canvasCommandToScriptValue(engine, list.at(i))); + } + + return array; +} + ScriptValue canvasImageToScriptValue(ScriptEngine* engine, const CanvasImage& img) { ScriptValue obj = engine->newObject(); obj.setProperty(IMG_WIDTH_PROP_NAME, img.width); diff --git a/libraries/script-engine/src/CanvasCommand.h b/libraries/script-engine/src/CanvasCommand.h index e2c63acb8d..3d64d50192 100644 --- a/libraries/script-engine/src/CanvasCommand.h +++ b/libraries/script-engine/src/CanvasCommand.h @@ -27,6 +27,9 @@ class ScriptEngine; struct CanvasImage { QByteArray buffer; int width, height; + + CanvasImage() : buffer(QByteArray()), width(0), height(0) {} + CanvasImage(QByteArray buffer, int width, int height) : buffer(buffer), width(width), height(height) {} }; class CanvasCommand { @@ -168,6 +171,31 @@ public: } } + CanvasCommand& operator=(const CanvasCommand& other) noexcept { + _tag = other._tag; + switch (other._tag) { + case Variant::Invalid: _invalid = other._invalid; break; + case Variant::SetStrokeWidth: _setStrokeWidth = _setStrokeWidth; break; + case Variant::SetColor: _setColor = other._setColor; break; + case Variant::SetHints: _setHints = other._setHints; break; + case Variant::SetBlendMode: _setBlendMode = other._setBlendMode; break; + case Variant::SetFont: _setFont = other._setFont; break; + case Variant::ClearRect: _clearRect = other._clearRect; break; + case Variant::FillPath: _fillPath = other._fillPath; break; + case Variant::FillRect: _fillRect = other._fillRect; break; + case Variant::FillEllipse: _fillEllipse = other._fillEllipse; break; + case Variant::FillText: _fillText = other._fillText; break; + case Variant::StrokePath: _strokePath = other._strokePath; break; + case Variant::StrokeRect: _strokeRect = other._strokeRect; break; + case Variant::StrokeArc: _strokeArc = other._strokeArc; break; + case Variant::StrokeEllipse: _strokeEllipse = other._strokeEllipse; break; + case Variant::Point: _point = other._point; break; + case Variant::Line: _line = other._line; break; + case Variant::ImageCopy: _imageCopy = other._imageCopy; break; + } + return *this; + } + void set(Invalid&& cmd) { _tag = Variant::Invalid; _invalid = cmd; } void set(SetStrokeWidth&& cmd) { _tag = Variant::SetStrokeWidth; _setStrokeWidth = cmd; } void set(SetColor&& cmd) { _tag = Variant::SetColor; _setColor = cmd; }