-
Notifications
You must be signed in to change notification settings - Fork 1
/
achievementwidget.cpp
210 lines (175 loc) · 5.2 KB
/
achievementwidget.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "achievementwidget.h"
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include "basicpainter.h"
#include "gamecommonitems.h"
#include "achievements.h"
#define LOGICAL_WIDTH 1024
#define LOGICAL_HEIGHT 600
#define DESCRIPTION_X 500
#define DESCRIPTION_Y 50
#define DESCRIPTION_WIDTH 500
#define DESCRIPTION_HEIGHT 450
#define VIEW_X (-DESCRIPTION_X)
#define VIEW_Y (-DESCRIPTION_Y)
#define VIEW_WIDTH LOGICAL_WIDTH
#define VIEW_HEIGHT LOGICAL_HEIGHT
#define Y_FROM 100.0
#define Y_TO 500.0
AchievementWidget::AchievementWidget() :
activeAchievementIndex(0)
{
// Create the items and initialize them
exitItem = new ButtonItem("Exit");
exitItem->setPos(QPointF(0.75, 0.9));
myItems.push_back(exitItem);
QPixmap p = QPixmap(1, 1);
QPainter *tmp = new QPainter(&p);
achievementItems = Achievements::getAchievementItems(tmp);
delete tmp;
QPointF positions[5];
positions[0] = QPointF(0.15, 0.2);
positions[1] = QPointF(0.35, 0.2);
positions[2] = QPointF(0.15, 0.8);
positions[3] = QPointF(0.35, 0.8);
positions[4] = QPointF(0.25, 0.5);
for (int i = 0;i < achievementItems.size();++i)
{
myItems.push_back(achievementItems[i]);
achievementItems[i]->setPos(positions[i]);
achievementItems[i]->loseDescriptionFocus();
}
// No items was chosen
itemAtPressPos = NULL;
t = new QTimer();
t->setInterval(30);
connect(t, SIGNAL(timeout()), this, SLOT(advance()));
t->start();
}
void AchievementWidget::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 AchievementWidget::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
painter->scale(1.0 * width / LOGICAL_WIDTH,
1.0 * height / LOGICAL_HEIGHT);
BasicPainter::paintBackGround(BasicPainter::Achievement, painter, LOGICAL_WIDTH, LOGICAL_HEIGHT, 0);
// Paint the items
BasicPainter::paintItems(painter,
myItems,
LOGICAL_WIDTH,
LOGICAL_HEIGHT,
0);
painter->translate(DESCRIPTION_X, DESCRIPTION_Y);
achievementItems[activeAchievementIndex]->paintDescription(painter,
DESCRIPTION_WIDTH,
DESCRIPTION_HEIGHT,
0);
painter->translate(-DESCRIPTION_X, -DESCRIPTION_Y);
painter->scale(1.0 * width / LOGICAL_WIDTH,
1.0 * height / LOGICAL_HEIGHT);
#ifdef USE_PIXMAP
// End the paint and release the space
painter->end();
delete painter;
#endif
}
void AchievementWidget::addEffect(
#ifdef USE_PIXMAP
QPixmap& pixmap,
#else
QPainter* painter,
#endif
int width,
int height){}
QPointF AchievementWidget::toScene(double xRate, double yRate)
{
return QPointF(xRate * LOGICAL_WIDTH,
yRate * LOGICAL_HEIGHT);
}
void AchievementWidget::dealPressed(QPointF mousePos, Qt::MouseButton button)
{
// Choose the correct item at press position
if (distanceOfTwoPoints(mousePos,
toScene(exitItem->getPos().x(),
exitItem->getPos().y())) < 50)
itemAtPressPos = exitItem;
for (int i = 0;i < achievementItems.size();++i)
if (achievementItems[i]->in(mousePos, LOGICAL_WIDTH, LOGICAL_HEIGHT))
{
if (activeAchievementIndex != i)
{
achievementItems[i]->loseDescriptionFocus();
activeAchievementIndex = i;
}
break;
}
}
void AchievementWidget::dealMoved(QPointF mousePos, Qt::MouseButton button)
{
for (int i = 0;i < achievementItems.size();++i)
if (achievementItems[i]->in(mousePos, LOGICAL_WIDTH, LOGICAL_HEIGHT))
{
if (activeAchievementIndex != i)
{
achievementItems[i]->loseDescriptionFocus();
activeAchievementIndex = i;
}
break;
}
}
void AchievementWidget::dealReleased(QPointF mousePos, Qt::MouseButton button)
{
if (itemAtPressPos == exitItem &&
exitItem->in(mousePos, LOGICAL_WIDTH, LOGICAL_HEIGHT))
{
// Exit
emit giveControlTo(NULL, true);
delete this;
return;
}
itemAtPressPos = NULL;
}
void AchievementWidget::advance()
{
achievementItems[activeAchievementIndex]->advanceDescription();
}
AchievementWidget::~AchievementWidget()
{
t->stop();
delete t;
// Release the space
for (int i = 0;i < myItems.size();++i)
delete myItems[i];
}