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
6 changes: 5 additions & 1 deletion src/tiled/abstractobjecttool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "tile.h"
#include "tmxmapformat.h"
#include "utils.h"
#include "invertyaxishelper.h"

#include <QFileDialog>
#include <QKeyEvent>
Expand Down Expand Up @@ -160,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(y).arg(pixelPos.x()).arg(pixelPos.y()));
setStatusInfo(QString(QLatin1String("%1, %2 (%3, %4)")).arg(x)
.arg(InvertYAxisHelper(mapDocument()).tileY(y))
.arg(pixelPos.x())
.arg(InvertYAxisHelper(MapDocumentActionHandler::instance()->mapDocument()).pixelY(pixelPos.y())));
AdrianFL marked this conversation as resolved.
Show resolved Hide resolved
}

void AbstractObjectTool::mousePressed(QGraphicsSceneMouseEvent *event)
Expand Down
3 changes: 2 additions & 1 deletion src/tiled/abstracttiletool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "tile.h"
#include "tilelayer.h"
#include "tilestamp.h"
#include "invertyaxishelper.h"

#include <QtMath>

Expand Down Expand Up @@ -146,7 +147,7 @@ void AbstractTileTool::updateStatusInfo()

setStatusInfo(QString(QLatin1String("%1, %2 [%3]"))
.arg(mTilePosition.x())
.arg(mTilePosition.y())
.arg(InvertYAxisHelper(mapDocument()).tileY(mTilePosition.y()))
.arg(tileIdString));
} else {
setStatusInfo(QString());
Expand Down
64 changes: 64 additions & 0 deletions src/tiled/invertyaxishelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* invertyaxishelper.h
* Copyright 2013, Thorbjørn Lindeijer <[email protected]>
* Copyright 2018, Adrian Frances <[email protected]>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "preferences.h"
#include "mapdocumentactionhandler.h"
#include "maprenderer.h"

namespace Tiled {
namespace Internal {

class InvertYAxisHelper
bjorn marked this conversation as resolved.
Show resolved Hide resolved
{
public:
bjorn marked this conversation as resolved.
Show resolved Hide resolved
// Constructors
bjorn marked this conversation as resolved.
Show resolved Hide resolved
InvertYAxisHelper(MapDocument *m) : mTarget(m) {}

// Inverts Y coordinate in grid
int tileY(int y) const
{
// Check if Invert Y Axis is set
if (Preferences::instance()->invertYAxis()) {
const MapRenderer *renderer = mTarget->renderer();
return renderer->map()->height() - 1 - y;
AdrianFL marked this conversation as resolved.
Show resolved Hide resolved
}
return y;
}

// Inverts Y coordinate in pixels
qreal pixelY(qreal y) const
{
// Obtain the map document
if (Preferences::instance()->invertYAxis()) {
const MapRenderer *renderer = mTarget->renderer();
return (renderer->map()->height() * renderer->map()->tileHeight())- y;
bjorn marked this conversation as resolved.
Show resolved Hide resolved
}
return y;
}

private:
MapDocument *mTarget;
};

} // Namespace Internal
} // Namespace Tiled
3 changes: 2 additions & 1 deletion src/tiled/mapobjectmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "mapdocument.h"
#include "objectgroup.h"
#include "renamelayer.h"
#include "invertyaxishelper.h"

#include <QApplication>
#include <QPalette>
Expand Down Expand Up @@ -129,7 +130,7 @@ QVariant MapObjectModel::data(const QModelIndex &index, int role) const
return QLatin1Char('(')
+ QString::number(mapObject->x())
+ QLatin1String(", ")
+ QString::number(mapObject->y())
+ QString::number(InvertYAxisHelper(mapDocument()).tileY(mapObject->y()))
+ QLatin1Char(')');
}
break;
Expand Down
12 changes: 12 additions & 0 deletions src/tiled/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Preferences::Preferences()
mLanguage = stringValue("Language");
mUseOpenGL = boolValue("OpenGL");
mWheelZoomsByDefault = boolValue("WheelZoomsByDefault");
mInvertYAxis = boolValue("InvertYAxis");
mObjectLabelVisibility = static_cast<ObjectLabelVisiblity>
(intValue("ObjectLabelVisibility", AllObjectLabels));
mLabelForHoveredObject = boolValue("LabelForHoveredObject", false);
Expand Down Expand Up @@ -683,6 +684,17 @@ void Preferences::setWheelZoomsByDefault(bool mode)
mSettings->setValue(QLatin1String("Interface/WheelZoomsByDefault"), mode);
}

void Preferences::setInvertYAxis(bool enabled)
{
if(mInvertYAxis == enabled)
bjorn marked this conversation as resolved.
Show resolved Hide resolved
return;

mInvertYAxis = enabled;
mSettings->setValue(QLatin1String("Interface/InvertYAxis"), enabled);

emit invertYAxisChanged();
}

