-
Notifications
You must be signed in to change notification settings - Fork 1
/
resetwidget.cpp
138 lines (118 loc) · 3.09 KB
/
resetwidget.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
134
135
136
137
138
#include "resetwidget.h"
#include <QPainter>
#include <QPixmap>
#include "basicpainter.h"
#include "gamecommonitems.h"
#define LOGICAL_WIDTH 1024
#define LOGICAL_HEIGHT 600
ResetWidget::ResetWidget()
{
// Create the items and initialize them
confirmItem = new ButtonItem("Confirm");
confirmItem->setPos(QPointF(0.3, 0.5));
myItems.push_back(confirmItem);
cancelItem = new ButtonItem("Cancel");
cancelItem->setPos(QPointF(0.7, 0.5));
myItems.push_back(cancelItem);
// No items was chosen
itemAtPressPos = NULL;
}
void ResetWidget::makePixmap(
#ifdef USE_PIXMAP
QPixmap& pixmap,
#else
QPainter* painter,
#endif
int width,
int height)
{
#ifdef USE_PIXMAP
makeBasicPixmap(pixmap, width, height);
addEffect(pixmap, width, height);
#else
makeBasicPixmap(painter, width, height);
addEffect(painter, width, height);
#endif
}
void ResetWidget::makeBasicPixmap(
#ifdef USE_PIXMAP
QPixmap& pixmap,
#else
QPainter* painter,
#endif
int width,
int height)
{
#ifdef USE_PIXMAP
pixmap = QPixmap(width, height);
// Fill the pixmap with black background
pixmap.fill(Qt::black);
// Get the painter
QPainter *painter = new QPainter(&pixmap);
#else
painter->fillRect(0,0,width,height,QColor(0,0,0));
#endif
// Paint the items
BasicPainter::paintItems(painter,
myItems,
width,
height,
0);
#ifdef USE_PIXMAP
// End the paint and release the space
painter->end();
delete painter;
#endif
}
void ResetWidget::addEffect(
#ifdef USE_PIXMAP
QPixmap& pixmap,
#else
QPainter* painter,
#endif
int width,
int height){}
QPointF ResetWidget::toScene(double xRate, double yRate)
{
return QPointF(xRate * LOGICAL_WIDTH,
yRate * LOGICAL_HEIGHT);
}
void ResetWidget::dealPressed(QPointF mousePos, Qt::MouseButton button)
{
// Choose the correct item at press position
if (confirmItem->in(mousePos, LOGICAL_WIDTH, LOGICAL_HEIGHT))
itemAtPressPos = confirmItem;
else if (cancelItem->in(mousePos, LOGICAL_WIDTH, LOGICAL_HEIGHT))
itemAtPressPos = cancelItem;
}
void ResetWidget::dealMoved(QPointF mousePos, Qt::MouseButton button){}
void ResetWidget::dealReleased(QPointF mousePos, Qt::MouseButton button)
{
if (itemAtPressPos == confirmItem &&
confirmItem->in(mousePos, LOGICAL_WIDTH, LOGICAL_HEIGHT))
{
// Exit
emit giveControlTo(NULL, true);
// Emit correct signal
emit confirm();
delete this;
return;
}
else if (itemAtPressPos == cancelItem &&
cancelItem->in(mousePos, LOGICAL_WIDTH, LOGICAL_HEIGHT))
{
// Exit
emit giveControlTo(NULL, true);
// Emit correct signal
emit cancel();
delete this;
return;
}
itemAtPressPos = NULL;
}
ResetWidget::~ResetWidget()
{
// Release the space
for (int i = 0;i < myItems.size();++i)
delete myItems[i];
}