Skip to content

Commit

Permalink
Tile image draw process modification
Browse files Browse the repository at this point in the history
  • Loading branch information
ElonDev committed Jan 15, 2015
1 parent e40d9c9 commit 9b55907
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ void MapReaderPrivate::readTilesetTile(Tileset *tileset)
while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("properties")) {
tile->mergeProperties(readProperties());
} else if (xml.name() == QLatin1String("imageoffset")) {
const QXmlStreamAttributes oa = xml.attributes();
int x = oa.value(QLatin1String("x")).toString().toInt();
int y = oa.value(QLatin1String("y")).toString().toInt();
tile->setImageOffset(QPointF(x, y));
xml.skipCurrentElement();
} else if (xml.name() == QLatin1String("image")) {
QString source = xml.attributes().value(QLatin1String("source")).toString();
if (!source.isEmpty())
Expand Down
11 changes: 9 additions & 2 deletions src/libtiled/maprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <QPainter>
#include <QVector2D>

#include <QDebug>

using namespace Tiled;

QRectF MapRenderer::boundingRect(const ImageLayer *imageLayer) const
Expand Down Expand Up @@ -115,11 +117,16 @@ void CellRenderer::render(const Cell &cell, const QPointF &pos, Origin origin)
const QPixmap &image = cell.tile->currentFrameImage();
const QSizeF size = image.size();
const QPoint offset = cell.tile->tileset()->tileOffset();
const QPointF imageOffset = cell.tile->imageOffset();
const QPointF sizeHalf = QPointF(size.width() / 2, size.height() / 2);

// Cell size is 86x43, get half size to set position correctly.
float cellHalfWidth = 43;
float cellHalfHeight = 21.5;

QPainter::PixmapFragment fragment;
fragment.x = pos.x() + offset.x() + sizeHalf.x();
fragment.y = pos.y() + offset.y() + sizeHalf.y() - size.height();
fragment.x = pos.x() + offset.x() + sizeHalf.x() + cellHalfWidth - imageOffset.x();
fragment.y = pos.y() + offset.y() + sizeHalf.y() - cellHalfHeight - imageOffset.y();
fragment.sourceLeft = 0;
fragment.sourceTop = 0;
fragment.width = size.width();
Expand Down
7 changes: 7 additions & 0 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ void MapWriterPrivate::writeTileset(QXmlStreamWriter &w, const Tileset *tileset,
for (int i = 0; i < tileset->tileCount(); ++i) {
const Tile *tile = tileset->tileAt(i);
const Properties properties = tile->properties();
const QPointF imageOffset = tile->imageOffset();
unsigned terrain = tile->terrain();
float probability = tile->terrainProbability();
ObjectGroup *objectGroup = tile->objectGroup();
Expand All @@ -335,6 +336,12 @@ void MapWriterPrivate::writeTileset(QXmlStreamWriter &w, const Tileset *tileset,
w.writeAttribute(QLatin1String("id"), QString::number(i));
if (terrain != 0xFFFFFFFF)
w.writeAttribute(QLatin1String("terrain"), makeTerrainAttribute(tile));
if (!imageOffset.isNull()) {
w.writeStartElement(QLatin1String("imageoffset"));
w.writeAttribute(QLatin1String("x"), QString::number(imageOffset.x()));
w.writeAttribute(QLatin1String("y"), QString::number(imageOffset.y()));
w.writeEndElement();
}
if (probability != -1.f)
w.writeAttribute(QLatin1String("probability"), QString::number(probability));
if (!properties.isEmpty())
Expand Down
7 changes: 7 additions & 0 deletions src/libtiled/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Tile::Tile(const QPixmap &image, int id, Tileset *tileset):
mTileset(tileset),
mImage(image),
mTerrain(-1),
mImageOffset(0, 0),
mTerrainProbability(-1.f),
mObjectGroup(0),
mCurrentFrameIndex(0),
Expand All @@ -53,6 +54,7 @@ Tile::Tile(const QPixmap &image, const QString &imageSource,
mImage(image),
mImageSource(imageSource),
mTerrain(-1),
mImageOffset(0, 0),
mTerrainProbability(-1.f),
mObjectGroup(0),
mCurrentFrameIndex(0),
Expand Down Expand Up @@ -92,6 +94,11 @@ void Tile::setTerrain(unsigned terrain)
mTileset->markTerrainDistancesDirty();
}

void Tile::setImageOffset(QPointF imageOffset)
{
mImageOffset = imageOffset;
}

/**
* Sets \a objectGroup to be the group of objects associated with this tile.
* The Tile takes ownership over the ObjectGroup and it can't also be part of
Expand Down
11 changes: 11 additions & 0 deletions src/libtiled/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ class TILEDSHARED_EXPORT Tile : public Object
*/
void setTerrain(unsigned terrain);

/**
* Returns the image offset to set correct position.
*/
QPointF imageOffset() const { return mImageOffset; }

/**
* Set the image offset for tile position.
*/
void setImageOffset(QPointF imageOffset);

/**
* Returns the probability of this terrain type appearing while painting (0-100%).
*/
Expand All @@ -167,6 +177,7 @@ class TILEDSHARED_EXPORT Tile : public Object
QPixmap mImage;
QString mImageSource;
unsigned mTerrain;
QPointF mImageOffset;
float mTerrainProbability;
ObjectGroup *mObjectGroup;

Expand Down

0 comments on commit 9b55907

Please sign in to comment.