Skip to content

Commit

Permalink
Added initial terrain painting tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkeyMan committed May 31, 2012
1 parent c1b1d80 commit 5ed684e
Show file tree
Hide file tree
Showing 9 changed files with 745 additions and 7 deletions.
35 changes: 35 additions & 0 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class MapReaderPrivate
Tileset *readTileset();
void readTilesetTile(Tileset *tileset);
void readTilesetImage(Tileset *tileset);
void readTilesetTerrainTypes(Tileset *tileset);

TileLayer *readLayer();
void readLayerData(TileLayer *tileLayer);
Expand Down Expand Up @@ -284,6 +285,8 @@ Tileset *MapReaderPrivate::readTileset()
tileset->mergeProperties(readProperties());
} else if (xml.name() == "image") {
readTilesetImage(tileset);
} else if (xml.name() == "terraintypes") {
readTilesetTerrainTypes(tileset);
} else {
readUnknownElement();
}
Expand Down Expand Up @@ -322,6 +325,19 @@ void MapReaderPrivate::readTilesetTile(Tileset *tileset)

// TODO: Add support for individual tiles (then it needs to be added here)

// Read tile quadrant terrain ids
QString terrain = atts.value(QLatin1String("terrain")).toString();
if (!terrain.isEmpty()) {
Tile *tile = tileset->tileAt(id);
QStringList quadrants = terrain.split(QLatin1String(","));
if (quadrants.size() == 4) {
for (int i = 0; i < 4; ++i) {
int t = quadrants[i].isEmpty() ? -1 : quadrants[i].toInt();
tile->setCornerTerrainType(i, t);
}
}
}

