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

Added Y flip option to the coordinates #2040

Closed
wants to merge 12 commits into from
5 changes: 4 additions & 1 deletion src/tiled/abstractobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ void AbstractObjectTool::mouseMoved(const QPointF &pos,
const QPointF tilePosF = mapDocument()->renderer()->screenToTileCoords(offsetPos);
const int x = qFloor(tilePosF.x());
const int y = qFloor(tilePosF.y());
setStatusInfo(QString(QLatin1String("%1, %2 (%3, %4)")).arg(x).arg(InvertYAxisHelper().getY(y)).arg(pixelPos.x()).arg(InvertYAxisHelper().getPixelY(pixelPos.y())));
setStatusInfo(QString(QLatin1String("%1, %2 (%3, %4)")).arg(x)
.arg(InvertYAxisHelper().tileY(y))
.arg(pixelPos.x())
.arg(InvertYAxisHelper(MapDocumentActionHandler::instance()->mapDocument()->map()).pixelY(pixelPos.y())));
}

void AbstractObjectTool::mousePressed(QGraphicsSceneMouseEvent *event)
Expand Down
2 changes: 1 addition & 1 deletion src/tiled/abstracttiletool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void AbstractTileTool::updateStatusInfo()

setStatusInfo(QString(QLatin1String("%1, %2 [%3]"))
.arg(mTilePosition.x())
.arg(InvertYAxisHelper().getY(mTilePosition.y()))
.arg(InvertYAxisHelper().tileY(mTilePosition.y()))
.arg(tileIdString));
} else {
setStatusInfo(QString());
Expand Down
53 changes: 31 additions & 22 deletions src/tiled/invertyaxishelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,41 @@
#include "preferences.h"
#include "mapdocumentactionhandler.h"

class InvertYAxisHelper{
public:
InvertYAxisHelper() {}
~InvertYAxisHelper() {}

// Inverts Y coordinate in grid
float getY(float y) const
namespace Tiled{
namespace Internal {
bjorn marked this conversation as resolved.
Show resolved Hide resolved

class InvertYAxisHelper
{
// Check if Invert Y Axis is set
if(Tiled::Internal::Preferences::instance()->invertYAxis())
public:
// Constructors
InvertYAxisHelper() {}
bjorn marked this conversation as resolved.
Show resolved Hide resolved
InvertYAxisHelper(Map* m) : target(m) {}
bjorn marked this conversation as resolved.
Show resolved Hide resolved

// Inverts Y coordinate in grid
qreal tileY(qreal y) const
bjorn marked this conversation as resolved.
Show resolved Hide resolved
{
return Tiled::Internal::MapDocumentActionHandler::instance()->mapDocument()->map()->height() - 1 - y;
// Check if Invert Y Axis is set
if(Preferences::instance()->invertYAxis())
{
bjorn marked this conversation as resolved.
Show resolved Hide resolved
return MapDocumentActionHandler::instance()->mapDocument()->map()->height() - 1 - y;
}
return y;
}
return y;
}

// Inverts Y coordinate in pixels
float getPixelY(float y) const
{
// Obtain the map document
auto map = Tiled::Internal::MapDocumentActionHandler::instance()->mapDocument()->map();
if(Tiled::Internal::Preferences::instance()->invertYAxis())
// Inverts Y coordinate in pixels
qreal pixelY(qreal y) const
{
return ((map->height() + 1) * map->tileHeight()) - y;
// Obtain the map document
if(Preferences::instance()->invertYAxis())
{
return ((target->height() + 1) * target->tileHeight()) - y;
}
return y;
}
return y;
}

};
private:
Map* target;
bjorn marked this conversation as resolved.
Show resolved Hide resolved
};

} // Namespace Internal
} // Namespace Tiled
bjorn marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/tiled/mapobjectmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ QVariant MapObjectModel::data(const QModelIndex &index, int role) const
return QLatin1Char('(')
+ QString::number(mapObject->x())
+ QLatin1String(", ")
+ QString::number(InvertYAxisHelper().getPixelY(mapObject->y()))
+ QString::number(InvertYAxisHelper().tileY(mapObject->y()))
+ QLatin1Char(')');
}
break;
Expand Down
6 changes: 3 additions & 3 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ void PropertyBrowser::wangSetChanged(Tileset *tileset, int index)

void PropertyBrowser::invertYAxisChanged()
{
if(mObject && mObject->typeId() == Object::MapObjectType)
if (mObject && mObject->typeId() == Object::MapObjectType)
updateProperties();
}

Expand Down Expand Up @@ -1030,7 +1030,7 @@ QUndoCommand *PropertyBrowser::applyMapObjectValueTo(PropertyId id, const QVaria
}
case YProperty: {
const QPointF oldPos = mapObject->position();
const QPointF newPos(oldPos.x(), InvertYAxisHelper().getPixelY(val.toReal()));
const QPointF newPos(oldPos.x(), InvertYAxisHelper(mMapDocument->map()).pixelY(val.toReal()));
command = new MoveMapObject(mMapDocument, mapObject, newPos, oldPos);
break;
}
Expand Down Expand Up @@ -1618,7 +1618,7 @@ void PropertyBrowser::updateProperties()
if (auto visibleProperty = mIdToProperty[VisibleProperty])
visibleProperty->setValue(mapObject->isVisible());
mIdToProperty[XProperty]->setValue(mapObject->x());
mIdToProperty[YProperty]->setValue(InvertYAxisHelper().getPixelY(mapObject->y()));
mIdToProperty[YProperty]->setValue(InvertYAxisHelper(MapDocumentActionHandler::instance()->mapDocument()->map()).pixelY(mapObject->y()));
bjorn marked this conversation as resolved.
Show resolved Hide resolved

if (flags & ObjectHasDimensions) {
mIdToProperty[WidthProperty]->setValue(mapObject->width());
Expand Down