Skip to content

Commit 4cc65cb

Browse files
Merge pull request #750 from GriffinRichards/update-cursor-rects
Cursor refactoring
2 parents 9a0dc28 + 0da6ae9 commit 4cc65cb

17 files changed

+287
-352
lines changed

include/core/maplayout.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Layout : public QObject {
100100
QRect getVisibleRect() const;
101101

102102
bool isWithinBounds(int x, int y) const;
103+
bool isWithinBounds(const QPoint &pos) const;
103104
bool isWithinBounds(const QRect &rect) const;
104105
bool isWithinBorderBounds(int x, int y) const;
105106

include/editor.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ class Editor : public QObject
122122
void updateEventPixmapItemZValue(EventPixmapItem *item);
123123
qreal getEventOpacity(const Event *event) const;
124124

125+
bool isMouseInMap() const;
125126
void setPlayerViewRect(const QRectF &rect);
126-
void updateCursorRectPos(int x, int y);
127-
void setCursorRectVisible(bool visible);
127+
void setCursorRectPos(const QPoint &pos);
128+
void updateCursorRectVisibility();
128129

129130
void onEventDragged(Event *event, const QPoint &oldPosition, const QPoint &newPosition);
130131
void onEventReleased(Event *event, const QPoint &position);
@@ -171,7 +172,7 @@ class Editor : public QObject
171172
void setEditMode(EditMode editMode);
172173
EditMode getEditMode() const { return this->editMode; }
173174

174-
bool getEditingLayout();
175+
bool getEditingLayout() const;
175176

176177
void setMapEditingButtonsEnabled(bool enabled);
177178

@@ -251,26 +252,26 @@ public slots:
251252
QString getMovementPermissionText(uint16_t collision, uint16_t elevation);
252253
QString getMetatileDisplayMessage(uint16_t metatileId);
253254
void setCollisionTabSpinBoxes(uint16_t collision, uint16_t elevation);
255+
void adjustStraightPathPos(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *item, QPoint *pos) const;
254256
static bool startDetachedProcess(const QString &command,
255257
const QString &workingDirectory = QString(),
256258
qint64 *pid = nullptr);
257-
258-
private slots:
259+
bool canPaintMetatiles() const;
259260
void onMapStartPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *item);
260261
void onMapEndPaint(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *item);
262+
263+
private slots:
261264
void setSmartPathCursorMode(QGraphicsSceneMouseEvent *event);
262-
void setStraightPathCursorMode(QGraphicsSceneMouseEvent *event);
263265
void mouseEvent_map(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *item);
264266
void mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item);
265267
void setSelectedConnectionItem(ConnectionPixmapItem *connectionItem);
266268
void onHoveredMovementPermissionChanged(uint16_t, uint16_t);
267269
void onHoveredMovementPermissionCleared();
268270
void onHoveredMetatileSelectionChanged(uint16_t);
269271
void onHoveredMetatileSelectionCleared();
270-
void onHoveredMapMetatileChanged(const QPoint &pos);
271-
void onHoveredMapMetatileCleared();
272-
void onHoveredMapMovementPermissionChanged(int, int);
273-
void onHoveredMapMovementPermissionCleared();
272+
void onMapHoverEntered(const QPoint &pos);
273+
void onMapHoverChanged(const QPoint &pos);
274+
void onMapHoverCleared();
274275
void onSelectedMetatilesChanged();
275276
void onWheelZoom(int);
276277

include/settings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class Settings
1010
Settings();
1111
bool smartPathsEnabled;
1212
bool betterCursors;
13-
QCursor mapCursor;
1413
bool playerViewRectEnabled;
1514
bool cursorTileRectEnabled;
1615
};

include/ui/collisionpixmapitem.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class CollisionPixmapItem : public LayoutPixmapItem {
2323
QSpinBox * selectedElevation;
2424
qreal *opacity;
2525
void updateMovementPermissionSelection(QGraphicsSceneMouseEvent *event);
26-
virtual void paint(QGraphicsSceneMouseEvent*);
27-
virtual void floodFill(QGraphicsSceneMouseEvent*);
28-
virtual void magicFill(QGraphicsSceneMouseEvent*);
29-
virtual void pick(QGraphicsSceneMouseEvent*);
30-
void draw(bool ignoreCache = false);
26+
virtual void paint(QGraphicsSceneMouseEvent*) override;
27+
virtual void floodFill(QGraphicsSceneMouseEvent*) override;
28+
virtual void magicFill(QGraphicsSceneMouseEvent*) override;
29+
virtual void pick(QGraphicsSceneMouseEvent*) override;
30+
void draw(bool ignoreCache = false) override;
3131

3232
private:
3333
unsigned actionId_ = 0;
@@ -36,16 +36,17 @@ class CollisionPixmapItem : public LayoutPixmapItem {
3636

3737
signals:
3838
void mouseEvent(QGraphicsSceneMouseEvent *, CollisionPixmapItem *);
39-
void hoveredMapMovementPermissionChanged(int, int);
40-
void hoveredMapMovementPermissionCleared();
39+
void hoverEntered(const QPoint &pos);
40+
void hoverChanged(const QPoint &pos);
41+
void hoverCleared();
4142

4243
protected:
43-
void hoverMoveEvent(QGraphicsSceneHoverEvent*);
44-
void hoverEnterEvent(QGraphicsSceneHoverEvent*);
45-
void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
46-
void mousePressEvent(QGraphicsSceneMouseEvent*);
47-
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
48-
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
44+
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent*) override;
45+
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
46+
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
47+
virtual void mousePressEvent(QGraphicsSceneMouseEvent*) override;
48+
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent*) override;
49+
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent*) override;
4950
};
5051

