1// Aseprite
2// Copyright (C) 2019 Igara Studio S.A.
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/cmd/remap_tilemaps.h"
12
13#include "doc/cel.h"
14#include "doc/cels_range.h"
15#include "doc/image.h"
16#include "doc/layer.h"
17#include "doc/layer_tilemap.h"
18#include "doc/remap.h"
19#include "doc/sprite.h"
20#include "doc/tileset.h"
21
22namespace app {
23namespace cmd {
24
25using namespace doc;
26
27RemapTilemaps::RemapTilemaps(Tileset* tileset,
28 const Remap& remap)
29 : WithTileset(tileset)
30 , m_remap(remap)
31{
32}
33
34void RemapTilemaps::onExecute()
35{
36 Tileset* tileset = this->tileset();
37 Sprite* spr = tileset->sprite();
38 spr->remapTilemaps(tileset, m_remap);
39 incrementVersions(tileset);
40}
41
42void RemapTilemaps::onUndo()
43{
44 Tileset* tileset = this->tileset();
45 Sprite* spr = tileset->sprite();
46 spr->remapTilemaps(tileset, m_remap.invert());
47 incrementVersions(tileset);
48}
49
50void RemapTilemaps::incrementVersions(Tileset* tileset)
51{
52 Sprite* spr = tileset->sprite();
53 for (const Cel* cel : spr->uniqueCels()) {
54 if (cel->layer()->isTilemap() &&
55 static_cast<LayerTilemap*>(cel->layer())->tileset() == tileset) {
56 cel->image()->incrementVersion();
57 }
58 }
59}
60
61} // namespace cmd
62} // namespace app
63