-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepiece.cpp
More file actions
98 lines (84 loc) · 1.68 KB
/
gamepiece.cpp
File metadata and controls
98 lines (84 loc) · 1.68 KB
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
#include "gamepiece.h"
#include <QMouseEvent>
/**
* Author: Yunus Rahbar
* Date: 6/10/13
*
*/
/**
* GamePiece
* Game Piece displayed on the board
*
*/
GamePiece::GamePiece(QString imgName, quint16 pieceID, QWidget *parent) :
QLabel(parent), pid(pieceID), pixName(imgName)
{
this->setPixmap(QPixmap(imgName));
}
GamePiece::GamePiece(QByteArray serial, QWidget *parent)
: QLabel(parent)
{
QDataStream in(&serial,QIODevice::ReadOnly);
QPoint tempPos;
in >> pid >> tempPos;
this->move(tempPos);
}
QString GamePiece::imageName()
{
return pixName;
}
/**
* Serialize this piece for saving to a file
*
*/
QByteArray GamePiece::serialize()
{
QByteArray block;
QDataStream out(&block,QIODevice::WriteOnly);
out << pid;
out << this->pos();
return block;
}
/**
* Get the id of this piece
*
*/
quint16 GamePiece::pieceID()
{
return pid;
}
/**
* Resize the game piece to the given width
*
*/
void GamePiece::setPixScale(int size)
{
this->setPixmap(QPixmap(pixName).scaledToWidth(size,Qt::SmoothTransformation));
}
/**
* On drag, move this game piece to where the mouse cursor is
*
*/
void GamePiece::mouseMoveEvent(QMouseEvent *ev)
{
this->move(ev->globalPos()-delta);
}
/**
* On left click, set this up for dragging.
* On right click, signal the server to delete this piece.
*/
void GamePiece::mousePressEvent(QMouseEvent *ev)
{
if (ev->button()==Qt::RightButton) {
emit deleted(pid);
}
else delta = ev->globalPos()-this->pos();
}
/**
* On mouse release, signal the server to move this piece
*
*/
void GamePiece::mouseReleaseEvent(QMouseEvent *ev)
{
emit moved(pid,this->pos());
}