Skip to content

Commit

Permalink
open TexturePacker format
Browse files Browse the repository at this point in the history
  • Loading branch information
amakaseev committed Dec 28, 2015
1 parent 1b676c7 commit e6ea23b
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
v1.0.2
======
* implement drag and drop files and folders intro spritesheet from finder
* Implement drag and drop files and folders intro spritesheet from finder
* Open TexturePacker format

v1.0.1
======
Expand Down
80 changes: 75 additions & 5 deletions SpriteSheetPacker/SpritePackerProjectFile.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "SpritePackerProjectFile.h"

#include "TPSParser.h"
#include "PListParser.h"
#include "PListSerializer.h"

GenericObjectFactory<std::string, SpritePackerProjectFile> SpritePackerProjectFile::_factory;

SpritePackerProjectFile::SpritePackerProjectFile() {

_trimThreshold = 1;
_textureBorder = 0;
_spriteBorder = 2;
_maxTextureSize = 8192;
_pot2 = false;
}


Expand Down Expand Up @@ -90,10 +95,6 @@ bool SpritePackerProjectFileOLD::read(const QString &fileName) {
return false;
}

// unsupported variables
_maxTextureSize = 2048;
_pot2 = false;

QVariantMap plistDict = plist.toMap();

// load property
Expand Down Expand Up @@ -148,5 +149,74 @@ bool SpritePackerProjectFileOLD::read(const QString &fileName) {
}

bool SpritePackerProjectFileTPS::read(const QString &fileName) {
QDir dir(QFileInfo(fileName).absolutePath());
QFile file(fileName);
file.open(QIODevice::ReadOnly);
QVariant tps = TPSParser::parse(&file);

if (!tps.isValid()) {
return false;
}

QVariantMap tpsMap = tps.toMap();

if (tpsMap.find("globalSpriteSettings") != tpsMap.end()) {
QVariantMap globalSpriteSettings = tpsMap["globalSpriteSettings"].toMap();
_trimThreshold = globalSpriteSettings["trimThreshold"].toInt();
}
if (tpsMap.find("borderPadding") != tpsMap.end()) {
_textureBorder = tpsMap["borderPadding"].toInt();
}
if (tpsMap.find("shapePadding") != tpsMap.end()) {
_spriteBorder = tpsMap["shapePadding"].toInt();
}
if (tpsMap.find("maxTextureSize") != tpsMap.end()) {
_maxTextureSize = qMax(tpsMap["maxTextureSize"].toMap()["width"].toInt(), tpsMap["maxTextureSize"].toMap()["height"].toInt());
}

if (tpsMap.find("globalSpriteSettings") != tpsMap.end()) {
QVariantMap globalSpriteSettings = tpsMap["globalSpriteSettings"].toMap();
_trimThreshold = globalSpriteSettings["trimThreshold"].toInt();
}
if (tpsMap.find("algorithmSettings") != tpsMap.end()) {
QVariantMap algorithmSettings = tpsMap["algorithmSettings"].toMap();
if (algorithmSettings["sizeConstraints"].toString() == "POT") {
_pot2 = true;
} else {
_pot2 = false;
}
}

if (tpsMap.find("dataFileNames") != tpsMap.end()) {
QVariantMap dataFileNames = tpsMap["dataFileNames"].toMap();
if (dataFileNames.find("data") != dataFileNames.end()) {
QVariantMap data = dataFileNames["data"].toMap();
if (data.find("name") != data.end()) {
QFileInfo fi(dir.absoluteFilePath(data["name"].toString()));
_destPath = fi.dir().canonicalPath();
_spriteSheetName = fi.baseName();
}
}
}

if (tpsMap.find("autoSDSettings") != tpsMap.end()) {
QVariantList autoSDSettings = tpsMap["autoSDSettings"].toList();
for (auto autoSDSettingVariant: autoSDSettings) {
QVariantMap autoSDSetting = autoSDSettingVariant.toMap();
ScalingVariant scalingVariant;
scalingVariant.name = autoSDSetting["extension"].toString();
scalingVariant.scale = autoSDSetting["scale"].toFloat();
_scalingVariants.push_back(scalingVariant);
}
}

_srcList.clear();
QVariantList spritesList = tpsMap["fileList"].toList();
foreach (QVariant spriteFile, spritesList) {
_srcList.append(dir.absoluteFilePath(spriteFile.toString()));
}

qDebug() << tps;

return true;
}
1 change: 1 addition & 0 deletions SpriteSheetPacker/SpriteSheetPacker.pro
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ FORMS += MainWindow.ui \
RESOURCES += resources.qrc

