1 /*
2  * editabletile.h
3  * Copyright 2019, 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 "editableobject.h"
24 #include "tile.h"
25 
26 #include <QJSValue>
27 
28 namespace Tiled {
29 
30 class EditableObjectGroup;
31 class EditableTileset;
32 class ScriptImage;
33 class TilesetDocument;
34 
35 class EditableTile : public EditableObject
36 {
37     Q_OBJECT
38 
39     Q_PROPERTY(int id READ id)
40     Q_PROPERTY(int width READ width)
41     Q_PROPERTY(int height READ height)
42     Q_PROPERTY(QSize size READ size)
43     Q_PROPERTY(QString type READ type WRITE setType)
44     Q_PROPERTY(QString imageFileName READ imageFileName WRITE setImageFileName)
45     Q_PROPERTY(qreal probability READ probability WRITE setProbability)
46     Q_PROPERTY(Tiled::EditableObjectGroup *objectGroup READ objectGroup WRITE setObjectGroup)
47     Q_PROPERTY(QJSValue frames READ frames WRITE setFrames)
48     Q_PROPERTY(bool animated READ isAnimated)
49     Q_PROPERTY(Tiled::EditableTileset *tileset READ tileset)
50 
51 public:
52     enum Flags {
53         FlippedHorizontally     = 0x01,
54         FlippedVertically       = 0x02,
55         FlippedAntiDiagonally   = 0x04,
56         RotatedHexagonal120     = 0x08
57     };
58     Q_ENUM(Flags)
59 
60     enum Corner {
61         TopLeft,
62         TopRight,
63         BottomLeft,
64         BottomRight
65     };
66     Q_ENUM(Corner)
67 
68     EditableTile(EditableTileset *tileset,
69                  Tile *tile,
70                  QObject *parent = nullptr);
71     ~EditableTile() override;
72 
73     int id() const;
74     int width() const;
75     int height() const;
76     QSize size() const;
77     const QString &type() const;
78     QString imageFileName() const;
79     qreal probability() const;
80     EditableObjectGroup *objectGroup() const;
81     QJSValue frames() const;
82     bool isAnimated() const;
83     EditableTileset *tileset() const;
84 
85     Q_INVOKABLE void setImage(Tiled::ScriptImage *image);
86 
87     Tile *tile() const;
88 
89     void detach();
90     void attach(EditableTileset *tileset);
91 
attachedObjectGroup()92     const ObjectGroup *attachedObjectGroup() const { return mAttachedObjectGroup; }
93     void detachObjectGroup();
94 
95 public slots:
96     void setType(const QString &type);
97     void setImageFileName(const QString &fileName);
98     void setProbability(qreal probability);
99     void setObjectGroup(EditableObjectGroup *editableObjectGroup);
100     void setFrames(QJSValue value);
101 
102 private:
103     TilesetDocument *tilesetDocument() const;
104 
105     std::unique_ptr<Tile> mDetachedTile;
106     mutable ObjectGroup *mAttachedObjectGroup = nullptr;
107 };
108 
109 
id()110 inline int EditableTile::id() const
111 {
112     return tile()->id();
113 }
114 
width()115 inline int EditableTile::width() const
116 {
117     return tile()->width();
118 }
119 
height()120 inline int EditableTile::height() const
121 {
122     return tile()->height();
123 }
124 
size()125 inline QSize EditableTile::size() const
126 {
127     return tile()->size();
128 }
129 
type()130 inline const QString &EditableTile::type() const
131 {
132     return tile()->type();
133 }
134 
imageFileName()135 inline QString EditableTile::imageFileName() const
136 {
137     return tile()->imageSource().toString(QUrl::PreferLocalFile);
138 }
139 
probability()140 inline qreal EditableTile::probability() const
141 {
142     return tile()->probability();
143 }
144 
isAnimated()145 inline bool EditableTile::isAnimated() const
146 {
147     return tile()->isAnimated();
148 }
149 
tile()150 inline Tile *EditableTile::tile() const
151 {
152     return static_cast<Tile*>(object());
153 }
154 
155 } // namespace Tiled
156 
157 Q_DECLARE_METATYPE(Tiled::EditableTile*)
158