Skip to content

Commit

Permalink
Properly handle set/undo/redo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase-san committed Oct 5, 2022
1 parent 6d33970 commit 9862eca
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/libtiled/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const QPoint &Tile::drawOffset() const
/**
* Sets the local draw offset of the tile (in pixels).
*/
void Tile::setDrawOffset(QPoint &offset)
void Tile::setDrawOffset(const QPoint &offset)
{
this->mDrawOffset.setX(offset.x());
this->mDrawOffset.setY(offset.y());
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TILEDSHARED_EXPORT Tile : public Object

QPoint offset() const;
const QPoint &drawOffset() const;
void setDrawOffset(QPoint &offset);
void setDrawOffset(const QPoint &offset);

// For Python API compatibility
const QString &type() const { return className(); }
Expand Down
37 changes: 37 additions & 0 deletions src/tiled/changetile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,41 @@ void ChangeTileImageRect::setValue(Tile *tile, const QRect &rect) const
emit mapDocument->tileImageSourceChanged(tile);
}


ChangeTileDrawOffset::ChangeTileDrawOffset(TilesetDocument *tilesetDocument,
const QList<Tile *> &tiles,
const QPoint &rects,
QUndoCommand *parent)
: ChangeValue(tilesetDocument, tiles, rects, parent)
{
setText(QCoreApplication::translate("Undo Commands",
"Change Image Draw Offset"));
}

ChangeTileDrawOffset::ChangeTileDrawOffset(TilesetDocument *tilesetDocument,
const QList<Tile *> &tiles,
const QVector<QPoint> &rects,
QUndoCommand *parent)
: ChangeValue(tilesetDocument, tiles, rects, parent)
{
setText(QCoreApplication::translate("Undo Commands",
"Change Image Draw Offset"));
}

QPoint ChangeTileDrawOffset::getValue(const Tile *tile) const
{
return tile->drawOffset();
}

void ChangeTileDrawOffset::setValue(Tile *tile, const QPoint &offset) const
{
tile->setDrawOffset(offset);

//since we changed the offset we need to tell it to update any maps
emit static_cast<TilesetDocument*>(document())->tileImageSourceChanged(tile);

for (MapDocument *mapDocument : static_cast<TilesetDocument*>(document())->mapDocuments())
emit mapDocument->tileImageSourceChanged(tile);
}

} // namespace Tiled
21 changes: 21 additions & 0 deletions src/tiled/changetile.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "undocommands.h"

#include <QVector>
#include <QPoint>

namespace Tiled {

Expand Down Expand Up @@ -66,4 +67,24 @@ class ChangeTileImageRect : public ChangeValue<Tile, QRect>
void setValue(Tile *tile, const QRect &rect) const override;
};

class ChangeTileDrawOffset : public ChangeValue<Tile, QPoint>
{
public:
ChangeTileDrawOffset(TilesetDocument *tilesetDocument,
const QList<Tile*> &tiles,
const QPoint &offsets,
QUndoCommand *parent = nullptr);

ChangeTileDrawOffset(TilesetDocument *tilesetDocument,
const QList<Tile*> &tiles,
const QVector<QPoint> &offsets,
QUndoCommand *parent = nullptr);

int id() const override { return Cmd_ChangeTileDrawOffset; }

protected:
QPoint getValue(const Tile *tile) const override;
void setValue(Tile *tile, const QPoint &offset) const override;
};

} // namespace Tiled
7 changes: 7 additions & 0 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,13 @@ void PropertyBrowser::applyTileValue(PropertyId id, const QVariant &val)
tile, filePath.url));
break;
}
case TileDrawOffsetProperty: {
undoStack->push(new ChangeTileDrawOffset(mTilesetDocument,
mTilesetDocument->selectedTiles(),
val.toPoint()));

break;
}
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/tiled/undocommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ enum UndoCommands {
Cmd_EraseTiles,
Cmd_PaintTileLayer,
Cmd_SetProperty,
Cmd_ChangeTileDrawOffset,
};

/**
Expand Down

0 comments on commit 9862eca

Please sign in to comment.