QtPBFImagePlugin/src/sprites.cpp

127 lines
2.7 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
}
2018-12-04 00:09:23 +01:00
static const QImage *atlas2x(const QString &fileName)
{
static QImage *img = new QImage(fileName);
return img;
}
2018-11-22 21:57:21 +01:00
Sprites::Sprite::Sprite(const QJsonObject &json)
{
int x, y, width, height;
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);
}
bool Sprites::load(const QString &jsonFile, const QString &imageFile)
{
_imageFile = imageFile;
2018-12-04 00:09:23 +01:00
return load(jsonFile, _sprites);
}
bool Sprites::load2x(const QString &jsonFile, const QString &imageFile)
{
_image2xFile = imageFile;
return load(jsonFile, _sprites2x);
}
2018-11-22 21:57:21 +01:00
2018-12-04 00:09:23 +01:00
bool Sprites::load(const QString &jsonFile, QMap<QString, Sprite> &map)
{
2018-11-22 21:57:21 +01:00
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-04 00:09:23 +01:00
map.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-04 00:09:23 +01:00
QImage Sprites::icon(const QString &name, bool hidpi) const
2018-11-22 21:57:21 +01:00
{
2018-12-04 00:09:23 +01:00
qreal ratio;
const QImage *img;
const QMap<QString, Sprite> *map;
if (hidpi && !_image2xFile.isNull()) {
img = atlas2x(_image2xFile);
map = &_sprites2x;
ratio = 2;
} else if (!_imageFile.isNull()) {
img = atlas(_imageFile);
map = &_sprites;
ratio = 1;
} else
2018-11-22 21:57:21 +01:00
return QImage();
2018-12-04 00:09:23 +01:00
if (img->isNull())
2018-11-22 21:57:21 +01:00
return QImage();
2018-12-04 00:09:23 +01:00
QMap<QString, Sprite>::const_iterator it = map->find(name);
if (it == map->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()));
ret.setDevicePixelRatio(ratio);
return ret;
2018-11-22 21:57:21 +01:00
}