bool Preferences::boolValue(const char *key, bool defaultValue) const
{
return mSettings->value(QLatin1String(key), defaultValue).toBool();
Expand Down
11 changes: 11 additions & 0 deletions src/tiled/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class Preferences : public QObject

bool wheelZoomsByDefault() const;

bool invertYAxis() const;

/**
* Provides access to the QSettings instance to allow storing/retrieving
* arbitrary values. The naming style for groups and keys is CamelCase.
Expand All @@ -190,6 +192,7 @@ public slots:
void setOpenLastFilesOnStartup(bool load);
void setPluginEnabled(const QString &fileName, bool enabled);
void setWheelZoomsByDefault(bool mode);
void setInvertYAxis(bool enabled);

void clearRecentFiles();

Expand Down Expand Up @@ -229,6 +232,8 @@ public slots:

void checkForUpdatesChanged();

void invertYAxisChanged();

private:
Preferences();
~Preferences() override;
Expand Down Expand Up @@ -282,6 +287,7 @@ public slots:
bool mIsPatron;
bool mCheckForUpdates;
bool mWheelZoomsByDefault;
bool mInvertYAxis;

static Preferences *mInstance;
};
Expand Down Expand Up @@ -392,6 +398,11 @@ inline bool Preferences::wheelZoomsByDefault() const
return mWheelZoomsByDefault;
}

inline bool Preferences::invertYAxis() const
{
return mInvertYAxis;
}

} // namespace Internal
} // namespace Tiled

Expand Down
3 changes: 3 additions & 0 deletions src/tiled/preferencesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent)
preferences, &Preferences::setUseOpenGL);
connect(mUi->wheelZoomsByDefault, &QCheckBox::toggled,
preferences, &Preferences::setWheelZoomsByDefault);
connect(mUi->invertYAxis, &QCheckBox::toggled,
preferences, &Preferences::setInvertYAxis);

connect(mUi->styleCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &PreferencesDialog::styleComboChanged);
Expand Down Expand Up @@ -170,6 +172,7 @@ void PreferencesDialog::fromPreferences()
if (mUi->openGL->isEnabled())
mUi->openGL->setChecked(prefs->useOpenGL());
mUi->wheelZoomsByDefault->setChecked(prefs->wheelZoomsByDefault());
mUi->invertYAxis->setChecked(prefs->invertYAxis());

// Not found (-1) ends up at index 0, system default
int languageIndex = mUi->languageCombo->findData(prefs->language());
Expand Down
8 changes: 8 additions & 0 deletions src/tiled/preferencesdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QCheckBox" name="invertYAxis">
<property name="text">
<string>Invert Y axis coordinates</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -441,6 +448,7 @@
<tabstop>objectLineWidth</tabstop>
<tabstop>openGL</tabstop>
<tabstop>wheelZoomsByDefault</tabstop>
<tabstop>invertYAxis</tabstop>
<tabstop>styleCombo</tabstop>
<tabstop>baseColor</tabstop>
<tabstop>selectionColor</tabstop>
Expand Down
14 changes: 12 additions & 2 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "variantpropertymanager.h"
#include "wangcolormodel.h"
#include "wangset.h"
#include "invertyaxishelper.h"

#include <QtGroupPropertyManager>

Expand Down Expand Up @@ -102,6 +103,9 @@ PropertyBrowser::PropertyBrowser(QWidget *parent)

connect(Preferences::instance(), &Preferences::objectTypesChanged,
this, &PropertyBrowser::objectTypesChanged);

connect(Preferences::instance(), &Preferences::invertYAxisChanged,
this, &PropertyBrowser::invertYAxisChanged);
}

void PropertyBrowser::setObject(Object *object)
Expand Down Expand Up @@ -323,6 +327,12 @@ void PropertyBrowser::wangSetChanged(Tileset *tileset, int index)
updateProperties();
}

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

static QVariant predefinedPropertyValue(Object *object, const QString &name)
{
QString objectType;
Expand Down Expand Up @@ -1020,7 +1030,7 @@ QUndoCommand *PropertyBrowser::applyMapObjectValueTo(PropertyId id, const QVaria
}
case YProperty: {
const QPointF oldPos = mapObject->position();
const QPointF newPos(oldPos.x(), val.toReal());
const QPointF newPos(oldPos.x(), InvertYAxisHelper(mMapDocument).pixelY(val.toReal()));
command = new MoveMapObject(mMapDocument, mapObject, newPos, oldPos);
break;
}
Expand Down Expand Up @@ -1608,7 +1618,7 @@ void PropertyBrowser::updateProperties()
if (auto visibleProperty = mIdToProperty[VisibleProperty])
visibleProperty->setValue(mapObject->isVisible());
mIdToProperty[XProperty]->setValue(mapObject->x());
mIdToProperty[YProperty]->setValue(mapObject->y());
mIdToProperty[YProperty]->setValue(InvertYAxisHelper(mMapDocument).pixelY(mapObject->y()));

if (flags & ObjectHasDimensions) {
mIdToProperty[WidthProperty]->setValue(mapObject->width());
Expand Down
1 change: 1 addition & 0 deletions src/tiled/propertybrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private slots:
void tileTypeChanged(Tile *tile);
void terrainChanged(Tileset *tileset, int index);
void wangSetChanged(Tileset *tileset, int index);
void invertYAxisChanged();

void propertyAdded(Object *object, const QString &name);
void propertyRemoved(Object *object, const QString &name);
Expand Down