1 /*
2  * changewangsetdata.cpp
3  * Copyright 2017, Benjamin Trotter <bdtrotte@ucsc.edu>
4  *
5  * This file is part of Tiled.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "changewangsetdata.h"
22 
23 #include "changeevents.h"
24 #include "changetilewangid.h"
25 #include "tileset.h"
26 #include "tilesetdocument.h"
27 #include "tilesetwangsetmodel.h"
28 
29 #include <QCoreApplication>
30 
31 #include "qtcompat_p.h"
32 
33 using namespace Tiled;
34 
RenameWangSet(TilesetDocument * tilesetDocument,WangSet * wangSet,const QString & newName)35 RenameWangSet::RenameWangSet(TilesetDocument *tilesetDocument,
36                              WangSet *wangSet,
37                              const QString &newName)
38     : QUndoCommand(QCoreApplication::translate("Undo Commands",
39                                                "Change Terrain Set Name"))
40     , mTilesetDocument(tilesetDocument)
41     , mWangSet(wangSet)
42     , mOldName(wangSet->name())
43     , mNewName(newName)
44 {
45 }
46 
undo()47 void RenameWangSet::undo()
48 {
49     mTilesetDocument->wangSetModel()->setWangSetName(mWangSet, mOldName);
50 }
51 
redo()52 void RenameWangSet::redo()
53 {
54     mTilesetDocument->wangSetModel()->setWangSetName(mWangSet, mNewName);
55 }
56 
57 
ChangeWangSetType(TilesetDocument * tilesetDocument,WangSet * wangSet,WangSet::Type newType,QUndoCommand * parent)58 ChangeWangSetType::ChangeWangSetType(TilesetDocument *tilesetDocument,
59                                      WangSet *wangSet,
60                                      WangSet::Type newType,
61                                      QUndoCommand *parent)
62     : QUndoCommand(parent)
63     , mTilesetDocument(tilesetDocument)
64     , mWangSet(wangSet)
65     , mOldType(wangSet->type())
66     , mNewType(newType)
67 {
68     setText(QCoreApplication::translate("Undo Commands", "Change Terrain Set Type"));
69 }
70 
undo()71 void ChangeWangSetType::undo()
72 {
73     mTilesetDocument->wangSetModel()->setWangSetType(mWangSet, mOldType);
74 }
75 
redo()76 void ChangeWangSetType::redo()
77 {
78     mTilesetDocument->wangSetModel()->setWangSetType(mWangSet, mNewType);
79 }
80 
81 
ChangeWangSetColorCount(TilesetDocument * tilesetDocument,WangSet * wangSet,int newValue)82 ChangeWangSetColorCount::ChangeWangSetColorCount(TilesetDocument *tilesetDocument,
83                                                  WangSet *wangSet,
84                                                  int newValue)
85     : QUndoCommand(QCoreApplication::translate("Undo Commands",
86                                                "Change Terrain Count"))
87     , mTilesetDocument(tilesetDocument)
88     , mWangSet(wangSet)
89     , mOldValue(wangSet->colorCount())
90     , mNewValue(newValue)
91 {
92     // when edge size changes, all tiles with WangIds need to be updated.
93     if (mNewValue < mOldValue) {
94         // when the size is reduced, some Wang assignments can be lost.
95         const auto changes = ChangeTileWangId::changesOnSetColorCount(wangSet, mNewValue);
96         if (!changes.isEmpty())
97             new ChangeTileWangId(mTilesetDocument, wangSet, changes, this);
98 
99         for (int i = mOldValue; i > mNewValue; --i) {
100             WangColorChange w;
101             w.index = i;
102             w.wangColor = wangSet->colorAt(i);
103 
104             mRemovedWangColors.append(w);
105         }
106     }
107 }
108 
undo()109 void ChangeWangSetColorCount::undo()
110 {
111     mTilesetDocument->wangSetModel()->setWangSetColorCount(mWangSet, mOldValue);
112 
113     for (const WangColorChange &w : qAsConst(mRemovedWangColors)) {
114         WangColor &wangColor = *mWangSet->colorAt(w.index);
115         wangColor.setName(w.wangColor->name());
116         wangColor.setImageId(w.wangColor->imageId());
117         wangColor.setColor(w.wangColor->color());
118         wangColor.setProbability(w.wangColor->probability());
119     }
120 
121     QUndoCommand::undo();
122 }
123 
redo()124 void ChangeWangSetColorCount::redo()
125 {
126     mTilesetDocument->wangSetModel()->setWangSetColorCount(mWangSet, mNewValue);
127 
128     QUndoCommand::redo();
129 }
130 
131 
RemoveWangSetColor(TilesetDocument * tilesetDocumnet,WangSet * wangSet,int color)132 RemoveWangSetColor::RemoveWangSetColor(TilesetDocument *tilesetDocumnet, WangSet *wangSet, int color)
133     : QUndoCommand(QCoreApplication::translate("Undo Commands",
134                                                "Remove Terrain"))
135     , mTilesetDocument(tilesetDocumnet)
136     , mWangSet(wangSet)
137     , mColor(color)
138 {
139     const auto changes = ChangeTileWangId::changesOnRemoveColor(wangSet, color);
140     if (!changes.isEmpty())
141         new ChangeTileWangId(mTilesetDocument, wangSet, changes, this);
142 }
143 
undo()144 void RemoveWangSetColor::undo()
145 {
146     mTilesetDocument->wangSetModel()->insertWangColor(mWangSet, std::move(mRemovedWangColor));
147     QUndoCommand::undo();
148 }
149 
redo()150 void RemoveWangSetColor::redo()
151 {
152     mRemovedWangColor = mTilesetDocument->wangSetModel()->takeWangColorAt(mWangSet, mColor);
153     QUndoCommand::redo();
154 }
155 
156 
SetWangSetImage(TilesetDocument * tilesetDocument,WangSet * wangSet,int tileId,QUndoCommand * parent)157 SetWangSetImage::SetWangSetImage(TilesetDocument *tilesetDocument,
158                                  WangSet *wangSet,
159                                  int tileId,
160                                  QUndoCommand *parent)
161     : QUndoCommand(QCoreApplication::translate("Undo Commands",
162                                                "Set Terrain Set Image"),
163                    parent)
164     , mTilesetDocument(tilesetDocument)
165     , mWangSet(wangSet)
166     , mOldImageTileId(wangSet->imageTileId())
167     , mNewImageTileId(tileId)
168 {
169 }
170 
undo()171 void SetWangSetImage::undo()
172 {
173     mTilesetDocument->wangSetModel()->setWangSetImage(mWangSet, mOldImageTileId);
174 }
175 
redo()176 void SetWangSetImage::redo()
177 {
178     mTilesetDocument->wangSetModel()->setWangSetImage(mWangSet, mNewImageTileId);
179 }
180