Files
integration/luprex/core/cpp/gui.cpp

48 lines
1.2 KiB
C++

#include "gui.hpp"
#include "luastack.hpp"
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;
}
void Gui::menu_item(const std::string &action, const std::string &label) {
GuiElt elt;
elt.type_ = GuiElt::TYPE_MENU_ITEM;
elt.action_ = action;
elt.label_ = label;
elts_.push_back(elt);
}
bool Gui::has_action(const std::string &action) const {
for (const GuiElt &elt : elts_) {
if (elt.action_ == action) {
return true;
}
}
return false;
}
LuaDefine(gui_menu_item, "c") {
Gui *gui = Gui::fetch_global_pointer(L);
LuaArg laction, llabel;
LuaStack LS(L, laction, llabel);
std::string action = LS.ckstring(laction);
std::string label = LS.ckstring(llabel);
gui->menu_item(action, label);
return LS.result();
}