/
tileset.h
327 lines (279 loc) · 9.62 KB
/
tileset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* tileset.h
* Copyright 2008-2009, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
* Copyrigth 2009, Edward Hutchins <eah1@yahoo.com>
*
* This file is part of libtiled.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TILESET_H
#define TILESET_H
#include "object.h"
#include <QColor>
#include <QList>
#include <QVector>
#include <QPoint>
#include <QString>
#include <QPixmap>
class QImage;
namespace Tiled {
class Tile;
class Terrain;
/**
* A tileset, representing a set of tiles.
*
* This class currently only supports loading tiles from a tileset image, using
* loadFromImage(). There is no way to add or remove arbitrary tiles.
*/
class TILEDSHARED_EXPORT Tileset : public Object
{
public:
/**
* Constructor.
*
* @param name the name of the tileset
* @param tileWidth the width of the tiles in the tileset
* @param tileHeight the height of the tiles in the tileset
* @param tileSpacing the spacing between the tiles in the tileset image
* @param margin the margin around the tiles in the tileset image
*/
Tileset(const QString &name, int tileWidth, int tileHeight,
int tileSpacing = 0, int margin = 0):
mName(name),
mTileWidth(tileWidth),
mTileHeight(tileHeight),
mTileSpacing(tileSpacing),
mMargin(margin),
mImageWidth(0),
mImageHeight(0),
mColumnCount(0),
mTerrainDistancesDirty(false)
{
Q_ASSERT(tileSpacing >= 0);
Q_ASSERT(margin >= 0);
}
/**
* Destructor.
*/
~Tileset();
/**
* Returns the name of this tileset.
*/
const QString &name() const { return mName; }
/**
* Sets the name of this tileset.
*/
void setName(const QString &name) { mName = name; }
/**
* Returns the file name of this tileset. When the tileset isn't an
* external tileset, the file name is empty.
*/
const QString &fileName() const { return mFileName; }
/**
* Sets the filename of this tileset.
*/
void setFileName(const QString &fileName) { mFileName = fileName; }
/**
* Returns whether this tileset is external.
*/
bool isExternal() const { return !mFileName.isEmpty(); }
/**
* Returns the maximum width of the tiles in this tileset.
*/
int tileWidth() const { return mTileWidth; }
/**
* Returns the maximum height of the tiles in this tileset.
*/
int tileHeight() const { return mTileHeight; }
/**
* Returns the maximum size of the tiles in this tileset.
*/
QSize tileSize() const { return QSize(mTileWidth, mTileHeight); }
/**
* Returns the spacing between the tiles in the tileset image.
*/
int tileSpacing() const { return mTileSpacing; }
/**
* Returns the margin around the tiles in the tileset image.
*/
int margin() const { return mMargin; }
/**
* Returns the offset that is applied when drawing the tiles in this
* tileset.
*/
QPoint tileOffset() const { return mTileOffset; }
/**
* @see tileOffset
*/
void setTileOffset(QPoint offset) { mTileOffset = offset; }
/**
* Returns a const reference to the list of tiles in this tileset.
*/
const QList<Tile*> &tiles() const { return mTiles; }
/**
* Returns the tile for the given tile ID.
* The tile ID is local to this tileset, which means the IDs are in range
* [0, tileCount() - 1].
*/
Tile *tileAt(int id) const;
/**
* Returns the number of tiles in this tileset.
*/
int tileCount() const { return mTiles.size(); }
/**
* Returns the number of tile columns in the tileset image.
*/
int columnCount() const { return mColumnCount; }
/**
* Returns the width of the tileset image.
*/
int imageWidth() const { return mImageWidth; }
/**
* Returns the height of the tileset image.
*/
int imageHeight() const { return mImageHeight; }
/**
* Returns the transparent color, or an invalid color if no transparent
* color is used.
*/
QColor transparentColor() const { return mTransparentColor; }
/**
* Sets the transparent color. Pixels with this color will be masked out
* when loadFromImage() is called.
*/
void setTransparentColor(const QColor &c) { mTransparentColor = c; }
/**
* Load this tileset from the given tileset \a image. This will replace
* existing tile images in this tileset with new ones. If the new image
* contains more tiles than exist in the tileset new tiles will be
* appended, if there are fewer tiles the excess images will be blanked.
*
* The tile width and height of this tileset must be higher than 0.
*
* @param image the image to load the tiles from
* @param fileName the file name of the image, which will be remembered
* as the image source of this tileset.
* @return <code>true</code> if loading was successful, otherwise
* returns <code>false</code>
*/
bool loadFromImage(const QImage &image, const QString &fileName);
/**
* This checks if there is a similar tileset in the given list.
* It is needed for replacing this tileset by its similar copy.
*/
Tileset *findSimilarTileset(const QList<Tileset*> &tilesets) const;
/**
* Returns the file name of the external image that contains the tiles in
* this tileset. Is an empty string when this tileset doesn't have a
* tileset image.
*/
const QString &imageSource() const { return mImageSource; }
/**
* Returns the column count that this tileset would have if the tileset
* image would have the given \a width. This takes into account the tile
* size, margin and spacing.
*/
int columnCountForWidth(int width) const;
/**
* Returns a const reference to the list of terrains in this tileset.
*/
const QList<Terrain*> &terrains() const { return mTerrainTypes; }
/**
* Returns the number of terrain types in this tileset.
*/
int terrainCount() const { return mTerrainTypes.size(); }
/**
* Returns the terrain type at the given \a index.
*/
Terrain *terrain(int index) const { return index >= 0 ? mTerrainTypes[index] : 0; }
/**
* Adds a new terrain type.
*
* @param name the name of the terrain
* @param imageTile the id of the tile that represents the terrain visually
* @return the created Terrain instance
*/
Terrain *addTerrain(const QString &name, int imageTileId);
/**
* Adds the \a terrain type at the given \a index.
*
* The terrain should already have this tileset associated with it.
*/
void insertTerrain(int index, Terrain *terrain);
/**
* Removes the terrain type at the given \a index and returns it. The
* caller becomes responsible for the lifetime of the terrain type.
*
* This will cause the terrain ids of subsequent terrains to shift up to
* fill the space and the terrain information of all tiles in this tileset
* will be updated accordingly.
*/
Terrain *takeTerrainAt(int index);
/**
* Returns the transition penalty(/distance) between 2 terrains. -1 if no transition is possible.
*/
int terrainTransitionPenalty(int terrainType0, int terrainType1);
/**
* Add a new tile to the end of the tileset
*/
void addTile(const QPixmap &image);
/**
* Set a tile's image
*/
void setTileImage(int index, const QPixmap &image);
/**
* Used by the Tile class when its terrain information changes.
*/
void markTerrainDistancesDirty() { mTerrainDistancesDirty = true; }
private:
/**
* Detaches from the external image. Should be called everytime the tileset
* is changed.
*/
void detachExternalImage();
/**
* Sets tile size to the maximum size.
*/
void updateTileSize();
/**
* Calculates the transition distance matrix for all terrain types.
*/
void recalculateTerrainDistances();
QString mName;
QString mFileName;
QString mImageSource;
QColor mTransparentColor;
int mTileWidth;
int mTileHeight;
int mTileSpacing;
int mMargin;
QPoint mTileOffset;
int mImageWidth;
int mImageHeight;
int mColumnCount;
QList<Tile*> mTiles;
QList<Terrain*> mTerrainTypes;
bool mTerrainDistancesDirty;
};
} // namespace Tiled
#endif // TILESET_H