QtPBFImagePlugin/src/sprites.cpp

105 lines
2.3 KiB
C++
Raw Normal View History

2018-11-22 21:57:21 +01:00
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
#include "sprites.h"
/*
Loading the sprites atlas image must be deferred until all image plugins
are loaded, otherwise reading the image will cause a deadlock!
*/
2018-12-04 00:09:23 +01:00
static const QImage *atlas(const QString &fileName)
2018-11-22 21:57:21 +01:00
{
static QImage *img = new QImage(fileName);
2018-12-04 00:09:23 +01:00
return img;
2018-11-22 21:57:21 +01:00
}
Sprites::Sprite::Sprite(const QJsonObject &json)
{
int x, y, width, height;
2018-12-05 00:03:41 +01:00
2018-11-22 21:57:21 +01:00
if (json.contains("x") && json["x"].isDouble())
x = json["x"].toInt();
else
return;
if (json.contains("y") && json["y"].isDouble())
y = json["y"].toInt();
else
return;
if (json.contains("width") && json["width"].isDouble())
width = json["width"].toInt();
else
return;
if (json.contains("height") && json["height"].isDouble())
height = json["height"].toInt();
else
return;
_rect = QRect(x, y, width, height);
2018-12-05 00:03:41 +01:00
if (json.contains("pixelRatio") && json["pixelRatio"].isDouble())
_pixelRatio = json["pixelRatio"].toDouble();
else
_pixelRatio = 1.0;
2018-11-22 21:57:21 +01:00
}
bool Sprites::load(const QString &jsonFile, const QString &imageFile)
{
_imageFile = imageFile;
QFile file(jsonFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCritical() << jsonFile << ": error opening file";
return false;
}
QByteArray ba(file.readAll());
QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(ba, &error));
if (doc.isNull()) {
qCritical() << jsonFile << ":" << error.errorString();
return false;
}
QJsonObject json(doc.object());
for (QJsonObject::const_iterator it = json.constBegin();
it != json.constEnd(); it++) {
QJsonValue val(*it);
2018-11-26 22:37:16 +01:00
if (val.isObject()) {
Sprite s(val.toObject());
if (s.rect().isValid())
2018-12-05 00:03:41 +01:00
_sprites.insert(it.key(), s);
2018-11-26 22:37:16 +01:00
else
qWarning() << it.key() << ": invalid sprite definition";
} else
2018-11-22 21:57:21 +01:00
qWarning() << it.key() << ": invalid sprite definition";
}
return true;
}
2018-12-05 00:03:41 +01:00
QImage Sprites::icon(const QString &name) const
2018-11-22 21:57:21 +01:00
{
if (_imageFile.isEmpty())
return QImage();
2018-12-05 00:03:41 +01:00
const QImage *img = atlas(_imageFile);
2018-12-04 00:09:23 +01:00
if (img->isNull())
2018-11-22 21:57:21 +01:00
return QImage();
2018-12-05 00:03:41 +01:00
QMap<QString, Sprite>::const_iterator it = _sprites.find(name);
if (it == _sprites.constEnd())
2018-11-22 21:57:21 +01:00
return QImage();
2018-12-04 00:09:23 +01:00
if (!img->rect().contains(it->rect()))
2018-11-22 21:57:21 +01:00
return QImage();
2018-12-04 00:09:23 +01:00
QImage ret(img->copy(it->rect()));
2018-12-05 00:03:41 +01:00
ret.setDevicePixelRatio(it->pixelRatio());
2018-12-04 00:09:23 +01:00
return ret;
2018-11-22 21:57:21 +01:00
}