5152
#endif // COLLISIONPIXMAPITEM_H

include/ui/cursortilerect.h

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,55 @@
88
class CursorTileRect : public QGraphicsItem
99
{
1010
public:
11-
CursorTileRect(bool *enabled, QRgb color);
12-
QRectF boundingRect() const override
13-
{
14-
int width = this->width;
15-
int height = this->height;
16-
if (this->singleTileMode) {
17-
width = 16;
18-
height = 16;
19-
} else if (!this->rightClickSelectionAnchored && this->smartPathMode && this->selectionHeight == 3 && this->selectionWidth == 3) {
20-
width = 32;
21-
height = 32;
22-
}
11+
CursorTileRect(const QSize &tileSize, const QRgb &color, QGraphicsItem *parent = nullptr);
12+
13+
QSize size() const;
14+
15+
QRectF boundingRect() const override {
16+
auto s = size();
2317
qreal penWidth = 4;
2418
return QRectF(-penWidth,
2519
-penWidth,
26-
width + penWidth * 2,
27-
height + penWidth * 2);
20+
s.width() + penWidth * 2,
21+
s.height() + penWidth * 2);
2822
}
2923

30-
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
31-
{
32-
if (!(*enabled)) return;
33-
int width = this->width;
34-
int height = this->height;
35-
if (this->singleTileMode) {
36-
width = 16;
37-
height = 16;
38-
} else if (this->smartPathInEffect()) {
39-
width = 32;
40-
height = 32;
41-
}
42-
43-
painter->setPen(this->color);
44-
painter->drawRect(x() - 1, y() - 1, width + 2, height + 2);
24+
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override {
25+
if (!isVisible()) return;
26+
auto rect = QRectF(pos(), size());
27+
painter->setPen(m_color);
28+
painter->drawRect(rect + QMargins(1,1,1,1)); // Fill
4529
painter->setPen(QColor(0, 0, 0));
46-
painter->drawRect(x() - 2, y() - 2, width + 4, height + 4);
47-
painter->drawRect(x(), y(), width, height);
30+
painter->drawRect(rect + QMargins(2,2,2,2)); // Outer border
31+
painter->drawRect(rect); // Inner border
4832
}
33+
4934
void initAnchor(int coordX, int coordY);
5035
void stopAnchor();
5136
void initRightClickSelectionAnchor(int coordX, int coordY);
5237
void stopRightClickSelectionAnchor();
5338

54-
void setSmartPathMode(bool enable) { this->smartPathMode = enable; }
55-
bool getSmartPathMode() const { return this->smartPathMode; }
56-
57-
void setStraightPathMode(bool enable) { this->straightPathMode = enable; }
58-
bool getStraightPathMode() const { return this->straightPathMode; }
39+
void setSmartPathMode(bool enable) { m_smartPathMode = enable; }
40+
bool getSmartPathMode() const { return m_smartPathMode; }
5941

60-
void setSingleTileMode(bool enable) { this->singleTileMode = enable; }
61-
bool getSingleTileMode() const { return this->singleTileMode; }
42+
void setSingleTileMode(bool enable) { m_singleTileMode = enable; }
43+
bool getSingleTileMode() const { return m_singleTileMode; }
6244

6345
void updateLocation(int x, int y);
6446
void updateSelectionSize(int width, int height);
65-
void setActive(bool active);
66-
bool getActive();
67-
bool *enabled;
47+
6848
private:
69-
bool active;
70-
int width;
71-
int height;
72-
bool anchored;
73-
bool rightClickSelectionAnchored;
74-
bool smartPathMode;
75-
bool straightPathMode;
76-
bool singleTileMode;
77-
int anchorCoordX;
78-
int anchorCoordY;
79-
int selectionWidth;
80-
int selectionHeight;
81-
QRgb color;
82-
bool smartPathInEffect();
49+
const QSize m_tileSize;
50+
QSize m_selectionSize;
51+
QPoint m_anchorCoord;
52+
QRgb m_color;
53+
54+
bool m_anchored = false;
55+
bool m_rightClickSelectionAnchored = false;
56+
bool m_smartPathMode = false;
57+
bool m_singleTileMode = false;
58+
59+
bool smartPathInEffect() const;
8360
};
8461

8562

include/ui/layoutpixmapitem.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,28 @@ class LayoutPixmapItem : public QObject, public QGraphicsPixmapItem {
8686
void lockNondominantAxis(QGraphicsSceneMouseEvent *event);
8787
QPoint adjustCoords(QPoint pos);
8888

89-
void setEditsEnabled(bool enabled) { this->editsEnabled = enabled; }
90-
bool getEditsEnabled() { return this->editsEnabled; }
91-
9289
private:
9390
void paintSmartPath(int x, int y, bool fromScriptCall = false);
9491
static QList<int> smartPathTable;
9592
QPoint lastMetatileSelectionPos = QPoint(-1,-1);
9693

9794
unsigned actionId_ = 0;
9895

99-
bool editsEnabled = true;
100-
10196
signals:
10297
void startPaint(QGraphicsSceneMouseEvent *, LayoutPixmapItem *);
10398
void endPaint(QGraphicsSceneMouseEvent *, LayoutPixmapItem *);
10499
void mouseEvent(QGraphicsSceneMouseEvent *, LayoutPixmapItem *);
105-
void hoveredMapMetatileChanged(const QPoint &pos);
106-
void hoveredMapMetatileCleared();
100+
void hoverEntered(const QPoint &pos);
101+
void hoverChanged(const QPoint &pos);
102+
void hoverCleared();
107103

108104
protected:
109-
void hoverMoveEvent(QGraphicsSceneHoverEvent*);
110-
void hoverEnterEvent(QGraphicsSceneHoverEvent*);
111-
void hoverLeaveEvent(QGraphicsSceneHoverEvent*);
112-
void mousePressEvent(QGraphicsSceneMouseEvent*);
113-
void mouseMoveEvent(QGraphicsSceneMouseEvent*);
114-
void mouseReleaseEvent(QGraphicsSceneMouseEvent*);
105+
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent*) override;
106+
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent*) override;
107+
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override;
108+
virtual void mousePressEvent(QGraphicsSceneMouseEvent*) override;
109+
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent*) override;
110+
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent*) override;
115111
};
116112

