From 16145681c523ab3cd0c3c41c1238aa42f8cd8622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Sun, 23 Nov 2014 22:53:34 +0100 Subject: [PATCH] Some object unique ID changes I think it's enough to just call it the object ID, and document that these IDs are unique for each object on the map. Since the first valid object ID is 1, the property is initialized with 0 now instead of -1. Removed unnecessary ID assignments from MapWriter and VariantToMapConverter. Any objects that don't have an ID assigned yet will get one assigned once the ObjectGroup is added to the Map. --- src/libtiled/map.cpp | 8 ++++---- src/libtiled/map.h | 21 ++++++++++----------- src/libtiled/mapobject.cpp | 8 ++++---- src/libtiled/mapobject.h | 11 ++++++----- src/libtiled/mapreader.cpp | 18 ++++++------------ src/libtiled/mapwriter.cpp | 9 +++------ src/libtiled/objectgroup.cpp | 8 ++++---- src/plugins/json/maptovariantconverter.cpp | 4 ++-- src/plugins/json/varianttomapconverter.cpp | 17 +++++------------ src/plugins/lua/luaplugin.cpp | 4 ++-- src/tiled/propertybrowser.cpp | 4 ++-- 11 files changed, 48 insertions(+), 64 deletions(-) diff --git a/src/libtiled/map.cpp b/src/libtiled/map.cpp index b9e20271a7..98889dca26 100644 --- a/src/libtiled/map.cpp +++ b/src/libtiled/map.cpp @@ -52,7 +52,7 @@ Map::Map(Orientation orientation, mStaggerAxis(StaggerY), mStaggerIndex(StaggerOdd), mLayerDataFormat(Base64Zlib), - mNextUid(0) + mNextObjectId(1) { } @@ -71,7 +71,7 @@ Map::Map(const Map &map): mDrawMargins(map.mDrawMargins), mTilesets(map.mTilesets), mLayerDataFormat(map.mLayerDataFormat), - mNextUid(0) + mNextObjectId(1) { foreach (const Layer *layer, map.mLayers) { Layer *clone = layer->clone(); @@ -188,8 +188,8 @@ void Map::adoptLayer(Layer *layer) if (ObjectGroup *group = layer->asObjectGroup()) { foreach (MapObject *o, group->objects()) { - if (o->uniqueID() == -1) - o->setUniqueID(nextUid()); + if (o->id() == 0) + o->setId(takeNextObjectId()); } } } diff --git a/src/libtiled/map.h b/src/libtiled/map.h index bb51bbb240..d14767bdc5 100644 --- a/src/libtiled/map.h +++ b/src/libtiled/map.h @@ -370,23 +370,23 @@ class TILEDSHARED_EXPORT Map : public Object { mLayerDataFormat = format; } /** - * Sets the next UniqueId of this map. + * Sets the next id to be used for objects on this map. */ - void setNextUid(int nextUid) { - if (nextUid == 0) - nextUid = 1; - mNextUid = nextUid; + void setNextObjectId(int nextId) + { + Q_ASSERT(nextId > 0); + mNextObjectId = nextId; } /** - * Returns the next UniqueId for this map. + * Returns the next object id for this map. */ - int nextUid() { return mNextUid++; } + int nextObjectId() const { return mNextObjectId; } /** - * Returns the current UniqueId for this map. + * Returns the next object id for this map and allocates a new one. */ - int currentNextUid() const { return mNextUid; } + int takeNextObjectId() { return mNextObjectId++; } private: void adoptLayer(Layer *layer); @@ -405,8 +405,7 @@ class TILEDSHARED_EXPORT Map : public Object QList mLayers; QList mTilesets; LayerDataFormat mLayerDataFormat; - - int mNextUid; + int mNextObjectId; }; diff --git a/src/libtiled/mapobject.cpp b/src/libtiled/mapobject.cpp index 8ec8ada205..f7046803e5 100644 --- a/src/libtiled/mapobject.cpp +++ b/src/libtiled/mapobject.cpp @@ -33,12 +33,12 @@ using namespace Tiled; MapObject::MapObject(): Object(MapObjectType), + mId(0), mSize(0, 0), mShape(Rectangle), mObjectGroup(0), mRotation(0.0f), - mVisible(true), - mUniqueID(-1) + mVisible(true) { } @@ -46,6 +46,7 @@ MapObject::MapObject(const QString &name, const QString &type, const QPointF &pos, const QSizeF &size): Object(MapObjectType), + mId(0), mName(name), mType(type), mPos(pos), @@ -53,8 +54,7 @@ MapObject::MapObject(const QString &name, const QString &type, mShape(Rectangle), mObjectGroup(0), mRotation(0.0f), - mVisible(true), - mUniqueID(-1) + mVisible(true) { } diff --git a/src/libtiled/mapobject.h b/src/libtiled/mapobject.h index fea84fa987..1f1ff8c25a 100644 --- a/src/libtiled/mapobject.h +++ b/src/libtiled/mapobject.h @@ -76,14 +76,15 @@ class TILEDSHARED_EXPORT MapObject : public Object const QSizeF &size); /** - * Returns the unique id of this object. + * Returns the id of this object. Each object gets an id assigned that is + * unique for the map the object is on. */ - int uniqueID() const { return mUniqueID; } + int id() const { return mId; } /** - * Sets the unique id of this object. + * Sets the id of this object. */ - void setUniqueID(int id) { mUniqueID = id; } + void setId(int id) { mId = id; } /** * Returns the name of this object. The name is usually just used for @@ -250,6 +251,7 @@ class TILEDSHARED_EXPORT MapObject : public Object MapObject *clone() const; private: + int mId; QString mName; QString mType; QPointF mPos; @@ -260,7 +262,6 @@ class TILEDSHARED_EXPORT MapObject : public Object ObjectGroup *mObjectGroup; qreal mRotation; bool mVisible; - int mUniqueID; }; } // namespace Tiled diff --git a/src/libtiled/mapreader.cpp b/src/libtiled/mapreader.cpp index 41e4016085..c5c68e87c3 100644 --- a/src/libtiled/mapreader.cpp +++ b/src/libtiled/mapreader.cpp @@ -238,15 +238,16 @@ Map *MapReaderPrivate::readMap() const Map::RenderOrder renderOrder = renderOrderFromString(renderOrderString); - const int nextUid = - atts.value(QLatin1String("nextUid")).toString().toInt(); + const int nextObjectId = + atts.value(QLatin1String("nextobjectid")).toString().toInt(); mMap = new Map(orientation, mapWidth, mapHeight, tileWidth, tileHeight); mMap->setHexSideLength(hexSideLength); mMap->setStaggerAxis(staggerAxis); mMap->setStaggerIndex(staggerIndex); mMap->setRenderOrder(renderOrder); - mMap->setNextUid(nextUid); + if (nextObjectId) + mMap->setNextObjectId(nextObjectId); mCreatedTilesets.clear(); @@ -825,7 +826,7 @@ MapObject *MapReaderPrivate::readObject() Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("object")); const QXmlStreamAttributes atts = xml.attributes(); - int uniqueID = atts.value(QLatin1String("uid")).toString().toInt(); + const int id = atts.value(QLatin1String("id")).toString().toInt(); const QString name = atts.value(QLatin1String("name")).toString(); const unsigned gid = atts.value(QLatin1String("gid")).toString().toUInt(); const qreal x = atts.value(QLatin1String("x")).toString().toDouble(); @@ -838,15 +839,8 @@ MapObject *MapReaderPrivate::readObject() const QPointF pos(x, y); const QSizeF size(width, height); - //If the uniqueID is 0, this must be an old map and this object - //has no UniqueID yet. So we create it a UniqueID here. - if(uniqueID == 0) - { - uniqueID = mMap->nextUid(); - } - MapObject *object = new MapObject(name, type, pos, size); - object->setUniqueID(uniqueID); + object->setId(id); bool ok; const qreal rotation = atts.value(QLatin1String("rotation")).toString().toDouble(&ok); diff --git a/src/libtiled/mapwriter.cpp b/src/libtiled/mapwriter.cpp index 5887c44d84..dfc194f66d 100644 --- a/src/libtiled/mapwriter.cpp +++ b/src/libtiled/mapwriter.cpp @@ -200,9 +200,8 @@ void MapWriterPrivate::writeMap(QXmlStreamWriter &w, const Map *map) map->backgroundColor().name()); } - int nextUid = map->currentNextUid(); - w.writeAttribute(QLatin1String("nextUid"), - QString::number(nextUid)); + w.writeAttribute(QLatin1String("nextobjectid"), + QString::number(map->nextObjectId())); writeProperties(w, map->properties()); @@ -530,15 +529,13 @@ void MapWriterPrivate::writeObject(QXmlStreamWriter &w, const MapObject *mapObject) { w.writeStartElement(QLatin1String("object")); + w.writeAttribute(QLatin1String("id"), QString::number(mapObject->id())); const QString &name = mapObject->name(); const QString &type = mapObject->type(); - const int &uniqueID = mapObject->uniqueID(); if (!name.isEmpty()) w.writeAttribute(QLatin1String("name"), name); if (!type.isEmpty()) w.writeAttribute(QLatin1String("type"), type); - if (uniqueID != 0) - w.writeAttribute(QLatin1String("uid"), QString::number(uniqueID)); if (!mapObject->cell().isEmpty()) { const unsigned gid = mGidMapper.cellToGid(mapObject->cell()); diff --git a/src/libtiled/objectgroup.cpp b/src/libtiled/objectgroup.cpp index 0a6de0f767..bb78627c47 100644 --- a/src/libtiled/objectgroup.cpp +++ b/src/libtiled/objectgroup.cpp @@ -62,16 +62,16 @@ void ObjectGroup::addObject(MapObject *object) { mObjects.append(object); object->setObjectGroup(this); - if (mMap && object->uniqueID() == -1) - object->setUniqueID(mMap->nextUid()); + if (mMap && object->id() == 0) + object->setId(mMap->takeNextObjectId()); } void ObjectGroup::insertObject(int index, MapObject *object) { mObjects.insert(index, object); object->setObjectGroup(this); - if (mMap && object->uniqueID() == -1) - object->setUniqueID(mMap->nextUid()); + if (mMap && object->id() == 0) + object->setId(mMap->takeNextObjectId()); } int ObjectGroup::removeObject(MapObject *object) diff --git a/src/plugins/json/maptovariantconverter.cpp b/src/plugins/json/maptovariantconverter.cpp index dfb2f66da2..d5332e993a 100644 --- a/src/plugins/json/maptovariantconverter.cpp +++ b/src/plugins/json/maptovariantconverter.cpp @@ -49,7 +49,7 @@ QVariant MapToVariantConverter::toVariant(const Map *map, const QDir &mapDir) mapVariant["tilewidth"] = map->tileWidth(); mapVariant["tileheight"] = map->tileHeight(); mapVariant["properties"] = toVariant(map->properties()); - mapVariant["nextUid"] = map->currentNextUid(); + mapVariant["nextobjectid"] = map->nextObjectId(); if (map->orientation() == Map::Hexagonal) { mapVariant["hexsidelength"] = map->hexSideLength(); @@ -236,6 +236,7 @@ QVariant MapToVariantConverter::toVariant(const ObjectGroup *objectGroup) const const QString &type = object->type(); objectVariant["properties"] = toVariant(object->properties()); + objectVariant["id"] = object->id(); objectVariant["name"] = name; objectVariant["type"] = type; if (!object->cell().isEmpty()) @@ -246,7 +247,6 @@ QVariant MapToVariantConverter::toVariant(const ObjectGroup *objectGroup) const objectVariant["width"] = object->width(); objectVariant["height"] = object->height(); objectVariant["rotation"] = object->rotation(); - objectVariant["uid"] = object->uniqueID(); objectVariant["visible"] = object->isVisible(); diff --git a/src/plugins/json/varianttomapconverter.cpp b/src/plugins/json/varianttomapconverter.cpp index 8ee7fe909f..b54c56d695 100644 --- a/src/plugins/json/varianttomapconverter.cpp +++ b/src/plugins/json/varianttomapconverter.cpp @@ -69,7 +69,7 @@ Map *VariantToMapConverter::toMap(const QVariant &variant, const QString renderOrderString = variantMap["renderorder"].toString(); Map::RenderOrder renderOrder = renderOrderFromString(renderOrderString); - int nextUid = variantMap["nextUid"].toString().toInt(); + const int nextObjectId = variantMap["nextobjectid"].toString().toInt(); typedef QScopedPointer MapPtr; MapPtr map(new Map(orientation, @@ -81,7 +81,8 @@ Map *VariantToMapConverter::toMap(const QVariant &variant, map->setStaggerAxis(staggerAxis); map->setStaggerIndex(staggerIndex); map->setRenderOrder(renderOrder); - map->setNextUid(nextUid); + if (nextObjectId) + map->setNextObjectId(nextObjectId); mMap = map.data(); map->setProperties(toProperties(variantMap["properties"])); @@ -351,7 +352,7 @@ ObjectGroup *VariantToMapConverter::toObjectGroup(const QVariantMap &variantMap) const QString name = objectVariantMap["name"].toString(); const QString type = objectVariantMap["type"].toString(); - int uniqueID = objectVariantMap["uid"].toString().toInt(); + const int id = objectVariantMap["id"].toString().toInt(); const int gid = objectVariantMap["gid"].toInt(); const qreal x = objectVariantMap["x"].toReal(); const qreal y = objectVariantMap["y"].toReal(); @@ -362,16 +363,8 @@ ObjectGroup *VariantToMapConverter::toObjectGroup(const QVariantMap &variantMap) const QPointF pos(x, y); const QSizeF size(width, height); - - //If the uniqueID is 0, this must be an old map and this object - //has no UniqueID yet. So we create it a UniqueID here. - if(uniqueID == 0) - { - uniqueID = mMap->nextUid(); - } - MapObject *object = new MapObject(name, type, pos, size); - object->setUniqueID(uniqueID); + object->setId(id); object->setRotation(rotation); if (gid) { diff --git a/src/plugins/lua/luaplugin.cpp b/src/plugins/lua/luaplugin.cpp index 13e8b0d72e..23551ae42e 100644 --- a/src/plugins/lua/luaplugin.cpp +++ b/src/plugins/lua/luaplugin.cpp @@ -115,7 +115,7 @@ void LuaPlugin::writeMap(LuaTableWriter &writer, const Map *map) writer.writeKeyAndValue("height", map->height()); writer.writeKeyAndValue("tilewidth", map->tileWidth()); writer.writeKeyAndValue("tileheight", map->tileHeight()); - writer.writeKeyAndValue("nextUid", map->currentNextUid()); + writer.writeKeyAndValue("nextobjectid", map->nextObjectId()); const QColor &backgroundColor = map->backgroundColor(); if (backgroundColor.isValid()) { @@ -405,7 +405,7 @@ void LuaPlugin::writeMapObject(LuaTableWriter &writer, const Tiled::MapObject *mapObject) { writer.writeStartTable(); - writer.writeKeyAndValue("uid", mapObject->uniqueID()); + writer.writeKeyAndValue("id", mapObject->id()); writer.writeKeyAndValue("name", mapObject->name()); writer.writeKeyAndValue("type", mapObject->type()); writer.writeKeyAndValue("shape", toString(mapObject->shape())); diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index 6b88ba8ff7..6fd31b20f7 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -385,6 +385,7 @@ static QStringList objectTypeNames() void PropertyBrowser::addMapObjectProperties() { QtProperty *groupProperty = mGroupManager->addProperty(tr("Object")); + createProperty(IdProperty, QVariant::Int, tr("ID"), groupProperty)->setEnabled(false); createProperty(NameProperty, QVariant::String, tr("Name"), groupProperty); QtVariantProperty *typeProperty = @@ -395,7 +396,6 @@ void PropertyBrowser::addMapObjectProperties() createProperty(PositionProperty, QVariant::PointF, tr("Position"), groupProperty); createProperty(SizeProperty, QVariant::SizeF, tr("Size"), groupProperty); createProperty(RotationProperty, QVariant::Double, tr("Rotation"), groupProperty); - createProperty(IdProperty, QVariant::Int, tr("uniqueID"), groupProperty)->setEnabled(false); if (!static_cast(mObject)->cell().isEmpty()) { QtVariantProperty *flippingProperty = @@ -805,13 +805,13 @@ void PropertyBrowser::updateProperties() } case Object::MapObjectType: { const MapObject *mapObject = static_cast(mObject); + mIdToProperty[IdProperty]->setValue(mapObject->id()); mIdToProperty[NameProperty]->setValue(mapObject->name()); mIdToProperty[TypeProperty]->setValue(mapObject->type()); mIdToProperty[VisibleProperty]->setValue(mapObject->isVisible()); mIdToProperty[PositionProperty]->setValue(mapObject->position()); mIdToProperty[SizeProperty]->setValue(mapObject->size()); mIdToProperty[RotationProperty]->setValue(mapObject->rotation()); - mIdToProperty[IdProperty]->setValue(mapObject->uniqueID()); if (QtVariantProperty *property = mIdToProperty[FlippingProperty]) { int flippingFlags = 0;