Skip to content

Commit

Permalink
Merge branch 'wip/templates'
Browse files Browse the repository at this point in the history
Conflicts:
	src/libtiled/mapreader.cpp
	src/libtiled/object.h
	src/tiled/brokenlinks.cpp
	src/tiled/propertybrowser.cpp
	src/tiled/tiled.qrc
  • Loading branch information
bjorn committed Aug 20, 2017
2 parents 2384dce + 04eaccf commit ab84788
Show file tree
Hide file tree
Showing 75 changed files with 3,863 additions and 155 deletions.
10 changes: 10 additions & 0 deletions src/libtiled/libtiled-src.pri
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ SOURCES += $$PWD/compression.cpp \
$$PWD/maptovariantconverter.cpp \
$$PWD/mapwriter.cpp \
$$PWD/objectgroup.cpp \
$$PWD/objecttemplate.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 @@ -52,13 +57,18 @@ HEADERS += $$PWD/compression.h \
$$PWD/mapwriter.h \
$$PWD/object.h \
$$PWD/objectgroup.h \
$$PWD/objecttemplate.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
10 changes: 10 additions & 0 deletions src/libtiled/libtiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ DynamicLibrary {
"mapwriter.h",
"objectgroup.cpp",
"objectgroup.h",
"objecttemplate.cpp",
"objecttemplate.h",
"object.h",
"orthogonalrenderer.cpp",
"orthogonalrenderer.h",
Expand All @@ -79,6 +81,14 @@ 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
30 changes: 30 additions & 0 deletions src/libtiled/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

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

bool Map::addTemplateGroup(TemplateGroup *templateGroup)
{
if (mTemplateGroups.contains(templateGroup))
return false;

mTemplateGroups.append(templateGroup);
return true;
}

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

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});
o->syncWithTemplate();
changedObjects.append(o);
}
}
}

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

void Map::initializeObjectIds(ObjectGroup &objectGroup)
{
for (MapObject *o : objectGroup) {
Expand Down
14 changes: 14 additions & 0 deletions src/libtiled/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@

namespace Tiled {

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

/**
* A tile map. Consists of a stack of layers, each can be either a TileLayer
Expand Down Expand Up @@ -342,11 +344,22 @@ 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);

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

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

#include <QFontMetricsF>
Expand Down Expand Up @@ -77,9 +78,11 @@ MapObject::MapObject():
mId(0),
mSize(0, 0),
mShape(Rectangle),
mTemplateRef({nullptr, 0}),
mObjectGroup(nullptr),
mRotation(0.0f),
mVisible(true)
mVisible(true),
mChangedProperties(0)
{
}

Expand All @@ -93,9 +96,11 @@ MapObject::MapObject(const QString &name, const QString &type,
mPos(pos),
mSize(size),
mShape(Rectangle),
mTemplateRef({nullptr, 0}),
mObjectGroup(nullptr),
mRotation(0.0f),
mVisible(true)
mVisible(true),
mChangedProperties(0)
{
}

Expand Down Expand Up @@ -178,6 +183,10 @@ QVariant MapObject::mapObjectProperty(Property property) const
case TextAlignmentProperty: return QVariant::fromValue(mTextData.alignment);
case TextWordWrapProperty: return mTextData.wordWrap;
case TextColorProperty: return mTextData.color;
case SizeProperty: return mSize;
case RotationProperty: return mRotation;
case CellProperty: Q_ASSERT(false); break;
case ShapeProperty: Q_ASSERT(false); break;
}
return QVariant();
}
Expand All @@ -195,6 +204,10 @@ void MapObject::setMapObjectProperty(Property property, const QVariant &value)
case TextAlignmentProperty: mTextData.alignment = value.value<Qt::Alignment>(); break;
case TextWordWrapProperty: mTextData.wordWrap = value.toBool(); break;
case TextColorProperty: mTextData.color = value.value<QColor>(); break;
case SizeProperty: mSize = value.toSizeF(); break;
case RotationProperty: mRotation = value.toReal(); break;
case CellProperty: Q_ASSERT(false); break;
case ShapeProperty: Q_ASSERT(false); break;
}
}

Expand Down Expand Up @@ -244,9 +257,68 @@ MapObject *MapObject::clone() const
o->setCell(mCell);
o->setRotation(mRotation);
o->setVisible(mVisible);
o->setChangedProperties(mChangedProperties);
o->setTemplateRef(templateRef());
return o;
}

const MapObject *MapObject::templateObject() const
{
if (!mTemplateRef.templateGroup)
return nullptr;

auto objectTemplate = mTemplateRef.templateGroup->findTemplate(mTemplateRef.templateId);

if (objectTemplate)
return objectTemplate->object();

return nullptr;
}

void MapObject::syncWithTemplate()
{
const MapObject *base = templateObject();

if (!base)
return;

if (!propertyChanged(MapObject::NameProperty))
setName(base->name());

if (!propertyChanged(MapObject::SizeProperty))
setSize(base->size());

if (!propertyChanged(MapObject::TypeProperty))
setType(base->type());

if (!propertyChanged(MapObject::TextProperty))
setTextData(base->textData());

if (!propertyChanged(MapObject::ShapeProperty)) {
setShape(base->shape());
setPolygon(base->polygon());
}

if (!propertyChanged(MapObject::CellProperty))
setCell(base->cell());

if (!propertyChanged(MapObject::RotationProperty))
setRotation(base->rotation());

if (!propertyChanged(MapObject::VisibleProperty))
setVisible(base->isVisible());
}

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

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

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

0 comments on commit ab84788

Please sign in to comment.