Skip to content

Commit

Permalink
Implemented marching ants effect on selected objects(mapeditor#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
thabetx committed Mar 21, 2017
1 parent 0d27be0 commit d805bf5
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/tiled/objectselectionitem.cpp
Expand Up @@ -29,6 +29,7 @@
#include "objectgroup.h"
#include "preferences.h"
#include "tile.h"
#include <QTimer>

#include <QGuiApplication>

Expand Down Expand Up @@ -127,14 +128,17 @@ static Preferences::ObjectLabelVisiblity objectLabelVisibility()
}


class MapObjectOutline : public QGraphicsItem
// MapObjectOuline inherits from QObject to use timer signals
class MapObjectOutline : public QObject , public QGraphicsItem
{
public:
MapObjectOutline(MapObject *object, QGraphicsItem *parent = nullptr)
: QGraphicsItem(parent)
, mObject(object)
{
setZValue(1); // makes sure outlines are above labels
connect(updateTimer, &QTimer::timeout, this, [this] {this->update();});
updateTimer->start(50);
}

void syncWithMapObject(MapRenderer *renderer);
Expand All @@ -147,6 +151,11 @@ class MapObjectOutline : public QGraphicsItem
private:
QRectF mBoundingRect;
MapObject *mObject;

// Timer is used to call update function repeatedly to animate selection
QTimer *updateTimer = new QTimer();
// Offset simulates the marching ants effect
int offset=0;
};

void MapObjectOutline::syncWithMapObject(MapRenderer *renderer)
Expand Down Expand Up @@ -184,17 +193,35 @@ void MapObjectOutline::paint(QPainter *painter,
QLineF(mBoundingRect.topRight(), mBoundingRect.bottomRight())
};

QPen dashPen(Qt::DashLine);
QPen dashPen(Qt::SolidLine);
dashPen.setCosmetic(true);
dashPen.setDashOffset(qMax(qreal(0), x()));

// draw a solid white line
dashPen.setColor(Qt::white);
painter->setPen(dashPen);
painter->drawLines(horizontal, 2);

// Draw a black dashed line above above the white line
dashPen.setColor(Qt::black);
dashPen.setStyle(Qt::DashLine);
dashPen.setDashOffset(qMax(qreal(0), x())+offset);
painter->setPen(dashPen);
painter->drawLines(horizontal, 2);

dashPen.setDashOffset(qMax(qreal(0), y()));
dashPen.setColor(Qt::white);
dashPen.setStyle(Qt::SolidLine);
painter->setPen(dashPen);
painter->drawLines(vertical, 2);
}

dashPen.setColor(Qt::black);
dashPen.setStyle(Qt::DashLine);
dashPen.setDashOffset(qMax(qreal(0), x())+offset);
painter->setPen(dashPen);
painter->drawLines(vertical, 2);

// Update offset used in drawing black dashed line
offset++;
}

class MapObjectLabel : public QGraphicsItem
{
Expand Down

0 comments on commit d805bf5

Please sign in to comment.