117113
#endif // MAPPIXMAPITEM_H

include/ui/movablerect.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class MovableRect : public QGraphicsRectItem
1111
{
1212
public:
13-
MovableRect(bool *enabled, const QRectF &rect, const QRgb &color);
13+
MovableRect(const QRectF &rect, const QRgb &color);
1414
QRectF boundingRect() const override {
1515
qreal penWidth = 4;
1616
return QRectF(-penWidth,
@@ -29,12 +29,7 @@ class MovableRect : public QGraphicsRectItem
2929
}
3030
void updateLocation(int x, int y);
3131

32-
void setActive(bool active);
33-
bool getActive() const { return this->active; }
34-
3532
protected:
36-
bool *enabled = nullptr;
37-
bool active = true;
3833
QRectF baseRect;
3934
QRgb color;
4035

@@ -48,7 +43,7 @@ class ResizableRect : public QObject, public MovableRect
4843
{
4944
Q_OBJECT
5045
public:
51-
ResizableRect(QObject *parent, bool *enabled, int width, int height, QRgb color);
46+
ResizableRect(QObject *parent, int width, int height, QRgb color);
5247

5348
QRectF boundingRect() const override {
5449
return QRectF(this->rect() + QMargins(lineWidth, lineWidth, lineWidth, lineWidth));

src/core/maplayout.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ bool Layout::isWithinBounds(int x, int y) const {
5555
return (x >= 0 && x < this->getWidth() && y >= 0 && y < this->getHeight());
5656
}
5757

58+
bool Layout::isWithinBounds(const QPoint &pos) const {
59+
return isWithinBounds(pos.x(), pos.y());
60+
}
61+
5862
bool Layout::isWithinBounds(const QRect &rect) const {
5963
return rect.left() >= 0 && rect.right() < this->getWidth() && rect.top() >= 0 && rect.bottom() < this->getHeight();
6064
}

src/core/metatile.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ int Metatile::getIndexInTileset(int metatileId) {
3838
QPoint Metatile::coordFromPixmapCoord(const QPointF &pixelCoord) {
3939
int x = static_cast<int>(pixelCoord.x()) / 16;
4040
int y = static_cast<int>(pixelCoord.y()) / 16;
41+
if (pixelCoord.x() < 0) x--;
42+
if (pixelCoord.y() < 0) y--;
4143
return QPoint(x, y);
4244
}
4345

0 commit comments

Comments
 (0)