-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66c85c2
Two tiles on TileLayer can now be swapped
theHacker 558e246
Implemented QUndoCommand for swapTiles
theHacker 6a95701
Added German translations for swapTiles
theHacker 4c46abc
Merge remote-tracking branch 'upstream/master'
theHacker c061705
Implemented review notes
theHacker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
).