Skip to content

Commit

Permalink
made the tileset import plugin handle pngs
Browse files Browse the repository at this point in the history
  • Loading branch information
MightyPrinny committed Dec 3, 2021
1 parent 7d8d0c5 commit 0a8409f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
74 changes: 70 additions & 4 deletions src/plugins/gmx/gmxplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1671,8 +1671,75 @@ GmxTilesetPlugin::GmxTilesetPlugin(QObject *parent)

}

SharedTileset GmxTilesetPlugin::tstFromPng(const QString &fileName, QSettings *prefs)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
mError = tr("Could not open file for reading.");
return SharedTileset();
}
file.close();

using namespace std;
using namespace Tiled;
int tileWidth = 16;
int tileHeight = 16;

if(prefs != nullptr)
{
auto diag = new BGXImporterDialog();
diag->SetDefaultsFromSettings(prefs);
diag->exec();
if(!diag->accepted)
{
mError = tr("Operation cancelled");
return SharedTileset();
}
else
{
tileWidth = diag->tileSize.width();
tileHeight = diag->tileSize.height();

prefs->setValue(QLatin1String("GMSMESizes/LastUsedTilesetTileSize"), QSize(tileWidth,tileHeight));
}
}
QFile imgFile(fileName);
if(!imgFile.exists())
{
mError = tr("Invalid background, image file doesn't exist");
qDebug() << imgFile.fileName();
return SharedTileset();
}

SharedTileset sh = TilesetManager::instance()->findTilesetAbsoluteWithSize(fileName,QSize(tileWidth, tileHeight));
if(!sh.isNull())
{
qDebug() << "reused tileset";
return sh;
}

QFileInfo imgFileInfo = QFileInfo(imgFile);

SharedTileset tileset = Tileset::create(imgFileInfo.fileName(), tileWidth, tileHeight, 0, 0);

if(tileset->loadFromImage(fileName))
{
tileset->setGridSize(QSize(tileWidth, tileHeight));
tileset->setFileName(fileName);
qDebug()<<"New tileset " << imgFileInfo.fileName() << ", " << tileset->name() << ", tw:"<<tileWidth << ", th:"<<tileHeight;
return tileset;
}
return SharedTileset();

return SharedTileset();
}

SharedTileset GmxTilesetPlugin::read(const QString &fileName, QSettings *prefs)
{
if(fileName.endsWith(".png"))
{
return tstFromPng(fileName, prefs);
}
using namespace rapidxml;
using namespace std;
using namespace Tiled;
Expand Down Expand Up @@ -1749,7 +1816,6 @@ SharedTileset GmxTilesetPlugin::read(const QString &fileName, QSettings *prefs)
}

//node = root_node->first_node();

QString filePath;


Expand Down Expand Up @@ -1831,13 +1897,13 @@ SharedTileset GmxTilesetPlugin::read(const QString &fileName, QSettings *prefs)

bool GmxTilesetPlugin::write(const Tileset &tst, const QString &filename)
{

return false;
}

bool GmxTilesetPlugin::supportsFile(const QString &fileName) const
{

return fileName.endsWith(QStringLiteral(".background.gmx"));
return fileName.endsWith(QStringLiteral(".background.gmx")) || fileName.endsWith(QStringLiteral(".png"));
}

QString GmxTilesetPlugin::errorString() const
Expand All @@ -1857,7 +1923,7 @@ FileFormat::Capabilities GmxTilesetPlugin::capabilities() const

QString GmxTilesetPlugin::nameFilter() const
{
return QStringLiteral("GameMaker background files (*.background.gmx)");
return QStringLiteral("GameMaker background files (*.background.gmx, *.png)");
}

void GmxTopPlugin::initialize()
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/gmx/gmxplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <QImage>
#include <unordered_map>
#include <plugin.h>
using namespace Tiled;

namespace Gmx {

Expand Down Expand Up @@ -80,6 +81,7 @@ class GMXSHARED_EXPORT GmxTilesetPlugin : public Tiled::TilesetFormat
QString nameFilter() const override;
QString errorString() const override;
QString shortName() const override;
SharedTileset tstFromPng(const QString &fileName, QSettings *prefs);
protected:
QString mError;

Expand Down
2 changes: 1 addition & 1 deletion tiled.qbs.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.0, 2021-12-02T21:43:57. -->
<!-- Written by QtCreator 4.10.0, 2021-12-02T22:48:41. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
Expand Down

0 comments on commit 0a8409f

Please sign in to comment.