-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainmenu.cpp
85 lines (66 loc) · 1.8 KB
/
mainmenu.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
//
// Created by Luna Steed on 3/24/2024.
//
#include "mainmenu.h"
#include "game.h"
#include <QPushButton>
#include <iostream>
/**
* @author Luna Steed
* @time 3/24/2024
* @brief Constructor for the main menu
* @param parent The parent widget
*/
MainMenu::MainMenu(QWidget *parent) {
grid = new QGridLayout();
vbox = new QVBoxLayout();
const int padding = 200;
grid->setContentsMargins(padding, padding, padding, padding);
this->setLayout(grid);
grid->addLayout(vbox, 0, 0);
if (std::filesystem::exists("assets/images/mainmenu.png")) {
hasBackground = true;
background = QPixmap("assets/images/mainmenu.png");
}
else{
hasBackground = false;
background = QPixmap();
}
title = new QLabel();
title->setText("TANKS");
title->setAlignment(Qt::AlignCenter);
title->setStyleSheet("font-size: 50px; font-weight: bold; color: white;");
vbox->addWidget(title);
size_t id = 0;
buttons = {"Select Level", "Quit"};
for (auto& btn : buttons) {
QPushButton* qbutton = new QPushButton(QString(btn.c_str()));
vbox->addWidget(qbutton);
connect(qbutton, &QPushButton::clicked, this, [this, id = id] { buttonClicked(id); });
id++;
}
}
void MainMenu::buttonClicked(int id) {
switch (id) {
case 0:
Game::getInstance()->getWindow()->changeWidget(LEVEL_MENU_KEY);
break;
case 1:
Game::destroyInstance();
break;
default:
break;
}
}
MainMenu::~MainMenu() {
delete grid;
}
void MainMenu::paintEvent(QPaintEvent *event) {
QPainter painter(this);
if (hasBackground) {
painter.drawPixmap(0, 0, this->width(), this->height(), background);
}
else{
QWidget::paintEvent(event);
}
}