-
Notifications
You must be signed in to change notification settings - Fork 0
/
btn.cpp
111 lines (101 loc) · 2.6 KB
/
btn.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
#include "btn.h"
#include <QDebug>
Btn::Btn(const QString &name,
const QPointF &pos,
qreal w, qreal h,
const QPixmaps2 &pixs2,
QGraphicsScene *scene,
QGraphicsObject *parent)
: AbstractGameItem(w, h, scene, parent),
m_name(name), m_pixStateIndex(0)
{
for (int j = 0; j < pixs2.size(); ++j) {
QPixmaps tempPixs;
for (int i = 0; i < pixs2[0].size(); ++i) {
QPixmap temp(pixs2.at(j).at(i));
QPixmap t = temp.scaled(m_w, m_h, Qt::KeepAspectRatioByExpanding);
tempPixs.append(t);
}
m_pixs2.append(tempPixs);
}
setPos(pos);
}
QRectF Btn::boundingRect() const
{
return m_pixs2.at(0).at(0).rect();
}
QPainterPath Btn::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
void Btn::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->drawPixmap(0, 0, m_pixs2.
at(m_pixStateIndex).
at(m_pixIndex));
}
const QString &Btn::name() const
{
return m_name;
}
void Btn::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
event->accept();
m_pixIndex = Config::PRESSED_BTN_INDEX;
/*
if (name() == "moreFoodBtn"){
if (m_pixStateIndex < Config::MORE_FOOD_STATE_COUNT - 1){
m_pixStateIndex++;
}
}
else if (name() == "eggBtn"){
if (m_pixStateIndex < Config::EGG_STATE_COUNT - 1){
m_pixStateIndex++;
}
}
*/
update();
} else {
event->ignore();
}
}
void Btn::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
event->accept();
m_pixIndex = Config::HOVER_BTN_INDEX;
update();
emit sgn_btnReleased(m_name);
}
void Btn::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
event->accept();
m_pixIndex = Config::HOVER_BTN_INDEX;
update();
}
void Btn::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
event->accept();
m_pixIndex = Config::DEFAULT_BTN_INDEX;
update();
}
void Btn::slt_clickReceived(const QString & btnName)
{
if (name() == "moreFoodBtn"
&& btnName == "moreFoodBtn"){
if (m_pixStateIndex < Config::MORE_FOOD_STATE_COUNT - 1){
m_pixStateIndex++;
}
}
else if (name() == "eggBtn"
&& btnName == "eggBtn"){
if (m_pixStateIndex < Config::EGG_STATE_COUNT - 1){
m_pixStateIndex++;
}
}
update();
}