2021-02-07 13:38:29 -05:00
|
|
|
#include "gui.hpp"
|
2021-02-25 14:09:16 -05:00
|
|
|
#include "luastack.hpp"
|
2021-02-07 13:38:29 -05:00
|
|
|
|
2021-02-25 14:09:16 -05:00
|
|
|
void Gui::store_global_pointer(lua_State *L, Gui *g) {
|
|
|
|
|
lua_pushstring(L, "gui");
|
|
|
|
|
lua_pushlightuserdata(L, g);
|
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Gui *Gui::fetch_global_pointer(lua_State *L) {
|
|
|
|
|
lua_pushstring(L, "gui");
|
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
|
Gui *result = (Gui *)lua_touserdata(L, -1);
|
|
|
|
|
if (result == nullptr) {
|
|
|
|
|
luaL_error(L, "Not currently building a GUI");
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-02-07 13:38:29 -05:00
|
|
|
|
2021-02-16 13:31:34 -05:00
|
|
|
void Gui::menu_item(const std::string &action, const std::string &label) {
|
2021-02-07 13:38:29 -05:00
|
|
|
GuiElt elt;
|
|
|
|
|
elt.type_ = GuiElt::TYPE_MENU_ITEM;
|
2021-02-16 13:31:34 -05:00
|
|
|
elt.action_ = action;
|
2021-02-07 13:38:29 -05:00
|
|
|
elt.label_ = label;
|
|
|
|
|
elts_.push_back(elt);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 14:15:40 -05:00
|
|
|
bool Gui::has_action(const std::string &action) const {
|
|
|
|
|
for (const GuiElt &elt : elts_) {
|
|
|
|
|
if (elt.action_ == action) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 12:20:11 -05:00
|
|
|
std::string Gui::get_action(int index) {
|
2021-11-16 13:14:59 -05:00
|
|
|
if ((index < 0) || (index >= int(elts_.size()))) {
|
2021-11-16 12:20:11 -05:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return elts_[index].action();
|
|
|
|
|
}
|
2021-07-19 15:32:34 -04:00
|
|
|
|
2021-11-16 13:14:59 -05:00
|
|
|
std::string Gui::menu_debug_string() const {
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
int index = 0;
|
|
|
|
|
for (const GuiElt &elt : elts()) {
|
|
|
|
|
oss << index << " " << elt.label() << std::endl;
|
|
|
|
|
index += 1;
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-10 16:22:24 -05:00
|
|
|
LuaDefine(gui_menu_item, "c") {
|
2021-02-25 14:09:16 -05:00
|
|
|
Gui *gui = Gui::fetch_global_pointer(L);
|
|
|
|
|
LuaArg laction, llabel;
|
|
|
|
|
LuaStack LS(L, laction, llabel);
|
2021-02-16 13:31:34 -05:00
|
|
|
std::string action = LS.ckstring(laction);
|
|
|
|
|
std::string label = LS.ckstring(llabel);
|
|
|
|
|
gui->menu_item(action, label);
|
2021-02-07 15:35:31 -05:00
|
|
|
return LS.result();
|
|
|
|
|
}
|