1 /*
2  * tilestamp.h
3  * Copyright 2015, Thorbjørn Lindeijer <bjorn@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 "map.h"
24 #include "tiled.h"
25 
26 #include <QDir>
27 #include <QJsonObject>
28 #include <QSharedData>
29 #include <QString>
30 #include <QVector>
31 
32 namespace Tiled {
33 
34 struct TileStampVariation
35 {
TileStampVariationTileStampVariation36     TileStampVariation()
37         : map(nullptr), probability(1.0)
38     {
39     }
40 
41     explicit TileStampVariation(Map *map, qreal probability = 1.0)
mapTileStampVariation42         : map(map), probability(probability)
43     {
44         Q_ASSERT(map->layerCount() >= 1);
45         Q_ASSERT(map->layerAt(0)->isTileLayer());
46     }
47 
48     Map *map;
49     qreal probability;
50 };
51 
52 class TileStampData;
53 
54 class TileStamp
55 {
56 public:
57     TileStamp();
58     explicit TileStamp(std::unique_ptr<Map> map);
59 
60     TileStamp(const TileStamp &other);
61     TileStamp &operator=(const TileStamp &other);
62 
63     bool operator==(const TileStamp &other) const;
64 
65     ~TileStamp();
66 
67     QString name() const;
68     void setName(const QString &name);
69 
70     QString fileName() const;
71     void setFileName(const QString &fileName);
72 
73     qreal probability(int index) const;
74     void setProbability(int index, qreal probability);
75 
76     QSize maxSize() const;
77 
78     const QVector<TileStampVariation> &variations() const;
79     void addVariation(std::unique_ptr<Map> map, qreal probability = 1.0);
80     void addVariation(const TileStampVariation &variation);
81     Map *takeVariation(int index);
82     bool isEmpty() const;
83 
84     int quickStampIndex() const;
85     void setQuickStampIndex(int quickStampIndex);
86 
87     const TileStampVariation &randomVariation() const;
88 
89     TileStamp flipped(FlipDirection direction) const;
90     TileStamp rotated(RotateDirection direction) const;
91 
92     TileStamp clone() const;
93 
94     QJsonObject toJson(const QDir &dir) const;
95 
96     static TileStamp fromJson(const QJsonObject &json,
97                               const QDir &mapDir);
98 
99 private:
100     QExplicitlySharedDataPointer<TileStampData> d;
101 };
102 
103 
104 /**
105  * Adds a \a variation to this tile stamp.
106  */
addVariation(const TileStampVariation & variation)107 inline void TileStamp::addVariation(const TileStampVariation &variation)
108 {
109     addVariation(variation.map->clone(),
110                  variation.probability);
111 }
112 
113 } // namespace Tiled
114