static bool sameTileImages(const Tileset &a, const Tileset &b) { Q_ASSERT(a.tileCount() == b.tileCount()); for (int i = 0; i < a.tileCount(); ++i) { const Tile *tileA = a.tileAt(i); const Tile *tileB = b.tileAt(i); if (tileA->imageSource() != tileB->imageSource()) return false; } return true; }
Tileset *VariantToMapConverter::toTileset(const QVariant &variant) { const QVariantMap variantMap = variant.toMap(); const int firstGid = variantMap["firstgid"].toInt(); const QString name = variantMap["name"].toString(); const int tileWidth = variantMap["tilewidth"].toInt(); const int tileHeight = variantMap["tileheight"].toInt(); const int spacing = variantMap["spacing"].toInt(); const int margin = variantMap["margin"].toInt(); const QVariantMap tileOffset = variantMap["tileoffset"].toMap(); const int tileOffsetX = tileOffset["x"].toInt(); const int tileOffsetY = tileOffset["y"].toInt(); if (tileWidth <= 0 || tileHeight <= 0 || firstGid == 0) { mError = tr("Invalid tileset parameters for tileset '%1'").arg(name); return 0; } Tileset *tileset = new Tileset(name, tileWidth, tileHeight, spacing, margin); tileset->setTileOffset(QPoint(tileOffsetX, tileOffsetY)); const QString trans = variantMap["transparentcolor"].toString(); if (!trans.isEmpty()) #if QT_VERSION >= 0x040700 if (QColor::isValidColor(trans)) #endif tileset->setTransparentColor(QColor(trans)); QString imageSource = variantMap["image"].toString(); if (QDir::isRelativePath(imageSource)) imageSource = mMapDir.path() + QLatin1Char('/') + imageSource; if (!tileset->loadFromImage(QImage(imageSource), imageSource)) { mError = tr("Error loading tileset image:\n'%1'").arg(imageSource); delete tileset; return 0; } tileset->setProperties(toProperties(variantMap["properties"])); QVariantMap propertiesVariantMap = variantMap["tileproperties"].toMap(); QVariantMap::const_iterator it = propertiesVariantMap.constBegin(); for (; it != propertiesVariantMap.constEnd(); ++it) { const int tileIndex = it.key().toInt(); const QVariant propertiesVar = it.value(); if (tileIndex >= 0 && tileIndex < tileset->tileCount()) { const Properties properties = toProperties(propertiesVar); tileset->tileAt(tileIndex)->setProperties(properties); } } mGidMapper.insert(firstGid, tileset); return tileset; }
QVariant MapToVariantConverter::toVariant(const Tileset &tileset, int firstGid) const { QVariantMap tilesetVariant; if (firstGid > 0) tilesetVariant[QLatin1String("firstgid")] = firstGid; else tilesetVariant[QLatin1String("version")] = 1.2; // external tileset const QString &fileName = tileset.fileName(); if (!fileName.isEmpty() && firstGid > 0) { QString source = mMapDir.relativeFilePath(fileName); tilesetVariant[QLatin1String("source")] = source; // Tileset is external, so no need to write any of the stuff below return tilesetVariant; } // Include a 'type' property if we are writing the tileset to its own file if (firstGid == 0) tilesetVariant[QLatin1String("type")] = QLatin1String("tileset"); tilesetVariant[QLatin1String("name")] = tileset.name(); tilesetVariant[QLatin1String("tilewidth")] = tileset.tileWidth(); tilesetVariant[QLatin1String("tileheight")] = tileset.tileHeight(); tilesetVariant[QLatin1String("spacing")] = tileset.tileSpacing(); tilesetVariant[QLatin1String("margin")] = tileset.margin(); tilesetVariant[QLatin1String("tilecount")] = tileset.tileCount(); tilesetVariant[QLatin1String("columns")] = tileset.columnCount(); const QColor bgColor = tileset.backgroundColor(); if (bgColor.isValid()) tilesetVariant[QLatin1String("backgroundcolor")] = colorToString(bgColor); addProperties(tilesetVariant, tileset.properties()); const QPoint offset = tileset.tileOffset(); if (!offset.isNull()) { QVariantMap tileOffset; tileOffset[QLatin1String("x")] = offset.x(); tileOffset[QLatin1String("y")] = offset.y(); tilesetVariant[QLatin1String("tileoffset")] = tileOffset; } if (tileset.orientation() != Tileset::Orthogonal || tileset.gridSize() != tileset.tileSize()) { QVariantMap grid; grid[QLatin1String("orientation")] = Tileset::orientationToString(tileset.orientation()); grid[QLatin1String("width")] = tileset.gridSize().width(); grid[QLatin1String("height")] = tileset.gridSize().height(); tilesetVariant[QLatin1String("grid")] = grid; } // Write the image element const QUrl &imageSource = tileset.imageSource(); if (!imageSource.isEmpty()) { const QString rel = toFileReference(imageSource, mMapDir); tilesetVariant[QLatin1String("image")] = rel; const QColor transColor = tileset.transparentColor(); if (transColor.isValid()) tilesetVariant[QLatin1String("transparentcolor")] = transColor.name(); tilesetVariant[QLatin1String("imagewidth")] = tileset.imageWidth(); tilesetVariant[QLatin1String("imageheight")] = tileset.imageHeight(); } // Write the properties, terrain, external image, object group and // animation for those tiles that have them. QVariantList tilesVariant; for (const Tile *tile : tileset.tiles()) { const Properties properties = tile->properties(); QVariantMap tileVariant; addProperties(tileVariant, properties); if (!tile->type().isEmpty()) tileVariant[QLatin1String("type")] = tile->type(); if (tile->terrain() != 0xFFFFFFFF) { QVariantList terrainIds; for (int j = 0; j < 4; ++j) terrainIds << QVariant(tile->cornerTerrainId(j)); tileVariant[QLatin1String("terrain")] = terrainIds; } if (tile->probability() != 1.0) tileVariant[QLatin1String("probability")] = tile->probability(); if (!tile->imageSource().isEmpty()) { const QString rel = toFileReference(tile->imageSource(), mMapDir); tileVariant[QLatin1String("image")] = rel; const QSize tileSize = tile->size(); if (!tileSize.isNull()) { tileVariant[QLatin1String("imagewidth")] = tileSize.width(); tileVariant[QLatin1String("imageheight")] = tileSize.height(); } } if (tile->objectGroup()) tileVariant[QLatin1String("objectgroup")] = toVariant(*tile->objectGroup()); if (tile->isAnimated()) { QVariantList frameVariants; for (const Frame &frame : tile->frames()) { QVariantMap frameVariant; frameVariant[QLatin1String("tileid")] = frame.tileId; frameVariant[QLatin1String("duration")] = frame.duration; frameVariants.append(frameVariant); } tileVariant[QLatin1String("animation")] = frameVariants; } if (!tileVariant.empty()) { tileVariant[QLatin1String("id")] = tile->id(); tilesVariant << tileVariant; } } if (!tilesVariant.empty()) tilesetVariant[QLatin1String("tiles")] = tilesVariant; // Write terrains if (tileset.terrainCount() > 0) { QVariantList terrainsVariant; for (int i = 0; i < tileset.terrainCount(); ++i) { Terrain *terrain = tileset.terrain(i); const Properties &properties = terrain->properties(); QVariantMap terrainVariant; terrainVariant[QLatin1String("name")] = terrain->name(); terrainVariant[QLatin1String("tile")] = terrain->imageTileId(); addProperties(terrainVariant, properties); terrainsVariant << terrainVariant; } tilesetVariant[QLatin1String("terrains")] = terrainsVariant; } return tilesetVariant; }