From da850bc69a58c7336abcbef5704b40c230f977d4 Mon Sep 17 00:00:00 2001 From: AdrianFL Date: Sun, 2 Dec 2018 04:46:48 +0100 Subject: [PATCH 01/18] Added checkbox to Preferences menu and all the methods needed --- src/tiled/preferences.cpp | 12 ++++++++++++ src/tiled/preferences.h | 11 +++++++++++ src/tiled/preferencesdialog.cpp | 3 +++ src/tiled/preferencesdialog.ui | 8 ++++++++ 4 files changed, 34 insertions(+) diff --git a/src/tiled/preferences.cpp b/src/tiled/preferences.cpp index f2270292e3..0f02aca3a7 100644 --- a/src/tiled/preferences.cpp +++ b/src/tiled/preferences.cpp @@ -93,6 +93,7 @@ Preferences::Preferences() mLanguage = stringValue("Language"); mUseOpenGL = boolValue("OpenGL"); mWheelZoomsByDefault = boolValue("WheelZoomsByDefault"); + mInvertYAxis = boolValue("InvertYAxis"); mObjectLabelVisibility = static_cast (intValue("ObjectLabelVisibility", AllObjectLabels)); mLabelForHoveredObject = boolValue("LabelForHoveredObject", false); @@ -683,6 +684,17 @@ void Preferences::setWheelZoomsByDefault(bool mode) mSettings->setValue(QLatin1String("Interface/WheelZoomsByDefault"), mode); } +void Preferences::setInvertYAxis(bool enabled) +{ + if(mInvertYAxis == enabled) + 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(); diff --git a/src/tiled/preferences.h b/src/tiled/preferences.h index 243825cf22..abf8663773 100644 --- a/src/tiled/preferences.h +++ b/src/tiled/preferences.h @@ -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. @@ -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(); @@ -229,6 +232,8 @@ public slots: void checkForUpdatesChanged(); + void invertYAxisChanged(); + private: Preferences(); ~Preferences() override; @@ -282,6 +287,7 @@ public slots: bool mIsPatron; bool mCheckForUpdates; bool mWheelZoomsByDefault; + bool mInvertYAxis; static Preferences *mInstance; }; @@ -392,6 +398,11 @@ inline bool Preferences::wheelZoomsByDefault() const return mWheelZoomsByDefault; } +inline bool Preferences::invertYAxis() const +{ + return mInvertYAxis; +} + } // namespace Internal } // namespace Tiled diff --git a/src/tiled/preferencesdialog.cpp b/src/tiled/preferencesdialog.cpp index b9d6e99318..39b0b4654b 100644 --- a/src/tiled/preferencesdialog.cpp +++ b/src/tiled/preferencesdialog.cpp @@ -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(&QComboBox::currentIndexChanged), this, &PreferencesDialog::styleComboChanged); @@ -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()); diff --git a/src/tiled/preferencesdialog.ui b/src/tiled/preferencesdialog.ui index db8194bc02..7dddb64e7f 100644 --- a/src/tiled/preferencesdialog.ui +++ b/src/tiled/preferencesdialog.ui @@ -225,6 +225,13 @@ + + + + Invert Y axis coordinates + + + @@ -441,6 +448,7 @@ objectLineWidth openGL wheelZoomsByDefault + invertYAxis styleCombo baseColor selectionColor From f045eec602a60a600b17a015f331c47ef4187304 Mon Sep 17 00:00:00 2001 From: AdrianFL Date: Sun, 2 Dec 2018 04:47:11 +0100 Subject: [PATCH 02/18] Added helper class to flip Y axis --- src/tiled/invertyaxishelper.h | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/tiled/invertyaxishelper.h diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h new file mode 100644 index 0000000000..e50ee16a78 --- /dev/null +++ b/src/tiled/invertyaxishelper.h @@ -0,0 +1,55 @@ +/* + * invertyaxishelper.h + * Copyright 2013, Thorbjørn Lindeijer + * Copyright 2018, Adrian Frances + * + * 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 . + */ + +#pragma once + +#include "preferences.h" +#include "mapdocumentactionhandler.h" + +class InvertYAxisHelper{ + public: + InvertYAxisHelper() {} + ~InvertYAxisHelper() {} + + // Inverts Y coordinate in grid + float getY(float y) const + { + // Check if Invert Y Axis is set + if(Tiled::Internal::Preferences::instance()->invertYAxis()) + { + return Tiled::Internal::MapDocumentActionHandler::instance()->mapDocument()->map()->height() - 1 - 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()) + { + return ((map->height() + 1) * map->tileHeight()) - y; + } + return y; + } + +}; \ No newline at end of file From 7064dfae9d69e61d2f67bef633ddc82b6f187867 Mon Sep 17 00:00:00 2001 From: AdrianFL Date: Sun, 2 Dec 2018 04:48:37 +0100 Subject: [PATCH 03/18] Added functionality of the Y flip to the bottom context numbers --- src/tiled/abstractobjecttool.cpp | 3 ++- src/tiled/abstracttiletool.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tiled/abstractobjecttool.cpp b/src/tiled/abstractobjecttool.cpp index 13e12985bf..3f731ae2d0 100644 --- a/src/tiled/abstractobjecttool.cpp +++ b/src/tiled/abstractobjecttool.cpp @@ -37,6 +37,7 @@ #include "tile.h" #include "tmxmapformat.h" #include "utils.h" +#include "invertyaxishelper.h" #include #include @@ -160,7 +161,7 @@ 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().getY(y)).arg(pixelPos.x()).arg(InvertYAxisHelper().getPixelY(pixelPos.y()))); } void AbstractObjectTool::mousePressed(QGraphicsSceneMouseEvent *event) diff --git a/src/tiled/abstracttiletool.cpp b/src/tiled/abstracttiletool.cpp index 0bf707fc14..97c9c13bc3 100644 --- a/src/tiled/abstracttiletool.cpp +++ b/src/tiled/abstracttiletool.cpp @@ -28,6 +28,7 @@ #include "tile.h" #include "tilelayer.h" #include "tilestamp.h" +#include "invertyaxishelper.h" #include @@ -146,7 +147,7 @@ void AbstractTileTool::updateStatusInfo() setStatusInfo(QString(QLatin1String("%1, %2 [%3]")) .arg(mTilePosition.x()) - .arg(mTilePosition.y()) + .arg(InvertYAxisHelper().getY(mTilePosition.y())) .arg(tileIdString)); } else { setStatusInfo(QString()); From 59ab00b7d7afa8217f936d9698c4fd477b8dc8f0 Mon Sep 17 00:00:00 2001 From: AdrianFL Date: Sun, 2 Dec 2018 04:49:06 +0100 Subject: [PATCH 04/18] Added the Y flip functionality to the object menu and object properties --- src/tiled/mapobjectmodel.cpp | 3 ++- src/tiled/propertybrowser.cpp | 14 ++++++++++++-- src/tiled/propertybrowser.h | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/tiled/mapobjectmodel.cpp b/src/tiled/mapobjectmodel.cpp index 28de43936a..e7f75fa6a1 100644 --- a/src/tiled/mapobjectmodel.cpp +++ b/src/tiled/mapobjectmodel.cpp @@ -29,6 +29,7 @@ #include "mapdocument.h" #include "objectgroup.h" #include "renamelayer.h" +#include "invertyaxishelper.h" #include #include @@ -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().getPixelY(mapObject->y())) + QLatin1Char(')'); } break; diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index 9a3af39fda..dd292e3aca 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -62,6 +62,7 @@ #include "variantpropertymanager.h" #include "wangcolormodel.h" #include "wangset.h" +#include "invertyaxishelper.h" #include @@ -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) @@ -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; @@ -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().getPixelY(val.toReal())); command = new MoveMapObject(mMapDocument, mapObject, newPos, oldPos); break; } @@ -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().getPixelY(mapObject->y())); if (flags & ObjectHasDimensions) { mIdToProperty[WidthProperty]->setValue(mapObject->width()); diff --git a/src/tiled/propertybrowser.h b/src/tiled/propertybrowser.h index fd32ae3b7c..34b15b219b 100644 --- a/src/tiled/propertybrowser.h +++ b/src/tiled/propertybrowser.h @@ -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); From 59773db7cae673a8315d7ac993eca425e988e113 Mon Sep 17 00:00:00 2001 From: AdrianFL Date: Sun, 2 Dec 2018 13:53:55 +0100 Subject: [PATCH 05/18] Small changes for coding style and other feedback provided --- src/tiled/abstractobjecttool.cpp | 5 ++- src/tiled/abstracttiletool.cpp | 2 +- src/tiled/invertyaxishelper.h | 53 +++++++++++++++++++------------- src/tiled/mapobjectmodel.cpp | 2 +- src/tiled/propertybrowser.cpp | 6 ++-- 5 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/tiled/abstractobjecttool.cpp b/src/tiled/abstractobjecttool.cpp index 3f731ae2d0..1cee482f16 100644 --- a/src/tiled/abstractobjecttool.cpp +++ b/src/tiled/abstractobjecttool.cpp @@ -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) diff --git a/src/tiled/abstracttiletool.cpp b/src/tiled/abstracttiletool.cpp index 97c9c13bc3..42a572c348 100644 --- a/src/tiled/abstracttiletool.cpp +++ b/src/tiled/abstracttiletool.cpp @@ -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()); diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h index e50ee16a78..64016d1793 100644 --- a/src/tiled/invertyaxishelper.h +++ b/src/tiled/invertyaxishelper.h @@ -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 { + + class InvertYAxisHelper { - // Check if Invert Y Axis is set - if(Tiled::Internal::Preferences::instance()->invertYAxis()) + public: + // Constructors + InvertYAxisHelper() {} + InvertYAxisHelper(Map* m) : target(m) {} + + // Inverts Y coordinate in grid + qreal tileY(qreal y) const { - return Tiled::Internal::MapDocumentActionHandler::instance()->mapDocument()->map()->height() - 1 - y; + // Check if Invert Y Axis is set + if(Preferences::instance()->invertYAxis()) + { + 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; - } -}; \ No newline at end of file + private: + Map* target; + }; + + } // Namespace Internal +} // Namespace Tiled \ No newline at end of file diff --git a/src/tiled/mapobjectmodel.cpp b/src/tiled/mapobjectmodel.cpp index e7f75fa6a1..9883d327cb 100644 --- a/src/tiled/mapobjectmodel.cpp +++ b/src/tiled/mapobjectmodel.cpp @@ -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; diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index dd292e3aca..085b6aac20 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -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(); } @@ -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; } @@ -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())); if (flags & ObjectHasDimensions) { mIdToProperty[WidthProperty]->setValue(mapObject->width()); From 09a183b22858e86730c5516bdedbf818ded70240 Mon Sep 17 00:00:00 2001 From: AdrianFL Date: Mon, 3 Dec 2018 22:52:41 +0100 Subject: [PATCH 06/18] Changed InvertYAxisHelper to use MapRenderer and other coding style fix --- src/tiled/abstractobjecttool.cpp | 4 +-- src/tiled/abstracttiletool.cpp | 2 +- src/tiled/invertyaxishelper.h | 62 ++++++++++++++++---------------- src/tiled/mapobjectmodel.cpp | 2 +- src/tiled/propertybrowser.cpp | 4 +-- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/tiled/abstractobjecttool.cpp b/src/tiled/abstractobjecttool.cpp index 1cee482f16..3fa4bbd35a 100644 --- a/src/tiled/abstractobjecttool.cpp +++ b/src/tiled/abstractobjecttool.cpp @@ -162,9 +162,9 @@ void AbstractObjectTool::mouseMoved(const QPointF &pos, const int x = qFloor(tilePosF.x()); const int y = qFloor(tilePosF.y()); setStatusInfo(QString(QLatin1String("%1, %2 (%3, %4)")).arg(x) - .arg(InvertYAxisHelper().tileY(y)) + .arg(InvertYAxisHelper(mapDocument()).tileY(y)) .arg(pixelPos.x()) - .arg(InvertYAxisHelper(MapDocumentActionHandler::instance()->mapDocument()->map()).pixelY(pixelPos.y()))); + .arg(InvertYAxisHelper(MapDocumentActionHandler::instance()->mapDocument()).pixelY(pixelPos.y()))); } void AbstractObjectTool::mousePressed(QGraphicsSceneMouseEvent *event) diff --git a/src/tiled/abstracttiletool.cpp b/src/tiled/abstracttiletool.cpp index 42a572c348..e80871e94d 100644 --- a/src/tiled/abstracttiletool.cpp +++ b/src/tiled/abstracttiletool.cpp @@ -147,7 +147,7 @@ void AbstractTileTool::updateStatusInfo() setStatusInfo(QString(QLatin1String("%1, %2 [%3]")) .arg(mTilePosition.x()) - .arg(InvertYAxisHelper().tileY(mTilePosition.y())) + .arg(InvertYAxisHelper(mapDocument()).tileY(mTilePosition.y())) .arg(tileIdString)); } else { setStatusInfo(QString()); diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h index 64016d1793..9ea438449b 100644 --- a/src/tiled/invertyaxishelper.h +++ b/src/tiled/invertyaxishelper.h @@ -23,42 +23,42 @@ #include "preferences.h" #include "mapdocumentactionhandler.h" +#include "maprenderer.h" -namespace Tiled{ - namespace Internal { +namespace Tiled { +namespace Internal { - class InvertYAxisHelper - { - public: - // Constructors - InvertYAxisHelper() {} - InvertYAxisHelper(Map* m) : target(m) {} + class InvertYAxisHelper + { + public: + // Constructors + InvertYAxisHelper(MapDocument *m) : mTarget(m) {} - // Inverts Y coordinate in grid - qreal tileY(qreal y) const - { - // Check if Invert Y Axis is set - if(Preferences::instance()->invertYAxis()) - { - return MapDocumentActionHandler::instance()->mapDocument()->map()->height() - 1 - y; - } - return y; + // 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; } + return y; + } - // Inverts Y coordinate in pixels - qreal pixelY(qreal y) const - { - // Obtain the map document - if(Preferences::instance()->invertYAxis()) - { - return ((target->height() + 1) * target->tileHeight()) - y; - } - 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; } + return y; + } - private: - Map* target; - }; + private: + MapDocument *mTarget; + }; - } // Namespace Internal -} // Namespace Tiled \ No newline at end of file +} // Namespace Internal +} // Namespace Tiled diff --git a/src/tiled/mapobjectmodel.cpp b/src/tiled/mapobjectmodel.cpp index 9883d327cb..5611caa02d 100644 --- a/src/tiled/mapobjectmodel.cpp +++ b/src/tiled/mapobjectmodel.cpp @@ -130,7 +130,7 @@ QVariant MapObjectModel::data(const QModelIndex &index, int role) const return QLatin1Char('(') + QString::number(mapObject->x()) + QLatin1String(", ") - + QString::number(InvertYAxisHelper().tileY(mapObject->y())) + + QString::number(InvertYAxisHelper(mapDocument()).tileY(mapObject->y())) + QLatin1Char(')'); } break; diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index 085b6aac20..e6fc4e159f 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -1030,7 +1030,7 @@ QUndoCommand *PropertyBrowser::applyMapObjectValueTo(PropertyId id, const QVaria } case YProperty: { const QPointF oldPos = mapObject->position(); - const QPointF newPos(oldPos.x(), InvertYAxisHelper(mMapDocument->map()).pixelY(val.toReal())); + const QPointF newPos(oldPos.x(), InvertYAxisHelper(mMapDocument).pixelY(val.toReal())); command = new MoveMapObject(mMapDocument, mapObject, newPos, oldPos); break; } @@ -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(MapDocumentActionHandler::instance()->mapDocument()->map()).pixelY(mapObject->y())); + mIdToProperty[YProperty]->setValue(InvertYAxisHelper(mMapDocument).pixelY(mapObject->y())); if (flags & ObjectHasDimensions) { mIdToProperty[WidthProperty]->setValue(mapObject->width()); From 7d8d924cd43363ee04cb9388e99baaaa85ddfab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Mon, 3 Dec 2018 23:23:52 +0100 Subject: [PATCH 07/18] Update src/tiled/abstractobjecttool.cpp Co-Authored-By: AdrianFL --- src/tiled/abstractobjecttool.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tiled/abstractobjecttool.cpp b/src/tiled/abstractobjecttool.cpp index 3fa4bbd35a..1f88186b30 100644 --- a/src/tiled/abstractobjecttool.cpp +++ b/src/tiled/abstractobjecttool.cpp @@ -164,7 +164,7 @@ void AbstractObjectTool::mouseMoved(const QPointF &pos, 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()))); + .arg(InvertYAxisHelper(mapDocument()).pixelY(pixelPos.y()))); } void AbstractObjectTool::mousePressed(QGraphicsSceneMouseEvent *event) From 8cd919f0b42d50f929089078fd559e48ac6081f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Mon, 3 Dec 2018 23:24:51 +0100 Subject: [PATCH 08/18] Update src/tiled/invertyaxishelper.h Co-Authored-By: AdrianFL --- src/tiled/invertyaxishelper.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h index 9ea438449b..b38f30f6b7 100644 --- a/src/tiled/invertyaxishelper.h +++ b/src/tiled/invertyaxishelper.h @@ -40,7 +40,7 @@ namespace Internal { // Check if Invert Y Axis is set if (Preferences::instance()->invertYAxis()) { const MapRenderer *renderer = mTarget->renderer(); - return renderer->map()->height() - 1 - y; + return mTarget->map()->height() - 1 - y; } return y; } From 7f1dbecb9f7c23e9cd634c387f514c58b4cd8a81 Mon Sep 17 00:00:00 2001 From: AdrianFL Date: Tue, 4 Dec 2018 00:03:56 +0100 Subject: [PATCH 09/18] Using MapRenderer to obtain size --- src/tiled/invertyaxishelper.h | 51 +++++++++++++++++------------------ src/tiled/preferences.cpp | 2 +- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h index b38f30f6b7..c28d37ec63 100644 --- a/src/tiled/invertyaxishelper.h +++ b/src/tiled/invertyaxishelper.h @@ -28,37 +28,36 @@ namespace Tiled { namespace Internal { - class InvertYAxisHelper - { - public: - // Constructors - InvertYAxisHelper(MapDocument *m) : mTarget(m) {} +class InvertYAxisHelper +{ +public: + 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 mTarget->map()->height() - 1 - y; - } - return y; + // Inverts Y coordinate in grid + int tileY(int y) const + { + // Check if Invert Y Axis is set + if (Preferences::instance()->invertYAxis()) { + return mTarget->map()->height() - 1 - y; } + 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; - } - 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(); + QRect boundingRect = renderer->boundingRect(QRect(QPoint(), mTarget->map()->size())); + return boundingRect.height() - y; } + return y; + } - private: - MapDocument *mTarget; - }; +private: + MapDocument *mTarget; +}; } // Namespace Internal } // Namespace Tiled diff --git a/src/tiled/preferences.cpp b/src/tiled/preferences.cpp index 0f02aca3a7..e1d5e81758 100644 --- a/src/tiled/preferences.cpp +++ b/src/tiled/preferences.cpp @@ -686,7 +686,7 @@ void Preferences::setWheelZoomsByDefault(bool mode) void Preferences::setInvertYAxis(bool enabled) { - if(mInvertYAxis == enabled) + if (mInvertYAxis == enabled) return; mInvertYAxis = enabled; From 02849580c9270aab72dde28f51549e3da5c4a5bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 27 Dec 2018 23:16:20 +0100 Subject: [PATCH 10/18] Some formatting adjustments Mostly just personal preference. --- src/tiled/invertyaxishelper.h | 58 ++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h index c28d37ec63..c8a8002469 100644 --- a/src/tiled/invertyaxishelper.h +++ b/src/tiled/invertyaxishelper.h @@ -21,9 +21,9 @@ #pragma once -#include "preferences.h" -#include "mapdocumentactionhandler.h" +#include "mapdocument.h" #include "maprenderer.h" +#include "preferences.h" namespace Tiled { namespace Internal { @@ -31,33 +31,41 @@ namespace Internal { class InvertYAxisHelper { public: - InvertYAxisHelper(MapDocument *m) : mTarget(m) {} + InvertYAxisHelper(MapDocument *mapDocument) + : mMapDocument(mapDocument) + {} - // Inverts Y coordinate in grid - int tileY(int y) const - { - // Check if Invert Y Axis is set - if (Preferences::instance()->invertYAxis()) { - return mTarget->map()->height() - 1 - y; - } - 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(); - QRect boundingRect = renderer->boundingRect(QRect(QPoint(), mTarget->map()->size())); - return boundingRect.height() - y; - } - return y; - } + int tileY(int y) const; + qreal pixelY(qreal y) const; private: - MapDocument *mTarget; + MapDocument *mMapDocument; }; +/** + * Inverts Y coordinate in tiles when applicable. + */ +inline int InvertYAxisHelper::tileY(int y) const +{ + // Check if Invert Y Axis is set + if (Preferences::instance()->invertYAxis()) + return mMapDocument->map()->height() - 1 - y; + return y; +} + +/** + * Inverts Y coordinate in pixels when applicable. + */ +inline qreal InvertYAxisHelper::pixelY(qreal y) const +{ + // Obtain the map document + if (Preferences::instance()->invertYAxis()) { + const MapRenderer *renderer = mMapDocument->renderer(); + QRect boundingRect = renderer->boundingRect(QRect(QPoint(), mMapDocument->map()->size())); + return boundingRect.height() - y; + } + return y; +} + } // Namespace Internal } // Namespace Tiled From ca94a395d198204673dc35b8a882b85dedb2e7ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 27 Dec 2018 23:18:10 +0100 Subject: [PATCH 11/18] Removed "Internal" namespace No longer using this namespace (71c1de5dc2c6f98c8d54105492dad0a32b73f811). --- src/tiled/invertyaxishelper.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h index c8a8002469..4fd5fab58b 100644 --- a/src/tiled/invertyaxishelper.h +++ b/src/tiled/invertyaxishelper.h @@ -26,7 +26,6 @@ #include "preferences.h" namespace Tiled { -namespace Internal { class InvertYAxisHelper { @@ -67,5 +66,4 @@ inline qreal InvertYAxisHelper::pixelY(qreal y) const return y; } -} // Namespace Internal } // Namespace Tiled From c76041789c39815532b6f338733fd2b235e58fcd Mon Sep 17 00:00:00 2001 From: vinnydiehl Date: Fri, 5 May 2023 09:54:46 -0400 Subject: [PATCH 12/18] Change "Invert Y-Axis" to a map property Since inverting the Y-axis changes the coordinates of objects, engines will need to know how the user has this set. --- src/libtiled/map.h | 17 ++++++++++++++++- src/libtiled/mapreader.cpp | 1 + src/libtiled/maptovariantconverter.cpp | 1 + src/libtiled/mapwriter.cpp | 2 ++ src/libtiled/varianttomapconverter.cpp | 1 + src/plugins/lua/luaplugin.cpp | 1 + src/tiled/changemapproperty.cpp | 10 ++++++++++ src/tiled/clipboardmanager.cpp | 1 + src/tiled/editablemap.cpp | 8 ++++++++ src/tiled/editablemap.h | 8 ++++++++ src/tiled/invertyaxishelper.h | 6 +++--- src/tiled/preferences.cpp | 11 ----------- src/tiled/preferences.h | 3 --- src/tiled/preferencesdialog.cpp | 3 --- src/tiled/preferencesdialog.ui | 8 -------- src/tiled/propertybrowser.cpp | 15 ++++++--------- src/tiled/propertybrowser.h | 2 +- src/tiled/tilestampmanager.cpp | 1 + 18 files changed, 60 insertions(+), 39 deletions(-) diff --git a/src/libtiled/map.h b/src/libtiled/map.h index 36bf729345..cc061eb051 100644 --- a/src/libtiled/map.h +++ b/src/libtiled/map.h @@ -81,6 +81,7 @@ class TILEDSHARED_EXPORT Map : public Object TileWidthProperty, TileHeightProperty, InfiniteProperty, + InvertYAxisProperty, HexSideLengthProperty, StaggerAxisProperty, StaggerIndexProperty, @@ -158,6 +159,7 @@ class TILEDSHARED_EXPORT Map : public Object int tileWidth = 0; int tileHeight = 0; bool infinite = false; + bool invertYAxis = false; int hexSideLength = 0; StaggerAxis staggerAxis = StaggerY; StaggerIndex staggerIndex = StaggerOdd; @@ -216,6 +218,9 @@ class TILEDSHARED_EXPORT Map : public Object bool infinite() const; void setInfinite(bool infinite); + bool invertYAxis() const; + void setInvertYAxis(bool invertYAxis); + int hexSideLength() const; void setHexSideLength(int hexSideLength); @@ -285,7 +290,7 @@ class TILEDSHARED_EXPORT Map : public Object QSize chunkSize() const; void setChunkSize(QSize size); - + bool isTilesetUsed(const Tileset *tileset) const; std::unique_ptr clone() const; @@ -465,6 +470,16 @@ inline void Map::setInfinite(bool infinite) mParameters.infinite = infinite; } +inline bool Map::invertYAxis() const +{ + return mParameters.invertYAxis; +} + +inline void Map::setInvertYAxis(bool invertYAxis) +{ + mParameters.invertYAxis = invertYAxis; +} + inline int Map::hexSideLength() const { return mParameters.hexSideLength; diff --git a/src/libtiled/mapreader.cpp b/src/libtiled/mapreader.cpp index 6dbb9c0ce1..49ce1cea2a 100644 --- a/src/libtiled/mapreader.cpp +++ b/src/libtiled/mapreader.cpp @@ -264,6 +264,7 @@ std::unique_ptr MapReaderPrivate::readMap() mapParameters.tileWidth = atts.value(QLatin1String("tilewidth")).toInt(); mapParameters.tileHeight = atts.value(QLatin1String("tileheight")).toInt(); mapParameters.infinite = atts.value(QLatin1String("infinite")).toInt(); + mapParameters.invertYAxis = atts.value(QLatin1String("invertyaxis")).toInt(); mapParameters.hexSideLength = atts.value(QLatin1String("hexsidelength")).toInt(); mapParameters.staggerAxis = staggerAxisFromString(staggerAxis); mapParameters.staggerIndex = staggerIndexFromString(staggerIndex); diff --git a/src/libtiled/maptovariantconverter.cpp b/src/libtiled/maptovariantconverter.cpp index 93e7fb882f..6bb5f99547 100644 --- a/src/libtiled/maptovariantconverter.cpp +++ b/src/libtiled/maptovariantconverter.cpp @@ -63,6 +63,7 @@ QVariant MapToVariantConverter::toVariant(const Map &map, const QDir &mapDir) mapVariant[QStringLiteral("tilewidth")] = map.tileWidth(); mapVariant[QStringLiteral("tileheight")] = map.tileHeight(); mapVariant[QStringLiteral("infinite")] = map.infinite(); + mapVariant[QStringLiteral("invertyaxis")] = map.invertYAxis(); mapVariant[QStringLiteral("nextlayerid")] = map.nextLayerId(); mapVariant[QStringLiteral("nextobjectid")] = map.nextObjectId(); mapVariant[QStringLiteral("compressionlevel")] = map.compressionLevel(); diff --git a/src/libtiled/mapwriter.cpp b/src/libtiled/mapwriter.cpp index 63758d2208..c87d05cb77 100644 --- a/src/libtiled/mapwriter.cpp +++ b/src/libtiled/mapwriter.cpp @@ -211,6 +211,8 @@ void MapWriterPrivate::writeMap(QXmlStreamWriter &w, const Map &map) QString::number(map.tileHeight())); w.writeAttribute(QStringLiteral("infinite"), QString::number(map.infinite())); + w.writeAttribute(QStringLiteral("invertyaxis"), + QString::number(map.invertYAxis())); if (map.orientation() == Map::Hexagonal) { w.writeAttribute(QStringLiteral("hexsidelength"), diff --git a/src/libtiled/varianttomapconverter.cpp b/src/libtiled/varianttomapconverter.cpp index a2242d10e4..b80e888757 100644 --- a/src/libtiled/varianttomapconverter.cpp +++ b/src/libtiled/varianttomapconverter.cpp @@ -77,6 +77,7 @@ std::unique_ptr VariantToMapConverter::toMap(const QVariant &variant, mapParameters.tileWidth = variantMap[QStringLiteral("tilewidth")].toInt(); mapParameters.tileHeight = variantMap[QStringLiteral("tileheight")].toInt(); mapParameters.infinite = variantMap[QStringLiteral("infinite")].toInt(); + mapParameters.invertYAxis = variantMap[QStringLiteral("invertyaxis")].toInt(); mapParameters.hexSideLength = variantMap[QStringLiteral("hexsidelength")].toInt(); mapParameters.staggerAxis = staggerAxisFromString(staggerAxis); mapParameters.staggerIndex = staggerIndexFromString(staggerIndex); diff --git a/src/plugins/lua/luaplugin.cpp b/src/plugins/lua/luaplugin.cpp index e8d53b2d37..1a3e3952c3 100644 --- a/src/plugins/lua/luaplugin.cpp +++ b/src/plugins/lua/luaplugin.cpp @@ -226,6 +226,7 @@ void LuaWriter::writeMap(const Map *map) mWriter.writeKeyAndValue("height", map->height()); mWriter.writeKeyAndValue("tilewidth", map->tileWidth()); mWriter.writeKeyAndValue("tileheight", map->tileHeight()); + mWriter.writeKeyAndValue("invertyaxis", map->invertYAxis()); mWriter.writeKeyAndValue("nextlayerid", map->nextLayerId()); mWriter.writeKeyAndValue("nextobjectid", map->nextObjectId()); diff --git a/src/tiled/changemapproperty.cpp b/src/tiled/changemapproperty.cpp index c52d4052d0..f93e031848 100644 --- a/src/tiled/changemapproperty.cpp +++ b/src/tiled/changemapproperty.cpp @@ -48,6 +48,10 @@ ChangeMapProperty::ChangeMapProperty(MapDocument *mapDocument, setText(QCoreApplication::translate("Undo Commands", "Change Infinite Property")); break; + case Map::InvertYAxisProperty: + setText(QCoreApplication::translate("Undo Commands", + "Change Invert Y-Axis Property")); + break; case Map::HexSideLengthProperty: setText(QCoreApplication::translate("Undo Commands", "Change Hex Side Length")); @@ -174,6 +178,12 @@ void ChangeMapProperty::swap() mIntValue = infinite; break; } + case Map::InvertYAxisProperty: { + const int invertYAxis = map->invertYAxis(); + map->setInvertYAxis(mIntValue); + mIntValue = invertYAxis; + break; + } case Map::OrientationProperty: { const Map::Orientation orientation = map->orientation(); map->setOrientation(mOrientation); diff --git a/src/tiled/clipboardmanager.cpp b/src/tiled/clipboardmanager.cpp index 51b01b937d..b3ce4a2e38 100644 --- a/src/tiled/clipboardmanager.cpp +++ b/src/tiled/clipboardmanager.cpp @@ -156,6 +156,7 @@ bool ClipboardManager::copySelection(const MapDocument &mapDocument) mapParameters.width = selectionBounds.width(); mapParameters.height = selectionBounds.height(); mapParameters.infinite = false; + mapParameters.invertYAxis = false; Map copyMap(mapParameters); if (!selectedArea.isEmpty()) { diff --git a/src/tiled/editablemap.cpp b/src/tiled/editablemap.cpp index 95acf8bd9d..25cbf5e4f6 100644 --- a/src/tiled/editablemap.cpp +++ b/src/tiled/editablemap.cpp @@ -539,6 +539,14 @@ void EditableMap::setInfinite(bool value) map()->setInfinite(value); } +void EditableMap::setInvertYAxis(bool value) +{ + if (auto doc = mapDocument()) + push(new ChangeMapProperty(doc, Map::InvertYAxisProperty, value)); + else if (!checkReadOnly()) + map()->setInvertYAxis(value); +} + void EditableMap::setHexSideLength(int value) { if (auto doc = mapDocument()) diff --git a/src/tiled/editablemap.h b/src/tiled/editablemap.h index 543c3ff849..c1510fbbb6 100644 --- a/src/tiled/editablemap.h +++ b/src/tiled/editablemap.h @@ -46,6 +46,7 @@ class EditableMap : public EditableAsset Q_PROPERTY(int tileWidth READ tileWidth WRITE setTileWidth) Q_PROPERTY(int tileHeight READ tileHeight WRITE setTileHeight) Q_PROPERTY(bool infinite READ infinite WRITE setInfinite) + Q_PROPERTY(bool invertYAxis READ invertYAxis WRITE setInvertYAxis) Q_PROPERTY(int hexSideLength READ hexSideLength WRITE setHexSideLength) Q_PROPERTY(StaggerAxis staggerAxis READ staggerAxis WRITE setStaggerAxis) Q_PROPERTY(StaggerIndex staggerIndex READ staggerIndex WRITE setStaggerIndex) @@ -121,6 +122,7 @@ class EditableMap : public EditableAsset int tileWidth() const; int tileHeight() const; bool infinite() const; + bool invertYAxis() const; int hexSideLength() const; StaggerAxis staggerAxis() const; StaggerIndex staggerIndex() const; @@ -184,6 +186,7 @@ class EditableMap : public EditableAsset void setTileHeight(int value); Q_INVOKABLE void setTileSize(int width, int height); void setInfinite(bool value); + void setInvertYAxis(bool value); void setHexSideLength(int value); void setStaggerAxis(StaggerAxis value); void setStaggerIndex(StaggerIndex value); @@ -264,6 +267,11 @@ inline bool EditableMap::infinite() const return map()->infinite(); } +inline bool EditableMap::invertYAxis() const +{ + return map()->invertYAxis(); +} + inline int EditableMap::hexSideLength() const { return map()->hexSideLength(); diff --git a/src/tiled/invertyaxishelper.h b/src/tiled/invertyaxishelper.h index 4fd5fab58b..255f068b03 100644 --- a/src/tiled/invertyaxishelper.h +++ b/src/tiled/invertyaxishelper.h @@ -33,7 +33,7 @@ class InvertYAxisHelper InvertYAxisHelper(MapDocument *mapDocument) : mMapDocument(mapDocument) {} - + int tileY(int y) const; qreal pixelY(qreal y) const; @@ -47,7 +47,7 @@ class InvertYAxisHelper inline int InvertYAxisHelper::tileY(int y) const { // Check if Invert Y Axis is set - if (Preferences::instance()->invertYAxis()) + if (mMapDocument->map()->invertYAxis()) return mMapDocument->map()->height() - 1 - y; return y; } @@ -58,7 +58,7 @@ inline int InvertYAxisHelper::tileY(int y) const inline qreal InvertYAxisHelper::pixelY(qreal y) const { // Obtain the map document - if (Preferences::instance()->invertYAxis()) { + if (mMapDocument->map()->invertYAxis()) { const MapRenderer *renderer = mMapDocument->renderer(); QRect boundingRect = renderer->boundingRect(QRect(QPoint(), mMapDocument->map()->size())); return boundingRect.height() - y; diff --git a/src/tiled/preferences.cpp b/src/tiled/preferences.cpp index 5c24669e40..c63b2f0a3f 100644 --- a/src/tiled/preferences.cpp +++ b/src/tiled/preferences.cpp @@ -754,17 +754,6 @@ void Preferences::setWheelZoomsByDefault(bool mode) setValue(QLatin1String("Interface/WheelZoomsByDefault"), mode); } -bool Preferences::invertYAxis() const -{ - return get("Interface/InvertYAxis", false); -} - -void Preferences::setInvertYAxis(bool on) -{ - setValue(QLatin1String("Interface/InvertYAxis"), on); - emit invertYAxisChanged(); -} - QString Preferences::homeLocation() { return QStandardPaths::writableLocation(QStandardPaths::HomeLocation); diff --git a/src/tiled/preferences.h b/src/tiled/preferences.h index 098f101765..7432a68038 100644 --- a/src/tiled/preferences.h +++ b/src/tiled/preferences.h @@ -172,8 +172,6 @@ class TILED_EDITOR_EXPORT Preferences : public QSettings bool wheelZoomsByDefault() const; - bool invertYAxis() const; - template T get(const char *key, const T &defaultValue = T()) const { return value(QLatin1String(key), defaultValue).template value(); } @@ -209,7 +207,6 @@ public slots: void setRestoreSessionOnStartup(bool enabled); void setPluginEnabled(const QString &fileName, bool enabled); void setWheelZoomsByDefault(bool mode); - void setInvertYAxis(bool enabled); void clearRecentFiles(); void clearRecentProjects(); diff --git a/src/tiled/preferencesdialog.cpp b/src/tiled/preferencesdialog.cpp index 88a06901a6..d4fc6af2e0 100644 --- a/src/tiled/preferencesdialog.cpp +++ b/src/tiled/preferencesdialog.cpp @@ -135,8 +135,6 @@ 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->autoScrolling, &QCheckBox::toggled, this, [] (bool checked) { MapView::ourAutoScrollingEnabled = checked; }); connect(mUi->smoothScrolling, &QCheckBox::toggled, @@ -229,7 +227,6 @@ void PreferencesDialog::fromPreferences() if (mUi->openGL->isEnabled()) mUi->openGL->setChecked(prefs->useOpenGL()); mUi->wheelZoomsByDefault->setChecked(prefs->wheelZoomsByDefault()); - mUi->invertYAxis->setChecked(prefs->invertYAxis()); mUi->autoScrolling->setChecked(MapView::ourAutoScrollingEnabled); mUi->smoothScrolling->setChecked(MapView::ourSmoothScrollingEnabled); diff --git a/src/tiled/preferencesdialog.ui b/src/tiled/preferencesdialog.ui index e19dfe58c6..27c83fbcd0 100644 --- a/src/tiled/preferencesdialog.ui +++ b/src/tiled/preferencesdialog.ui @@ -372,13 +372,6 @@ - - - - Invert Y axis coordinates - - - @@ -662,7 +655,6 @@ preciseTileObjectSelection openGL wheelZoomsByDefault - invertYAxis autoScrolling smoothScrolling displayNewsCheckBox diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index 6ecf33ea18..0de675ad0e 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -153,9 +153,6 @@ PropertyBrowser::PropertyBrowser(QWidget *parent) connect(variantEditorFactory, &VariantEditorFactory::resetProperty, this, &PropertyBrowser::resetProperty); - connect(Preferences::instance(), &Preferences::invertYAxisChanged, - this, &PropertyBrowser::invertYAxisChanged); - connect(Preferences::instance(), &Preferences::propertyTypesChanged, this, &PropertyBrowser::propertyTypesChanged); @@ -422,12 +419,6 @@ void PropertyBrowser::wangSetChanged(WangSet *wangSet) updateProperties(); } -void PropertyBrowser::invertYAxisChanged() -{ - if (mObject && mObject->typeId() == Object::MapObjectType) - updateProperties(); -} - static bool isAutomappingRulesMap(const MapDocument *mapDocument) { if (!mapDocument) @@ -749,6 +740,7 @@ void PropertyBrowser::addMapProperties() auto tileWidthProperty = addProperty(TileWidthProperty, QMetaType::Int, tr("Tile Width"), groupProperty); auto tileHeightProperty = addProperty(TileHeightProperty, QMetaType::Int, tr("Tile Height"), groupProperty); addProperty(InfiniteProperty, QMetaType::Bool, tr("Infinite"), groupProperty); + addProperty(InvertYAxisProperty, QMetaType::Bool, tr("Invert Y-Axis"), groupProperty); tileWidthProperty->setAttribute(QStringLiteral("minimum"), 1); tileHeightProperty->setAttribute(QStringLiteral("minimum"), 1); @@ -1189,6 +1181,10 @@ void PropertyBrowser::applyMapValue(PropertyId id, const QVariant &val) undoStack->endMacro(); break; } + case InvertYAxisProperty: { + command = new ChangeMapProperty(mMapDocument, Map::InvertYAxisProperty, val.toInt()); + break; + } case OrientationProperty: { Map::Orientation orientation = static_cast(val.toInt() + 1); command = new ChangeMapProperty(mMapDocument, orientation); @@ -1862,6 +1858,7 @@ void PropertyBrowser::updateProperties() mIdToProperty[TileWidthProperty]->setValue(map->tileWidth()); mIdToProperty[TileHeightProperty]->setValue(map->tileHeight()); mIdToProperty[InfiniteProperty]->setValue(map->infinite()); + mIdToProperty[InvertYAxisProperty]->setValue(map->invertYAxis()); mIdToProperty[OrientationProperty]->setValue(map->orientation() - 1); mIdToProperty[HexSideLengthProperty]->setValue(map->hexSideLength()); mIdToProperty[StaggerAxisProperty]->setValue(map->staggerAxis()); diff --git a/src/tiled/propertybrowser.h b/src/tiled/propertybrowser.h index 87c34b8e07..125fcfbf71 100644 --- a/src/tiled/propertybrowser.h +++ b/src/tiled/propertybrowser.h @@ -80,7 +80,6 @@ class PropertyBrowser : public QtTreePropertyBrowser void tileChanged(Tile *tile); void tileTypeChanged(Tile *tile); void wangSetChanged(WangSet *wangSet); - void invertYAxisChanged(); void propertyAdded(Object *object, const QString &name); void propertyRemoved(Object *object, const QString &name); @@ -149,6 +148,7 @@ class PropertyBrowser : public QtTreePropertyBrowser WangColorProbabilityProperty, WangSetTypeProperty, InfiniteProperty, + InvertYAxisProperty, TemplateProperty, CompressionLevelProperty, ChunkWidthProperty, diff --git a/src/tiled/tilestampmanager.cpp b/src/tiled/tilestampmanager.cpp index 3fc63e25d7..b5fc7e3e14 100644 --- a/src/tiled/tilestampmanager.cpp +++ b/src/tiled/tilestampmanager.cpp @@ -98,6 +98,7 @@ static TileStamp stampFromContext(AbstractTool *selectedTool) mapParameters.width = selectionBounds.width(); mapParameters.height = selectionBounds.height(); mapParameters.infinite = false; + mapParameters.invertYAxis = false; auto copyMap = std::make_unique(mapParameters); map->copyLayers(mapDocument->selectedLayers(), selectedArea, *copyMap); From 16bb723a49438292efe0e06a83dcc3ad8e4e8d90 Mon Sep 17 00:00:00 2001 From: vinnydiehl Date: Fri, 5 May 2023 12:55:13 -0400 Subject: [PATCH 13/18] Invert object anchor point when Y-axis is inverted --- src/libtiled/mapobject.cpp | 1 - src/libtiled/mapobject.h | 10 +++++++++- src/libtiled/orthogonalrenderer.cpp | 2 +- src/tiled/propertybrowser.cpp | 8 ++++++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libtiled/mapobject.cpp b/src/libtiled/mapobject.cpp index d25b5a7b76..f3d8a5bd25 100644 --- a/src/libtiled/mapobject.cpp +++ b/src/libtiled/mapobject.cpp @@ -30,7 +30,6 @@ #include "mapobject.h" -#include "map.h" #include "maprenderer.h" #include "objectgroup.h" #include "objecttemplate.h" diff --git a/src/libtiled/mapobject.h b/src/libtiled/mapobject.h index e335af8a52..c3a5c32cbc 100644 --- a/src/libtiled/mapobject.h +++ b/src/libtiled/mapobject.h @@ -30,6 +30,7 @@ #pragma once +#include "map.h" #include "object.h" #include "tiled.h" #include "tilelayer.h" @@ -154,6 +155,7 @@ class TILEDSHARED_EXPORT MapObject : public Object void setX(qreal x); qreal y() const; + qreal nonInvertedY() const; void setY(qreal y); const QSizeF &size() const; @@ -313,10 +315,16 @@ inline void MapObject::setX(qreal x) * Returns the y position of this object. */ inline qreal MapObject::y() const +{ return map()->invertYAxis() && !isTileObject() ? mPos.y() + height() : mPos.y(); } + +/** + * Returns the y position of this object as though the y coordinates were not inverted. + */ +inline qreal MapObject::nonInvertedY() const { return mPos.y(); } /** - * Sets the x position of this object. + * Sets the y position of this object. */ inline void MapObject::setY(qreal y) { mPos.setY(y); } diff --git a/src/libtiled/orthogonalrenderer.cpp b/src/libtiled/orthogonalrenderer.cpp index e8096d1bcd..2ca53a550e 100644 --- a/src/libtiled/orthogonalrenderer.cpp +++ b/src/libtiled/orthogonalrenderer.cpp @@ -133,7 +133,7 @@ QPainterPath OrthogonalRenderer::shape(const MapObject *object) const switch (object->shape()) { case MapObject::Rectangle: { if (bounds.isNull()) { - path.addRect(object->x() - 10, object->y() - 10, 20, 20); + path.addRect(object->x() - 10, object->nonInvertedY() - 10, 20, 20); } else { if (const Tile *tile = object->cell().tile()) { QPointF tileOffset = tile->offset(); diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index 0de675ad0e..1861eb70ed 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -1271,14 +1271,18 @@ QUndoCommand *PropertyBrowser::applyMapObjectValueTo(PropertyId id, const QVaria case XProperty: { command = new ChangeMapObject(mDocument, mapObject, MapObject::PositionProperty, - QPointF(val.toReal(), mapObject->y())); + QPointF(val.toReal(), mapObject->nonInvertedY())); break; } case YProperty: { + qreal y = val.toReal(); + if (mapObject->map()->invertYAxis() && !mapObject->isTileObject()) + y += mapObject->height(); + command = new ChangeMapObject(mDocument, mapObject, MapObject::PositionProperty, QPointF(mapObject->x(), - InvertYAxisHelper(mMapDocument).pixelY(val.toReal()))); + InvertYAxisHelper(mMapDocument).pixelY(y))); break; } case WidthProperty: { From d44f60001b110632906631c712712bf730e31cd3 Mon Sep 17 00:00:00 2001 From: vinnydiehl Date: Sun, 7 May 2023 05:30:01 -0400 Subject: [PATCH 14/18] Invert Y-axis when reading/writing the map --- src/libtiled/mapreader.cpp | 7 ++++++- src/libtiled/mapwriter.cpp | 13 ++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/libtiled/mapreader.cpp b/src/libtiled/mapreader.cpp index 49ce1cea2a..1c767954d4 100644 --- a/src/libtiled/mapreader.cpp +++ b/src/libtiled/mapreader.cpp @@ -314,11 +314,11 @@ std::unique_ptr MapReaderPrivate::readMap() tileset->loadImage(); } - // Fix up sizes of tile objects. This is for backwards compatibility. LayerIterator iterator(mMap.get()); while (Layer *layer = iterator.next()) { if (ObjectGroup *objectGroup = layer->asObjectGroup()) { for (MapObject *object : *objectGroup) { + // Fix up sizes of tile objects. This is for backwards compatibility. if (const Tile *tile = object->cell().tile()) { const QSizeF tileSize = tile->size(); if (object->width() == 0) @@ -326,6 +326,11 @@ std::unique_ptr MapReaderPrivate::readMap() if (object->height() == 0) object->setHeight(tileSize.height()); } + + // Invert Y-axis if set + if (mMap->invertYAxis()) + object->setY(mMap->height() * mMap->tileHeight() - object->y()); + } } } diff --git a/src/libtiled/mapwriter.cpp b/src/libtiled/mapwriter.cpp index c87d05cb77..50b9fd15fb 100644 --- a/src/libtiled/mapwriter.cpp +++ b/src/libtiled/mapwriter.cpp @@ -786,7 +786,18 @@ void MapWriterPrivate::writeObject(QXmlStreamWriter &w, if (!mapObject.isTemplateBase()) { w.writeAttribute(QStringLiteral("x"), QString::number(pos.x())); - w.writeAttribute(QStringLiteral("y"), QString::number(pos.y())); + + qreal y = pos.y(); + if (mapObject.map()->invertYAxis()) { + y = mapObject.map()->height() * mapObject.map()->tileHeight() - pos.y(); + // Objects which anchor in the upper-left need to be translated by their height + if (mapObject.shape() != MapObject::Polygon && + mapObject.shape() != MapObject::Polyline && + !mapObject.isTileObject()) + y -= mapObject.height(); + } + + w.writeAttribute(QStringLiteral("y"), QString::number(y)); } if (shouldWrite(true, isTemplateInstance, mapObject.propertyChanged(MapObject::SizeProperty))) { From a40a2b30ca163795701636e26a226601f28b5632 Mon Sep 17 00:00:00 2001 From: vinnydiehl Date: Sun, 7 May 2023 07:40:07 -0400 Subject: [PATCH 15/18] Snap lower-left corner to grid when Y is inverted --- src/libtiled/maprenderer.cpp | 5 ++++- src/libtiled/maprenderer.h | 3 ++- src/tiled/objectselectiontool.cpp | 11 ++++++++++- src/tiled/snaphelper.cpp | 11 +++++++---- src/tiled/snaphelper.h | 3 ++- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/libtiled/maprenderer.cpp b/src/libtiled/maprenderer.cpp index acfa47e284..163fe117ec 100644 --- a/src/libtiled/maprenderer.cpp +++ b/src/libtiled/maprenderer.cpp @@ -237,6 +237,9 @@ QPainterPath MapRenderer::pointInteractionShape(const MapObject *object) const } QPointF MapRenderer::snapToGrid(const QPointF &pixelCoords, int subdivisions) const +{ return snapToGrid(pixelCoords, QPointF(0, 0), subdivisions); } + +QPointF MapRenderer::snapToGrid(const QPointF &pixelCoords, QPointF offset, int subdivisions) const { QPointF tileCoords = pixelToTileCoords(pixelCoords); if (subdivisions > 1) { @@ -245,7 +248,7 @@ QPointF MapRenderer::snapToGrid(const QPointF &pixelCoords, int subdivisions) co } else { tileCoords = tileCoords.toPoint(); } - return tileToPixelCoords(tileCoords); + return tileToPixelCoords(tileCoords) + offset; } void MapRenderer::drawTileLayer(QPainter *painter, const TileLayer *layer, const QRectF &exposed) const diff --git a/src/libtiled/maprenderer.h b/src/libtiled/maprenderer.h index ba5e29ae06..79df7c62d2 100644 --- a/src/libtiled/maprenderer.h +++ b/src/libtiled/maprenderer.h @@ -138,7 +138,8 @@ class TILEDSHARED_EXPORT MapRenderer virtual void drawGrid(QPainter *painter, const QRectF &rect, QColor gridColor = Qt::black, QSize gridMajor = QSize()) const = 0; - virtual QPointF snapToGrid(const QPointF &pixelCoords, + virtual QPointF snapToGrid(const QPointF &pixelCoords, int subdivisions = 1) const; + virtual QPointF snapToGrid(const QPointF &pixelCoords, QPointF offset, int subdivisions = 1) const; using RenderTileCallback = std::function; diff --git a/src/tiled/objectselectiontool.cpp b/src/tiled/objectselectiontool.cpp index 758eb7a113..2fd7a28db7 100644 --- a/src/tiled/objectselectiontool.cpp +++ b/src/tiled/objectselectiontool.cpp @@ -1765,7 +1765,16 @@ QPointF ObjectSelectionTool::snapToGrid(const QPointF &diff, const QPointF newAlignScreenPos = alignScreenPos + diff; QPointF newAlignPixelPos = renderer->screenToPixelCoords(newAlignScreenPos); - snapHelper.snap(newAlignPixelPos); + + QPointF offset(0, 0); + Map *map = mClickedObject->map(); + + // If Y-axis is inverted, snap the lower left corner to the grid + if (map->invertYAxis() && !mClickedObject->isTileObject()) + offset.ry() += map->tileHeight() * floor(mClickedObject->height() / map->tileHeight()) + - mClickedObject->height(); + + snapHelper.snap(newAlignPixelPos, offset); return renderer->pixelToScreenCoords(newAlignPixelPos) - alignScreenPos; } diff --git a/src/tiled/snaphelper.cpp b/src/tiled/snaphelper.cpp index 7c29e51df9..bf945b8eeb 100644 --- a/src/tiled/snaphelper.cpp +++ b/src/tiled/snaphelper.cpp @@ -40,7 +40,7 @@ SnapHelper::SnapHelper(const MapRenderer *renderer, if (modifiers & Qt::ControlModifier) { if (modifiers & Qt::ShiftModifier) { toggleFineSnap(); - } else { + } else { toggleSnap(); } } @@ -58,7 +58,7 @@ void SnapHelper::toggleSnap() break; } } - + void SnapHelper::toggleFineSnap() { switch (mSnapMode) { @@ -73,13 +73,16 @@ void SnapHelper::toggleFineSnap() } void SnapHelper::snap(QPointF &pixelPos) const +{ snap(pixelPos, QPointF(0, 0)); } + +void SnapHelper::snap(QPointF &pixelPos, QPointF offset) const { if (mSnapMode != NoSnap) { if (mSnapMode == SnapToFineGrid) { const int gridFine = Preferences::instance()->gridFine(); - pixelPos = mRenderer->snapToGrid(pixelPos, gridFine); + pixelPos = mRenderer->snapToGrid(pixelPos, offset, gridFine); } else { - pixelPos = mRenderer->snapToGrid(pixelPos); + pixelPos = mRenderer->snapToGrid(pixelPos, offset); } } else if (mSnapToPixels) { QPointF screenPos = mRenderer->pixelToScreenCoords(pixelPos); diff --git a/src/tiled/snaphelper.h b/src/tiled/snaphelper.h index b846f1ac07..3097f9bb52 100644 --- a/src/tiled/snaphelper.h +++ b/src/tiled/snaphelper.h @@ -31,12 +31,13 @@ class SnapHelper SnapHelper(const MapRenderer *renderer, Qt::KeyboardModifiers modifiers = {}); void toggleSnap(); - + void toggleFineSnap(); bool snaps() const { return mSnapMode != NoSnap || mSnapToPixels; } void snap(QPointF &pixelPos) const; + void snap(QPointF &pixelPos, QPointF offset) const; private: const MapRenderer *mRenderer; From fdad7b1a2495794b5ef62308cdece3db8a08874c Mon Sep 17 00:00:00 2001 From: vinnydiehl Date: Sun, 7 May 2023 07:51:27 -0400 Subject: [PATCH 16/18] Remove vestigal preferences declaration --- src/tiled/preferences.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/tiled/preferences.h b/src/tiled/preferences.h index 7432a68038..7b8ffa349d 100644 --- a/src/tiled/preferences.h +++ b/src/tiled/preferences.h @@ -253,8 +253,6 @@ public slots: void aboutToSwitchSession(); - void invertYAxisChanged(); - private: void addToRecentFileList(const QString &fileName, QStringList &files); From db25d422ad0cf361add6588ead521fafeaa4943f Mon Sep 17 00:00:00 2001 From: vinnydiehl Date: Mon, 8 May 2023 08:59:12 -0400 Subject: [PATCH 17/18] Use MapObject::alignment() to invert anchor point --- src/libtiled/map.cpp | 18 ++++++++++++++++++ src/libtiled/map.h | 5 ----- src/libtiled/mapobject.cpp | 2 +- src/libtiled/mapobject.h | 7 ------- src/libtiled/maprenderer.cpp | 5 +---- src/libtiled/maprenderer.h | 3 +-- src/libtiled/mapwriter.cpp | 10 ++-------- src/libtiled/orthogonalrenderer.cpp | 2 +- src/tiled/objectselectiontool.cpp | 10 +--------- src/tiled/propertybrowser.cpp | 8 ++------ src/tiled/snaphelper.cpp | 7 ++----- src/tiled/snaphelper.h | 1 - 12 files changed, 29 insertions(+), 49 deletions(-) diff --git a/src/libtiled/map.cpp b/src/libtiled/map.cpp index f9f84f0354..5e39f52474 100644 --- a/src/libtiled/map.cpp +++ b/src/libtiled/map.cpp @@ -69,6 +69,24 @@ Map::~Map() qDeleteAll(mLayers); } +void Map::setInvertYAxis(bool invertYAxis) +{ + mParameters.invertYAxis = invertYAxis; + + for (auto layer : mLayers) { + if (!layer->isObjectGroup()) + continue; + + auto og = layer->asObjectGroup(); + for (auto it = og->begin(); it != og->end(); ++it) { + auto object = *it; + // Tile objects are anchored in the lower-left already, so don't height-adjust + if (!object->isTileObject()) + object->setY(object->y() + object->height() * (invertYAxis ? 1 : -1)); + } + } +} + /** * Returns the margins that have to be taken into account when figuring * out which part of the map to repaint after changing some tiles. diff --git a/src/libtiled/map.h b/src/libtiled/map.h index cc061eb051..d54384525f 100644 --- a/src/libtiled/map.h +++ b/src/libtiled/map.h @@ -475,11 +475,6 @@ inline bool Map::invertYAxis() const return mParameters.invertYAxis; } -inline void Map::setInvertYAxis(bool invertYAxis) -{ - mParameters.invertYAxis = invertYAxis; -} - inline int Map::hexSideLength() const { return mParameters.hexSideLength; diff --git a/src/libtiled/mapobject.cpp b/src/libtiled/mapobject.cpp index f3d8a5bd25..50d46af7b4 100644 --- a/src/libtiled/mapobject.cpp +++ b/src/libtiled/mapobject.cpp @@ -271,7 +271,7 @@ Alignment MapObject::alignment(const Map *map) const if (alignment == Unspecified) { if (mCell.isEmpty()) - return TopLeft; + return map && map->invertYAxis() ? BottomLeft : TopLeft; else if (map && map->orientation() == Map::Isometric) return Bottom; diff --git a/src/libtiled/mapobject.h b/src/libtiled/mapobject.h index c3a5c32cbc..dbfd67edea 100644 --- a/src/libtiled/mapobject.h +++ b/src/libtiled/mapobject.h @@ -155,7 +155,6 @@ class TILEDSHARED_EXPORT MapObject : public Object void setX(qreal x); qreal y() const; - qreal nonInvertedY() const; void setY(qreal y); const QSizeF &size() const; @@ -315,12 +314,6 @@ inline void MapObject::setX(qreal x) * Returns the y position of this object. */ inline qreal MapObject::y() const -{ return map()->invertYAxis() && !isTileObject() ? mPos.y() + height() : mPos.y(); } - -/** - * Returns the y position of this object as though the y coordinates were not inverted. - */ -inline qreal MapObject::nonInvertedY() const { return mPos.y(); } /** diff --git a/src/libtiled/maprenderer.cpp b/src/libtiled/maprenderer.cpp index 163fe117ec..acfa47e284 100644 --- a/src/libtiled/maprenderer.cpp +++ b/src/libtiled/maprenderer.cpp @@ -237,9 +237,6 @@ QPainterPath MapRenderer::pointInteractionShape(const MapObject *object) const } QPointF MapRenderer::snapToGrid(const QPointF &pixelCoords, int subdivisions) const -{ return snapToGrid(pixelCoords, QPointF(0, 0), subdivisions); } - -QPointF MapRenderer::snapToGrid(const QPointF &pixelCoords, QPointF offset, int subdivisions) const { QPointF tileCoords = pixelToTileCoords(pixelCoords); if (subdivisions > 1) { @@ -248,7 +245,7 @@ QPointF MapRenderer::snapToGrid(const QPointF &pixelCoords, QPointF offset, int } else { tileCoords = tileCoords.toPoint(); } - return tileToPixelCoords(tileCoords) + offset; + return tileToPixelCoords(tileCoords); } void MapRenderer::drawTileLayer(QPainter *painter, const TileLayer *layer, const QRectF &exposed) const diff --git a/src/libtiled/maprenderer.h b/src/libtiled/maprenderer.h index 79df7c62d2..ba5e29ae06 100644 --- a/src/libtiled/maprenderer.h +++ b/src/libtiled/maprenderer.h @@ -138,8 +138,7 @@ class TILEDSHARED_EXPORT MapRenderer virtual void drawGrid(QPainter *painter, const QRectF &rect, QColor gridColor = Qt::black, QSize gridMajor = QSize()) const = 0; - virtual QPointF snapToGrid(const QPointF &pixelCoords, int subdivisions = 1) const; - virtual QPointF snapToGrid(const QPointF &pixelCoords, QPointF offset, + virtual QPointF snapToGrid(const QPointF &pixelCoords, int subdivisions = 1) const; using RenderTileCallback = std::function; diff --git a/src/libtiled/mapwriter.cpp b/src/libtiled/mapwriter.cpp index 50b9fd15fb..21d95f152d 100644 --- a/src/libtiled/mapwriter.cpp +++ b/src/libtiled/mapwriter.cpp @@ -788,14 +788,8 @@ void MapWriterPrivate::writeObject(QXmlStreamWriter &w, w.writeAttribute(QStringLiteral("x"), QString::number(pos.x())); qreal y = pos.y(); - if (mapObject.map()->invertYAxis()) { - y = mapObject.map()->height() * mapObject.map()->tileHeight() - pos.y(); - // Objects which anchor in the upper-left need to be translated by their height - if (mapObject.shape() != MapObject::Polygon && - mapObject.shape() != MapObject::Polyline && - !mapObject.isTileObject()) - y -= mapObject.height(); - } + if (mapObject.map()->invertYAxis()) + y = mapObject.map()->height() * mapObject.map()->tileHeight() - y; w.writeAttribute(QStringLiteral("y"), QString::number(y)); } diff --git a/src/libtiled/orthogonalrenderer.cpp b/src/libtiled/orthogonalrenderer.cpp index 2ca53a550e..e8096d1bcd 100644 --- a/src/libtiled/orthogonalrenderer.cpp +++ b/src/libtiled/orthogonalrenderer.cpp @@ -133,7 +133,7 @@ QPainterPath OrthogonalRenderer::shape(const MapObject *object) const switch (object->shape()) { case MapObject::Rectangle: { if (bounds.isNull()) { - path.addRect(object->x() - 10, object->nonInvertedY() - 10, 20, 20); + path.addRect(object->x() - 10, object->y() - 10, 20, 20); } else { if (const Tile *tile = object->cell().tile()) { QPointF tileOffset = tile->offset(); diff --git a/src/tiled/objectselectiontool.cpp b/src/tiled/objectselectiontool.cpp index 2fd7a28db7..f0436c99aa 100644 --- a/src/tiled/objectselectiontool.cpp +++ b/src/tiled/objectselectiontool.cpp @@ -1766,15 +1766,7 @@ QPointF ObjectSelectionTool::snapToGrid(const QPointF &diff, QPointF newAlignPixelPos = renderer->screenToPixelCoords(newAlignScreenPos); - QPointF offset(0, 0); - Map *map = mClickedObject->map(); - - // If Y-axis is inverted, snap the lower left corner to the grid - if (map->invertYAxis() && !mClickedObject->isTileObject()) - offset.ry() += map->tileHeight() * floor(mClickedObject->height() / map->tileHeight()) - - mClickedObject->height(); - - snapHelper.snap(newAlignPixelPos, offset); + snapHelper.snap(newAlignPixelPos); return renderer->pixelToScreenCoords(newAlignPixelPos) - alignScreenPos; } diff --git a/src/tiled/propertybrowser.cpp b/src/tiled/propertybrowser.cpp index 1861eb70ed..0de675ad0e 100644 --- a/src/tiled/propertybrowser.cpp +++ b/src/tiled/propertybrowser.cpp @@ -1271,18 +1271,14 @@ QUndoCommand *PropertyBrowser::applyMapObjectValueTo(PropertyId id, const QVaria case XProperty: { command = new ChangeMapObject(mDocument, mapObject, MapObject::PositionProperty, - QPointF(val.toReal(), mapObject->nonInvertedY())); + QPointF(val.toReal(), mapObject->y())); break; } case YProperty: { - qreal y = val.toReal(); - if (mapObject->map()->invertYAxis() && !mapObject->isTileObject()) - y += mapObject->height(); - command = new ChangeMapObject(mDocument, mapObject, MapObject::PositionProperty, QPointF(mapObject->x(), - InvertYAxisHelper(mMapDocument).pixelY(y))); + InvertYAxisHelper(mMapDocument).pixelY(val.toReal()))); break; } case WidthProperty: { diff --git a/src/tiled/snaphelper.cpp b/src/tiled/snaphelper.cpp index bf945b8eeb..4b98f931d0 100644 --- a/src/tiled/snaphelper.cpp +++ b/src/tiled/snaphelper.cpp @@ -73,16 +73,13 @@ void SnapHelper::toggleFineSnap() } void SnapHelper::snap(QPointF &pixelPos) const -{ snap(pixelPos, QPointF(0, 0)); } - -void SnapHelper::snap(QPointF &pixelPos, QPointF offset) const { if (mSnapMode != NoSnap) { if (mSnapMode == SnapToFineGrid) { const int gridFine = Preferences::instance()->gridFine(); - pixelPos = mRenderer->snapToGrid(pixelPos, offset, gridFine); + pixelPos = mRenderer->snapToGrid(pixelPos, gridFine); } else { - pixelPos = mRenderer->snapToGrid(pixelPos, offset); + pixelPos = mRenderer->snapToGrid(pixelPos); } } else if (mSnapToPixels) { QPointF screenPos = mRenderer->pixelToScreenCoords(pixelPos); diff --git a/src/tiled/snaphelper.h b/src/tiled/snaphelper.h index 3097f9bb52..e2fe6444f7 100644 --- a/src/tiled/snaphelper.h +++ b/src/tiled/snaphelper.h @@ -37,7 +37,6 @@ class SnapHelper bool snaps() const { return mSnapMode != NoSnap || mSnapToPixels; } void snap(QPointF &pixelPos) const; - void snap(QPointF &pixelPos, QPointF offset) const; private: const MapRenderer *mRenderer; From 32dbfdb72609240ae5432d4b60fb3dc054ee00f7 Mon Sep 17 00:00:00 2001 From: vinnydiehl Date: Wed, 10 May 2023 09:29:34 -0400 Subject: [PATCH 18/18] Clean up changes on `invert-y` Removed vestigal whitespace modifications from development, addressed include order, removed unnecessary map property references. --- src/libtiled/mapobject.cpp | 1 + src/libtiled/mapobject.h | 1 - src/tiled/abstractobjecttool.cpp | 2 +- src/tiled/abstracttiletool.cpp | 2 +- src/tiled/clipboardmanager.cpp | 1 - src/tiled/mapobjectmodel.cpp | 2 +- src/tiled/objectselectiontool.cpp | 1 - src/tiled/preferencesdialog.cpp | 1 + src/tiled/tilestampmanager.cpp | 1 - 9 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/libtiled/mapobject.cpp b/src/libtiled/mapobject.cpp index 50d46af7b4..b9f0e73094 100644 --- a/src/libtiled/mapobject.cpp +++ b/src/libtiled/mapobject.cpp @@ -30,6 +30,7 @@ #include "mapobject.h" +#include "map.h" #include "maprenderer.h" #include "objectgroup.h" #include "objecttemplate.h" diff --git a/src/libtiled/mapobject.h b/src/libtiled/mapobject.h index dbfd67edea..1d52ec2ea4 100644 --- a/src/libtiled/mapobject.h +++ b/src/libtiled/mapobject.h @@ -30,7 +30,6 @@ #pragma once -#include "map.h" #include "object.h" #include "tiled.h" #include "tilelayer.h" diff --git a/src/tiled/abstractobjecttool.cpp b/src/tiled/abstractobjecttool.cpp index 6ddef35aa5..70eb675a92 100644 --- a/src/tiled/abstractobjecttool.cpp +++ b/src/tiled/abstractobjecttool.cpp @@ -26,6 +26,7 @@ #include "changepolygon.h" #include "changetileobjectgroup.h" #include "documentmanager.h" +#include "invertyaxishelper.h" #include "mapdocument.h" #include "map.h" #include "mapobject.h" @@ -39,7 +40,6 @@ #include "tile.h" #include "tmxmapformat.h" #include "utils.h" -#include "invertyaxishelper.h" #include #include diff --git a/src/tiled/abstracttiletool.cpp b/src/tiled/abstracttiletool.cpp index 1336e994a8..23793fc967 100644 --- a/src/tiled/abstracttiletool.cpp +++ b/src/tiled/abstracttiletool.cpp @@ -21,13 +21,13 @@ #include "abstracttiletool.h" #include "brushitem.h" +#include "invertyaxishelper.h" #include "map.h" #include "mapdocument.h" #include "maprenderer.h" #include "mapscene.h" #include "tilelayer.h" #include "tilestamp.h" -#include "invertyaxishelper.h" #include #include diff --git a/src/tiled/clipboardmanager.cpp b/src/tiled/clipboardmanager.cpp index b3ce4a2e38..51b01b937d 100644 --- a/src/tiled/clipboardmanager.cpp +++ b/src/tiled/clipboardmanager.cpp @@ -156,7 +156,6 @@ bool ClipboardManager::copySelection(const MapDocument &mapDocument) mapParameters.width = selectionBounds.width(); mapParameters.height = selectionBounds.height(); mapParameters.infinite = false; - mapParameters.invertYAxis = false; Map copyMap(mapParameters); if (!selectedArea.isEmpty()) { diff --git a/src/tiled/mapobjectmodel.cpp b/src/tiled/mapobjectmodel.cpp index 1507d944ac..c91b8826f8 100644 --- a/src/tiled/mapobjectmodel.cpp +++ b/src/tiled/mapobjectmodel.cpp @@ -26,11 +26,11 @@ #include "changemapobject.h" #include "changeproperties.h" #include "grouplayer.h" +#include "invertyaxishelper.h" #include "layermodel.h" #include "map.h" #include "mapdocument.h" #include "objectgroup.h" -#include "invertyaxishelper.h" #include #include diff --git a/src/tiled/objectselectiontool.cpp b/src/tiled/objectselectiontool.cpp index f0436c99aa..758eb7a113 100644 --- a/src/tiled/objectselectiontool.cpp +++ b/src/tiled/objectselectiontool.cpp @@ -1765,7 +1765,6 @@ QPointF ObjectSelectionTool::snapToGrid(const QPointF &diff, const QPointF newAlignScreenPos = alignScreenPos + diff; QPointF newAlignPixelPos = renderer->screenToPixelCoords(newAlignScreenPos); - snapHelper.snap(newAlignPixelPos); return renderer->pixelToScreenCoords(newAlignPixelPos) - alignScreenPos; diff --git a/src/tiled/preferencesdialog.cpp b/src/tiled/preferencesdialog.cpp index d4fc6af2e0..426e68dec3 100644 --- a/src/tiled/preferencesdialog.cpp +++ b/src/tiled/preferencesdialog.cpp @@ -139,6 +139,7 @@ PreferencesDialog::PreferencesDialog(QWidget *parent) this, [] (bool checked) { MapView::ourAutoScrollingEnabled = checked; }); connect(mUi->smoothScrolling, &QCheckBox::toggled, this, [] (bool checked) { MapView::ourSmoothScrollingEnabled = checked; }); + connect(mUi->styleCombo, static_cast(&QComboBox::currentIndexChanged), this, &PreferencesDialog::styleComboChanged); connect(mUi->objectSelectionBehaviorCombo, static_cast(&QComboBox::currentIndexChanged), diff --git a/src/tiled/tilestampmanager.cpp b/src/tiled/tilestampmanager.cpp index b5fc7e3e14..3fc63e25d7 100644 --- a/src/tiled/tilestampmanager.cpp +++ b/src/tiled/tilestampmanager.cpp @@ -98,7 +98,6 @@ static TileStamp stampFromContext(AbstractTool *selectedTool) mapParameters.width = selectionBounds.width(); mapParameters.height = selectionBounds.height(); mapParameters.infinite = false; - mapParameters.invertYAxis = false; auto copyMap = std::make_unique(mapParameters); map->copyLayers(mapDocument->selectedLayers(), selectedArea, *copyMap);