Skip to content

Commit

Permalink
Moved output chunk size into "editorsettings" section
Browse files Browse the repository at this point in the history
For both the TMX and the JSON map formats.

For the Lua format, the this information was simply removed since each
chunk already specifies its size and this additional information was
only for the editor. Lua files are currently not readable by the editor.

Finalizes issue #2130
  • Loading branch information
bjorn committed Sep 8, 2019
1 parent 81b0cbd commit 4951627
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
22 changes: 13 additions & 9 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,19 @@ void MapReaderPrivate::readMapEditorSettings(Map &map)
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("editorsettings"));

while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("export")) {
if (xml.name() == QLatin1String("chunksize")) {
const QXmlStreamAttributes atts = xml.attributes();

int chunkWidth = atts.value(QLatin1String("width")).toInt();
int chunkHeight = atts.value(QLatin1String("height")).toInt();

chunkWidth = chunkWidth == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkWidth);
chunkHeight = chunkHeight == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkHeight);

map.setChunkSize(QSize(chunkWidth, chunkHeight));

xml.skipCurrentElement();
} else if (xml.name() == QLatin1String("export")) {
const QXmlStreamAttributes atts = xml.attributes();

map.exportFileName = QDir::cleanPath(mPath.filePath(atts.value(QLatin1String("target")).toString()));
Expand Down Expand Up @@ -838,14 +850,6 @@ void MapReaderPrivate::readTileLayerData(TileLayer &tileLayer)

mMap->setLayerDataFormat(layerDataFormat);

int chunkWidth = atts.value(QLatin1String("outputchunkwidth")).toInt();
int chunkHeight = atts.value(QLatin1String("outputchunkheight")).toInt();

chunkWidth = chunkWidth == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkWidth);
chunkHeight = chunkHeight == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkHeight);

mMap->setChunkSize(QSize(chunkWidth, chunkHeight));

