-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenu.cpp
More file actions
154 lines (130 loc) · 3.71 KB
/
Menu.cpp
File metadata and controls
154 lines (130 loc) · 3.71 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
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
#include "Menu.h"
#include "MenuMain.h"
#include "MenuPause.h"
#include "MenuHighScores.h"
#include "MenuNewHighScore.h"
namespace typing
{
// Singleton implementation
std::auto_ptr<Menu> Menu::m_singleton(new Menu);
Menu& Menu::GetMenu ()
{
return *(m_singleton.get());
}
// Main menu stuff
bool Menu::Init ()
{
// Register menus with the menu manager
RegisterMenu<MainMenu>(MainMenu::MENU_NAME);
RegisterMenu<PauseMenu>(PauseMenu::MENU_NAME);
RegisterMenu<EndGameConfirmMenu>(EndGameConfirmMenu::MENU_NAME);
RegisterMenu<QuitConfirmMenu>(QuitConfirmMenu::MENU_NAME);
RegisterMenu<HighScoresMenu>(HighScoresMenu::MENU_NAME);
RegisterMenu<NewHighScoreMenu>(NewHighScoreMenu::MENU_NAME);
// Push the first menu onto the stack
NextMenu(MainMenu::MENU_NAME);
m_active = true;
return true;
}
void Menu::ClearMenuStack()
{
while (!m_menuStack.empty())
{
m_menuStack.pop();
}
}
void Menu::Update ()
{
if (IsActive() && !m_menuStack.empty())
{
MenuScreenPtr& menu = m_menuStack.top();
MenuScreen::NextAction act = menu->Update();
HandleAction(act);
}
}
void Menu::Draw ()
{
if (IsActive() && !m_menuStack.empty())
{
MenuScreenPtr& menu = m_menuStack.top();
if (menu)
{
menu->Draw();
}
}
}
template<typename menuType> void Menu::RegisterMenu (const std::string& menuName)
{
if (m_menuFactory.find(menuName) == m_menuFactory.end())
{
m_menuFactory[menuName] = CreateMenu<menuType>;
}
}
void Menu::OnKeyDown(SDL_Keycode keycode)
{
if (IsActive()) {
MenuScreenPtr& menu = m_menuStack.top();
MenuScreen::NextAction act = menu->OnKeyDown(keycode);
HandleAction(act);
}
}
void Menu::OnType(char c)
{
if (IsActive()) {
MenuScreenPtr& menu = m_menuStack.top();
menu->OnType(c);
}
}
void Menu::HandleAction(MenuScreen::NextAction act)
{
switch (act)
{
case MenuScreen::ACTION_PREV:
if (m_menuStack.size() > 1)
{
m_menuStack.pop();
}
break;
case MenuScreen::ACTION_NEXT:
if (!m_menuStack.empty())
{
NextMenu(m_menuStack.top()->NextMenu());
}
break;
case MenuScreen::ACTION_NEXT_AND_POP:
if (!m_menuStack.empty())
{
// Move to the next menu, but don't keep the current menu on the stack.
const std::string nextMenu = m_menuStack.top()->NextMenu();
m_menuStack.pop();
NextMenu(nextMenu);
}
break;
case MenuScreen::ACTION_GAME:
// We've gone to the game now, so make the menu inactive
ClearMenuStack();
m_active = false;
break;
case MenuScreen::ACTION_NONE:
default:
break;
}
}
void Menu::NextMenu (const std::string& menuName)
{
MenuFactoryIter iterMenu = m_menuFactory.find(menuName);
if (iterMenu != m_menuFactory.end())
{
MenuCreator creator = iterMenu->second;
MenuScreenPtr menu = creator();
menu->Init();
m_menuStack.push(menu);
}
}
void Menu::Activate(const std::string& menuName)
{
ClearMenuStack();
NextMenu(menuName);
m_active = true;
}
}