CanvasCommand stuff building properly but not functional from script yet

This commit is contained in:
Ada 2025-02-28 04:41:34 +10:00
parent 0026a9f9d2
commit d502a292b7
4 changed files with 85 additions and 4 deletions

View file

@ -2701,17 +2701,60 @@ void EntityScriptingInterface::canvasPushImage(const QUuid& entityID, const Canv
auto canvas = std::dynamic_pointer_cast<CanvasEntityItem>(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<CanvasCommand>& commands) {
EntityItemPointer entity = _entityTree->findEntityByEntityItemID(EntityItemID(entityID));
if (!entity) {
return;
}
if (entity->getType() == EntityTypes::Canvas) {
auto canvas = std::dynamic_pointer_cast<CanvasEntityItem>(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<CanvasEntityItem>(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<CanvasEntityItem>(entity);
canvas->commit();
} else {
qCWarning(entities) << "canvasCommit called on a non-canvas entity " << entityID;
}
}

View file

@ -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

View file

@ -347,6 +347,16 @@ bool qVectorCanvasCommandFromScriptValue(const ScriptValue& array, QVector<Canva
return true;
}
ScriptValue qVectorCanvasCommandToScriptValue(ScriptEngine* engine, const QVector<CanvasCommand>& 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);

View file

@ -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; }