-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.cpp
134 lines (109 loc) · 2.82 KB
/
Item.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "Item.h"
#include "Consts.h"
#include "Board.h"
#include <QtWidgets/QGraphicsSceneEvent>
Item::Item(EventListener* listener, const std::string& path, int row, int column, QGraphicsItem* parent)
: QGraphicsPixmapItem(QPixmap(path.c_str()).scaled(Consts::BOARD_ITEM_SIZE, Consts::BOARD_ITEM_SIZE), parent)
, _listener(listener)
, _path(path)
, _row(row)
, _column(column)
{
}
void Item::moveTo(Item& other, bool canRevert)
{
auto tuple = moveToInternal(other.x(), other.y());
auto timer = std::get<0>(tuple);
auto animation = std::get<1>(tuple);
connect(timer, &QTimeLine::finished, [this, timer, animation, canRevert, other = &other]() {
delete timer;
delete animation;
_listener->itemMoveFinished(this, other, canRevert);
});
}
void Item::moveTo(int toX, int toY)
{
auto tuple = moveToInternal(toX, toY);
auto timer = std::get<0>(tuple);
auto animation = std::get<1>(tuple);
connect(timer, &QTimeLine::finished, [this, timer, animation]() {
delete timer;
delete animation;
_listener->itemMoveFinished(this, nullptr, false);
});
}
auto Item::moveToInternal(int toX, int toY) -> std::tuple<QTimeLine*, QGraphicsItemAnimation*>
{
double diffX = toX - x();
double diffY = toY - y();
double time = 0;
time += qAbs(diffX) * 200 / 60;
time += qAbs(diffY) * 200 / 60;
// ¾Ö´Ï¸ÞÀÌ¼Ç ¼³¸í ÇØ¾ß ÇÔ
QTimeLine* timer = new QTimeLine(time);
timer->setFrameRange(0, 100);
QGraphicsItemAnimation* animation = new QGraphicsItemAnimation;
animation->setItem(this);
animation->setTimeLine(timer);
for (int i = 0; i < 200; ++i)
{
double currX = x() + diffX * (i / 200.0);
double currY = y() + diffY * (i / 200.0);
animation->setPosAt(i / 200.0, QPointF(currX, currY));
}
timer->start();
return { timer, animation };
}
std::string Item::path() const
{
return _path;
}
int Item::row() const
{
return _row;
}
int Item::column() const
{
return _column;
}
void Item::setRow(int row)
{
_row = row;
}
void Item::setColumn(int column)
{
_column = column;
}
void Item::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
_pressPos = event->pos();
}
void Item::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
Event newEvent;
newEvent.item = this;
QPointF diff = event->pos() - _pressPos;
if (qAbs(diff.x()) > qAbs(diff.y()))
{
if (diff.x() > 0)
{
newEvent.dir = Direction::Right;
}
else
{
newEvent.dir = Direction::Left;
}
}
else
{
if (diff.y() > 0)
{
newEvent.dir = Direction::Up;
}
else
{
newEvent.dir = Direction::Down;
}
}
_listener->itemDragEvent(&newEvent);
}