Skip to content

Commit

Permalink
Added basic "major grid" option with stronger lines
Browse files Browse the repository at this point in the history
Useful for rendering larger sectors or simply making it easier to see
the distance in tiles. By default, there's a major grid line every 10
tiles.

Resolves part of #368, though the goal is to eventually be more
flexible.

Closes #3032 (this patch is based on that pull request)

Co-authored-by: Ilya Arkhanhelsky <ilya.arkhanhelsky@vizor-games.com>
  • Loading branch information
bjorn and iarkhanhelsky committed Apr 26, 2021
1 parent 323f9a1 commit a1a9e18
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 90 deletions.
8 changes: 6 additions & 2 deletions src/libtiled/hexagonalrenderer.cpp
Expand Up @@ -138,8 +138,10 @@ QRect HexagonalRenderer::boundingRect(const QRect &rect) const
}

void HexagonalRenderer::drawGrid(QPainter *painter, const QRectF &exposed,
QColor gridColor) const
QColor gridColor, int gridMajor) const
{
Q_UNUSED(gridMajor) // Unclear how this should apply to hexagonal maps

QRect rect = exposed.toAlignedRect();
if (rect.isNull())
return;
Expand Down Expand Up @@ -184,7 +186,9 @@ void HexagonalRenderer::drawGrid(QPainter *painter, const QRectF &exposed,
QVector<QLine> lines;
lines.reserve(8);

QPen gridPen = makeGridPen(painter->device(), gridColor);
QPen _gridPen, gridPen; // always use major grid pen for hex maps
setupGridPens(painter->device(), gridColor, _gridPen, gridPen);

painter->setPen(gridPen);

if (p.staggerX) {
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/hexagonalrenderer.h
Expand Up @@ -78,7 +78,7 @@ class TILEDSHARED_EXPORT HexagonalRenderer : public OrthogonalRenderer
QRect boundingRect(const QRect &rect) const override;

void drawGrid(QPainter *painter, const QRectF &exposed,
QColor gridColor) const override;
QColor gridColor, int gridMajor = 0) const override;

using MapRenderer::drawTileLayer;
void drawTileLayer(const RenderTileCallback &renderTile,
Expand Down
10 changes: 7 additions & 3 deletions src/libtiled/isometricrenderer.cpp
Expand Up @@ -226,7 +226,7 @@ QPainterPath IsometricRenderer::interactionShape(const MapObject *object) const
}

void IsometricRenderer::drawGrid(QPainter *painter, const QRectF &rect,
QColor gridColor) const
QColor gridColor, int gridMajor) const
{
const int tileWidth = map()->tileWidth();
const int tileHeight = map()->tileHeight();
Expand All @@ -247,17 +247,21 @@ void IsometricRenderer::drawGrid(QPainter *painter, const QRectF &rect,
endY = qMin(map()->height(), endY);
}

QPen gridPen = makeGridPen(painter->device(), gridColor);
painter->setPen(gridPen);
QPen gridPen, majorGridPen;
setupGridPens(painter->device(), gridColor, gridPen, majorGridPen);

for (int y = startY; y <= endY; ++y) {
const QPointF start = tileToScreenCoords(startX, y);
const QPointF end = tileToScreenCoords(endX, y);

painter->setPen(gridMajor != 0 && y % gridMajor == 0 ? majorGridPen : gridPen);
painter->drawLine(start, end);
}
for (int x = startX; x <= endX; ++x) {
const QPointF start = tileToScreenCoords(x, startY);
const QPointF end = tileToScreenCoords(x, endY);

painter->setPen(gridMajor != 0 && x % gridMajor == 0 ? majorGridPen : gridPen);
painter->drawLine(start, end);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/isometricrenderer.h
Expand Up @@ -52,7 +52,7 @@ class TILEDSHARED_EXPORT IsometricRenderer final : public MapRenderer
QPainterPath shape(const MapObject *object) const override;
QPainterPath interactionShape(const MapObject *object) const override;

void drawGrid(QPainter *painter, const QRectF &rect, QColor grid) const override;
void drawGrid(QPainter *painter, const QRectF &rect, QColor grid, int gridMajor) const override;

using MapRenderer::drawTileLayer;
void drawTileLayer(const RenderTileCallback &renderTile,
Expand Down
19 changes: 12 additions & 7 deletions src/libtiled/maprenderer.cpp
Expand Up @@ -242,7 +242,8 @@ QPolygonF MapRenderer::lineToPolygon(const QPointF &start, const QPointF &end)
return polygon;
}

QPen MapRenderer::makeGridPen(const QPaintDevice *device, QColor color)
void MapRenderer::setupGridPens(const QPaintDevice *device, QColor color,
QPen &gridPen, QPen &majorGridPen)
{
const qreal devicePixelRatio = device->devicePixelRatioF();

Expand All @@ -252,14 +253,18 @@ QPen MapRenderer::makeGridPen(const QPaintDevice *device, QColor color)
const qreal dpiScale = device->logicalDpiX() / 96.0;
#endif

const qreal dashLength = std::ceil(2.0 * dpiScale * devicePixelRatio);
const qreal dashLength = std::ceil(2.0 * dpiScale);

color.setAlpha(128);
color.setAlpha(96);

QPen pen(color);
pen.setCosmetic(true);
pen.setDashPattern({dashLength, dashLength});
return pen;
gridPen = QPen(color, 1.0 * devicePixelRatio);
gridPen.setCosmetic(true);
gridPen.setDashPattern({dashLength, dashLength});

color.setAlpha(192);

majorGridPen = gridPen;
majorGridPen.setColor(color);
}


Expand Down
5 changes: 3 additions & 2 deletions src/libtiled/maprenderer.h
Expand Up @@ -133,7 +133,7 @@ class TILEDSHARED_EXPORT MapRenderer
* \a painter.
*/
virtual void drawGrid(QPainter *painter, const QRectF &rect,
QColor gridColor = Qt::black) const = 0;
QColor gridColor = Qt::black, int gridMajor = 0) const = 0;

typedef std::function<void(QPoint, const QPointF &)> RenderTileCallback;

Expand Down Expand Up @@ -270,7 +270,8 @@ class TILEDSHARED_EXPORT MapRenderer
static QPolygonF lineToPolygon(const QPointF &start, const QPointF &end);

protected:
static QPen makeGridPen(const QPaintDevice *device, QColor color);
static void setupGridPens(const QPaintDevice *device, QColor color,
QPen &gridPen, QPen &majorGridPen);

void setCellType(CellType cellType) { mCellType = cellType; }

Expand Down
39 changes: 23 additions & 16 deletions src/libtiled/orthogonalrenderer.cpp
Expand Up @@ -224,40 +224,47 @@ QPainterPath OrthogonalRenderer::interactionShape(const MapObject *object) const
}

void OrthogonalRenderer::drawGrid(QPainter *painter, const QRectF &rect,
QColor gridColor) const
QColor gridColor, int gridMajor) const
{
const int tileWidth = map()->tileWidth();
const int tileHeight = map()->tileHeight();

if (tileWidth <= 0 || tileHeight <= 0)
return;

int startX = qFloor(rect.x() / tileWidth) * tileWidth;
int startY = qFloor(rect.y() / tileHeight) * tileHeight;
int endX = qCeil(rect.right());
int endY = qCeil(rect.bottom());
int startX = qFloor(rect.x() / tileWidth);
int startY = qFloor(rect.y() / tileHeight);
int endX = qCeil(rect.right() / tileWidth);
int endY = qCeil(rect.bottom() / tileHeight);

if (!map()->infinite()) {
startX = qMax(0, startX);
startY = qMax(0, startY);
endX = qMin(endX, map()->width() * tileWidth + 1);
endY = qMin(endY, map()->height() * tileHeight + 1);
endX = qMin(endX, map()->width());
endY = qMin(endY, map()->height());
}

QPen gridPen = makeGridPen(painter->device(), gridColor);
QPen gridPen, majorGridPen;
setupGridPens(painter->device(), gridColor, gridPen, majorGridPen);

if (startY < endY) {
gridPen.setDashOffset(startY);
painter->setPen(gridPen);
for (int x = startX; x < endX; x += tileWidth)
painter->drawLine(x, startY, x, endY - 1);
gridPen.setDashOffset(startY * tileHeight);
majorGridPen.setDashOffset(startY * tileHeight);

for (int x = startX; x < endX; ++x) {
painter->setPen(gridMajor != 0 && x % gridMajor == 0 ? majorGridPen : gridPen);
painter->drawLine(x * tileWidth, startY * tileHeight, x * tileWidth, endY * tileHeight);
}
}

if (startX < endX) {
gridPen.setDashOffset(startX);
painter->setPen(gridPen);
for (int y = startY; y < endY; y += tileHeight)
painter->drawLine(startX, y, endX - 1, y);
gridPen.setDashOffset(startX * tileWidth);
majorGridPen.setDashOffset(startX * tileWidth);

for (int y = startY; y < endY; ++y) {
painter->setPen(gridMajor != 0 && y % gridMajor == 0 ? majorGridPen : gridPen);
painter->drawLine(startX * tileWidth, y * tileHeight, endX * tileWidth, y * tileHeight);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/orthogonalrenderer.h
Expand Up @@ -50,7 +50,7 @@ class TILEDSHARED_EXPORT OrthogonalRenderer : public MapRenderer
QPainterPath interactionShape(const MapObject *object) const override;

void drawGrid(QPainter *painter, const QRectF &rect,
QColor gridColor) const override;
QColor gridColor, int gridMajor = 0) const override;

using MapRenderer::drawTileLayer;
void drawTileLayer(const RenderTileCallback &renderTile,
Expand Down
3 changes: 2 additions & 1 deletion src/tiled/mapitem.cpp
Expand Up @@ -71,6 +71,7 @@ class TileGridItem : public QGraphicsObject
Preferences *prefs = Preferences::instance();
connect(prefs, &Preferences::showGridChanged, this, [this] (bool visible) { setVisible(visible); });
connect(prefs, &Preferences::gridColorChanged, this, [this] { update(); });
connect(prefs, &Preferences::gridMajorChanged, this, [this] { update(); });

// New layer may have a different offset
connect(mapDocument, &MapDocument::currentLayerChanged,
Expand Down Expand Up @@ -107,7 +108,7 @@ class TileGridItem : public QGraphicsObject
Preferences *prefs = Preferences::instance();
mMapDocument->renderer()->drawGrid(painter,
option->exposedRect.translated(-mOffset),
prefs->gridColor());
prefs->gridColor(), prefs->gridMajor());
}

void updateOffset()
Expand Down
11 changes: 11 additions & 0 deletions src/tiled/preferences.cpp
Expand Up @@ -164,6 +164,11 @@ int Preferences::gridFine() const
return get<int>("Interface/GridFine", 4);
}

int Preferences::gridMajor() const
{
return get<int>("Interface/GridMajor", 10);
}

qreal Preferences::objectLineWidth() const
{
return get<qreal>("Interface/ObjectLineWidth", 2.0);
Expand Down Expand Up @@ -315,6 +320,12 @@ void Preferences::setGridFine(int gridFine)
emit gridFineChanged(gridFine);
}

void Preferences::setGridMajor(int gridMajor)
{
setValue(QLatin1String("Interface/GridMajor"), gridMajor);
emit gridMajorChanged(gridMajor);
}

void Preferences::setObjectLineWidth(qreal lineWidth)
{
setValue(QLatin1String("Interface/ObjectLineWidth"), lineWidth);
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/preferences.h
Expand Up @@ -63,6 +63,7 @@ class Preferences : public QSettings
bool snapToPixels() const;
QColor gridColor() const;
int gridFine() const;
int gridMajor() const;
qreal objectLineWidth() const;

bool highlightCurrentLayer() const;
Expand Down Expand Up @@ -187,6 +188,7 @@ public slots:
void setSnapToPixels(bool snapToPixels);
void setGridColor(QColor gridColor);
void setGridFine(int gridFine);
void setGridMajor(int gridMajor);
void setObjectLineWidth(qreal lineWidth);
void setHighlightCurrentLayer(bool highlight);
void setHighlightHoveredObject(bool highlight);
Expand All @@ -210,6 +212,7 @@ public slots:
void snapToPixelsChanged(bool snapToPixels);
void gridColorChanged(QColor gridColor);
void gridFineChanged(int gridFine);
void gridMajorChanged(int gridMajor);
void objectLineWidthChanged(qreal lineWidth);
void highlightCurrentLayerChanged(bool highlight);
void highlightHoveredObjectChanged(bool highlight);
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/preferencesdialog.cpp
Expand Up @@ -125,6 +125,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent)
preferences, &Preferences::setGridColor);
connect(mUi->gridFine, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
preferences, &Preferences::setGridFine);
connect(mUi->gridMajor, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
preferences, &Preferences::setGridMajor);
connect(mUi->objectLineWidth, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
preferences, &Preferences::setObjectLineWidth);
connect(mUi->openGL, &QCheckBox::toggled,
Expand Down Expand Up @@ -222,6 +224,7 @@ void PreferencesDialog::fromPreferences()
mUi->languageCombo->setCurrentIndex(languageIndex);
mUi->gridColor->setColor(prefs->gridColor());
mUi->gridFine->setValue(prefs->gridFine());
mUi->gridMajor->setValue(prefs->gridMajor());
mUi->objectLineWidth->setValue(prefs->objectLineWidth());

// Updates
Expand Down

0 comments on commit a1a9e18

Please sign in to comment.