1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2024-12-29 12:20: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();
sol::table api = context.mLua->newTable();
@ -293,15 +295,8 @@ namespace MWLua
sol::table initUserInterfacePackage(const Context& context)
{
std::string_view menuCache = "openmw_ui_menu";
std::string_view gameCache = "openmw_ui_game";
std::string_view cacheKey = context.mType == Context::Menu ? menuCache : gameCache;
if (context.initializeOnce("openmw_ui_usertypes"))
{
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");
element[sol::meta_function::to_string] = [](const LuaUi::Element& element) {
std::stringstream res;
@ -327,17 +322,20 @@ namespace MWLua
};
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[sol::meta_function::to_string]
= [](LuaUi::Layer& self) { return Misc::StringUtils::format("UiLayer(%s)", self.name()); };
}
sol::table menuApi = registerUiApi(context, true);
sol::table gameApi = registerUiApi(context, false);
sol::state_view& lua = context.mLua->sol();
lua[menuCache] = LuaUtil::makeReadOnly(menuApi);
lua[gameCache] = LuaUtil::makeReadOnly(gameApi);
return lua[cacheKey];
sol::object cached = context.getTypePackage("openmw_ui");
if (cached != sol::nil)
return cached;
else
{
sol::table api = LuaUtil::makeReadOnly(registerUiApi(context));
return context.setTypePackage(api, "openmw_ui");
}
}
}