Repository navigation
Expand file tree
/
Copy pathmenu.cpp
More file actions
213 lines (167 loc) · 5.99 KB
/
Copy pathmenu.cpp
File metadata and controls
213 lines (167 loc) · 5.99 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
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
#include "menu.h"
#include "cppsuckdef.h"
#include <iostream>
static inline void nothing(Menu *m) noexcept {}
/*************************************************************************************************
* *
* Menu input commands *
* *
*************************************************************************************************/
std::array<std::function<void(Menu *)>, InputAction::IA_NUM> Menu::commands;
void Menu::up() noexcept
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " cursor before: " << cursor << "\n";
#endif
items_text[cursor].Color = al_map_rgb_f(1.f, 1.f, 1.f);
cursor = cursor == 0 ? 0 : cursor - 1;
/* Highlight the cursor */
items_text[cursor].Color = al_map_rgb_f(0.f, 1.f, 0.f);
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " cursor after: " << cursor << "\n";
#endif
}
void Menu::down() noexcept
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " cursor before: " << cursor << "\n";
#endif
items[cursor].TR->Color = al_map_rgb_f(1.f, 1.f, 1.f);
cursor = cursor == btn_n - 1 ? cursor : cursor + 1;
/* Highlight the cursor */
items[cursor].TR->Color = al_map_rgb_f(0.f, 1.f, 0.f);
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " cursor after: " << cursor << "\n";
#endif
}
void Menu::confirm()
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " confirmed: \"" << items_text[cursor].Text.Text << "\"\n";
#endif
/* Highlight the confirm */
items_text[cursor].Color = al_map_rgb_f(1.f, 0.f, 0.f);
items[cursor].F(this);
}
/*************************************************************************************************
* *
* Menu item functions *
* *
*************************************************************************************************/
std::unordered_map<std::string, std::function<void(Menu *)>> Menu::menu_cmds;
void Menu::test_start() const
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " link start executed!\n";
#endif
GameCon->LinkStart();
}
void Menu::test_end() const
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " link end executed!\n";
#endif
GameCon->LinkEnd();
}
void Menu::stg_resume() const
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " STG resume executed!\n";
#endif
GameCon->STGResume();
}
void Menu::stg_return() const
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " STG return executed!\n";
#endif
GameCon->STGReturn(false);
}
void Menu::InitMenuStaff()
{
commands.fill(std::function<void(Menu *)>(nothing));
commands[InputAction::IA_MOVE_UP] = std::mem_fn(&Menu::up);
commands[InputAction::IA_MOVE_DOWN] = std::mem_fn(&Menu::down);
commands[InputAction::IA_ACTION] = std::mem_fn(&Menu::confirm);
menu_cmds["start"] = std::mem_fn(&Menu::test_start);
menu_cmds["quit"] = std::mem_fn(&Menu::test_end);
menu_cmds["stg_resume"] = std::mem_fn(&Menu::stg_resume);
menu_cmds["return_title"] = std::mem_fn(&Menu::stg_return);
}
/*************************************************************************************************
* *
* Initialize / Destroy Function *
* *
*************************************************************************************************/
Menu::Menu() : item_n(0), cursor(0), GameCon(nullptr)
{
Recv = al_create_event_queue();
if (!Recv)
{
std::cerr << "Failed to initialize Menu's event queue!\n";
std::abort();
}
}
Menu::~Menu()
{
al_destroy_event_queue(Recv);
}
void Menu::Setup(const std::vector<TextItem> &its, const std::vector<std::string> &funcs,
const std::string &code_name)
{
#ifdef _DEBUG
std::cout << "Menu-" << code_name << " setup.\n";
#endif
CodeName = code_name;
item_n = 0;
btn_n = 0;
for (auto &&e : its)
{
items_text[item_n].Text = e;
if (btn_n < funcs.size())
{
items[btn_n].TR = items_text + item_n;
items[btn_n].F = menu_cmds.at(funcs[btn_n]);
btn_n += 1;
}
item_n += 1;
}
al_identity_transform(&T);
}
void Menu::Attach() noexcept
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " attached!\n";
#endif
cursor = 0;
items[cursor].TR->Color = al_map_rgb_f(0.f, 1.f, 0.f);
}
void Menu::Detach() noexcept
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " deattached!\n";
#endif
items[cursor].TR->Color = al_map_rgb_f(1.f, 1.f, 1.f);
}
/*************************************************************************************************
* *
* Update Function *
* *
*************************************************************************************************/
void Menu::Update()
{
ALLEGRO_EVENT event;
while (al_get_next_event(Recv, &event))
{
#ifdef _DEBUG
std::cout << "Menu-" << CodeName << " recv input action: " << event.user.data1 << "\n";
#endif
commands[event.user.data1](this);
}
}
void Menu::Render(float forward_time)
{
al_use_transform(&T);
for (int i = 0; i < item_n; i++)
items_text[i].Draw();
}