-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamewindow.cpp
129 lines (116 loc) · 3.15 KB
/
gamewindow.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
///
/// \brief LS S24 The GameWindow class governs the main window that displays the game and all menus.
///
#include "gamewindow.h"
/**
* @author Luna Steed
* @time Spring 2024
* @brief GameWindow: Constructor
* @details Constructor for the GameWindow class. Initializes the MenuManager, QWindow, Renderer, and widgetCache.
*/
GameWindow::GameWindow(QObject *parent, int startKey)
{
// Set up the widget cache
rend = new Renderer();
mainMenu = new MainMenu();
pauseMenu = new PauseMenu();
gameOver = new GameOver();
levelMenu = new LevelMenu();
widgets = {
{GAME_KEY, rend},
{MAIN_MENU_KEY, mainMenu},
{PAUSE_MENU_KEY, pauseMenu},
{GAME_OVER_KEY, gameOver},
{LEVEL_MENU_KEY, levelMenu}
};
activeKey = startKey;
changeWidget(activeKey);
this->create();
}
/**
* @author Luna Steed
* @time Spring 2024
* @brief changeWidget: Changes the active QWidget
* @details This method changes the active QWidget. It hides the current widget and displays the new widget.
* @return QWidget* The active widget
* @param key
*/
QWidget* GameWindow::changeWidget(int key)
{
if (key == activeKey) { // Don't change if already active
displayWidget();
return widgets.at(activeKey);
}
else {
hideWidget(); // Hide current widget
activeKey = key;
displayWidget(); // Display new widget
return widgets.at(activeKey);
}
}
/**
* @author Luna Steed
* @time Spring 2024
* @brief displayWidget: Displays the active QWidget
*/
void GameWindow::displayWidget()
{
try {
QWidget* wpoint = widgets.at(activeKey);
this->setCentralWidget(wpoint);
wpoint->setParent(this);
}
catch(std::exception& e) {
std::cerr << "Error encountered in GameWindow: " << e.what() << std::endl;
throw e;
}
}
/**
* @author Luna Steed
* @time Spring 2024
* @brief hideWidget: Hide the active widget
*/
void GameWindow::hideWidget()
{
try {
QWidget *wpoint = widgets.at(activeKey);
wpoint->setParent(nullptr);
this->setCentralWidget(nullptr);
}
catch(std::exception& e) {
std::cerr << "Error encountered in GameWindow: " << e.what() << std::endl;
throw e;
}
}
// Public Q Slots: keyPressEvent(QKeyEvent *event), keyReleaseEvent(QKeyEvent *event)
/**
* @author Luna Steed
* @time Spring 2024
* @brief GameWindow::keyPressEvent: Handle key press events
* @details Handle key press events by passing them to the game window.
* @param event The key press event
*/
void GameWindow::keyPressEvent(QKeyEvent *event)
{
emit keySignal(event);
}
/**
* @author Luna Steed
* @time Spring 2024
* @brief GameWindow::keyReleaseEvent: Handle key release events
* @details Handle key release events by passing them on to
* @param event The key release event
*/
void GameWindow::keyReleaseEvent(QKeyEvent *event)
{
emit keySignal(event);
}
/**
* @author Tyson Cox
* @date Spring 2024
* @param key The key of the widget to fetch
* @return the QWidget* of the menu that the key represents
*/
QWidget* GameWindow::getWidget(int key) {
return widgets.at(key);
}