Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wang Tiles #1582

Merged
merged 26 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
904e8bc
Created WangSet class, as well as some functions for wangIds
Bdtrotte May 28, 2017
cb8d9ae
Moved wangId into a class of its own
Bdtrotte May 29, 2017
e93cc74
Merge branch 'master' into WangTiles
Bdtrotte Jun 11, 2017
c329a97
Merge branch 'master' of https://github.com/bjorn/tiled into WangTiles
Bdtrotte Jun 21, 2017
4757fb6
Refined WangSet class, and added them into Tileset
Bdtrotte Jun 21, 2017
31689c6
Made requested changes
Bdtrotte Jun 21, 2017
e10151e
Enabled file saving and loading. (Needs testing)
Bdtrotte Jun 24, 2017
c7e67d0
Fixed some issues with saving/loading
willt Jun 23, 2017
0ef5add
Style update
Bdtrotte Jun 25, 2017
74dbae3
Foundation for adding UI support to wangTile assignment
Bdtrotte Jun 27, 2017
400d47c
merged upstream
Bdtrotte Jun 27, 2017
ff6c624
Added a couple Undo commands, and made a bit more progress on the mod…
Bdtrotte Jun 28, 2017
3916cd6
WangSets are now able to be created and edited from the ui
Bdtrotte Jun 28, 2017
acae98a
Added new files to tiled.pro
Bdtrotte Jun 28, 2017
68bd9af
adjusted reading/writting so that all the wang set info (including ti…
Bdtrotte Jun 28, 2017
52ae862
Cleaned up a few things
Bdtrotte Jun 29, 2017
4398cea
remoced references to 'addremocewangset
Bdtrotte Jun 29, 2017
7f58709
Now can assign wangsets an image, added functions for getting the wan…
Bdtrotte Jun 30, 2017
bb6b98a
Beging to store info about tile rotation and inversion
Bdtrotte Jul 2, 2017
0969d46
Wangsets now store info about rotation and flipping, mapreader/writer…
Bdtrotte Jul 2, 2017
8b959a9
Merge branch 'wip/wangtiles' into WangTiles
Bdtrotte Jul 4, 2017
1df48f5
Made requested changes
Bdtrotte Jul 4, 2017
25f8494
Merge branch 'WangTiles' of https://github.com/Bdtrotte/tiled into Wa…
Bdtrotte Jul 4, 2017
888ea7b
fixed issue in 'wangIdFromSurrounding'
Bdtrotte Jul 4, 2017
825774a
Made requested changes!
Bdtrotte Jul 4, 2017
0d3f353
Removed file that should not be in version control
bjorn Jul 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
6 changes: 4 additions & 2 deletions src/libtiled/libtiled-src.pri
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ SOURCES += $$PWD/compression.cpp \
$$PWD/tileset.cpp \
$$PWD/tilesetformat.cpp \
$$PWD/tilesetmanager.cpp \
$$PWD/varianttomapconverter.cpp
$$PWD/varianttomapconverter.cpp \
$$PWD/wangset.cpp
HEADERS += $$PWD/compression.h \
$$PWD/filesystemwatcher.h \
$$PWD/gidmapper.h \
Expand Down Expand Up @@ -65,4 +66,5 @@ HEADERS += $$PWD/compression.h \
$$PWD/tileset.h \
$$PWD/tilesetformat.h \
$$PWD/tilesetmanager.h \
$$PWD/varianttomapconverter.h
$$PWD/varianttomapconverter.h \
$$PWD/wangset.h
2 changes: 2 additions & 0 deletions src/libtiled/libtiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ DynamicLibrary {
"tilesetmanager.h",
"varianttomapconverter.cpp",
"varianttomapconverter.h",
"wangset.cpp",
"wangset.h",
]

Group {
Expand Down
51 changes: 51 additions & 0 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "tilelayer.h"
#include "tilesetmanager.h"
#include "terrain.h"
#include "wangset.h"

#include <QCoreApplication>
#include <QDebug>
Expand Down Expand Up @@ -84,6 +85,7 @@ class MapReaderPrivate
void readTilesetGrid(Tileset &tileset);
void readTilesetImage(Tileset &tileset);
void readTilesetTerrainTypes(Tileset &tileset);
void readTilesetWangSets(Tileset &tileset);
ImageReference readImage();

Layer *tryReadLayer();
Expand Down Expand Up @@ -353,6 +355,8 @@ SharedTileset MapReaderPrivate::readTileset()
}
} else if (xml.name() == QLatin1String("terraintypes")) {
readTilesetTerrainTypes(*tileset);
} else if (xml.name() == QLatin1String("wangsets")) {
readTilesetWangSets(*tileset);
} else {
readUnknownElement();
}
Expand Down Expand Up @@ -564,6 +568,53 @@ void MapReaderPrivate::readTilesetTerrainTypes(Tileset &tileset)
}
}