while (xml.readNextStartElement()) {
if (xml.name() == "properties") {
Tile *tile = tileset->tileAt(id);
Expand Down Expand Up @@ -359,6 +375,25 @@ void MapReaderPrivate::readTilesetImage(Tileset *tileset)
xml.skipCurrentElement();
}

void MapReaderPrivate::readTilesetTerrainTypes(Tileset *tileset)
{
Q_ASSERT(xml.isStartElement() && xml.name() == "terraintypes");

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

tileset->addTerrainType(name, tile);

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

static void readLayerAttributes(Layer *layer,
const QXmlStreamAttributes &atts)
{
Expand Down
35 changes: 33 additions & 2 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ void MapWriterPrivate::writeMap(QXmlStreamWriter &w, const Map *map)
w.writeEndElement();
}

static QString makeTerrainAttribute(const Tile *tile)
{
QString terrain;
for (int i = 0; i < 4; ++i ) {
if (i > 0)
terrain += QLatin1String(",");
int t = tile->cornerTerrainType(i);
if (t > -1)
terrain += QString::number(t);
}
return terrain;
}

void MapWriterPrivate::writeTileset(QXmlStreamWriter &w, const Tileset *tileset,
uint firstGid)
{
Expand Down Expand Up @@ -259,14 +272,32 @@ void MapWriterPrivate::writeTileset(QXmlStreamWriter &w, const Tileset *tileset,
w.writeEndElement();
}

// Write the terrain types
if (tileset->terrainTypeCount() > 0) {
w.writeStartElement(QLatin1String("terraintypes"));
for (int i = 0; i < tileset->terrainTypeCount(); ++i) {
TerrainType* tt = tileset->terrainType(i);
w.writeStartElement(QLatin1String("terrain"));
w.writeAttribute(QLatin1String("name"), tt->name());
// w.writeAttribute(QLatin1String("color"), tt->color());
w.writeAttribute(QLatin1String("tile"), QString::number(tt->paletteImageTile()));
w.writeEndElement();
}
w.writeEndElement();
}

// Write the properties for those tiles that have them
for (int i = 0; i < tileset->tileCount(); ++i) {
const Tile *tile = tileset->tileAt(i);
const Properties properties = tile->properties();
if (!properties.isEmpty()) {
unsigned int terrain = tile->terrain();
if (!properties.isEmpty() || terrain != 0xFFFFFFFF) {
w.writeStartElement(QLatin1String("tile"));
w.writeAttribute(QLatin1String("id"), QString::number(i));
writeProperties(w, properties);
if (terrain != 0xFFFFFFFF)
w.writeAttribute(QLatin1String("terrain"), makeTerrainAttribute(tile));
if (!properties.isEmpty())
writeProperties(w, properties);
w.writeEndElement();
}
}
Expand Down
36 changes: 33 additions & 3 deletions src/libtiled/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@
#define TILE_H

#include "object.h"
#include "tileset.h"

#include <QPixmap>

namespace Tiled {

class Tileset;

class TILEDSHARED_EXPORT Tile : public Object
{
public:
Tile(const QPixmap &image, int id, Tileset *tileset):
mId(id),
mTileset(tileset),
mImage(image)
mImage(image),
mTerrain(-1)
{}

/**
Expand Down Expand Up @@ -82,10 +82,40 @@ class TILEDSHARED_EXPORT Tile : public Object
*/
QSize size() const { return mImage.size(); }

/**
* Returns the TerrainType of a given corner.
*/
TerrainType *cornerTerrain(int corner) const { return mTileset->terrainType(cornerTerrainType(corner)); }

/**
* Returns the terrain at a given corner.
*/
int cornerTerrainType(int corner) const { unsigned int t = (terrain() >> (3 - corner)*8) & 0xFF; return t == 0xFF ? -1 : (int)t; }

/**
* Set the terrain type of a given corner.
*/
void setCornerTerrainType(int corner, int terrain)
{
unsigned int mask = 0xFF << (3 - corner)*8;
unsigned int insert = terrain << (3 - corner)*8;
mTerrain = (mTerrain & ~mask) | (insert & mask);
}

/**
* Functions to get various terrain type information from tiles.
*/
unsigned short topEdge() const { return terrain() >> 16; }
unsigned short bottomEdge() const { return terrain() & 0xFFFF; }
unsigned short leftEdge() const { return((terrain() >> 16) & 0xFF00) | ((terrain() >> 8) & 0xFF); }
unsigned short rightEdge() const { return ((terrain() >> 8) & 0xFF00) | (terrain() & 0xFF); }
unsigned int terrain() const { return this == NULL ? 0xFFFFFFFF : mTerrain; } // HACK: NULL Tile has 'none' terrain type.

private:
int mId;
Tileset *mTileset;
QPixmap mImage;
unsigned int mTerrain;
};

} // namespace Tiled
Expand Down
36 changes: 36 additions & 0 deletions src/libtiled/tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ namespace Tiled {

class Tile;

class TerrainType
{
public:
TerrainType(int id, QString name, int imageTile):
mId(id),
mName(name),
mImageTile(imageTile)
{
}

int id() const { return mId; }
QString name() const { return mName; }
int paletteImageTile() const { return mImageTile; }

private:
int mId;
QString mName;
int mImageTile;
};

/**
* A tileset, representing a set of tiles.
*
Expand Down Expand Up @@ -213,6 +233,21 @@ class TILEDSHARED_EXPORT Tileset : public Object
*/
int columnCountForWidth(int width) const;

/**
* Returns the number of terrain types in this tileset.
*/
int terrainTypeCount() const { return mTerrainTypes.size(); }

/**
* Returns the number of tiles in this tileset.
*/
TerrainType *terrainType(int terrain) const { return terrain >= 0 ? mTerrainTypes[terrain] : NULL; }

/**
* Add a new terrain type.
*/
void addTerrainType(QString name, int imageTile);

private:
QString mName;
QString mFileName;
Expand All @@ -227,6 +262,7 @@ class TILEDSHARED_EXPORT Tileset : public Object
int mImageHeight;
int mColumnCount;
QList<Tile*> mTiles;
QList<TerrainType*> mTerrainTypes;
};

} // namespace Tiled
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "quickstampmanager.h"
#include "saveasimagedialog.h"
#include "stampbrush.h"
#include "terrainbrush.h"
#include "tilelayer.h"
#include "tileselectiontool.h"
#include "tileset.h"
Expand Down Expand Up @@ -317,6 +318,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
setThemeIcon(mUi->actionAbout, "help-about");

mStampBrush = new StampBrush(this);
mTerrainBrush = new TerrainBrush(this);
mBucketFillTool = new BucketFillTool(this);
CreateObjectTool *tileObjectsTool = new CreateObjectTool(
CreateObjectTool::CreateTile, this);
Expand All @@ -341,6 +343,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)

ToolManager *toolManager = ToolManager::instance();
toolManager->registerTool(mStampBrush);
toolManager->registerTool(mTerrainBrush);
toolManager->registerTool(mBucketFillTool);
toolManager->registerTool(new Eraser(this));
toolManager->registerTool(new TileSelectionTool(this));
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class MapDocumentActionHandler;
class MapScene;
class StampBrush;
class BucketFillTool;
class TerrainBrush;
class TilesetDock;
class MapView;
class CommandButton;
Expand Down Expand Up @@ -210,6 +211,7 @@ public slots:

StampBrush *mStampBrush;
BucketFillTool *mBucketFillTool;
TerrainBrush *mTerrainBrush;

ClipboardManager *mClipboardManager;

Expand Down
Loading

0 comments on commit 5ed684e

Please sign in to comment.