Skip to content

Commit

Permalink
AutoMapper: Some code simplifications and small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn committed Jun 6, 2016
1 parent 4a46797 commit e943c67
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/tiled/automapper.cpp
Expand Up @@ -555,19 +555,17 @@ QRect AutoMapper::applyRule(const int ruleIndex, const QRect &where)
}

if (anymatch) {
int r = 0;
// choose by chance which group of rule_layers should be used:
if (mLayerList.size() > 1)
r = qrand() % mLayerList.size();
const int r = qrand() % mLayerList.size();
const RuleOutput *translationTable = mLayerList.at(r);

if (!mNoOverlappingRules) {
copyMapRegion(ruleOutput, QPoint(x, y), mLayerList.at(r));
copyMapRegion(ruleOutput, QPoint(x, y), translationTable);
ret = ret.united(rbr.translated(QPoint(x, y)));
continue;
}

bool missmatch = false;
RuleOutput *translationTable = mLayerList.at(r);
QList<Layer*> layers = translationTable->keys();

// check if there are no overlaps within this rule.
Expand All @@ -592,7 +590,7 @@ QRect AutoMapper::applyRule(const int ruleIndex, const QRect &where)
if (missmatch)
continue;

copyMapRegion(ruleOutput, QPoint(x, y), mLayerList.at(r));
copyMapRegion(ruleOutput, QPoint(x, y), translationTable);
ret = ret.united(rbr.translated(QPoint(x, y)));
for (int i = 0; i < translationTable->size(); ++i) {
appliedRegions[i] +=
Expand Down Expand Up @@ -781,21 +779,19 @@ static bool compareLayerTo(const TileLayer *setLayer,
void AutoMapper::copyMapRegion(const QRegion &region, QPoint offset,
const RuleOutput *layerTranslation)
{
for (int i = 0; i < layerTranslation->keys().size(); ++i) {
Layer *from = layerTranslation->keys().at(i);
for (auto it = layerTranslation->begin(), end = layerTranslation->end(); it != end; ++it) {
Layer *from = it.key();
Layer *to = mMapWork->layerAt(layerTranslation->value(from));
foreach (const QRect &rect, region.rects()) {
TileLayer *fromTileLayer = from->asTileLayer();
ObjectGroup *fromObjectGroup = from->asObjectGroup();
if (fromTileLayer) {
if (TileLayer *fromTileLayer = from->asTileLayer()) {
TileLayer *toTileLayer = to->asTileLayer();
Q_ASSERT(toTileLayer); //TODO check this before in prepareAutomap or such!
copyTileRegion(fromTileLayer, rect.x(), rect.y(),
rect.width(), rect.height(),
toTileLayer,
rect.x() + offset.x(), rect.y() + offset.y());

} else if (fromObjectGroup) {
} else if (ObjectGroup *fromObjectGroup = from->asObjectGroup()) {
ObjectGroup *toObjectGroup = to->asObjectGroup();
copyObjectRegion(fromObjectGroup, rect.x(), rect.y(),
rect.width(), rect.height(),
Expand Down Expand Up @@ -916,11 +912,9 @@ void AutoMapper::cleanUpRuleMapLayers()
{
cleanTileLayers();

QList<RuleOutput*>::const_iterator it;
for (it = mLayerList.constBegin(); it != mLayerList.constEnd(); ++it)
delete (*it);

qDeleteAll(mLayerList);
mLayerList.clear();

// do not delete mLayerRuleRegions, it is owned by the rulesmap
mLayerInputRegions = nullptr;
mLayerOutputRegions = nullptr;
Expand Down

0 comments on commit e943c67

Please sign in to comment.