void MapReaderPrivate::readTilesetWangSets(Tileset &tileset)
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("wangsets"));

while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("wangset")) {
const QXmlStreamAttributes atts = xml.attributes();
QString name = atts.value(QLatin1String("name")).toString();
int edges = atts.value(QLatin1String("edges")).toInt();
int corners = atts.value(QLatin1String("corners")).toInt();
int tile = atts.value(QLatin1String("tile")).toInt();

WangSet *wangSet = new WangSet(&tileset, edges, corners, name, tile);

tileset.addWangSet(wangSet);

while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("properties")) {
wangSet->mergeProperties(readProperties());
} else if (xml.name() == QLatin1String("wangtile")) {
const QXmlStreamAttributes tileAtts = xml.attributes();
int tileId = tileAtts.value(QLatin1String("tileid")).toInt();
unsigned wangId = tileAtts.value(QLatin1String("wangid")).toUInt();
bool fH = tileAtts.value(QLatin1String("hflip")).toInt();
bool fV = tileAtts.value(QLatin1String("vflip")).toInt();
bool fA = tileAtts.value(QLatin1String("dflip")).toInt();

Tile *tile = tileset.findOrCreateTile(tileId);

WangTile wangTile(tile, wangId);
wangTile.setFlippedHorizontally(fH);
wangTile.setFlippedVertically(fV);
wangTile.setFlippedAntiDiagonally(fA);

wangSet->addWangTile(wangTile);

xml.skipCurrentElement();
} else {
readUnknownElement();
}
}
} else {
readUnknownElement();
}
}
}

static void readLayerAttributes(Layer &layer,
const QXmlStreamAttributes &atts)
{
Expand Down
37 changes: 36 additions & 1 deletion src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "tilelayer.h"
#include "tileset.h"
#include "terrain.h"
#include "wangset.h"

#include <QBuffer>
#include <QCoreApplication>
Expand Down Expand Up @@ -421,11 +422,45 @@ void MapWriterPrivate::writeTileset(QXmlStreamWriter &w, const Tileset &tileset,
}
w.writeEndElement(); // </animation>
}

w.writeEndElement(); // </tile>
}
}

// Write the wangsets
if (tileset.wangSetCount() > 0) {
w.writeStartElement(QLatin1String("wangsets"));
for (const WangSet *ws : tileset.wangSets()) {
w.writeStartElement(QLatin1String("wangset"));

w.writeAttribute(QLatin1String("name"), ws->name());
w.writeAttribute(QLatin1String("edges"), QString::number(ws->edgeColors()));
w.writeAttribute(QLatin1String("corners"), QString::number(ws->cornerColors()));
w.writeAttribute(QLatin1String("tile"), QString::number(ws->imageTileId()));

for (const WangTile &wangTile : ws->wangTiles()) {
w.writeStartElement(QLatin1String("wangtile"));
w.writeAttribute(QLatin1String("tileid"), QString::number(wangTile.tile()->id()));
w.writeAttribute(QLatin1String("wangid"), QString::number(wangTile.wangId()));

if (wangTile.flippedHorizontally())
w.writeAttribute(QLatin1String("hflip"), QString::number(1));

if (wangTile.flippedVertically())
w.writeAttribute(QLatin1String("vflip"), QString::number(1));

if (wangTile.flippedAntiDiagonally())
w.writeAttribute(QLatin1String("dflip"), QString::number(1));

w.writeEndElement(); // </wangtile>
}

writeProperties(w, ws->properties());

w.writeEndElement(); // </wangset>
}
w.writeEndElement(); // </wangsets>
}

w.writeEndElement();
}

Expand Down
2 changes: 2 additions & 0 deletions src/libtiled/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class TILEDSHARED_EXPORT Object
MapObjectType,
MapType,
TerrainType,
WangSetType,
TilesetType,
TileType
};
Expand Down Expand Up @@ -135,6 +136,7 @@ inline bool Object::isPartOfTileset() const
case Object::TilesetType:
case Object::TileType:
case Object::TerrainType:
case Object::WangSetType:
return true;
default:
return false;
Expand Down
40 changes: 40 additions & 0 deletions src/libtiled/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "terrain.h"
#include "tile.h"
#include "tilesetformat.h"
#include "wangset.h"

