1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-01 03:21:41 +00:00

Use new Context helpers for UI bindings

This commit is contained in:
Anton Uramer 2024-07-23 12:39:56 +02:00
parent 6851e6e56a
commit a5d1db2afd

View File

@ -80,8 +80,10 @@ namespace MWLua
}(); }();
} }
sol::table registerUiApi(const Context& context, bool menu) sol::table registerUiApi(const Context& context)
{ {
bool menu = context.mType == Context::Menu;
MWBase::WindowManager* windowManager = MWBase::Environment::get().getWindowManager(); MWBase::WindowManager* windowManager = MWBase::Environment::get().getWindowManager();
sol::table api = context.mLua->newTable(); sol::table api = context.mLua->newTable();
@ -293,15 +295,8 @@ namespace MWLua
sol::table initUserInterfacePackage(const Context& context) sol::table initUserInterfacePackage(const Context& context)
{ {
std::string_view menuCache = "openmw_ui_menu"; if (context.initializeOnce("openmw_ui_usertypes"))
std::string_view gameCache = "openmw_ui_game";
std::string_view cacheKey = context.mType == Context::Menu ? menuCache : gameCache;
{ {
sol::state_view& lua = context.mLua->sol();
if (lua[cacheKey] != sol::nil)
return lua[cacheKey];
}
auto element = context.mLua->sol().new_usertype<LuaUi::Element>("UiElement"); auto element = context.mLua->sol().new_usertype<LuaUi::Element>("UiElement");
element[sol::meta_function::to_string] = [](const LuaUi::Element& element) { element[sol::meta_function::to_string] = [](const LuaUi::Element& element) {
std::stringstream res; std::stringstream res;
@ -327,17 +322,20 @@ namespace MWLua
}; };
auto uiLayer = context.mLua->sol().new_usertype<LuaUi::Layer>("UiLayer"); auto uiLayer = context.mLua->sol().new_usertype<LuaUi::Layer>("UiLayer");
uiLayer["name"] = sol::readonly_property([](LuaUi::Layer& self) -> std::string_view { return self.name(); }); uiLayer["name"]
= sol::readonly_property([](LuaUi::Layer& self) -> std::string_view { return self.name(); });
uiLayer["size"] = sol::readonly_property([](LuaUi::Layer& self) { return self.size(); }); uiLayer["size"] = sol::readonly_property([](LuaUi::Layer& self) { return self.size(); });
uiLayer[sol::meta_function::to_string] uiLayer[sol::meta_function::to_string]
= [](LuaUi::Layer& self) { return Misc::StringUtils::format("UiLayer(%s)", self.name()); }; = [](LuaUi::Layer& self) { return Misc::StringUtils::format("UiLayer(%s)", self.name()); };
}
sol::table menuApi = registerUiApi(context, true); sol::object cached = context.getTypePackage("openmw_ui");
sol::table gameApi = registerUiApi(context, false); if (cached != sol::nil)
return cached;
sol::state_view& lua = context.mLua->sol(); else
lua[menuCache] = LuaUtil::makeReadOnly(menuApi); {
lua[gameCache] = LuaUtil::makeReadOnly(gameApi); sol::table api = LuaUtil::makeReadOnly(registerUiApi(context));
return lua[cacheKey]; return context.setTypePackage(api, "openmw_ui");
}
} }
} }