Skip to content

Commit

Permalink
Improved Terrain Brush for Hexagonal (Staggered) maps with side length 0
Browse files Browse the repository at this point in the history
Now Hexagonal (Staggered) maps are treated the same way as Isometric
(Staggered) maps, which might be better than treating them as orthogonal
maps. It at least fixes the behavior when "hex side length" is 0, but it
does not mean hexagonal maps are actually supported by the Terrain
Brush.

Closes #3617
  • Loading branch information
bjorn committed Mar 20, 2023
1 parent 16229d0 commit 9aaada7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 54 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Unreleased

* Fixed object labels to adjust to application font changes
* Improved Terrain Brush for Hexagonal (Staggered) maps with side length 0 (#3617)
* Qt 6: Increased the image allocation limit from 128 MB to 1 GB (#3616)

### Tiled 1.10.0 (10 March 2023)
Expand Down
7 changes: 2 additions & 5 deletions src/tiled/bucketfilltool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@

#include "bucketfilltool.h"

#include "addremovetileset.h"
#include "brushitem.h"
#include "tilepainter.h"
#include "tile.h"
#include "tilelayer.h"
#include "mapdocument.h"
#include "painttilelayer.h"
#include "staggeredrenderer.h"
#include "stampactions.h"

#include <QApplication>
#include <QCoreApplication>
#include <QUndoStack>

#include <memory>

Expand Down
12 changes: 4 additions & 8 deletions src/tiled/stampbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@

#include "stampbrush.h"

#include "addremovelayer.h"
#include "addremovetileset.h"
#include "brushitem.h"
#include "geometry.h"
#include "map.h"
#include "mapdocument.h"
#include "mapscene.h"
#include "painttilelayer.h"
#include "staggeredrenderer.h"
#include "stampactions.h"
#include "tile.h"
#include "tilestamp.h"
Expand Down Expand Up @@ -59,13 +55,13 @@ StampBrush::StampBrush(QObject *parent)
connect(mStampActions->random(), &QAction::toggled, this, &StampBrush::randomChanged);
connect(mStampActions->wangFill(), &QAction::toggled, this, &StampBrush::wangFillChanged);

connect(mStampActions->flipHorizontal(), &QAction::triggered,
connect(mStampActions->flipHorizontal(), &QAction::triggered, this,
[this] { emit stampChanged(mStamp.flipped(FlipHorizontally)); });
connect(mStampActions->flipVertical(), &QAction::triggered,
connect(mStampActions->flipVertical(), &QAction::triggered, this,
[this] { emit stampChanged(mStamp.flipped(FlipVertically)); });
connect(mStampActions->rotateLeft(), &QAction::triggered,
connect(mStampActions->rotateLeft(), &QAction::triggered, this,
[this] { emit stampChanged(mStamp.rotated(RotateLeft)); });
connect(mStampActions->rotateRight(), &QAction::triggered,
connect(mStampActions->rotateRight(), &QAction::triggered, this,
[this] { emit stampChanged(mStamp.rotated(RotateRight)); });
}

Expand Down
49 changes: 24 additions & 25 deletions src/tiled/wangbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
#include "brushitem.h"
#include "containerhelpers.h"
#include "geometry.h"
#include "hexagonalrenderer.h"
#include "map.h"
#include "mapdocument.h"
#include "maprenderer.h"
#include "mapscene.h"
#include "painttilelayer.h"
#include "randompicker.h"
#include "staggeredrenderer.h"
#include "tilelayer.h"
#include "wangfiller.h"

Expand Down Expand Up @@ -279,11 +278,11 @@ void WangBrush::mouseMoved(const QPointF &pos, Qt::KeyboardModifiers modifiers)
case Idle: // can't happen due to check above
return;
case PaintCorner:
if (StaggeredRenderer *staggeredRenderer = dynamic_cast<StaggeredRenderer*>(mapDocument()->renderer())) {
if (auto hexagonalRenderer = dynamic_cast<HexagonalRenderer*>(mapDocument()->renderer())) {
if (tileLocalPos.x() >= 0.5)
tilePos = staggeredRenderer->bottomRight(tilePos.x(), tilePos.y());
tilePos = hexagonalRenderer->bottomRight(tilePos.x(), tilePos.y());
if (tileLocalPos.y() >= 0.5)
tilePos = staggeredRenderer->bottomLeft(tilePos.x(), tilePos.y());
tilePos = hexagonalRenderer->bottomLeft(tilePos.x(), tilePos.y());
} else {
if (tileLocalPos.x() >= 0.5)
tilePos.rx() += 1;
Expand Down Expand Up @@ -324,19 +323,19 @@ void WangBrush::mouseMoved(const QPointF &pos, Qt::KeyboardModifiers modifiers)
break;
}
case PaintEdgeAndCorner:
if (StaggeredRenderer *staggeredRenderer = dynamic_cast<StaggeredRenderer*>(mapDocument()->renderer())) {
if (auto hexagonalRenderer = dynamic_cast<HexagonalRenderer*>(mapDocument()->renderer())) {
switch (wangIndex) {
case WangId::BottomRight:
tilePos = staggeredRenderer->bottomRight(tilePos.x(), tilePos.y());
tilePos = staggeredRenderer->bottomLeft(tilePos.x(), tilePos.y());
tilePos = hexagonalRenderer->bottomRight(tilePos.x(), tilePos.y());
tilePos = hexagonalRenderer->bottomLeft(tilePos.x(), tilePos.y());
wangIndex = WangId::TopLeft;
break;
case WangId::BottomLeft:
tilePos = staggeredRenderer->bottomLeft(tilePos.x(), tilePos.y());
tilePos = hexagonalRenderer->bottomLeft(tilePos.x(), tilePos.y());
wangIndex = WangId::TopLeft;
break;
case WangId::TopRight:
tilePos = staggeredRenderer->bottomRight(tilePos.x(), tilePos.y());
tilePos = hexagonalRenderer->bottomRight(tilePos.x(), tilePos.y());
wangIndex = WangId::TopLeft;
break;
default:
Expand Down Expand Up @@ -645,7 +644,7 @@ void WangBrush::updateBrush()

void WangBrush::updateBrushAt(FillRegion &fill, QPoint pos)
{
auto staggeredRenderer = dynamic_cast<StaggeredRenderer*>(mapDocument()->renderer());
auto hexgonalRenderer = dynamic_cast<HexagonalRenderer*>(mapDocument()->renderer());
Grid<WangFiller::CellInfo> &grid = fill.grid;
QRegion &region = fill.region;

Expand All @@ -654,11 +653,11 @@ void WangBrush::updateBrushAt(FillRegion &fill, QPoint pos)
if (mIsTileMode || (mBrushBehavior == Line && mBrushMode == PaintEdgeAndCorner)) {
//array of adjacent positions which is assigned based on map orientation.
QPoint adjacentPositions[WangId::NumIndexes];
if (staggeredRenderer) {
adjacentPositions[0] = staggeredRenderer->topRight(pos.x(), pos.y());
adjacentPositions[2] = staggeredRenderer->bottomRight(pos.x(), pos.y());
adjacentPositions[4] = staggeredRenderer->bottomLeft(pos.x(), pos.y());
adjacentPositions[6] = staggeredRenderer->topLeft(pos.x(), pos.y());
if (hexgonalRenderer) {
adjacentPositions[0] = hexgonalRenderer->topRight(pos.x(), pos.y());
adjacentPositions[2] = hexgonalRenderer->bottomRight(pos.x(), pos.y());
adjacentPositions[4] = hexgonalRenderer->bottomLeft(pos.x(), pos.y());
adjacentPositions[6] = hexgonalRenderer->topLeft(pos.x(), pos.y());

if (mapDocument()->map()->staggerAxis() == Map::StaggerX) {
adjacentPositions[1] = pos + QPoint(2, 0);
Expand Down Expand Up @@ -742,11 +741,11 @@ void WangBrush::updateBrushAt(FillRegion &fill, QPoint pos)
case PaintCorner: {
QPoint adjacentPoints[WangId::NumCorners];

if (staggeredRenderer) {
adjacentPoints[0] = staggeredRenderer->topRight(pos.x(), pos.y());
if (hexgonalRenderer) {
adjacentPoints[0] = hexgonalRenderer->topRight(pos.x(), pos.y());
adjacentPoints[1] = pos;
adjacentPoints[2] = staggeredRenderer->topLeft(pos.x(), pos.y());
adjacentPoints[3] = staggeredRenderer->topRight(adjacentPoints[2].x(), adjacentPoints[2].y());
adjacentPoints[2] = hexgonalRenderer->topLeft(pos.x(), pos.y());
adjacentPoints[3] = hexgonalRenderer->topRight(adjacentPoints[2].x(), adjacentPoints[2].y());
} else {
for (int i = 0; i < WangId::NumCorners; ++i)
adjacentPoints[i] = pos + aroundVertexPoints[i];
Expand All @@ -768,19 +767,19 @@ void WangBrush::updateBrushAt(FillRegion &fill, QPoint pos)
}
case PaintEdge: {
QPoint dirPoint;
if (staggeredRenderer) {
if (hexgonalRenderer) {
switch (mWangIndex) {
case WangId::Top:
dirPoint = staggeredRenderer->topRight(pos.x(), pos.y());
dirPoint = hexgonalRenderer->topRight(pos.x(), pos.y());
break;
case WangId::Right:
dirPoint = staggeredRenderer->bottomRight(pos.x(), pos.y());
dirPoint = hexgonalRenderer->bottomRight(pos.x(), pos.y());
break;
case WangId::Bottom:
dirPoint = staggeredRenderer->bottomLeft(pos.x(), pos.y());
dirPoint = hexgonalRenderer->bottomLeft(pos.x(), pos.y());
break;
case WangId::Left:
dirPoint = staggeredRenderer->topLeft(pos.x(), pos.y());
dirPoint = hexgonalRenderer->topLeft(pos.x(), pos.y());
break;
default: // Other color indexes not handled when painting edges
break;
Expand Down
27 changes: 14 additions & 13 deletions src/tiled/wangfiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
#include "wangfiller.h"

#include "grid.h"
#include "hexagonalrenderer.h"
#include "map.h"
#include "randompicker.h"
#include "staggeredrenderer.h"
#include "tilelayer.h"
#include "wangset.h"

Expand All @@ -43,21 +44,21 @@ WangFiller::WangFiller(const WangSet &wangSet,
const MapRenderer *mapRenderer)
: mWangSet(wangSet)
, mMapRenderer(mapRenderer)
, mStaggeredRenderer(dynamic_cast<const StaggeredRenderer*>(mapRenderer))
, mHexagonalRenderer(dynamic_cast<const HexagonalRenderer*>(mapRenderer))
{
}

static void getSurroundingPoints(QPoint point,
const StaggeredRenderer *staggeredRenderer,
const HexagonalRenderer *hexagonalRenderer,
QPoint *points)
{
if (staggeredRenderer) {
points[0] = staggeredRenderer->topRight(point.x(), point.y());
points[2] = staggeredRenderer->bottomRight(point.x(), point.y());
points[4] = staggeredRenderer->bottomLeft(point.x(), point.y());
points[6] = staggeredRenderer->topLeft(point.x(), point.y());
if (hexagonalRenderer) {
points[0] = hexagonalRenderer->topRight(point.x(), point.y());
points[2] = hexagonalRenderer->bottomRight(point.x(), point.y());
points[4] = hexagonalRenderer->bottomLeft(point.x(), point.y());
points[6] = hexagonalRenderer->topLeft(point.x(), point.y());

if (staggeredRenderer->map()->staggerAxis() == Map::StaggerX) {
if (hexagonalRenderer->map()->staggerAxis() == Map::StaggerX) {
points[1] = point + QPoint(2, 0);
points[3] = point + QPoint(0, 1);
points[5] = point + QPoint(-2, 0);
Expand Down Expand Up @@ -155,7 +156,7 @@ void WangFiller::fillRegion(TileLayer &target,

// Determine the bounds of the affected area
QRect bounds = region.boundingRect();
int margin = mWangSet.maximumColorDistance() + (mStaggeredRenderer != nullptr);
int margin = mWangSet.maximumColorDistance() + (mHexagonalRenderer != nullptr);
bounds.adjust(-margin, -margin, margin, margin);

// Don't try to make corrections outside of a fixed map
Expand Down Expand Up @@ -185,7 +186,7 @@ void WangFiller::fillRegion(TileLayer &target,

// Adjust the desired WangIds for the surrounding tiles based on the placed one
QPoint adjacentPoints[WangId::NumIndexes];
getSurroundingPoints(QPoint(x, y), mStaggeredRenderer, adjacentPoints);
getSurroundingPoints(QPoint(x, y), mHexagonalRenderer, adjacentPoints);

for (int i = 0; i < WangId::NumIndexes; ++i) {
const QPoint p = adjacentPoints[i];
Expand Down Expand Up @@ -237,7 +238,7 @@ WangId WangFiller::wangIdFromSurroundings(const TileLayer &back,
{
Cell surroundingCells[8];
QPoint adjacentPoints[8];
getSurroundingPoints(point, mStaggeredRenderer, adjacentPoints);
getSurroundingPoints(point, mHexagonalRenderer, adjacentPoints);

for (int i = 0; i < 8; ++i) {
if (!region.contains(adjacentPoints[i]))
Expand Down Expand Up @@ -324,7 +325,7 @@ bool WangFiller::findBestMatch(const TileLayer &target,
// Adjust the desired WangIds for the surrounding tiles based on
// the to be placed one.
QPoint adjacentPoints[WangId::NumIndexes];
getSurroundingPoints(position, mStaggeredRenderer, adjacentPoints);
getSurroundingPoints(position, mHexagonalRenderer, adjacentPoints);

for (int i = 0; i < WangId::NumIndexes; ++i) {
const QPoint p = adjacentPoints[i];
Expand Down
5 changes: 2 additions & 3 deletions src/tiled/wangfiller.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#pragma once

#include "grid.h"
#include "map.h"
#include "wangset.h"

#include <QList>
Expand All @@ -33,7 +32,7 @@
namespace Tiled {

class MapRenderer;
class StaggeredRenderer;
class HexagonalRenderer;

/**
* WangFiller provides functions for choosing cells based on a surrounding map
Expand Down Expand Up @@ -87,7 +86,7 @@ class WangFiller

const WangSet &mWangSet;
const MapRenderer * const mMapRenderer;
const StaggeredRenderer * const mStaggeredRenderer;
const HexagonalRenderer * const mHexagonalRenderer;
bool mCorrectionsEnabled = false;

QPainter *mDebugPainter = nullptr;
Expand Down

0 comments on commit 9aaada7

Please sign in to comment.