1 /*
2  * editablewangset.h
3  * Copyright 2019, Your Name <your.name@domain>
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 "wangset.h"
25 
26 #include <QJSValue>
27 
28 namespace Tiled {
29 
30 class EditableTile;
31 class EditableTileset;
32 class TilesetDocument;
33 
34 class EditableWangSet : public EditableObject
35 {
36     Q_OBJECT
37 
38     Q_PROPERTY(QString name READ name WRITE setName)
39     Q_PROPERTY(Type type READ type WRITE setType)
40     Q_PROPERTY(Tiled::EditableTile *imageTile READ imageTile WRITE setImageTile)
41     Q_PROPERTY(int colorCount READ colorCount WRITE setColorCount)
42     Q_PROPERTY(Tiled::EditableTileset *tileset READ tileset)
43 
44 public:
45     enum Type {
46         Corner,
47         Edge,
48         Mixed
49     };
50     Q_ENUM(Type)
51 
52     EditableWangSet(EditableTileset *tileset,
53                     WangSet *wangSet,
54                     QObject *parent = nullptr);
55     ~EditableWangSet() override;
56 
57     QString name() const;
58     Type type() const;
59     EditableTile *imageTile() const;
60     int colorCount() const;
61     EditableTileset *tileset() const;
62 
63     Q_INVOKABLE QJSValue wangId(Tiled::EditableTile *tile);
64     Q_INVOKABLE void setWangId(Tiled::EditableTile *tile, QJSValue value);
65 
66     void setName(const QString &name);
67     void setType(Type type);
68     void setImageTile(Tiled::EditableTile *imageTile);
69     void setColorCount(int n);
70 
71     WangSet *wangSet() const;
72 
73     void detach();
74     void attach(EditableTileset *tileset);
75     void hold();
76     void release();
77 
78 private:
79     TilesetDocument *tilesetDocument() const;
80 
81     std::unique_ptr<WangSet> mDetachedWangSet;
82 };
83 
84 
name()85 inline QString EditableWangSet::name() const
86 {
87     return wangSet()->name();
88 }
89 
type()90 inline EditableWangSet::Type EditableWangSet::type() const
91 {
92     return static_cast<Type>(wangSet()->type());
93 }
94 
colorCount()95 inline int EditableWangSet::colorCount() const
96 {
97     return wangSet()->colorCount();
98 }
99 
wangSet()100 inline WangSet *EditableWangSet::wangSet() const
101 {
102     return static_cast<WangSet*>(object());
103 }
104 
105 } // namespace Tiled
106 
107 Q_DECLARE_METATYPE(Tiled::EditableWangSet*)
108