-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
126 lines (109 loc) · 4.94 KB
/
Copy pathbutton.cpp
File metadata and controls
126 lines (109 loc) · 4.94 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
#include "button.h"
#include "catalogwindow.h"
#include "mainwindow.h"
#include "startmenu.h"
Button::Button(QSoundEffect *s, QTimer *t)
{
sound = s;
timer = t;
}
QRectF Button::boundingRect() const
{
return QRectF(-80, -20, 160, 40);
}
void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
painter->drawPixmap(QRect(-80, -20, 160, 40), QPixmap(":/images/Button.png"));
painter->setPen(Qt::green);
QFont font("Calibri", 18, QFont::Bold, true);
painter->setFont(font);
if (timer->isActive())
painter->drawText(boundingRect(), Qt::AlignCenter, "MENU");
else
painter->drawText(boundingRect(), Qt::AlignCenter, "CONTINUE");
}
void Button::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
if (timer->isActive()) // 当前是 "MENU" 状态
{
sound->stop();
timer->stop();
// 创建无边框窗口
QWidget *imageWindow = new QWidget();
imageWindow->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
imageWindow->setAttribute(Qt::WA_TranslucentBackground);
// 创建 QLabel 显示背景图
QLabel *label = new QLabel(imageWindow);
QPixmap pixmap(":/images/dialog.png");
QPixmap scaledPixmap = pixmap.scaled(pixmap.width() * 0.5, pixmap.height() * 0.5, Qt::KeepAspectRatio, Qt::SmoothTransformation);
label->setPixmap(scaledPixmap);
label->setFixedSize(scaledPixmap.size());
label->move(0, 0);
// 设置窗口大小为背景图加底部空间
int windowWidth = scaledPixmap.width();
int windowHeight = scaledPixmap.height() + 60;
imageWindow->setFixedSize(windowWidth, windowHeight);
// 图鉴按钮
QPushButton *encyclopediaButton = new QPushButton("图鉴", imageWindow);
encyclopediaButton->setFixedSize(115, 40);
encyclopediaButton->move((windowWidth - 115) / 2, 140); // 图像内部
encyclopediaButton->setStyleSheet("QPushButton { "
"background-image: url(:/images/Button.png); "
"background-size: 200% 200%; "
"color: #00FF00; font: bold 18px 'Calibri'; border: none; }");
// 回到主菜单按钮
QPushButton *menuButton = new QPushButton("回到主菜单", imageWindow);
menuButton->setFixedSize(115, 40);
menuButton->move((windowWidth - 115) / 2, 220); // 图像内部偏下
menuButton->setStyleSheet("QPushButton { "
"background-image: url(:/images/Button.png); "
"background-size: 200% 200%; "
"color: #00FF00; font: bold 18px 'Calibri'; border: none; }");
// 关闭按钮
QPushButton *closeButton = new QPushButton("关闭", imageWindow);
closeButton->setFixedSize(115, 40);
closeButton->move((windowWidth - 115) / 2, scaledPixmap.height() + 10); // 图像下方
closeButton->setStyleSheet("QPushButton { "
"background-image: url(:/images/Button.png); "
"background-size: 200% 200%; "
"color: #00FF00; font: bold 18px 'Calibri'; border: none; }");
// 按钮信号槽
QObject::connect(closeButton, &QPushButton::clicked, imageWindow, [=]() {
imageWindow->close();
sound->play();
timer->start();
});
QObject::connect(encyclopediaButton, &QPushButton::clicked, imageWindow, [=]() {
imageWindow->close();
QWidget *parentWidget = this->scene()->views().first();
CatalogWindow *catalogWindow = new CatalogWindow(parentWidget);
catalogWindow->exec();
});
QObject::connect(menuButton, &QPushButton::clicked, imageWindow, [=]() {
// 关闭当前弹窗
imageWindow->close();
// 关闭主游戏窗口
QWidget *parentWidget = this->scene()->views().first();
MainWindow *mainWindow = qobject_cast<MainWindow*>(parentWidget->parentWidget());
if (mainWindow) {
mainWindow->hide();
}
// 创建新的开始菜单并显示
StartMenu *startMenu = new StartMenu();
startMenu->show();
});
// 显示窗口
imageWindow->show();
}
else // 当前是 "CONTINUE" 状态
{
sound->play();
timer->start();
}
}
update();
}