readTileLayerRect(tileLayer,
layerDataFormat,
encoding,
Expand Down
26 changes: 16 additions & 10 deletions src/libtiled/maptovariantconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,24 @@ QVariant MapToVariantConverter::toVariant(const Map &map, const QDir &mapDir)
mapVariant[QLatin1String("nextobjectid")] = map.nextObjectId();
mapVariant[QLatin1String("compressionlevel")] = map.compressionLevel();

if (!map.exportFileName.isEmpty() || !map.exportFormat.isEmpty()) {
if (map.chunkSize() != QSize(CHUNK_SIZE, CHUNK_SIZE) || !map.exportFileName.isEmpty() || !map.exportFormat.isEmpty()) {
QVariantMap editorSettingsVariant;

QVariantMap exportVariant;
exportVariant[QLatin1String("target")] = mDir.relativeFilePath(map.exportFileName);
exportVariant[QLatin1String("format")] = map.exportFormat;
editorSettingsVariant[QLatin1String("export")] = exportVariant;
if (map.chunkSize() != QSize(CHUNK_SIZE, CHUNK_SIZE)) {
QVariantMap chunkSizeVariant;
chunkSizeVariant[QLatin1String("width")] = map.chunkSize().width();
chunkSizeVariant[QLatin1String("height")] = map.chunkSize().height();
editorSettingsVariant[QLatin1String("chunksize")] = chunkSizeVariant;
}

if (!map.exportFileName.isEmpty() || !map.exportFormat.isEmpty()) {
QVariantMap exportVariant;
if (!map.exportFileName.isEmpty())
exportVariant[QLatin1String("target")] = mDir.relativeFilePath(map.exportFileName);
if (!map.exportFormat.isEmpty())
exportVariant[QLatin1String("format")] = map.exportFormat;
editorSettingsVariant[QLatin1String("export")] = exportVariant;
}

mapVariant[QLatin1String("editorsettings")] = editorSettingsVariant;
}
Expand Down Expand Up @@ -474,11 +485,6 @@ QVariant MapToVariantConverter::toVariant(const TileLayer &tileLayer,
}

if (tileLayer.map()->infinite()) {
if (chunkSize.width() != CHUNK_SIZE || chunkSize.height() != CHUNK_SIZE) {
tileLayerVariant[QLatin1String("outputchunkwidth")] = chunkSize.width();
tileLayerVariant[QLatin1String("outputchunkheight")] = chunkSize.height();
}

QVariantList chunkVariants;

const auto chunks = tileLayer.sortedChunksToWrite(chunkSize);
Expand Down
30 changes: 19 additions & 11 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,26 @@ void MapWriterPrivate::writeMap(QXmlStreamWriter &w, const Map &map)
w.writeAttribute(QLatin1String("nextobjectid"),
QString::number(map.nextObjectId()));

if (!map.exportFileName.isEmpty() || !map.exportFormat.isEmpty()) {
if (map.chunkSize() != QSize(CHUNK_SIZE, CHUNK_SIZE) || !map.exportFileName.isEmpty() || !map.exportFormat.isEmpty()) {
w.writeStartElement(QLatin1String("editorsettings"));
w.writeStartElement(QLatin1String("export"));
w.writeAttribute(QLatin1String("target"), mDir.relativeFilePath(map.exportFileName));
w.writeAttribute(QLatin1String("format"), map.exportFormat);
w.writeEndElement();
w.writeEndElement();

if (map.chunkSize() != QSize(CHUNK_SIZE, CHUNK_SIZE)) {
w.writeStartElement(QLatin1String("chunksize"));
w.writeAttribute(QLatin1String("width"), QString::number(map.chunkSize().width()));
w.writeAttribute(QLatin1String("height"), QString::number(map.chunkSize().height()));
w.writeEndElement();
}

if (!map.exportFileName.isEmpty() || !map.exportFormat.isEmpty()) {
w.writeStartElement(QLatin1String("export"));
if (!map.exportFileName.isEmpty())
w.writeAttribute(QLatin1String("target"), mDir.relativeFilePath(map.exportFileName));
if (!map.exportFormat.isEmpty())
w.writeAttribute(QLatin1String("format"), map.exportFormat);
w.writeEndElement();
}

w.writeEndElement(); // </editorsettings>
}

writeProperties(w, map.properties());
Expand Down Expand Up @@ -601,11 +614,6 @@ void MapWriterPrivate::writeTileLayer(QXmlStreamWriter &w,
w.writeAttribute(QLatin1String("compression"), compression);

if (tileLayer.map()->infinite()) {
if (mChunkSize.width() != CHUNK_SIZE || mChunkSize.height() != CHUNK_SIZE) {
w.writeAttribute(QLatin1String("outputchunkwidth"), QString::number(mChunkSize.width()));
w.writeAttribute(QLatin1String("outputchunkheight"), QString::number(mChunkSize.height()));
}

const auto chunks = tileLayer.sortedChunksToWrite(mChunkSize);
for (const QRect &rect : chunks) {
w.writeStartElement(QLatin1String("chunk"));
Expand Down
15 changes: 7 additions & 8 deletions src/libtiled/varianttomapconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,6 @@ std::unique_ptr<TileLayer> VariantToMapConverter::toTileLayer(const QVariantMap
}
mMap->setLayerDataFormat(layerDataFormat);

int chunkWidth = variantMap[QLatin1String("outputchunkwidth")].toInt();
int chunkHeight = variantMap[QLatin1String("outputchunkheight")].toInt();

chunkWidth = chunkWidth == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkWidth);
chunkHeight = chunkHeight == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkHeight);

mMap->setChunkSize(QSize(chunkWidth, chunkHeight));

if (dataVariant.isValid() && !dataVariant.isNull()) {
if (!readTileLayerData(*tileLayer, dataVariant, layerDataFormat,
QRect(startX, startY, tileLayer->width(), tileLayer->height()))) {
Expand Down Expand Up @@ -822,6 +814,13 @@ TextData VariantToMapConverter::toTextData(const QVariantMap &variant) const

void VariantToMapConverter::readMapEditorSettings(Map &map, const QVariantMap &editorSettings)
{
const QVariantMap chunkSizeVariant = editorSettings[QLatin1String("chunksize")].toMap();
int chunkWidth = chunkSizeVariant[QLatin1String("width")].toInt();
int chunkHeight = chunkSizeVariant[QLatin1String("height")].toInt();
chunkWidth = chunkWidth == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkWidth);
chunkHeight = chunkHeight == 0 ? CHUNK_SIZE : qMax(CHUNK_SIZE_MIN, chunkHeight);
map.setChunkSize(QSize(chunkWidth, chunkHeight));

const QVariantMap exportVariant = editorSettings[QLatin1String("export")].toMap();
map.exportFileName = QDir::cleanPath(mDir.filePath(exportVariant[QLatin1String("target")].toString()));
map.exportFormat = exportVariant[QLatin1String("format")].toString();
Expand Down
5 changes: 0 additions & 5 deletions src/plugins/lua/luaplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,6 @@ void LuaWriter::writeTileLayer(const TileLayer *tileLayer,
}

if (tileLayer->map()->infinite()) {
if (chunkSize.width() != CHUNK_SIZE || chunkSize.height() != CHUNK_SIZE) {
mWriter.writeKeyAndValue("outputchunkwidth", chunkSize.width());
mWriter.writeKeyAndValue("outputchunkheight", chunkSize.height());
}

mWriter.writeStartTable("chunks");
const auto chunks = tileLayer->sortedChunksToWrite(chunkSize);
for (const QRect &rect : chunks) {
Expand Down

0 comments on commit 4951627

Please sign in to comment.