-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitlescreen.cpp
223 lines (207 loc) · 7.1 KB
/
titlescreen.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
211
212
213
214
215
216
217
218
219
220
221
222
223
// #include <cassert>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#include <windows.h>
#endif
#include <QAbstractAnimation>
#include <QApplication>
#include <QDebug>
#include <QDesktopServices>
#include <QFont>
#include <QFontDatabase>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QMessageBox>
#include <QObject>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QScreen>
#include <QSize>
#include <QSound>
#include <QTimer>
#include <QUrl>
#include "aboutscreen.h"
#include "titlescreen.h"
#include "ui_titlescreen.h"
#include "uiconfig.h"
TitleScreen::TitleScreen(QWidget *parent)
: QWidget(parent),
ui(new Ui::TitleScreen),
m_opacityEffect(nullptr),
m_opacityAnimation(nullptr),
m_timer(nullptr),
m_player(nullptr),
m_volumeAnimation(nullptr),
m_aboutScreen(nullptr),
m_connectScreen(nullptr),
m_exitScreen(nullptr) {
ui->setupUi(this);
initUI();
// 启动游戏按钮
connect(ui->pushButton_1, &QPushButton::clicked, this,
&TitleScreen::jumpToConnectScreen);
// 打开百科按钮
connect(ui->pushButton_2, &QPushButton::clicked, this,
&TitleScreen::openZZZWiki);
// 关于按钮
connect(ui->pushButton_3, &QPushButton::clicked, this,
&TitleScreen::jumpToAboutScreen);
// 退出按钮
connect(ui->pushButton_4, &QPushButton::clicked, this,
&TitleScreen::jumpToExitScreen);
// 按钮透明度动画
btnsOpacityEffect();
}
TitleScreen::~TitleScreen() {
delete ui;
delete m_player;
delete m_volumeAnimation;
}
void TitleScreen::btnsOpacityEffect() {
m_opacityEffect = new QGraphicsOpacityEffect(this);
m_opacityAnimation = new QPropertyAnimation(m_opacityEffect, "opacity");
m_opacityAnimation->setDuration(1000);
m_opacityAnimation->setStartValue(0);
m_opacityAnimation->setEndValue(1);
ui->layoutWidget->setGraphicsEffect(m_opacityEffect);
m_timer = new QTimer(this);
// 按钮透明度动画结束后,开始计时器
connect(m_opacityAnimation, &QPropertyAnimation::finished, this,
&TitleScreen::playThemeMusic);
// 标题延时显示
connect(m_timer, &QTimer::timeout, this, &TitleScreen::updateLabel);
m_opacityAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}
void TitleScreen::updateLabel() {
static int count = 0;
static QString title = "欢迎,「法厄同」";
if (count < title.size()) {
QString text = ui->label->text().insert(count, title[count]);
ui->label->setText(text);
count++;
} else {
QObject::disconnect(m_timer, &QTimer::timeout, this,
&TitleScreen::updateLabel);
delete m_timer;
m_timer = nullptr;
}
}
void TitleScreen::playThemeMusic() {
disconnect(m_opacityAnimation, &QPropertyAnimation::finished, this,
&TitleScreen::playThemeMusic);
delete m_opacityEffect;
m_opacityEffect = nullptr;
m_opacityAnimation = nullptr;
ui->label->setText("_");
m_timer->start(500);
m_player = new QMediaPlayer;
m_player->setMedia(QUrl("qrc:/res/zzzExplorer.mp3"));
m_volumeAnimation = new QPropertyAnimation(m_player, "volume");
// 音量降为0后,重新播放
connect(m_volumeAnimation, &QPropertyAnimation::finished, this,
&TitleScreen::rePlayThemeMusic);
// 音量渐变
connect(m_player, &QMediaPlayer::stateChanged, this,
&TitleScreen::louderThemeMusic);
m_player->play();
}
void TitleScreen::jumpToAboutScreen() {
if (m_connectScreen) {
m_connectScreen->hide();
delete m_connectScreen;
m_connectScreen = nullptr;
}
if (m_exitScreen) {
m_exitScreen->hide();
delete m_exitScreen;
m_exitScreen = nullptr;
}
if (!m_aboutScreen) {
m_aboutScreen = new AboutScreen(this);
}
m_aboutScreen->show();
}
void TitleScreen::jumpToConnectScreen() {
if (m_aboutScreen) {
m_aboutScreen->hide();
delete m_aboutScreen;
m_aboutScreen = nullptr;
}
if (m_exitScreen) {
m_exitScreen->hide();
delete m_exitScreen;
m_exitScreen = nullptr;
}
if (!m_connectScreen) {
m_connectScreen = new ConnectScreen(this);
}
m_connectScreen->show();
QTimer::singleShot(5000, this, [this]() {
m_connectScreen->processConnect();
m_connectScreen->hide();
});
}
void TitleScreen::jumpToExitScreen() {
if (m_aboutScreen) {
m_aboutScreen->hide();
delete m_aboutScreen;
m_aboutScreen = nullptr;
}
if (m_connectScreen) {
m_connectScreen->hide();
delete m_connectScreen;
m_connectScreen = nullptr;
}
if (!m_exitScreen) {
m_exitScreen = new ExitScreen(this);
}
m_exitScreen->show();
}
void TitleScreen::initUI() {
// 根据父窗口大小调整控件大小
QSize screenSize = CustomWidget::adjustSize(this);
ui->frame->resize(screenSize);
QSize btnSize(screenSize.width() / 5, screenSize.height() / 25);
// 设置圆角
int btnRadius = btnSize.height() / 2;
QString btnStyle = QString("border-radius: %1px;").arg(btnRadius);
// 设置按钮字体
QString fontName = ZZZFont::get();
QFont btnFont(fontName, btnSize.height() / 3);
// 设置按钮样式
CustomBtn::setCustomBtn(ui->pushButton_1, btnSize, btnStyle, btnFont);
CustomBtn::setCustomBtn(ui->pushButton_2, btnSize, btnStyle, btnFont);
CustomBtn::setCustomBtn(ui->pushButton_3, btnSize, btnStyle, btnFont);
CustomBtn::setCustomBtn(ui->pushButton_4, btnSize, btnStyle, btnFont);
// 四个按钮放在屏幕左下角
ui->layoutWidget->setGeometry(btnSize.width() * 0.3,
screenSize.height() - btnSize.height() * 12,
btnSize.width(), btnSize.height() * 6);
// 设置标题字体
QFont titleFont(fontName, screenSize.height() / 15);
ui->label->setFont(titleFont);
// 标题位置
ui->label->setGeometry(screenSize.width() * 0.1, screenSize.height() * 0.05,
screenSize.width() * 0.8, screenSize.height() / 5);
}
void TitleScreen::openZZZWiki() {
QDesktopServices::openUrl(QUrl("https://baike.mihoyo.com/zzz/wiki"));
}
void TitleScreen::rePlayThemeMusic() {
if (m_player->state() == QMediaPlayer::StoppedState) {
m_player->play();
}
}
void TitleScreen::louderThemeMusic() {
// 开始播放后,音量渐趋上升
m_volumeAnimation->setDuration(5000);
m_volumeAnimation->setStartValue(0);
m_volumeAnimation->setEndValue(30);
m_volumeAnimation->start();
// 将要结束时,音量渐趋下降
QTimer::singleShot(48000, this, &TitleScreen::quieterThemeMusic);
}
void TitleScreen::quieterThemeMusic() {
m_volumeAnimation->setStartValue(30);
m_volumeAnimation->setEndValue(0);
m_volumeAnimation->start();
}