Skip to content

Commit

Permalink
Some object unique ID changes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bjorn committed Nov 23, 2014
1 parent 129ff56 commit 1614568
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 64 deletions.
8 changes: 4 additions & 4 deletions src/libtiled/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Map::Map(Orientation orientation,
mStaggerAxis(StaggerY),
mStaggerIndex(StaggerOdd),
mLayerDataFormat(Base64Zlib),
mNextUid(0)
mNextObjectId(1)
{
}

Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions src/libtiled/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -405,8 +405,7 @@ class TILEDSHARED_EXPORT Map : public Object
QList<Layer*> mLayers;
QList<Tileset*> mTilesets;
LayerDataFormat mLayerDataFormat;

int mNextUid;
int mNextObjectId;
};


Expand Down
8 changes: 4 additions & 4 deletions src/libtiled/mapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ 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)
{
}

MapObject::MapObject(const QString &name, const QString &type,
const QPointF &pos,
const QSizeF &size):
Object(MapObjectType),
mId(0),
mName(name),
mType(type),
mPos(pos),
mSize(size),
mShape(Rectangle),
mObjectGroup(0),
mRotation(0.0f),
mVisible(true),
mUniqueID(-1)
mVisible(true)
{
}

Expand Down
11 changes: 6 additions & 5 deletions src/libtiled/mapobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -250,6 +251,7 @@ class TILEDSHARED_EXPORT MapObject : public Object
MapObject *clone() const;

private:
int mId;
QString mName;
QString mType;
QPointF mPos;
Expand All @@ -260,7 +262,6 @@ class TILEDSHARED_EXPORT MapObject : public Object
ObjectGroup *mObjectGroup;
qreal mRotation;
bool mVisible;
int mUniqueID;
};

} // namespace Tiled
Expand Down
18 changes: 6 additions & 12 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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());
Expand Down
8 changes: 4 additions & 4 deletions src/libtiled/objectgroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/json/maptovariantconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())
Expand All @@ -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();

Expand Down
17 changes: 5 additions & 12 deletions src/plugins/json/varianttomapconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map> MapPtr;
MapPtr map(new Map(orientation,
Expand All @@ -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"]));
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/lua/luaplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()));
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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<const MapObject*>(mObject)->cell().isEmpty()) {
QtVariantProperty *flippingProperty =
Expand Down Expand Up @@ -805,13 +805,13 @@ void PropertyBrowser::updateProperties()
}
case Object::MapObjectType: {
const MapObject *mapObject = static_cast<const MapObject*>(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;
Expand Down

0 comments on commit 1614568

Please sign in to comment.