Skip to content

Commit

Permalink
Allow changing the column count of image collection tilesets
Browse files Browse the repository at this point in the history
Closes #1090
  • Loading branch information
bjorn committed Nov 16, 2015
1 parent 778fac9 commit 2e953f4
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 26 deletions.
22 changes: 10 additions & 12 deletions src/libtiled/mapreader.cpp
Expand Up @@ -268,7 +268,7 @@ Map *MapReaderPrivate::readMap()
// Try to load the tileset images
auto tilesets = mMap->tilesets();
for (SharedTileset &tileset : tilesets) {
if (!tileset->imageSource().isEmpty() && tileset->fileName().isEmpty())
if (!tileset->isCollection() && tileset->fileName().isEmpty())
tileset->loadImage();
}

Expand Down Expand Up @@ -305,16 +305,12 @@ SharedTileset MapReaderPrivate::readTileset()
SharedTileset tileset;

if (source.isEmpty()) { // Not an external tileset
const QString name =
atts.value(QLatin1String("name")).toString();
const int tileWidth =
atts.value(QLatin1String("tilewidth")).toInt();
const int tileHeight =
atts.value(QLatin1String("tileheight")).toInt();
const int tileSpacing =
atts.value(QLatin1String("spacing")).toInt();
const int margin =
atts.value(QLatin1String("margin")).toInt();
const QString name = atts.value(QLatin1String("name")).toString();
const int tileWidth = atts.value(QLatin1String("tilewidth")).toInt();
const int tileHeight = atts.value(QLatin1String("tileheight")).toInt();
const int tileSpacing = atts.value(QLatin1String("spacing")).toInt();
const int margin = atts.value(QLatin1String("margin")).toInt();
const int columns = atts.value(QLatin1String("columns")).toInt();

if (tileWidth < 0 || tileHeight < 0
|| (firstGid == 0 && !mReadingExternalTileset)) {
Expand All @@ -324,6 +320,8 @@ SharedTileset MapReaderPrivate::readTileset()
tileset = Tileset::create(name, tileWidth, tileHeight,
tileSpacing, margin);

tileset->setColumnCount(columns);

while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("tile")) {
readTilesetTile(*tileset);
Expand Down Expand Up @@ -969,7 +967,7 @@ Map *MapReader::readMap(const QString &fileName)
SharedTileset MapReader::readTileset(QIODevice *device, const QString &path)
{
SharedTileset tileset = d->readTileset(device, path);
if (tileset && !tileset->imageSource().isEmpty())
if (tileset && !tileset->isCollection())
tileset->loadImage();

return tileset;
Expand Down
1 change: 1 addition & 0 deletions src/libtiled/maptovariantconverter.cpp
Expand Up @@ -123,6 +123,7 @@ QVariant MapToVariantConverter::toVariant(const Tileset *tileset,
tilesetVariant[QLatin1String("spacing")] = tileset->tileSpacing();
tilesetVariant[QLatin1String("margin")] = tileset->margin();
tilesetVariant[QLatin1String("tilecount")] = tileset->tileCount();
tilesetVariant[QLatin1String("columns")] = tileset->columnCount();
tilesetVariant[QLatin1String("properties")] = toVariant(tileset->properties());

const QPoint offset = tileset->tileOffset();
Expand Down
2 changes: 2 additions & 0 deletions src/libtiled/mapwriter.cpp
Expand Up @@ -266,6 +266,8 @@ void MapWriterPrivate::writeTileset(QXmlStreamWriter &w, const Tileset &tileset,

w.writeAttribute(QLatin1String("tilecount"),
QString::number(tileset.tileCount()));
w.writeAttribute(QLatin1String("columns"),
QString::number(tileset.columnCount()));

const QPoint offset = tileset.tileOffset();
if (!offset.isNull()) {
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/tileset.cpp
Expand Up @@ -252,7 +252,7 @@ SharedTileset Tileset::findSimilarTileset(const QVector<SharedTileset> &tilesets
continue;

// For an image collection tileset, check the image sources
if (imageSource().isEmpty())
if (isCollection())
if (!sameTileImages(*this, *candidate))
continue;

Expand Down
20 changes: 20 additions & 0 deletions src/libtiled/tileset.h
Expand Up @@ -124,6 +124,7 @@ class TILEDSHARED_EXPORT Tileset : public Object
int tileCount() const;

int columnCount() const;
void setColumnCount(int columnCount);
int expectedColumnCount() const;
void syncExpectedColumnCount();

Expand All @@ -143,6 +144,7 @@ class TILEDSHARED_EXPORT Tileset : public Object

const QString &imageSource() const;
void setImageSource(const QString &imageSource);
bool isCollection() const;

int columnCountForWidth(int width) const;

Expand Down Expand Up @@ -334,6 +336,15 @@ inline int Tileset::columnCount() const
return mColumnCount;
}

/**
* Sets the column count to use when displaying this tileset. For tileset image
* based tilesets, this reflects the number of tile columns in the image.
*/
inline void Tileset::setColumnCount(int columnCount)
{
mColumnCount = columnCount;
}

/**
* Returns the number of tile columns expected to be in the tileset image. This
* may differ from the actual amount of columns encountered when loading the
Expand Down Expand Up @@ -397,6 +408,15 @@ inline const QString &Tileset::imageSource() const
return mImageReference.source;
}

/**
* Returns whether this tileset is a collection of images. In this case, the
* tileset itself has no image source.
*/
inline bool Tileset::isCollection() const
{
return imageSource().isEmpty();
}

/**
* Returns a const reference to the list of terrains in this tileset.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/libtiled/varianttomapconverter.cpp
Expand Up @@ -175,6 +175,7 @@ SharedTileset VariantToMapConverter::toTileset(const QVariant &variant)
const QVariantMap tileOffset = variantMap[QLatin1String("tileoffset")].toMap();
const int tileOffsetX = tileOffset[QLatin1String("x")].toInt();
const int tileOffsetY = tileOffset[QLatin1String("y")].toInt();
const int columns = tileOffset[QLatin1String("columns")].toInt();

if (tileWidth <= 0 || tileHeight <= 0 ||
(firstGid == 0 && !mReadingExternalTileset)) {
Expand All @@ -187,6 +188,7 @@ SharedTileset VariantToMapConverter::toTileset(const QVariant &variant)
spacing, margin));

tileset->setTileOffset(QPoint(tileOffsetX, tileOffsetY));
tileset->setColumnCount(columns);

QVariant imageVariant = variantMap[QLatin1String("image")];

Expand Down
4 changes: 2 additions & 2 deletions src/tiled/brokenlinks.cpp
Expand Up @@ -86,7 +86,7 @@ void BrokenLinksModel::refresh()
mBrokenLinks.clear();

for (const SharedTileset &tileset : mMapDocument->map()->tilesets()) {
if (!tileset->imageSource().isEmpty() && !tileset->imageLoaded()) {
if (!tileset->isCollection() && !tileset->imageLoaded()) {
BrokenLink link;
link.type = TilesetImageSource;
link.tileset = tileset.data();
Expand Down Expand Up @@ -206,7 +206,7 @@ void BrokenLinksModel::tilesetChanged(Tileset *tileset)
mBrokenLinks.end(),
matchesTileset);

if (!tileset->imageSource().isEmpty() && !tileset->imageLoaded()) {
if (!tileset->isCollection() && !tileset->imageLoaded()) {
if (it != mBrokenLinks.end()) {
int linkIndex = it - mBrokenLinks.begin();
emit dataChanged(index(linkIndex, 0), index(linkIndex, 1));
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/documentmanager.cpp
Expand Up @@ -553,7 +553,7 @@ void DocumentManager::centerViewOn(qreal x, qreal y)

static bool mayNeedColumnCountAdjustment(const Tileset &tileset)
{
if (tileset.imageSource().isEmpty())
if (tileset.isCollection())
return false;
if (!tileset.imageLoaded())
return false;
Expand Down
14 changes: 12 additions & 2 deletions src/tiled/propertybrowser.cpp
Expand Up @@ -539,8 +539,11 @@ void PropertyBrowser::addTilesetProperties()
createProperty(NameProperty, QVariant::String, tr("Name"), groupProperty);
createProperty(TileOffsetProperty, QVariant::Point, tr("Drawing Offset"), groupProperty);

QtVariantProperty *columnsProperty = createProperty(ColumnCountProperty, QVariant::Int, tr("Columns"), groupProperty);
columnsProperty->setAttribute(QLatin1String("minimum"), 1);

// Next properties we should add only for non 'Collection of Images' tilesets
if (!tileset->imageSource().isEmpty()) {
if (!tileset->isCollection()) {
QtVariantProperty *parametersProperty =
createProperty(TilesetImageParametersProperty, VariantPropertyManager::tilesetParametersTypeId(), tr("Image"), groupProperty);

Expand Down Expand Up @@ -897,6 +900,11 @@ void PropertyBrowser::applyTilesetValue(PropertyBrowser::PropertyId id, const QV
tileset,
val.toPoint()));
break;
case ColumnCountProperty:
undoStack->push(new ChangeTilesetColumnCount(mMapDocument,
*tileset,
val.toInt()));
break;
default:
break;
}
Expand Down Expand Up @@ -1089,11 +1097,13 @@ void PropertyBrowser::updateProperties()

mIdToProperty[NameProperty]->setValue(tileset->name());
mIdToProperty[TileOffsetProperty]->setValue(tileset->tileOffset());
mIdToProperty[ColumnCountProperty]->setValue(tileset->columnCount());

mIdToProperty[NameProperty]->setEnabled(!external);
mIdToProperty[TileOffsetProperty]->setEnabled(!external);
mIdToProperty[ColumnCountProperty]->setEnabled(!external && tileset->isCollection());

if (!tileset->imageSource().isEmpty()) {
if (!tileset->isCollection()) {
EmbeddedTileset embeddedTileset(mMapDocument, tileset);

mIdToProperty[TilesetImageParametersProperty]->setValue(QVariant::fromValue(embeddedTileset));
Expand Down
1 change: 1 addition & 0 deletions src/tiled/propertybrowser.h
Expand Up @@ -133,6 +133,7 @@ private slots:
MarginProperty,
SpacingProperty,
TileProbabilityProperty,
ColumnCountProperty,
IdProperty,
CustomProperty
};
Expand Down
19 changes: 19 additions & 0 deletions src/tiled/tilesetchanges.cpp
Expand Up @@ -143,5 +143,24 @@ void ChangeTilesetParameters::apply(const TilesetParameters &parameters)
emit mMapDocument->tilesetChanged(&mTileset);
}

ChangeTilesetColumnCount::ChangeTilesetColumnCount(MapDocument *mapDocument,
Tileset &tileset,
int columnCount)
: QUndoCommand(QCoreApplication::translate("Undo Commands", "Change Columns"))
, mMapDocument(mapDocument)
, mTileset(tileset)
, mColumnCount(columnCount)
{
}

void ChangeTilesetColumnCount::swap()
{
int oldColumnCount = mTileset.columnCount();
mTileset.setColumnCount(mColumnCount);
mColumnCount = oldColumnCount;

emit mMapDocument->tilesetChanged(&mTileset);
}

} // namespace Internal
} // namespace Tiled
18 changes: 18 additions & 0 deletions src/tiled/tilesetchanges.h
Expand Up @@ -111,6 +111,24 @@ class ChangeTilesetParameters : public QUndoCommand
TilesetParameters mNewParameters;
};

class ChangeTilesetColumnCount : public QUndoCommand
{
public:
ChangeTilesetColumnCount(MapDocument *mapDocument,
Tileset &tileset,
int columnCount);

void undo() override { swap(); }
void redo() override { swap(); }

private:
void swap();

MapDocument *mMapDocument;
Tileset &mTileset;
int mColumnCount;
};

} // namespace Internal
} // namespace Tiled

Expand Down
8 changes: 4 additions & 4 deletions src/tiled/tilesetdock.cpp
Expand Up @@ -516,7 +516,7 @@ void TilesetDock::currentChanged(const QModelIndex &index)
void TilesetDock::updateActions()
{
bool external = false;
bool hasImageSource = false;
bool isCollection = false;
bool hasSelection = false;
TilesetView *view = nullptr;
const int index = mTabBar->currentIndex();
Expand All @@ -531,7 +531,7 @@ void TilesetDock::updateActions()

mViewStack->setCurrentIndex(index);
external = tileset->isExternal();
hasImageSource = !tileset->imageSource().isEmpty();
isCollection = tileset->isCollection();
hasSelection = view->selectionModel()->hasSelection();
}
}
Expand All @@ -545,8 +545,8 @@ void TilesetDock::updateActions()
mPropertiesTileset->setEnabled(tilesetIsDisplayed);
mDeleteTileset->setEnabled(tilesetIsDisplayed);
mEditTerrain->setEnabled(tilesetIsDisplayed && !external);
mAddTiles->setEnabled(tilesetIsDisplayed && !hasImageSource && !external);
mRemoveTiles->setEnabled(tilesetIsDisplayed && !hasImageSource
mAddTiles->setEnabled(tilesetIsDisplayed && isCollection && !external);
mRemoveTiles->setEnabled(tilesetIsDisplayed && isCollection
&& hasSelection && !external);
}

Expand Down
8 changes: 4 additions & 4 deletions src/tiled/tilesetview.cpp
Expand Up @@ -221,7 +221,7 @@ void TileDelegate::paint(QPainter *painter,
QSize tileSize = tileImage.size();
if (tileImage.isNull()) {
Tileset *tileset = model->tileset();
if (tileset->imageSource().isEmpty()) {
if (tileset->isCollection()) {
tileSize = QSize(32, 32);
} else {
int max = std::max(tileset->tileWidth(), tileset->tileWidth());
Expand Down Expand Up @@ -332,7 +332,7 @@ QSize TileDelegate::sizeHint(const QStyleOptionViewItem & /* option */,

if (image.isNull()) {
Tileset *tileset = m->tileset();
if (tileset->imageSource().isEmpty()) {
if (tileset->isCollection()) {
tileSize = QSize(32, 32);
} else {
int max = std::max(tileset->tileWidth(), tileset->tileWidth());
Expand Down Expand Up @@ -407,7 +407,7 @@ int TilesetView::sizeHintForColumn(int column) const
if (!model)
return -1;
#if QT_VERSION >= 0x050200
if (model->tileset()->imageSource().isEmpty())
if (model->tileset()->isCollection())
return QTableView::sizeHintForColumn(column);
#endif

Expand All @@ -422,7 +422,7 @@ int TilesetView::sizeHintForRow(int row) const
if (!model)
return -1;
#if QT_VERSION >= 0x050200
if (model->tileset()->imageSource().isEmpty())
if (model->tileset()->isCollection())
return QTableView::sizeHintForRow(row);
#endif

Expand Down

0 comments on commit 2e953f4

Please sign in to comment.