1 /*
2 * object.h
3 * Copyright 2010, 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 "properties.h"
32 #include "objecttypes.h"
33
34 namespace Tiled {
35
36 /**
37 * The base class for anything that can hold properties.
38 */
39 class TILEDSHARED_EXPORT Object
40 {
41 public:
42 enum TypeId {
43 LayerType,
44 MapObjectType,
45 MapType,
46 ObjectTemplateType,
47 TilesetType,
48 TileType,
49 WangSetType,
50 WangColorType
51 };
52
Object(TypeId typeId)53 explicit Object(TypeId typeId) : mTypeId(typeId) {}
54
55 /**
56 * Virtual destructor.
57 */
58 virtual ~Object();
59
60 /**
61 * Returns the type of this object.
62 */
typeId()63 TypeId typeId() const { return mTypeId; }
64
65 /**
66 * Returns the properties of this object.
67 */
properties()68 const Properties &properties() const { return mProperties; }
69
70 /**
71 * Replaces all existing properties with a new set of properties.
72 */
setProperties(const Properties & properties)73 void setProperties(const Properties &properties)
74 { mProperties = properties; }
75
76 /**
77 * Clears all existing properties
78 */
clearProperties()79 void clearProperties ()
80 { mProperties.clear(); }
81
82 /**
83 * Merges \a properties with the existing properties. Properties with the
84 * same name will be overridden.
85 *
86 * \sa Tiled::mergeProperties
87 */
mergeProperties(const Properties & properties)88 void mergeProperties(const Properties &properties)
89 { Tiled::mergeProperties(mProperties, properties); }
90
91 /**
92 * Returns the value of the object's \a name property.
93 */
property(const QString & name)94 QVariant property(const QString &name) const
95 { return mProperties.value(name); }
96
97 QVariant resolvedProperty(const QString &name) const;
98 QVariantMap resolvedProperties() const;
99
100 /**
101 * Returns the value of the object's \a name property, as a string.
102 *
103 * This is a workaround for the Python plugin, because I do not know how
104 * to pass a QVariant back to Python.
105 */
propertyAsString(const QString & name)106 QString propertyAsString(const QString &name) const
107 { return mProperties.value(name).toString(); }
108
109 /**
110 * Returns the type of the object's \a name property, as a string.
111 */
propertyType(const QString & name)112 QString propertyType(const QString &name) const
113 { return typeToName(mProperties.value(name).userType()); }
114
115 /**
116 * Returns whether this object has a property with the given \a name.
117 */
hasProperty(const QString & name)118 bool hasProperty(const QString &name) const
119 { return mProperties.contains(name); }
120
121 /**
122 * Sets the value of the object's \a name property to \a value.
123 */
setProperty(const QString & name,const QVariant & value)124 void setProperty(const QString &name, const QVariant &value)
125 { mProperties.insert(name, value); }
126
127 /**
128 * Removes the property with the given \a name.
129 */
removeProperty(const QString & name)130 void removeProperty(const QString &name)
131 { mProperties.remove(name); }
132
133 bool isPartOfTileset() const;
134
135 static void setObjectTypes(const ObjectTypes &objectTypes);
objectTypes()136 static const ObjectTypes &objectTypes()
137 { return mObjectTypes; }
138
139 private:
140 const TypeId mTypeId;
141 Properties mProperties;
142
143 static ObjectTypes mObjectTypes;
144 };
145
146
147 /**
148 * Returns whether this object is stored as part of a tileset.
149 */
isPartOfTileset()150 inline bool Object::isPartOfTileset() const
151 {
152 switch (mTypeId) {
153 case Object::TilesetType:
154 case Object::TileType:
155 case Object::WangSetType:
156 case Object::WangColorType:
157 return true;
158 default:
159 return false;
160 }
161 }
162
163 } // namespace Tiled
164