From 0f219964751c55ffa32ee43ebe82c00b54ffae56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 5 Dec 2024 16:44:43 +0100 Subject: [PATCH] AutoMapping: Ignore rules with empty input or output regions Rules with an empty input region were causing issues, because they don't have a valid bounding rectangle. They are also commonly created by accident. If intended as a rule that always matches, such a rule can be made by placing the special "Ignore" tile in the rule's input. Rules with empty output region are no longer matched because they would not produce any output anyway. As a drive-by, AutoMapper no longer derives from QObject, which appears to have only been done to access the tr() function. This is now made available using Q_DECLARE_TR_FUNCTIONS instead. Closes #3834 --- src/tiled/automapper.cpp | 13 +++++++++---- src/tiled/automapper.h | 7 ++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/tiled/automapper.cpp b/src/tiled/automapper.cpp index f6dd498e86..cbf8d1ea59 100644 --- a/src/tiled/automapper.cpp +++ b/src/tiled/automapper.cpp @@ -639,9 +639,16 @@ void AutoMapper::setupRules() mRules.reserve(combinedRegions.size()); for (const QRegion &combinedRegion : combinedRegions) { + QRegion inputRegion = combinedRegion & regionInput; + QRegion outputRegion = combinedRegion & regionOutput; + + // Skip rules where either input or output region is empty + if (inputRegion.isEmpty() || outputRegion.isEmpty()) + continue; + Rule &rule = mRules.emplace_back(); - rule.inputRegion = combinedRegion & regionInput; - rule.outputRegion = combinedRegion & regionOutput; + rule.inputRegion = std::move(inputRegion); + rule.outputRegion = std::move(outputRegion); rule.options = mRuleOptions; for (const auto &optionsArea : setup.mRuleOptionsAreas) @@ -1474,5 +1481,3 @@ void AutoMapper::addWarning(const QString &message, std::function callb } } // namespace Tiled - -#include "moc_automapper.cpp" diff --git a/src/tiled/automapper.h b/src/tiled/automapper.h index 52a43ad098..a8ca7da548 100644 --- a/src/tiled/automapper.h +++ b/src/tiled/automapper.h @@ -27,6 +27,7 @@ #include "tilelayer.h" #include "tileset.h" +#include #include #include #include @@ -275,9 +276,9 @@ struct TILED_EDITOR_EXPORT AutoMappingContext * - copy regions of Maps (multiple Layers, the layerlist is a * lookup-table for matching the Layers) */ -class TILED_EDITOR_EXPORT AutoMapper : public QObject +class TILED_EDITOR_EXPORT AutoMapper { - Q_OBJECT + Q_DECLARE_TR_FUNCTIONS(Tiled::AutoMapper) public: struct Options @@ -333,7 +334,7 @@ class TILED_EDITOR_EXPORT AutoMapper : public QObject * AutoMapper takes ownership of this map. */ AutoMapper(std::unique_ptr rulesMap, const QRegularExpression &mapNameFilter = {}); - ~AutoMapper() override; + ~AutoMapper(); QString rulesMapFileName() const; const QRegularExpression &mapNameFilter() const;