1 /*
2  * gidmapper.h
3  * Copyright 2011, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
4  *
5  * This file is part of libtiled.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  *    1. Redistributions of source code must retain the above copyright notice,
11  *       this list of conditions and the following disclaimer.
12  *
13  *    2. Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in the
15  *       documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20  * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include "map.h"
32 #include "tilelayer.h"
33 
34 #include <QMap>
35 
36 namespace Tiled {
37 
38 /**
39  * A class that maps cells to global IDs (gids) and back.
40  */
41 class TILEDSHARED_EXPORT GidMapper
42 {
43 public:
44     GidMapper();
45     GidMapper(const QVector<SharedTileset> &tilesets);
46 
47     void insert(unsigned firstGid, const SharedTileset &tileset);
48     void clear();
49     bool isEmpty() const;
50 
51     Cell gidToCell(unsigned gid, bool &ok) const;
52     unsigned cellToGid(const Cell &cell) const;
53 
54     QByteArray encodeLayerData(const TileLayer &tileLayer,
55                                Map::LayerDataFormat format,
56                                QRect bounds = QRect(),
57                                int compressionLevel = -1) const;
58 
59     enum DecodeError {
60         NoError = 0,
61         CorruptLayerData,
62         TileButNoTilesets,
63         InvalidTile
64     };
65 
66     DecodeError decodeLayerData(TileLayer &tileLayer,
67                                 const QByteArray &layerData,
68                                 Map::LayerDataFormat format,
69                                 QRect bounds) const;
70 
71     unsigned invalidTile() const;
72 
73 private:
74     QMap<unsigned, SharedTileset> mFirstGidToTileset;
75 
76     mutable unsigned mInvalidTile = 0;
77 };
78 
79 
80 /**
81  * Insert the given \a tileset with \a firstGid as its first global ID.
82  */
insert(unsigned firstGid,const SharedTileset & tileset)83 inline void GidMapper::insert(unsigned firstGid, const SharedTileset &tileset)
84 {
85     mFirstGidToTileset.insert(firstGid, tileset);
86 }
87 
88 /**
89  * Clears the gid mapper, so that it can be reused.
90  */
clear()91 inline void GidMapper::clear()
92 {
93     mFirstGidToTileset.clear();
94 }
95 
96 /**
97  * Returns true when no tilesets are known to this gid mapper.
98  */
isEmpty()99 inline bool GidMapper::isEmpty() const
100 {
101     return mFirstGidToTileset.isEmpty();
102 }
103 
104 /**
105  * Returns the GID of the invalid tile in case decodeLayerData() returns
106  * the InvalidTile error.
107  */
invalidTile()108 inline unsigned GidMapper::invalidTile() const
109 {
110     return mInvalidTile;
111 }
112 
113 } // namespace Tiled
114