X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Ftheme.cpp;h=35046c18ee17a0b77dc474c51d3cf5fae9c21b39;hb=c9181491b62cb8b928e7c92ae204ed234bf7f3ea;hp=71a59a24525a8c123da024212e40d9395f5d1769;hpb=021fa9fa5ce103ae5843647fee3d5d1d7038d32f;p=nageru diff --git a/nageru/theme.cpp b/nageru/theme.cpp index 71a59a2..35046c1 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -39,6 +39,7 @@ #include "input_state.h" #include "lua_utils.h" #include "pbo_frame_allocator.h" +#include "scene.h" class Mixer; @@ -77,21 +78,6 @@ int ThemeMenu_set(lua_State *L) return theme->set_theme_menu(L); } -namespace { - -// Contains basically the same data as InputState, but does not hold on to -// a reference to the frames. This is important so that we can release them -// without having to wait for Lua's GC. -struct InputStateInfo { - InputStateInfo(const InputState& input_state); - - unsigned last_width[MAX_VIDEO_CARDS], last_height[MAX_VIDEO_CARDS]; - bool last_interlaced[MAX_VIDEO_CARDS], last_has_signal[MAX_VIDEO_CARDS], last_is_connected[MAX_VIDEO_CARDS]; - unsigned last_frame_rate_nom[MAX_VIDEO_CARDS], last_frame_rate_den[MAX_VIDEO_CARDS]; - bool has_last_subtitle[MAX_VIDEO_CARDS]; - std::string last_subtitle[MAX_VIDEO_CARDS]; -}; - InputStateInfo::InputStateInfo(const InputState &input_state) { for (unsigned signal_num = 0; signal_num < MAX_VIDEO_CARDS; ++signal_num) { @@ -116,21 +102,19 @@ InputStateInfo::InputStateInfo(const InputState &input_state) } } -enum EffectType { - WHITE_BALANCE_EFFECT, - RESAMPLE_EFFECT, - PADDING_EFFECT, - INTEGRAL_PADDING_EFFECT, - OVERLAY_EFFECT, - RESIZE_EFFECT, - MULTIPLY_EFFECT, - MIX_EFFECT, - LIFT_GAMMA_GAIN_EFFECT +// An effect that does nothing. +class IdentityEffect : public Effect { +public: + IdentityEffect() {} + string effect_type_id() const override { return "IdentityEffect"; } + string output_fragment_shader() override { return read_file("identity.frag"); } }; Effect *instantiate_effect(EffectChain *chain, EffectType effect_type) { switch (effect_type) { + case IDENTITY_EFFECT: + return new IdentityEffect; case WHITE_BALANCE_EFFECT: return new WhiteBalanceEffect; case RESAMPLE_EFFECT: @@ -155,27 +139,12 @@ Effect *instantiate_effect(EffectChain *chain, EffectType effect_type) } } -// An EffectBlueprint refers to an Effect before it's being added to the graph. -// It contains enough information to instantiate the effect, including any -// parameters that were set before it was added to the graph. Once it is -// instantiated, it forwards its calls on to the real Effect instead. -struct EffectBlueprint { - EffectBlueprint(EffectType effect_type) : effect_type(effect_type) {} - - EffectType effect_type; - map int_parameters; - map float_parameters; - map> vec3_parameters; - map> vec4_parameters; - - Effect *effect = nullptr; // Gets filled out when it's instantiated. -}; +namespace { Effect *get_effect_from_blueprint(EffectChain *chain, lua_State *L, int idx) { EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint"); if (blueprint->effect != nullptr) { - // NOTE: This will change in the future. luaL_error(L, "An effect can currently only be added to one chain.\n"); } @@ -217,6 +186,8 @@ InputStateInfo *get_input_state_info(lua_State *L, int idx) return nullptr; } +} // namespace + bool checkbool(lua_State* L, int idx) { luaL_checktype(L, idx, LUA_TBOOLEAN); @@ -230,6 +201,28 @@ string checkstdstring(lua_State *L, int index) return string(cstr, len); } +namespace { + +int Scene_new(lua_State* L) +{ + assert(lua_gettop(L) == 2); + Theme *theme = get_theme_updata(L); + int aspect_w = luaL_checknumber(L, 1); + int aspect_h = luaL_checknumber(L, 2); + + return wrap_lua_object(L, "Scene", theme, aspect_w, aspect_h); +} + +int Scene_gc(lua_State* L) +{ + assert(lua_gettop(L) == 1); + Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene"); + chain->~Scene(); + return 0; +} + +} // namespace + void add_outputs_and_finalize(EffectChain *chain, bool is_main_chain) { // Add outputs as needed. @@ -281,6 +274,8 @@ void add_outputs_and_finalize(EffectChain *chain, bool is_main_chain) chain->finalize(); } +namespace { + int EffectChain_new(lua_State* L) { assert(lua_gettop(L) == 2); @@ -560,6 +555,12 @@ int HTMLInput_get_signal_num(lua_State* L) } #endif +int IdentityEffect_new(lua_State* L) +{ + assert(lua_gettop(L) == 0); + return wrap_lua_object_nonowned(L, "EffectBlueprint", IDENTITY_EFFECT); +} + int WhiteBalanceEffect_new(lua_State* L) { assert(lua_gettop(L) == 0); @@ -618,6 +619,7 @@ int InputStateInfo_get_width(lua_State* L) { assert(lua_gettop(L) == 2); InputStateInfo *input_state_info = get_input_state_info(L, 1); + Theme *theme = get_theme_updata(L); int signal_num = theme->map_signal(luaL_checknumber(L, 2)); lua_pushnumber(L, input_state_info->last_width[signal_num]); @@ -698,6 +700,69 @@ int InputStateInfo_get_last_subtitle(lua_State* L) return 1; } +namespace { + +// Helper function to write e.g. “60” or “59.94”. +string format_frame_rate(int nom, int den) +{ + char buf[256]; + if (nom % den == 0) { + snprintf(buf, sizeof(buf), "%d", nom / den); + } else { + snprintf(buf, sizeof(buf), "%.2f", double(nom) / den); + } + return buf; +} + +// Helper function to write e.g. “720p60”. +string get_human_readable_resolution(const InputStateInfo *input_state_info, int signal_num) +{ + char buf[256]; + if (input_state_info->last_interlaced[signal_num]) { + snprintf(buf, sizeof(buf), "%di", input_state_info->last_height[signal_num] * 2); + + // Show field rate instead of frame rate; really for cosmetics only + // (and actually contrary to EBU recommendations, although in line + // with typical user expectations). + return buf + format_frame_rate(input_state_info->last_frame_rate_nom[signal_num] * 2, + input_state_info->last_frame_rate_den[signal_num]); + } else { + snprintf(buf, sizeof(buf), "%dp", input_state_info->last_height[signal_num]); + return buf + format_frame_rate(input_state_info->last_frame_rate_nom[signal_num], + input_state_info->last_frame_rate_den[signal_num]); + } +} + +} // namespace + +int InputStateInfo_get_human_readable_resolution(lua_State* L) +{ + assert(lua_gettop(L) == 2); + InputStateInfo *input_state_info = get_input_state_info(L, 1); + Theme *theme = get_theme_updata(L); + int signal_num = theme->map_signal(luaL_checknumber(L, 2)); + + string str; + if (!input_state_info->last_is_connected[signal_num]) { + str = "disconnected"; + } else if (input_state_info->last_height[signal_num] <= 0) { + str = "no signal"; + } else if (!input_state_info->last_has_signal[signal_num]) { + if (input_state_info->last_height[signal_num] == 525) { + // Special mode for the USB3 cards. + str = "no signal"; + } else { + str = get_human_readable_resolution(input_state_info, signal_num) + ", no signal"; + } + } else { + str = get_human_readable_resolution(input_state_info, signal_num); + } + + lua_pushstring(L, str.c_str()); + return 1; +} + + int EffectBlueprint_set_int(lua_State *L) { assert(lua_gettop(L) == 3); @@ -777,6 +842,30 @@ int EffectBlueprint_set_vec4(lua_State *L) return 0; } +const luaL_Reg Scene_funcs[] = { + { "new", Scene_new }, + { "__gc", Scene_gc }, + { "add_input", Scene::add_input }, + { "add_effect", Scene::add_effect }, + { "add_optional_effect", Scene::add_optional_effect }, + { "finalize", Scene::finalize }, + { NULL, NULL } +}; + +const luaL_Reg Block_funcs[] = { + { "display", Block_display }, + { "choose", Block_choose }, + { "enable", Block_enable }, + { "enable_if", Block_enable_if }, + { "disable", Block_disable }, + { "always_disable_if_disabled", Block_always_disable_if_disabled }, + { "set_int", Block_set_int }, + { "set_float", Block_set_float }, + { "set_vec3", Block_set_vec3 }, + { "set_vec4", Block_set_vec4 }, + { NULL, NULL } +}; + const luaL_Reg EffectBlueprint_funcs[] = { // NOTE: No new() function; that's for the individual effects. { "set_int", EffectBlueprint_set_int }, @@ -835,6 +924,11 @@ const luaL_Reg HTMLInput_funcs[] = { // All of these are solely for new(); the returned metatable will be that of // EffectBlueprint, and Effect (returned from add_effect()) is its own type. +const luaL_Reg IdentityEffect_funcs[] = { + { "new", IdentityEffect_new }, + { NULL, NULL } +}; + const luaL_Reg WhiteBalanceEffect_funcs[] = { { "new", WhiteBalanceEffect_new }, { NULL, NULL } @@ -891,6 +985,7 @@ const luaL_Reg InputStateInfo_funcs[] = { { "get_frame_rate_nom", InputStateInfo_get_frame_rate_nom }, { "get_frame_rate_den", InputStateInfo_get_frame_rate_den }, { "get_last_subtitle", InputStateInfo_get_last_subtitle }, + { "get_human_readable_resolution", InputStateInfo_get_human_readable_resolution }, { NULL, NULL } }; @@ -1118,6 +1213,7 @@ int call_num_channels(lua_State *L) if (lua_pcall(L, 0, 1, 0) != 0) { fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_num_channels(...) at the start of the script instead.\n"); abort(); } @@ -1129,6 +1225,57 @@ int call_num_channels(lua_State *L) } // namespace +int Nageru_set_channel_name(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + unsigned channel = luaL_checknumber(L, 1); + const string text = checkstdstring(L, 2); + theme->channel_names[channel] = text; + lua_pop(L, 2); + return 0; +} + +int Nageru_set_num_channels(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_num_channels() can only be called at startup."); + } + theme->num_channels = luaL_checknumber(L, 1); + lua_pop(L, 1); + return 0; +} + +int Nageru_set_channel_signal(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_channel_signal() can only be called at startup."); + } + unsigned channel = luaL_checknumber(L, 1); + int signal = luaL_checknumber(L, 2); + theme->channel_signals[channel] = signal; + lua_pop(L, 2); + return 0; +} + +int Nageru_set_supports_wb(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_supports_wb() can only be called at startup."); + } + unsigned channel = luaL_checknumber(L, 1); + bool supports_wb = checkbool(L, 2); + theme->channel_supports_wb[channel] = supports_wb; + lua_pop(L, 2); + return 0; +} + Theme::Theme(const string &filename, const vector &search_dirs, ResourcePool *resource_pool, unsigned num_cards) : resource_pool(resource_pool), num_cards(num_cards), signal_to_card_mapping(global_flags.default_stream_mapping) { @@ -1199,22 +1346,25 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource } // Set up the API we provide. - register_constants(); + register_globals(); + register_class("Scene", Scene_funcs); + register_class("Block", Block_funcs); register_class("EffectBlueprint", EffectBlueprint_funcs); register_class("EffectChain", EffectChain_funcs); register_class("LiveInputWrapper", LiveInputWrapper_funcs); register_class("ImageInput", ImageInput_funcs); register_class("VideoInput", VideoInput_funcs); register_class("HTMLInput", HTMLInput_funcs); - register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs); - register_class("ResampleEffect", ResampleEffect_funcs); - register_class("PaddingEffect", PaddingEffect_funcs); - register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs); - register_class("OverlayEffect", OverlayEffect_funcs); - register_class("ResizeEffect", ResizeEffect_funcs); - register_class("MultiplyEffect", MultiplyEffect_funcs); - register_class("MixEffect", MixEffect_funcs); - register_class("LiftGammaGainEffect", LiftGammaGainEffect_funcs); + register_class("IdentityEffect", IdentityEffect_funcs, IDENTITY_EFFECT); + register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs, WHITE_BALANCE_EFFECT); + register_class("ResampleEffect", ResampleEffect_funcs, RESAMPLE_EFFECT); + register_class("PaddingEffect", PaddingEffect_funcs, PADDING_EFFECT); + register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs, INTEGRAL_PADDING_EFFECT); + register_class("OverlayEffect", OverlayEffect_funcs, OVERLAY_EFFECT); + register_class("ResizeEffect", ResizeEffect_funcs, RESIZE_EFFECT); + register_class("MultiplyEffect", MultiplyEffect_funcs, MULTIPLY_EFFECT); + register_class("MixEffect", MixEffect_funcs, MIX_EFFECT); + register_class("LiftGammaGainEffect", LiftGammaGainEffect_funcs, LIFT_GAMMA_GAIN_EFFECT); register_class("InputStateInfo", InputStateInfo_funcs); register_class("ThemeMenu", ThemeMenu_funcs); @@ -1227,16 +1377,20 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource } assert(lua_gettop(L) == 0); - // Ask it for the number of channels. - num_channels = call_num_channels(L); + if (num_channels == -1) { + // Ask it for the number of channels. + num_channels = call_num_channels(L); + } + startup_finished = true; } Theme::~Theme() { + theme_menu.reset(); lua_close(L); } -void Theme::register_constants() +void Theme::register_globals() { // Set Nageru.VIDEO_FORMAT_BGRA = bmusb::PixelFormat_8BitBGRA, etc. const vector> num_constants = { @@ -1260,11 +1414,21 @@ void Theme::register_constants() lua_settable(L, 1); // t[key] = value } + const luaL_Reg Nageru_funcs[] = { + { "set_channel_name", Nageru_set_channel_name }, + { "set_num_channels", Nageru_set_num_channels }, + { "set_channel_signal", Nageru_set_channel_signal }, + { "set_supports_wb", Nageru_set_supports_wb }, + { NULL, NULL } + }; + lua_pushlightuserdata(L, this); + luaL_setfuncs(L, Nageru_funcs, 1); // for (name,f in funcs) { mt[name] = f, with upvalue {theme} } + lua_setglobal(L, "Nageru"); // Nageru = t assert(lua_gettop(L) == 0); } -void Theme::register_class(const char *class_name, const luaL_Reg *funcs) +void Theme::register_class(const char *class_name, const luaL_Reg *funcs, EffectType effect_type) { assert(lua_gettop(L) == 0); luaL_newmetatable(L, class_name); // mt = {} @@ -1272,35 +1436,16 @@ void Theme::register_class(const char *class_name, const luaL_Reg *funcs) luaL_setfuncs(L, funcs, 1); // for (name,f in funcs) { mt[name] = f, with upvalue {theme} } lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); // mt.__index = mt + if (effect_type != NO_EFFECT_TYPE) { + lua_pushnumber(L, effect_type); + lua_setfield(L, -2, "__effect_type_id"); // mt.__effect_type_id = effect_type + } lua_setglobal(L, class_name); // ClassName = mt assert(lua_gettop(L) == 0); } -Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, const InputState &input_state) +Theme::Chain Theme::get_chain_from_effect_chain(EffectChain *effect_chain, unsigned num, const InputState &input_state) { - Chain chain; - - lock_guard lock(m); - assert(lua_gettop(L) == 0); - lua_getglobal(L, "get_chain"); /* function to be called */ - lua_pushnumber(L, num); - lua_pushnumber(L, t); - lua_pushnumber(L, width); - lua_pushnumber(L, height); - wrap_lua_object(L, "InputStateInfo", input_state); - - if (lua_pcall(L, 5, 2, 0) != 0) { - fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1)); - abort(); - } - - EffectChain *effect_chain = (EffectChain *)luaL_testudata(L, -2, "EffectChain"); - if (effect_chain == nullptr) { - fprintf(stderr, "get_chain() for chain number %d did not return an EffectChain\n", - num); - abort(); - } - chain.chain = effect_chain; if (!lua_isfunction(L, -1)) { fprintf(stderr, "Argument #-1 should be a function\n"); abort(); @@ -1308,8 +1453,9 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he lua_pushvalue(L, -1); shared_ptr funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX))); lua_pop(L, 2); - assert(lua_gettop(L) == 0); + Chain chain; + chain.chain = effect_chain; chain.setup_chain = [this, funcref, input_state, effect_chain]{ lock_guard lock(m); @@ -1341,6 +1487,53 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he this->input_state = nullptr; }; + return chain; +} + +Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, const InputState &input_state) +{ + const char *func_name = "get_scene"; // For error reporting. + Chain chain; + + lock_guard lock(m); + assert(lua_gettop(L) == 0); + lua_getglobal(L, "get_scene"); /* function to be called */ + if (lua_isnil(L, -1)) { + // Try the pre-1.9.0 name for compatibility. + lua_pop(L, 1); + lua_getglobal(L, "get_chain"); + func_name = "get_chain"; + } + lua_pushnumber(L, num); + lua_pushnumber(L, t); + lua_pushnumber(L, width); + lua_pushnumber(L, height); + wrap_lua_object(L, "InputStateInfo", input_state); + + if (lua_pcall(L, 5, LUA_MULTRET, 0) != 0) { + fprintf(stderr, "error running function “%s”: %s\n", func_name, lua_tostring(L, -1)); + abort(); + } + + if (luaL_testudata(L, -1, "Scene") != nullptr) { + if (lua_gettop(L) != 1) { + luaL_error(L, "%s() for chain number %d returned an Scene, but also other items", func_name); + } + Scene *auto_effect_chain = (Scene *)luaL_testudata(L, -1, "Scene"); + auto chain_and_setup = auto_effect_chain->get_chain(this, L, num, input_state); + chain.chain = chain_and_setup.first; + chain.setup_chain = move(chain_and_setup.second); + } else if (luaL_testudata(L, -2, "EffectChain") != nullptr) { + // Old-style (pre-Nageru 1.9.0) return of a single chain and prepare function. + if (lua_gettop(L) != 2) { + luaL_error(L, "%s() for chain number %d returned an EffectChain, but needs to also return a prepare function (or use Scene)", func_name); + } + EffectChain *effect_chain = (EffectChain *)luaL_testudata(L, -2, "EffectChain"); + chain = get_chain_from_effect_chain(effect_chain, num, input_state); + } else { + luaL_error(L, "%s() for chain number %d did not return an EffectChain or Scene\n", func_name, num); + } + assert(lua_gettop(L) == 0); // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references? // Actually, setup_chain does maybe hold all the references we need now anyway? @@ -1357,7 +1550,17 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he string Theme::get_channel_name(unsigned channel) { lock_guard lock(m); + lua_getglobal(L, "channel_name"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + if (channel_names.count(channel)) { + return channel_names[channel]; + } else { + return "(no title)"; + } + } + lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `channel_name': %s\n", lua_tostring(L, -1)); @@ -1366,6 +1569,7 @@ string Theme::get_channel_name(unsigned channel) const char *ret = lua_tostring(L, -1); if (ret == nullptr) { fprintf(stderr, "function `channel_name' returned nil for channel %d\n", channel); + fprintf(stderr, "Try Nageru.set_channel_name(channel, name) at the start of the script instead.\n"); abort(); } @@ -1379,9 +1583,19 @@ int Theme::get_channel_signal(unsigned channel) { lock_guard lock(m); lua_getglobal(L, "channel_signal"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + if (channel_signals.count(channel)) { + return channel_signals[channel]; + } else { + return -1; + } + } + lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `channel_signal': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_channel_signal(channel, signal) at the start of the script instead.\n"); abort(); } @@ -1417,9 +1631,19 @@ bool Theme::get_supports_set_wb(unsigned channel) { lock_guard lock(m); lua_getglobal(L, "supports_set_wb"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + if (channel_supports_wb.count(channel)) { + return channel_supports_wb[channel]; + } else { + return false; + } + } + lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_supports_wb(channel, bool) at the start of the script instead.\n"); abort(); } @@ -1535,25 +1759,71 @@ void Theme::channel_clicked(int preview_num) assert(lua_gettop(L) == 0); } -int Theme::set_theme_menu(lua_State *L) +template +void destroy(T &ref) +{ + ref.~T(); +} + +Theme::MenuEntry::~MenuEntry() { - for (const Theme::MenuEntry &entry : theme_menu) { - luaL_unref(L, LUA_REGISTRYINDEX, entry.lua_ref); + if (is_submenu) { + destroy(submenu); + } else { + luaL_unref(entry.L, LUA_REGISTRYINDEX, entry.lua_ref); } - theme_menu.clear(); +} - int num_elements = lua_gettop(L); - for (int i = 1; i <= num_elements; ++i) { - lua_rawgeti(L, i, 1); - const string text = checkstdstring(L, -1); - lua_pop(L, 1); +namespace { + +vector> create_recursive_theme_menu(lua_State *L); + +unique_ptr create_theme_menu_entry(lua_State *L, int index) +{ + unique_ptr entry; + + lua_rawgeti(L, index, 1); + const string text = checkstdstring(L, -1); + lua_pop(L, 1); - lua_rawgeti(L, i, 2); + lua_rawgeti(L, index, 2); + if (lua_istable(L, -1)) { + vector> submenu = create_recursive_theme_menu(L); + entry.reset(new Theme::MenuEntry{ text, move(submenu) }); + lua_pop(L, 1); + } else { luaL_checktype(L, -1, LUA_TFUNCTION); int ref = luaL_ref(L, LUA_REGISTRYINDEX); + entry.reset(new Theme::MenuEntry{ text, L, ref }); + } + return entry; +} + +vector> create_recursive_theme_menu(lua_State *L) +{ + vector> menu; + size_t num_elements = lua_objlen(L, -1); + for (size_t i = 1; i <= num_elements; ++i) { + lua_rawgeti(L, -1, i); + menu.emplace_back(create_theme_menu_entry(L, -1)); + lua_pop(L, 1); + } + return menu; +} + +} // namespace - theme_menu.push_back(MenuEntry{ text, ref }); +int Theme::set_theme_menu(lua_State *L) +{ + theme_menu.reset(); + + vector> root_menu; + int num_elements = lua_gettop(L); + for (int i = 1; i <= num_elements; ++i) { + root_menu.emplace_back(create_theme_menu_entry(L, i)); } + theme_menu.reset(new MenuEntry("", move(root_menu))); + lua_pop(L, num_elements); assert(lua_gettop(L) == 0);