-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcards.cpp
More file actions
178 lines (141 loc) · 6 KB
/
Copy pathcards.cpp
File metadata and controls
178 lines (141 loc) · 6 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "cards.h"
#include "cardimgfactory.h"
#include "definitions.h"
// Base Class ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
Card::Card() :
clicked_(false),
rect_(0,0,0,0),
img_(new QPixmap),
card_border_(0,0,0),
type_(cardType::Default) {}
// Overridden methods from inherited QGraphicsObject /////////////////////////////////////////
QRectF Card::boundingRect() const {
return QRectF(rect_);
}
QPainterPath Card::shape() const {
QPainterPath path;
QRectF gameRec(rect_);
path.addRect(gameRec);
return path;
}
void Card::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
Q_UNUSED(widget);
StandardPaint(painter, option, HAND_WIDTH, HAND_HEIGHT);
}
void Card::mousePressEvent(QGraphicsSceneMouseEvent *e) {
QGraphicsItem::mousePressEvent(e);
emit cardClicked(this);
qDebug() << "default";
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/* set_rect is used to change the position and potentially the size of the Card object.
* prepareGeometryChange() prepares the scene for this change and calls update if necessary.
* Param rect: QRect object
*/
void Card::set_rect(QRect rect) {
prepareGeometryChange();
rect_.setRect(rect.x(), rect.y(), rect.width(), rect.height());
}
/* set_border_color is a setter for the border color of the Card */
void Card::set_border_color(QColor new_color) {
card_border_ = new_color;
this->update();
}
/* StandardPaint is a method that is called when a typical Card is being painted. Using the parameter QPainter,
* the method adds an image in the form of a QPixmap object and a border around this image. Several render hints are
* set to ensure the best possible image and border.
*/
void Card::StandardPaint(QPainter *painter, const QStyleOptionGraphicsItem *option, int width, int height) {
*img_ = img_->scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QRect border_rect(rect_.x() - 1, rect_.y() - 1, rect_.width() + 2, rect_.height() + 2);
if (option->state) {
QPen rect_pen = QPen(option->palette.windowText(), 1.3, Qt::SolidLine);
rect_pen.setColor(card_border_);
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->drawPixmap(rect_, *img_);
painter->setPen(rect_pen);
painter->setRenderHint(QPainter::HighQualityAntialiasing);
painter->drawRoundedRect(border_rect, 3, 3);
}
}
// Remedy Subclass ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
RemedyCard::RemedyCard(remedies rem_card) :
rem_(rem_card) {
type_ = cardType::Remedy;
rect_ = QRect(0,0,0,0);
img_ = CardImgFactory::get_image(rem_card);
}
void RemedyCard::mousePressEvent(QGraphicsSceneMouseEvent *e) {
QGraphicsItem::mousePressEvent(e);
emit cardClicked(this);
qDebug() << "remedy";
}
// Hazard Subclass///////////////////////////////////////////////////////////////////////////////////////////////////////////////
HazardCard::HazardCard(hazards haz_card) :
haz_(haz_card) {
type_ = cardType::Hazard;
rect_ = QRect(0,0,0,0);
img_ = CardImgFactory::get_image(haz_card);
}
void HazardCard::mousePressEvent(QGraphicsSceneMouseEvent *e) {
QGraphicsItem::mousePressEvent(e);
emit cardClicked(this);
qDebug() << "hazard";
}
// Safety Subclass///////////////////////////////////////////////////////////////////////////////////////////////////////////////
SafetyCard::SafetyCard(safeties safe_card) :
safety_(safe_card),
coup_fourre_(false),
play_(false) {
type_ = cardType::Safety;
rect_ = QRect(0,0,0,0);
img_ = CardImgFactory::get_image(safe_card);
}
/* SafetyCard has a special paint method that displays the card differently based on some current data members
* attributes. If coupe_fourre_ is true the image is displayed on its side and small. If play_ it is just displayed
* small, finally it is displayed traditionally if neither of these are true.
*/
void SafetyCard::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
Q_UNUSED(widget);
if (coup_fourre_) {
*img_ = img_->scaled(SAFETY_WIDTH, SAFETY_HEIGHT, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
QRect border_rect(rect_.x() - 1, rect_.y() - 1, rect_.width() + 2, rect_.height() + 2);
if (option->state) {
QPen rect_pen = QPen(option->palette.windowText(), 1.3, Qt::SolidLine);
rect_pen.setColor(card_border_);
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->translate(rect_.x() + img_->size().width()/2, rect_.y() + img_->size().height()/2);
painter->rotate(-90);
painter->translate(-rect_.x() -img_->size().width()/2, -rect_.y() -img_->size().height()/2);
painter->drawPixmap(rect_, *img_);
painter->setPen(rect_pen);
painter->setRenderHint(QPainter::HighQualityAntialiasing);
painter->drawRoundedRect(border_rect, 3, 3);
}
}
else if (play_) {
StandardPaint(painter, option, SAFETY_WIDTH, SAFETY_HEIGHT);
}
else {
Card::paint(painter, option, widget);
}
}
void SafetyCard::mousePressEvent(QGraphicsSceneMouseEvent *e) {
QGraphicsItem::mousePressEvent(e);
emit cardClicked(this);
qDebug() << "safety";
}
// Distance Subclass/////////////////////////////////////////////////////////////////////////////////////////////////////////////
DistanceCard::DistanceCard(distances dist_card) :
dist_(dist_card) {
type_ = cardType::Distance;
rect_ = QRect(0,0,0,0);
img_ = CardImgFactory::get_image(dist_card);
int poss_dist[] = { DIST_1, DIST_2, DIST_3, DIST_4, DIST_5 };
value_ = poss_dist[static_cast<int>(dist_card)];
}
void DistanceCard::mousePressEvent(QGraphicsSceneMouseEvent *e) {
QGraphicsItem::mousePressEvent(e);
emit cardClicked(this);
qDebug() << "distance";
}