-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuAPI.cpp
More file actions
276 lines (247 loc) · 6.54 KB
/
MenuAPI.cpp
File metadata and controls
276 lines (247 loc) · 6.54 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/** @file MenuAPI.cpp */
#include "LuaContext.h"
#include "Surface.h"
#include "lua.hpp"
#include <list>
const std::string LuaContext::menu_module_name = "kq.menu";
void LuaContext::register_menu_module()
{
static const luaL_Reg functions[] =
{
{ "start", menu_api_start },/*
{ "stop", menu_api_stop },
{ "stop_all", menu_api_stop_all },*/
{ NULL, NULL }
};
register_functions(menu_module_name, functions);
}
/**
* @brief Registers a menu into a context (table or a userdata).
*
* This function can be called safely even while iterating on the menus list.
*
* @param menu_ref Lua ref of the menu to add.
* @param context_index Index of the table or userdata in the stack.
*/
void LuaContext::add_menu(int menu_ref, int context_index)
{
const void* context;
if (lua_type(l, context_index) == LUA_TUSERDATA)
{
ExportableToLua** userdata = static_cast<ExportableToLua**>(lua_touserdata(l, context_index));
context = *userdata;
}
else
{
context = lua_topointer(l, context_index);
}
menus.push_back(LuaMenuData(menu_ref, context));
menu_on_started(menu_ref);
}
/**
* \brief Unregisters all menus associated to a context.
*
* This function can be called safely even while iterating on the menus list.
*
* \param context_index Index of a table or userdata containing menus.
*/
void LuaContext::remove_menus(int context_index)
{
const void* context;
if (lua_type(l, context_index) == LUA_TUSERDATA)
{
ExportableToLua** userdata = static_cast<ExportableToLua**>(lua_touserdata(l, context_index));
context = *userdata;
}
else
{
context = lua_topointer(l, context_index);
}
std::list<LuaMenuData>::iterator it;
for (it = menus.begin(); it != menus.end(); ++it)
{
int menu_ref = it->ref;
if (it->context == context)
{
menu_on_finished(menu_ref);
destroy_ref(menu_ref);
it->ref = LUA_REFNIL;
it->context = NULL;
}
}
}
/**
* \brief Unregisters all existing menus.
*
* This function can be called safely even while iterating on the menus list.
*/
void LuaContext::remove_menus()
{
std::list<LuaMenuData>::iterator it;
for (it = menus.begin(); it != menus.end(); ++it)
{
int menu_ref = it->ref;
if (menu_ref != LUA_REFNIL)
{
menu_on_finished(menu_ref);
destroy_ref(menu_ref);
it->ref = LUA_REFNIL;
it->context = NULL;
}
}
}
/**
* \brief Destroys immediately all existing menus.
*/
void LuaContext::destroy_menus()
{
std::list<LuaMenuData>::iterator it;
for (it = menus.begin(); it != menus.end(); ++it)
{
int menu_ref = it->ref;
if (menu_ref != LUA_REFNIL)
{
destroy_ref(menu_ref);
}
}
menus.clear();
}
/**
* @brief Checks all menus and removes the ones that have to be removed.
*
* Note that the on_update() is called by the context of each menu, not
* by this function.
*/
void LuaContext::update_menus()
{
// Destroy the ones that should be removed.
std::list<LuaMenuData>::iterator it;
for (it = menus.begin(); it != menus.end(); ++it) {
if (it->ref == LUA_REFNIL)
{
// LUA_REFNIL on a menu means that we should remove it.
// In this case, context must also be NULL.
//Debug::check_assertion(it->context == NULL, "Menu with context and no ref");
menus.erase(it--);
}
}
}
/**
* @brief Implementation of kq.menu.start().
* @param l The Lua context that is calling this function.
* @return Number of values to return to Lua.
*/
int LuaContext::menu_api_start(lua_State *l) {
// Parameters: context table.
if (lua_type(l, 1) != LUA_TTABLE
&& lua_type(l, 1) != LUA_TUSERDATA) {
luaL_typerror(l, 1, "table or userdata");
}
luaL_checktype(l, 2, LUA_TTABLE);
lua_settop(l, 2);
LuaContext& lua_context = get_lua_context(l);
int menu_ref = lua_context.create_ref();
lua_context.add_menu(menu_ref, 1);
return 0;
}
void LuaContext::menu_on_started(int menu_ref)
{
push_ref(l, menu_ref);
on_started();
lua_pop(l, 1);
}
/**
* \brief Calls the on_finished() method of a Lua menu.
* \param menu_ref A reference to the menu object.
*/
void LuaContext::menu_on_finished(int menu_ref)
{
push_ref(l, menu_ref);
on_finished();
remove_timers(-1); // Stop timers associated to this menu.
lua_pop(l, 1);
}
/**
* @brief Calls the on_draw() method of a Lua menu.
* @param menu_ref A reference to the menu object.
* @param dst_surface The destination surface.
*/
void LuaContext::menu_on_draw(int menu_ref, Surface& dst_surface)
{
push_ref(l, menu_ref);
on_draw(dst_surface);
lua_pop(l, 1);
}
/**
* @brief Calls an input callback method of a Lua menu.
* @param menu_ref A reference to the menu object.
* @param event The input event to forward.
* @return \c true if the event was handled and should stop being propagated.
*/
bool LuaContext::menu_on_input(int menu_ref, InputEvent& event)
{
// Get the Lua menu.
push_ref(l, menu_ref);
// Trigger its appropriate callback if it exists.
bool handled = on_input(event);
// Remove the menu from the stack.
lua_pop(l, 1);
return handled;
}
/**
* @brief Calls the on_draw() method of the menus associated to a context.
* @param context_index Index of an object with menus.
* @param dst_surface The destination surface to draw.
*/
void LuaContext::menus_on_draw(int context_index, Surface& dst_surface)
{
const void* context;
if (lua_type(l, context_index) == LUA_TUSERDATA)
{
ExportableToLua** userdata = static_cast<ExportableToLua**>(lua_touserdata(l, context_index));
context = *userdata;
}
else
{
context = lua_topointer(l, context_index);
}
std::list<LuaMenuData>::iterator it;
for (it = menus.begin(); it != menus.end(); ++it)
{
int menu_ref = it->ref;
if (it->context == context)
{
menu_on_draw(menu_ref, dst_surface);
}
}
}
/**
* @brief Calls the on_input() method of the menus associated to a context.
* @param context_index Index of an object with menus.
* @param event The input event to handle.
* @return \c true if the event was handled and should stop being propagated.
*/
bool LuaContext::menus_on_input(int context_index, InputEvent& event)
{
const void* context;
if (lua_type(l, context_index) == LUA_TUSERDATA)
{
ExportableToLua** userdata = static_cast<ExportableToLua**>(lua_touserdata(l, context_index));
context = *userdata;
}
else
{
context = lua_topointer(l, context_index);
}
bool handled = false;
std::list<LuaMenuData>::reverse_iterator it;
for (it = menus.rbegin(); it != menus.rend() && !handled; ++it)
{
int menu_ref = it->ref;
if (it->context == context)
{
handled = menu_on_input(menu_ref, event);
}
}
return handled;
}