#include <QBitmap>

Expand Down Expand Up @@ -62,6 +63,7 @@ Tileset::~Tileset()
{
qDeleteAll(mTiles);
qDeleteAll(mTerrainTypes);
qDeleteAll(mWangSets);
}

void Tileset::setFormat(TilesetFormat *format)
Expand Down Expand Up @@ -505,6 +507,35 @@ void Tileset::recalculateTerrainDistances()
} while (bNewConnections);
}

void Tileset::addWangSet(WangSet *wangSet)
{
Q_ASSERT(wangSet->tileset() == this);

mWangSets.append(wangSet);
}

/**
* @brief Tileset::insertWangSet Adds a wangSet.
* @param wangSet A pointer to the wangset to add.
*/
void Tileset::insertWangSet(WangSet *wangSet, int index)
{
Q_ASSERT(wangSet->tileset() == this);

mWangSets.insert(index, wangSet);
}

/**
* @brief Tileset::takeWangSetAt Removes the wangset at a given index
* And returns it to the caller.
* @param index Index to take at.
* @return
*/
WangSet *Tileset::takeWangSetAt(int index)
{
return mWangSets.takeAt(index);
}

/**
* Adds a new tile to the end of the tileset.
*/
Expand Down Expand Up @@ -617,6 +648,7 @@ void Tileset::swap(Tileset &other)
std::swap(mTiles, other.mTiles);
std::swap(mNextTileId, other.mNextTileId);
std::swap(mTerrainTypes, other.mTerrainTypes);
std::swap(mWangSets, other.mWangSets);
std::swap(mTerrainDistancesDirty, other.mTerrainDistancesDirty);
std::swap(mLoaded, other.mLoaded);
std::swap(mBackgroundColor, other.mBackgroundColor);
Expand All @@ -629,11 +661,15 @@ void Tileset::swap(Tileset &other)
tile->mTileset = this;
for (auto terrain : mTerrainTypes)
terrain->mTileset = this;
for (auto wangSet : mWangSets)
wangSet->setTileset(this);

for (auto tile : other.mTiles)
tile->mTileset = &other;
for (auto terrain : other.mTerrainTypes)
terrain->mTileset = &other;
for (auto wangSet : other.mWangSets)
wangSet->setTileset(&other);
}

SharedTileset Tileset::clone() const
Expand Down Expand Up @@ -669,6 +705,10 @@ SharedTileset Tileset::clone() const
for (Terrain *terrain : mTerrainTypes)
c->mTerrainTypes.append(terrain->clone(c.data()));

c->mWangSets.reserve(mWangSets.size());
for (WangSet *wangSet : mWangSets)
c->mWangSets.append(wangSet->clone(c.data()));

return c;
}

Expand Down
25 changes: 25 additions & 0 deletions src/libtiled/tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Tile;
class Tileset;
class TilesetFormat;
class Terrain;
class WangSet;

typedef QSharedPointer<Tileset> SharedTileset;

Expand Down Expand Up @@ -184,6 +185,14 @@ class TILEDSHARED_EXPORT Tileset : public Object

int terrainTransitionPenalty(int terrainType0, int terrainType1) const;

const QList<WangSet*> &wangSets() const;
int wangSetCount() const;
WangSet *wangSet(int index) const;

void addWangSet(WangSet *wangSet);
void insertWangSet(WangSet *wangSet, int index);
WangSet *takeWangSetAt(int index);

Tile *addTile(const QPixmap &image, const QString &source = QString());
void addTiles(const QList<Tile*> &tiles);
void removeTiles(const QList<Tile *> &tiles);
Expand Down Expand Up @@ -246,6 +255,7 @@ class TILEDSHARED_EXPORT Tileset : public Object
QMap<int, Tile*> mTiles;
int mNextTileId;
QList<Terrain*> mTerrainTypes;
QList<WangSet*> mWangSets;
bool mTerrainDistancesDirty;
bool mLoaded;
QColor mBackgroundColor;
Expand Down Expand Up @@ -553,6 +563,21 @@ inline Terrain *Tileset::terrain(int index) const
return index >= 0 ? mTerrainTypes[index] : nullptr;
}

inline const QList<WangSet*> &Tileset::wangSets() const
{
return mWangSets;
}

inline int Tileset::wangSetCount() const
{
return mWangSets.size();
}

inline WangSet *Tileset::wangSet(int index) const
{
return index >= 0 ? mWangSets[index] : nullptr;
}

/**
* Sets the next id to be used for tiles in this tileset.
*/
Expand Down
Loading