Skip to content

Commit

Permalink
Refer to templates by file name
Browse files Browse the repository at this point in the history
This change removes the "template group" file and the related template
ID system. Instead, each template is stored in its own file. Template
instances refer to the template by the file name. This greatly
simplifies the system while providing almost the same functionality,
since templates can still be grouped using folders.

The ObjectTemplateModel was rewritten based on QFileSystemModel.

The GidMapper now uses strong tileset references, to avoid tilesets
loaded as part of templates getting deleted too soon due to the lack of
another strong reference.
  • Loading branch information
bjorn committed Nov 21, 2017
1 parent d3b3c36 commit ce60ac9
Show file tree
Hide file tree
Showing 67 changed files with 628 additions and 2,331 deletions.
12 changes: 6 additions & 6 deletions src/libtiled/gidmapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ GidMapper::GidMapper(const QVector<SharedTileset> &tilesets)
{
unsigned firstGid = 1;
for (const SharedTileset &tileset : tilesets) {
insert(firstGid, tileset.data());
insert(firstGid, tileset);
firstGid += tileset->nextTileId();
}
}
Expand Down Expand Up @@ -91,16 +91,16 @@ Cell GidMapper::gidToCell(unsigned gid, bool &ok) const
ok = false;
} else {
// Find the tileset containing this tile
QMap<unsigned, Tileset*>::const_iterator i = mFirstGidToTileset.upperBound(gid);
QMap<unsigned, SharedTileset>::const_iterator i = mFirstGidToTileset.upperBound(gid);
if (i == mFirstGidToTileset.begin()) {
// Invalid global tile ID, since it lies before the first tileset
ok = false;
} else {
--i; // Navigate one tileset back since upper bound finds the next
int tileId = gid - i.key();
Tileset *tileset = i.value();
const SharedTileset &tileset = i.value();

result.setTile(tileset, tileId);
result.setTile(tileset.data(), tileId);

ok = true;
}
Expand All @@ -121,8 +121,8 @@ unsigned GidMapper::cellToGid(const Cell &cell) const
const Tileset *tileset = cell.tileset();

// Find the first GID for the tileset
QMap<unsigned, Tileset*>::const_iterator i = mFirstGidToTileset.begin();
QMap<unsigned, Tileset*>::const_iterator i_end = mFirstGidToTileset.end();
QMap<unsigned, SharedTileset>::const_iterator i = mFirstGidToTileset.begin();
QMap<unsigned, SharedTileset>::const_iterator i_end = mFirstGidToTileset.end();
while (i != i_end && i.value() != tileset)
++i;

Expand Down
6 changes: 3 additions & 3 deletions src/libtiled/gidmapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TILEDSHARED_EXPORT GidMapper
GidMapper();
GidMapper(const QVector<SharedTileset> &tilesets);

void insert(unsigned firstGid, Tileset *tileset);
void insert(unsigned firstGid, const SharedTileset &tileset);
void clear();
bool isEmpty() const;

Expand All @@ -70,7 +70,7 @@ class TILEDSHARED_EXPORT GidMapper
unsigned invalidTile() const;

private:
QMap<unsigned, Tileset*> mFirstGidToTileset;
QMap<unsigned, SharedTileset> mFirstGidToTileset;

mutable unsigned mInvalidTile;
};
Expand All @@ -79,7 +79,7 @@ class TILEDSHARED_EXPORT GidMapper
/**
* Insert the given \a tileset with \a firstGid as its first global ID.
*/
inline void GidMapper::insert(unsigned firstGid, Tileset *tileset)
inline void GidMapper::insert(unsigned firstGid, const SharedTileset &tileset)
{
mFirstGidToTileset.insert(firstGid, tileset);
}
Expand Down
8 changes: 2 additions & 6 deletions src/libtiled/libtiled-src.pri
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ SOURCES += $$PWD/compression.cpp \
$$PWD/object.cpp \
$$PWD/objectgroup.cpp \
$$PWD/objecttemplate.cpp \
$$PWD/objecttemplateformat.cpp \
$$PWD/objecttypes.cpp \
$$PWD/orthogonalrenderer.cpp \
$$PWD/plugin.cpp \
$$PWD/pluginmanager.cpp \
$$PWD/properties.cpp \
$$PWD/savefile.cpp \
$$PWD/staggeredrenderer.cpp \
$$PWD/templategroup.cpp \
$$PWD/templategroupformat.cpp \
$$PWD/templatemanager.cpp \
$$PWD/tidmapper.cpp \
$$PWD/tile.cpp \
$$PWD/tileanimationdriver.cpp \
$$PWD/tiled.cpp \
Expand Down Expand Up @@ -61,18 +59,16 @@ HEADERS += $$PWD/compression.h \
$$PWD/object.h \
$$PWD/objectgroup.h \
$$PWD/objecttemplate.h \
$$PWD/objecttemplateformat.h \
$$PWD/objecttypes.h \
$$PWD/orthogonalrenderer.h \
$$PWD/plugin.h \
$$PWD/pluginmanager.h \
$$PWD/properties.h \
$$PWD/savefile.h \
$$PWD/staggeredrenderer.h \
$$PWD/templategroup.h \
$$PWD/templategroupformat.h \
$$PWD/templatemanager.h \
$$PWD/terrain.h \
$$PWD/tidmapper.h \
$$PWD/tile.h \
$$PWD/tileanimationdriver.h \
$$PWD/tiled.h \
Expand Down
8 changes: 2 additions & 6 deletions src/libtiled/libtiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ DynamicLibrary {
"objectgroup.h",
"objecttemplate.cpp",
"objecttemplate.h",
"objecttemplateformat.cpp",
"objecttemplateformat.h",
"objecttypes.cpp",
"objecttypes.h",
"orthogonalrenderer.cpp",
Expand All @@ -86,14 +88,8 @@ DynamicLibrary {
"savefile.h",
"staggeredrenderer.cpp",
"staggeredrenderer.h",
"templategroup.cpp",
"templategroup.h",
"templategroupformat.cpp",
"templategroupformat.h",
"templatemanager.cpp",
"templatemanager.h",
"tidmapper.cpp",
"tidmapper.h",
"tile.cpp",
"tileanimationdriver.cpp",
"tileanimationdriver.h",
Expand Down
36 changes: 7 additions & 29 deletions src/libtiled/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#include "layer.h"
#include "objectgroup.h"
#include "templategroup.h"
#include "objecttemplate.h"
#include "tile.h"
#include "tilelayer.h"
#include "mapobject.h"
Expand Down Expand Up @@ -291,32 +291,23 @@ bool Map::isTilesetUsed(const Tileset *tileset) const
return false;
}

bool Map::addTemplateGroup(TemplateGroup *templateGroup)
QList<MapObject*> Map::replaceObjectTemplate(const ObjectTemplate *oldObjectTemplate,
const ObjectTemplate *newObjectTemplate)
{
if (mTemplateGroups.contains(templateGroup))
return false;

mTemplateGroups.append(templateGroup);
return true;
}

QList<MapObject*> Map::replaceTemplateGroup(TemplateGroup *oldTemplateGroup, TemplateGroup *newTemplateGroup)
{
Q_ASSERT(oldTemplateGroup != newTemplateGroup);
Q_ASSERT(oldObjectTemplate != newObjectTemplate);

QList<MapObject*> changedObjects;
const int index = mTemplateGroups.indexOf(oldTemplateGroup);

for (auto group : objectGroups()) {
for (auto o : group->objects()){
if (o->templateRef().templateGroup == oldTemplateGroup) {
o->setTemplateRef({newTemplateGroup, o->templateRef().templateId});
if (o->objectTemplate() == oldObjectTemplate) {
o->setObjectTemplate(newObjectTemplate);
o->syncWithTemplate();
changedObjects.append(o);
}
}
}

mTemplateGroups.replace(index, newTemplateGroup);
return changedObjects;
}

Expand All @@ -334,10 +325,8 @@ QString Tiled::staggerAxisToString(Map::StaggerAxis staggerAxis)
default:
case Map::StaggerY:
return QLatin1String("y");
break;
case Map::StaggerX:
return QLatin1String("x");
break;
}
}

