Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to swap tiles #866

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/tiled/swaptiles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* swaptiles.cpp
* Copyright 2015, Alexander "theHacker" Münch <git@thehacker.biz>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "swaptiles.h"

#include "mapdocument.h"
#include "tileset.h"

#include <QCoreApplication>

#include <limits.h>

namespace Tiled {
namespace Internal {

SwapTiles::SwapTiles(MapDocument *mapDocument,
TileLayer *tileLayer,
Tile *tile1,
Tile *tile2)
: QUndoCommand(QCoreApplication::translate("Undo Commands",
"Swap Tiles"))
, mMapDocument(mapDocument)
, mTileLayer(tileLayer)
, mTile1(tile1)
, mTile2(tile2)
{}

void SwapTiles::swap()
{
// Remember smallest and largest x/y to give emitRegionChanged() the smallest possible rectangle
int minXChanged = INT_MAX;
int minYChanged = INT_MAX;
int maxXChanged = INT_MIN;
int maxYChanged = INT_MIN;

for (int y = 0; y < mTileLayer->height(); y++) {
for (int x = 0; x < mTileLayer->width(); x++) {
bool tileSwapped = false;

const Cell &cell = mTileLayer->cellAt(x, y);
if (cell.tile == mTile1) {
Cell swapCell = cell;
swapCell.tile = mTile2;
mTileLayer->setCell(x, y, swapCell);

tileSwapped = true;
}
else if (cell.tile == mTile2) {
Cell swapCell = cell;
swapCell.tile = mTile1;
mTileLayer->setCell(x, y, swapCell);

tileSwapped = true;
}

if (tileSwapped) {
if (x < minXChanged) minXChanged = x;
if (y < minYChanged) minYChanged = y;
if (x > maxXChanged) maxXChanged = x;
if (y > maxYChanged) maxYChanged = y;
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this fails to call MapDocument::emitRegionChanged to trigger a repaint. I don't think it makes sense to determine this region in detail, but just emit it for the region of the layer (Layer::bounds).


// We changed at least one tile? Call emitRegionChanged() to redraw
if (minXChanged != INT_MAX) {
QRegion regionChanged(
minXChanged,
minYChanged,
maxXChanged - minXChanged + 1,
maxYChanged - minYChanged + 1
);
mMapDocument->emitRegionChanged(regionChanged);
}
}

} // namespace Internal
} // namespace Tiled
73 changes: 73 additions & 0 deletions src/tiled/swaptiles.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* swaptiles.h
* Copyright 2015, Alexander "theHacker" Münch <git@thehacker.biz>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SWAPTILES_H
#define SWAPTILES_H

#include "undocommands.h"

#include <QUndoCommand>

namespace Tiled {

class Tile;
class TileLayer;

namespace Internal {

class MapDocument;

/**
* A command that swaps two tiles on a TileLayer
*/
class SwapTiles : public QUndoCommand
{
public:
/**
* Constructor.
*
* @param mapDocument the map document that's being edited
* @param tileLayer the TileLayer to swap the tiles
* @param tile1 the first tile
* @param tile2 the second tile
*/
SwapTiles(MapDocument *mapDocument,
TileLayer *tileLayer,
Tile *tile1,
Tile *tile2);

~SwapTiles() {}

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

private:
void swap();

MapDocument *mMapDocument;
TileLayer *mTileLayer;
Tile *mTile1;
Tile *mTile2;
};

} // namespace Internal
} // namespace Tiled

#endif // SWAPTILES_H
1 change: 1 addition & 0 deletions src/tiled/tiled.pro
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ SOURCES += aboutdialog.cpp \
propertiesdock.cpp \
propertybrowser.cpp \
quickstampmanager.cpp \
swaptiles.cpp \
raiselowerhelper.cpp \
renamelayer.cpp \
renameterrain.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/tiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ QtGuiApplication {
"snaphelper.h",
"stampbrush.cpp",
"stampbrush.h",
"swaptiles.cpp",
"swaptiles.h",
"terrainbrush.cpp",
"terrainbrush.h",
"terraindock.cpp",
Expand Down
29 changes: 29 additions & 0 deletions src/tiled/tilesetview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "map.h"
#include "mapdocument.h"
#include "preferences.h"
#include "swaptiles.h"
#include "tmxmapwriter.h"
#include "tile.h"
#include "tileset.h"
Expand Down Expand Up @@ -582,6 +583,15 @@ void TilesetView::contextMenuEvent(QContextMenuEvent *event)
Utils::setThemeIcon(tileProperties, "document-properties");
connect(tileProperties, SIGNAL(triggered()),
SLOT(editTileProperties()));

// Enable "swap" if there are exactly 2 tiles selected
bool exactlyTwoTilesSelected =
(selectionModel()->selectedIndexes().size() == 2);

QAction *tileSwap = menu.addAction(tr("&Swap Tiles"));
tileSwap->setEnabled(exactlyTwoTilesSelected);
connect(tileSwap, SIGNAL(triggered()),
SLOT(swapTiles()));
}

menu.addSeparator();
Expand Down Expand Up @@ -620,6 +630,25 @@ void TilesetView::editTileProperties()
mMapDocument->emitEditCurrentObject();
}

void TilesetView::swapTiles()
{
const QModelIndexList selectedIndexes = selectionModel()->selectedIndexes();
if (selectedIndexes.size() != 2)
return;

TileLayer *tileLayer = dynamic_cast<TileLayer*>(mMapDocument->currentLayer());
if (!tileLayer)
return;

const TilesetModel *model = tilesetModel();
Tile *tile1 = model->tileAt(selectedIndexes[0]);
Tile *tile2 = model->tileAt(selectedIndexes[1]);

QUndoStack *undoStack = mMapDocument->undoStack();
QUndoCommand *command = new SwapTiles(mMapDocument, tileLayer, tile1, tile2);
undoStack->push(command);
}

void TilesetView::setDrawGrid(bool drawGrid)
{
mDrawGrid = drawGrid;
Expand Down
1 change: 1 addition & 0 deletions src/tiled/tilesetview.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ private slots:
void createNewTerrain();
void selectTerrainImage();
void editTileProperties();
void swapTiles();
void setDrawGrid(bool drawGrid);

void adjustScale();
Expand Down
10 changes: 10 additions & 0 deletions translations/tiled_de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3564,6 +3564,11 @@ Do you want to replace it?</source>
</message>
<message>
<location line="+16"/>
<source>&amp;Swap Tiles</source>
<translation>Kacheln ver&amp;tauschen</translation>
</message>
<message>
<location line="+9"/>
<source>Show &amp;Grid</source>
<translation>&amp;Raster anzeigen</translation>
</message>
Expand Down Expand Up @@ -3949,6 +3954,11 @@ Do you want to replace it?</source>
<source>Change Layer Data Format</source>
<translation>Ebenen-Datenformat ändern</translation>
</message>
<message>
<location filename="../src/tiled/swaptiles.cpp" line="+35"/>
<source>Swap Tiles</source>
<translation>Kacheln vertauschen</translation>
</message>
</context>
<context>
<name>Utils</name>
Expand Down