/** * Searches for a tileset matching the given file name. * @return a tileset matching the given file name, or 0 if none exists */ SharedTileset TilesetManager::findTileset(const QString &fileName) const { for (Tileset *tileset : mTilesets) if (tileset->fileName() == fileName) return tileset->sharedPointer(); return SharedTileset(); }
SharedTileset VariantToMapConverter::toTileset(const QVariant &variant) { const QVariantMap variantMap = variant.toMap(); const int firstGid = variantMap[QLatin1String("firstgid")].toInt(); // Handle external tilesets const QVariant sourceVariant = variantMap[QLatin1String("source")]; if (!sourceVariant.isNull()) { QString source = resolvePath(mMapDir, sourceVariant); QString error; SharedTileset tileset = TilesetManager::instance()->loadTileset(source, &error); if (!tileset) { // Insert a placeholder to allow the map to load tileset = Tileset::create(QFileInfo(source).completeBaseName(), 32, 32); tileset->setFileName(source); tileset->setLoaded(false); } else { mGidMapper.insert(firstGid, tileset.data()); } return tileset; } const QString name = variantMap[QLatin1String("name")].toString(); const int tileWidth = variantMap[QLatin1String("tilewidth")].toInt(); const int tileHeight = variantMap[QLatin1String("tileheight")].toInt(); const int spacing = variantMap[QLatin1String("spacing")].toInt(); const int margin = variantMap[QLatin1String("margin")].toInt(); const QVariantMap tileOffset = variantMap[QLatin1String("tileoffset")].toMap(); const QVariantMap grid = variantMap[QLatin1String("grid")].toMap(); const int tileOffsetX = tileOffset[QLatin1String("x")].toInt(); const int tileOffsetY = tileOffset[QLatin1String("y")].toInt(); const int columns = tileOffset[QLatin1String("columns")].toInt(); const QString bgColor = variantMap[QLatin1String("backgroundcolor")].toString(); if (tileWidth <= 0 || tileHeight <= 0 || (firstGid == 0 && !mReadingExternalTileset)) { mError = tr("Invalid tileset parameters for tileset '%1'").arg(name); return SharedTileset(); } SharedTileset tileset(Tileset::create(name, tileWidth, tileHeight, spacing, margin)); tileset->setTileOffset(QPoint(tileOffsetX, tileOffsetY)); tileset->setColumnCount(columns); if (!grid.isEmpty()) { const QString orientation = grid[QLatin1String("orientation")].toString(); const QSize gridSize(grid[QLatin1String("width")].toInt(), grid[QLatin1String("height")].toInt()); tileset->setOrientation(Tileset::orientationFromString(orientation)); if (!gridSize.isEmpty()) tileset->setGridSize(gridSize); } if (!bgColor.isEmpty() && QColor::isValidColor(bgColor)) tileset->setBackgroundColor(QColor(bgColor)); QVariant imageVariant = variantMap[QLatin1String("image")]; if (!imageVariant.isNull()) { const int imageWidth = variantMap[QLatin1String("imagewidth")].toInt(); const int imageHeight = variantMap[QLatin1String("imageheight")].toInt(); ImageReference imageRef; imageRef.source = resolvePath(mMapDir, imageVariant); imageRef.size = QSize(imageWidth, imageHeight); tileset->setImageReference(imageRef); } const QString trans = variantMap[QLatin1String("transparentcolor")].toString(); if (!trans.isEmpty() && QColor::isValidColor(trans)) tileset->setTransparentColor(QColor(trans)); tileset->setProperties(extractProperties(variantMap)); // Read terrains QVariantList terrainsVariantList = variantMap[QLatin1String("terrains")].toList(); for (int i = 0; i < terrainsVariantList.count(); ++i) { QVariantMap terrainMap = terrainsVariantList[i].toMap(); Terrain *terrain = tileset->addTerrain(terrainMap[QLatin1String("name")].toString(), terrainMap[QLatin1String("tile")].toInt()); terrain->setProperties(extractProperties(terrainMap)); } // Read tiles (everything except their properties) const QVariantMap tilesVariantMap = variantMap[QLatin1String("tiles")].toMap(); QVariantMap::const_iterator it = tilesVariantMap.constBegin(); for (; it != tilesVariantMap.end(); ++it) { bool ok; const int tileId = it.key().toInt(); if (tileId < 0) { mError = tr("Invalid (negative) tile id: %1").arg(tileId); return SharedTileset(); } Tile *tile = tileset->findOrCreateTile(tileId); const QVariantMap tileVar = it.value().toMap(); tile->setType(tileVar[QLatin1String("type")].toString()); QList<QVariant> terrains = tileVar[QLatin1String("terrain")].toList(); if (terrains.count() == 4) { for (int i = 0; i < 4; ++i) { int terrainId = terrains.at(i).toInt(&ok); if (ok && terrainId >= 0 && terrainId < tileset->terrainCount()) tile->setCornerTerrainId(i, terrainId); } } float probability = tileVar[QLatin1String("probability")].toFloat(&ok); if (ok) tile->setProbability(probability); imageVariant = tileVar[QLatin1String("image")]; if (!imageVariant.isNull()) { QString imagePath = resolvePath(mMapDir, imageVariant); tileset->setTileImage(tile, QPixmap(imagePath), imagePath); } QVariantMap objectGroupVariant = tileVar[QLatin1String("objectgroup")].toMap(); if (!objectGroupVariant.isEmpty()) tile->setObjectGroup(toObjectGroup(objectGroupVariant)); QVariantList frameList = tileVar[QLatin1String("animation")].toList(); if (!frameList.isEmpty()) { QVector<Frame> frames(frameList.size()); for (int i = frameList.size() - 1; i >= 0; --i) { const QVariantMap frameVariantMap = frameList[i].toMap(); Frame &frame = frames[i]; frame.tileId = frameVariantMap[QLatin1String("tileid")].toInt(); frame.duration = frameVariantMap[QLatin1String("duration")].toInt(); } tile->setFrames(frames); } } // Read tile properties QVariantMap propertiesVariantMap = variantMap[QLatin1String("tileproperties")].toMap(); QVariantMap propertyTypesVariantMap = variantMap[QLatin1String("tilepropertytypes")].toMap(); for (it = propertiesVariantMap.constBegin(); it != propertiesVariantMap.constEnd(); ++it) { const int tileId = it.key().toInt(); const QVariant propertiesVar = it.value(); const QVariant propertyTypesVar = propertyTypesVariantMap.value(it.key()); const Properties properties = toProperties(propertiesVar, propertyTypesVar); tileset->findOrCreateTile(tileId)->setProperties(properties); } if (!mReadingExternalTileset) mGidMapper.insert(firstGid, tileset.data()); return tileset; }