Expand All @@ -355,10 +344,8 @@ QString Tiled::staggerIndexToString(Map::StaggerIndex staggerIndex)
default:
case Map::StaggerOdd:
return QLatin1String("odd");
break;
case Map::StaggerEven:
return QLatin1String("even");
break;
}
}

Expand All @@ -376,19 +363,14 @@ QString Tiled::orientationToString(Map::Orientation orientation)
default:
case Map::Unknown:
return QLatin1String("unknown");
break;
case Map::Orthogonal:
return QLatin1String("orthogonal");
break;
case Map::Isometric:
return QLatin1String("isometric");
break;
case Map::Staggered:
return QLatin1String("staggered");
break;
case Map::Hexagonal:
return QLatin1String("hexagonal");
break;
}
}

Expand All @@ -413,16 +395,12 @@ QString Tiled::renderOrderToString(Map::RenderOrder renderOrder)
default:
case Map::RightDown:
return QLatin1String("right-down");
break;
case Map::RightUp:
return QLatin1String("right-up");
break;
case Map::LeftDown:
return QLatin1String("left-down");
break;
case Map::LeftUp:
return QLatin1String("left-up");
break;
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/libtiled/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
namespace Tiled {

class MapObject;
class Tile;
class ObjectGroup;
class TemplateGroup;
class ObjectTemplate;
class Tile;

/**
* A tile map. Consists of a stack of layers, each can be either a TileLayer
Expand Down Expand Up @@ -344,21 +344,16 @@ class TILEDSHARED_EXPORT Map : public Object
*/
SharedTileset tilesetAt(int index) const { return mTilesets.at(index); }

TemplateGroup *templateAt(int index) const { return mTemplateGroups.at(index); }

/**
* Returns the tilesets that the tiles on this map are using.
*/
const QVector<SharedTileset> &tilesets() const { return mTilesets; }

const QList<TemplateGroup*> &templateGroups() const { return mTemplateGroups; }

bool addTemplateGroup(TemplateGroup *templateGroup);

/**
* Returns a list of MapObjects to be updated in the map scene
*/
QList<MapObject*> replaceTemplateGroup(TemplateGroup *oldTemplateGroup, TemplateGroup *templateGroup);
QList<MapObject*> replaceObjectTemplate(const ObjectTemplate *oldObjectTemplate,
const ObjectTemplate *newObjectTemplate);

/**
* Returns the background color of this map.
Expand Down Expand Up @@ -414,7 +409,6 @@ class TILEDSHARED_EXPORT Map : public Object
mutable bool mDrawMarginsDirty;
QList<Layer*> mLayers;
QVector<SharedTileset> mTilesets;
QList<TemplateGroup*> mTemplateGroups;
LayerDataFormat mLayerDataFormat;
int mNextObjectId;
};
Expand Down
22 changes: 5 additions & 17 deletions src/libtiled/mapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

#include "map.h"
#include "objectgroup.h"
#include "templategroup.h"
#include "objecttemplate.h"
#include "tile.h"

#include <QFontMetricsF>
Expand Down Expand Up @@ -88,7 +88,7 @@ MapObject::MapObject(const QString &name, const QString &type,
mPos(pos),
mSize(size),
mShape(Rectangle),
mTemplateRef({nullptr, 0}),
mObjectTemplate(nullptr),
mObjectGroup(nullptr),
mRotation(0.0f),
mVisible(true),
Expand Down Expand Up @@ -251,16 +251,14 @@ MapObject *MapObject::clone() const
o->setRotation(mRotation);
o->setVisible(mVisible);
o->setChangedProperties(mChangedProperties);
o->setTemplateRef(templateRef());
o->setObjectTemplate(mObjectTemplate);
return o;
}

const MapObject *MapObject::templateObject() const
{
if (auto group = templateGroup())
if (auto objectTemplate = group->findTemplate(mTemplateRef.templateId))
return objectTemplate->object();

if (mObjectTemplate)
return mObjectTemplate->object();
return nullptr;
}

Expand Down Expand Up @@ -298,16 +296,6 @@ void MapObject::syncWithTemplate()
setVisible(base->isVisible());
}

bool MapObject::isTemplateInstance() const
{
return mTemplateRef.templateGroup;
}

TemplateGroup *MapObject::templateGroup() const
{
return mTemplateRef.templateGroup;
}

void MapObject::flipRectObject(const QTransform &flipTransform)
{
QPointF oldBottomLeftPoint = QPointF(cos(qDegreesToRadians(rotation() + 90)) * height() + x(),
Expand Down
Loading

0 comments on commit ce60ac9

Please sign in to comment.