1 /*
2  * changemapobject.h
3  * Copyright 2009, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
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 #pragma once
22 
23 #include "mapobject.h"
24 #include "tilelayer.h"
25 
26 #include <QUndoCommand>
27 #include <QVector>
28 
29 namespace Tiled {
30 
31 class ObjectTemplate;
32 class Tile;
33 
34 class Document;
35 
36 class ChangeMapObject : public QUndoCommand
37 {
38 public:
39     /**
40      * Creates an undo command that sets the given \a object's \a property to
41      * \a value.
42      */
43     ChangeMapObject(Document *document,
44                     MapObject *object,
45                     MapObject::Property property,
46                     const QVariant &value);
47 
undo()48     void undo() override { swap(); }
redo()49     void redo() override { swap(); }
50 
51 private:
52     void swap();
53 
54     Document *mDocument;
55     MapObject *mMapObject;
56     MapObject::Property mProperty;
57     QVariant mValue;
58     bool mOldChangeState;
59     bool mNewChangeState;
60 };
61 
62 
63 struct MapObjectCell
64 {
65     MapObject *object;
66     Cell cell;
67     bool propertyChanged = true;
68 };
69 
70 class ChangeMapObjectCells : public QUndoCommand
71 {
72 public:
73     /**
74      * Creates an undo command that applies the given map object changes.
75      */
76     ChangeMapObjectCells(Document *document,
77                          const QVector<MapObjectCell> &changes,
78                          QUndoCommand *parent = nullptr);
79 
undo()80     void undo() override { swap(); }
redo()81     void redo() override { swap(); }
82 
83 private:
84     void swap();
85 
86     Document *mDocument;
87     QVector<MapObjectCell> mChanges;
88 };
89 
90 class ChangeMapObjectsTile : public QUndoCommand
91 {
92 public:
93     ChangeMapObjectsTile(Document *document,
94                          const QList<MapObject *> &mapObjects,
95                          Tile *tile);
96 
97     void undo() override;
98     void redo() override;
99 
100 private:
101     void changeTiles();
102     void restoreTiles();
103 
104     Document *mDocument;
105     const QList<MapObject *> mMapObjects;
106     Tile * const mTile;
107     QVector<Cell> mOldCells;
108     QVector<bool> mUpdateSize;
109     QVector<MapObject::ChangedProperties> mOldChangedProperties;
110 };
111 
112 class DetachObjects : public QUndoCommand
113 {
114 public:
115     /**
116      * Creates an undo command that detaches the given template instances
117      * from their templates.
118      */
119     DetachObjects(Document *document,
120                   const QList<MapObject *> &mapObjects,
121                   QUndoCommand *parent = nullptr);
122 
123     void redo() override;
124     void undo() override;
125 
126 private:
127     Document *mDocument;
128     const QList<MapObject*> mMapObjects;
129     QVector<const ObjectTemplate*> mObjectTemplates;
130     QVector<Properties> mProperties;
131 };
132 
133 class ResetInstances : public QUndoCommand
134 {
135 public:
136     ResetInstances(Document *document,
137                    const QList<MapObject *> &mapObjects,
138                    QUndoCommand *parent = nullptr);
139 
140     ~ResetInstances() override;
141 
142     void redo() override;
143     void undo() override;
144 
145 private:
146     Document *mDocument;
147     const QList<MapObject*> mMapObjects;
148     QList<MapObject*> mOldMapObjects;
149 };
150 
151 class ReplaceObjectsWithTemplate : public QUndoCommand
152 {
153 public:
154     /**
155      * Creates an undo command that replaces the given objects with a template
156      */
157     ReplaceObjectsWithTemplate(Document *document,
158                                const QList<MapObject *> &mapObjects,
159                                ObjectTemplate *objectTemplate,
160                                QUndoCommand *parent = nullptr);
161 
162     ~ReplaceObjectsWithTemplate() override;
163 
164     void redo() override;
165     void undo() override;
166 
167 private:
168     Document *mDocument;
169     const QList<MapObject*> mMapObjects;
170     QList<MapObject*> mOldMapObjects;
171     ObjectTemplate *mObjectTemplate;
172 };
173 
174 } // namespace Tiled
175