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

71 lines
1.8 KiB
C++
Raw Normal View History

#include "wrap-string.hpp"
#include "wrap-map.hpp"
#include "wrap-vector.hpp"
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
void Gui::menu_item(const eng::string &action, const eng::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);
}
bool Gui::has_action(const eng::string &action) const {
2021-02-16 14:15:40 -05:00
for (const GuiElt &elt : elts_) {
if (elt.action_ == action) {
return true;
}
}
return false;
}
eng::string Gui::get_action(int64_t index) {
if ((index < 0) || (index >= int64_t(elts_.size()))) {
2021-11-16 12:20:11 -05:00
return "";
}
return elts_[index].action();
}
eng::string Gui::menu_debug_string() const {
eng::ostringstream oss;
2021-11-16 13:14:59 -05:00
int index = 0;
for (const GuiElt &elt : elts()) {
oss << index << " " << elt.label() << std::endl;
index += 1;
}
return oss.str();
}
2021-12-15 23:03:43 -05:00
LuaDefine(gui_menu_item, "action,label", "add a menu item to the current gui") {
2021-02-25 14:09:16 -05:00
Gui *gui = Gui::fetch_global_pointer(L);
LuaArg laction, llabel;
LuaStack LS(L, laction, llabel);
eng::string action = LS.ckstring(laction);
eng::string label = LS.ckstring(llabel);
2021-12-28 14:07:15 -05:00
if (!util::has_prefix(action, "cb_")) {
luaL_error(L, "menuitem callbacks must start with cb_");
}
2021-02-16 13:31:34 -05:00
gui->menu_item(action, label);
2021-02-07 15:35:31 -05:00
return LS.result();
}