Skip to content

Commit

Permalink
Center tile objects at the mouse on insertion
Browse files Browse the repository at this point in the history
This fixes mapeditor#67 without changing the TMX map format.

This means the bottom-left remains the origin of the tile objects even
though plain rectangle objects have their origin in the top-left. This
difference was the reason for the wrong positioning of tile objects when
inserting them, which is now fixed by taking the difference into account
in the Insert Tile Objects tool. The tool now centers the inserted tile
object at the mouse position.

The reason tile objects have their origin in the bottom-left is because
tiles in tile layers are also bottom-left aligned within their grid
cell. This in turn has its origins in top-down maps where this behavior
is usually desirable when working with tile images that are larger than
the grid size.
  • Loading branch information
bjorn committed Sep 27, 2011
1 parent 135a26b commit 5266429
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Small improvement to the Lua export plugin (incompatible!)
* Fixed a bug in the Create Object tool
* Fixed reading of maps without tilesets but with a tile layer
* Fixed position of tile objects to center on the mouse on insertion
* Updated the Czech translation

0.7.0 (20 July 2011)
Expand Down
5 changes: 5 additions & 0 deletions src/libtiled/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class TILEDSHARED_EXPORT Tile : public Object
*/
int height() const { return mImage.height(); }

/**
* Returns the size of this tile.
*/
QSize size() const { return mImage.size(); }

private:
int mId;
Tileset *mTileset;
Expand Down
36 changes: 26 additions & 10 deletions src/tiled/createobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ void CreateObjectTool::mouseMoved(const QPointF &pos,
return;

const MapRenderer *renderer = mapDocument()->renderer();
QPointF tileCoords = renderer->pixelToTileCoords(pos);

bool snapToGrid = Preferences::instance()->snapToGrid();
if (modifiers & Qt::ControlModifier)
snapToGrid = !snapToGrid;

if (mMode == AreaObjects) {
const QPointF tileCoords = renderer->pixelToTileCoords(pos);

// Update the size of the new map object
const QPointF objectPos = mNewMapObjectItem->mapObject()->position();
QSizeF newSize(qMax(qreal(0), tileCoords.x() - objectPos.x()),
Expand All @@ -98,6 +99,10 @@ void CreateObjectTool::mouseMoved(const QPointF &pos,

mNewMapObjectItem->resize(newSize);
} else {
const QSize imgSize = mNewMapObjectItem->mapObject()->tile()->size();
const QPointF diff(-imgSize.width() / 2, imgSize.height() / 2);
QPointF tileCoords = renderer->pixelToTileCoords(pos + diff);

if (snapToGrid)
tileCoords = tileCoords.toPoint();

Expand All @@ -121,19 +126,30 @@ void CreateObjectTool::mousePressed(QGraphicsSceneMouseEvent *event)
}

ObjectGroup *objectGroup = currentObjectGroup();
if (objectGroup && objectGroup->isVisible() && !mNewMapObjectItem) {
const MapRenderer *renderer = mapDocument()->renderer();
QPointF tileCoords = renderer->pixelToTileCoords(event->scenePos());
if (!objectGroup || !objectGroup->isVisible())
return;

bool snapToGrid = Preferences::instance()->snapToGrid();
if (event->modifiers() & Qt::ControlModifier)
snapToGrid = !snapToGrid;
const MapRenderer *renderer = mapDocument()->renderer();
QPointF tileCoords;

if (snapToGrid)
tileCoords = tileCoords.toPoint();
if (mMode == AreaObjects) {
tileCoords = renderer->pixelToTileCoords(event->scenePos());
} else {
if (!mTile)
return;

startNewMapObject(tileCoords, objectGroup);
const QPointF diff(-mTile->width() / 2, mTile->height() / 2);
tileCoords = renderer->pixelToTileCoords(event->scenePos() + diff);
}

bool snapToGrid = Preferences::instance()->snapToGrid();
if (event->modifiers() & Qt::ControlModifier)
snapToGrid = !snapToGrid;

if (snapToGrid)
tileCoords = tileCoords.toPoint();

startNewMapObject(tileCoords, objectGroup);
}

void CreateObjectTool::mouseReleased(QGraphicsSceneMouseEvent *event)
Expand Down

0 comments on commit 5266429

Please sign in to comment.