Example #1
0
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;
}
Example #2
0
void Tileset::swap(Tileset &other)
{
    const Properties p = properties();
    setProperties(other.properties());
    other.setProperties(p);

    std::swap(mFileName, other.mFileName);
    std::swap(mImageReference, other.mImageReference);
    std::swap(mTileWidth, other.mTileWidth);
    std::swap(mTileHeight, other.mTileHeight);
    std::swap(mTileSpacing, other.mTileSpacing);
    std::swap(mMargin, other.mMargin);
    std::swap(mTileOffset, other.mTileOffset);
    std::swap(mOrientation, other.mOrientation);
    std::swap(mGridSize, other.mGridSize);
    std::swap(mColumnCount, other.mColumnCount);
    std::swap(mExpectedColumnCount, other.mExpectedColumnCount);
    std::swap(mExpectedRowCount, other.mExpectedRowCount);
    std::swap(mTiles, other.mTiles);
    std::swap(mNextTileId, other.mNextTileId);
    std::swap(mTerrainTypes, other.mTerrainTypes);
    std::swap(mWangSets, other.mWangSets);
    std::swap(mTerrainDistancesDirty, other.mTerrainDistancesDirty);
    std::swap(mStatus, other.mStatus);
    std::swap(mBackgroundColor, other.mBackgroundColor);
    std::swap(mFormat, other.mFormat);

    // Don't swap mWeakPointer, since it's a reference to this.

    // Update back references from tiles and terrains
    for (auto tile : mTiles)
        tile->mTileset = this;
    for (auto terrain : mTerrainTypes)
        terrain->mTileset = this;
    for (auto wangSet : mWangSets)
        wangSet->setTileset(this);

    for (auto tile : other.mTiles)
        tile->mTileset = &other;
    for (auto terrain : other.mTerrainTypes)
        terrain->mTileset = &other;
    for (auto wangSet : other.mWangSets)
        wangSet->setTileset(&other);
}