include(qtplist-master/qtplist-master.pri)
include(TPSParser/TPSParser.pri)

OTHER_FILES += \
defaultFormats/cocos2d.js \
Expand Down
86 changes: 86 additions & 0 deletions SpriteSheetPacker/TPSParser/TPSParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "TPSParser.h"
#include <QDomElement>
#include <QDomNode>
#include <QDomDocument>

QVariant TPSParser::parse(QIODevice *device) {
QVariantMap result;
QDomDocument doc;
QString errorMessage;
int errorLine;
int errorColumn;
bool success = doc.setContent(device, false, &errorMessage, &errorLine, &errorColumn);
if (!success) {
qDebug() << "TPSParser Warning: Could not parse tps file!";
qDebug() << "Error message: " << errorMessage;
qDebug() << "Error line: " << errorLine;
qDebug() << "Error column: " << errorColumn;
return result;
}
QDomElement root = doc.documentElement();
if (root.attribute("version", "1.0") != "1.0") {
qDebug() << "TPSParser Warning: tps is using an unknown format version, parsing might fail unexpectedly";
}
return parseElement(root.firstChild().toElement());
}

QVariant TPSParser::parseElement(const QDomElement &e) {
QString tagName = e.tagName();
QVariant result;
if ((tagName == "dict")||(tagName == "map")||(tagName == "struct")||(tagName == "QSize")) {
result = parseDictElement(e);
} else if (tagName == "array") {
result = parseArrayElement(e);
} else if ((tagName == "string")||(tagName == "filename")||(tagName == "enum")) {
result = e.text();
// } else if (tagName == "data") {
// result = QByteArray::fromBase64(e.text().toUtf8());
} else if ((tagName == "integer")||(tagName == "uint")||(tagName == "int")) {
result = e.text().toInt();
} else if ((tagName == "real")||(tagName == "double")) {
result = e.text().toFloat();
} else if (tagName == "true") {
result = true;
} else if (tagName == "false") {
result = false;
} else if (tagName == "date") {
result = QDateTime::fromString(e.text(), Qt::ISODate);
} else {
qDebug() << "TPSParser Warning: Invalid tag found: " << e.tagName() << e.text();
}
return result;
}


QVariantList TPSParser::parseArrayElement(const QDomElement& element) {
QVariantList result;
QDomNodeList children = element.childNodes();
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
result.append(parseElement(e));
}
}
return result;
}

QVariantMap TPSParser::parseDictElement(const QDomElement& element) {
QVariantMap result;
QDomNodeList children = element.childNodes();
QString currentKey = "";
for (int i = 0; i < children.count(); i++) {
QDomNode child = children.at(i);
QDomElement e = child.toElement();
if (!e.isNull()) {
QString tagName = e.tagName();
if (tagName == "key") {
currentKey = e.text();
}
else if (currentKey != "") {
result[currentKey] = parseElement(e);
}
}
}
return result;
}
16 changes: 16 additions & 0 deletions SpriteSheetPacker/TPSParser/TPSParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TPS_PARSER_H
#define TPS_PARSER_H

#include <QtCore>
#include <QDomElement>

class TPSParser {
public:
static QVariant parse(QIODevice *device);
private:
static QVariant parseElement(const QDomElement &e);
static QVariantList parseArrayElement(const QDomElement& node);
static QVariantMap parseDictElement(const QDomElement& element);
};

#endif
7 changes: 7 additions & 0 deletions SpriteSheetPacker/TPSParser/TPSParser.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

QT += xml

INCLUDEPATH += $$PWD

SOURCES += $$PWD/TPSParser.cpp
HEADERS += $$PWD/TPSParser.h

0 comments on commit e6ea23b

Please sign in to comment.