Skip to content

Commit

Permalink
AutoMapping: Ignore rules with empty input or output regions
Browse files Browse the repository at this point in the history
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 mapeditor#3834
  • Loading branch information
bjorn committed Dec 5, 2024
1 parent 8e81703 commit 0f21996
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/tiled/automapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1474,5 +1481,3 @@ void AutoMapper::addWarning(const QString &message, std::function<void ()> callb
}

} // namespace Tiled

#include "moc_automapper.cpp"
7 changes: 4 additions & 3 deletions src/tiled/automapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "tilelayer.h"
#include "tileset.h"

#include <QCoreApplication>
#include <QList>
#include <QMap>
#include <QRegion>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -333,7 +334,7 @@ class TILED_EDITOR_EXPORT AutoMapper : public QObject
* AutoMapper takes ownership of this map.
*/
AutoMapper(std::unique_ptr<Map> rulesMap, const QRegularExpression &mapNameFilter = {});
~AutoMapper() override;
~AutoMapper();

QString rulesMapFileName() const;
const QRegularExpression &mapNameFilter() const;
Expand Down

0 comments on commit 0f21996

Please sign in to comment.