diff --git a/apps/openmw/mwlua/uibindings.cpp b/apps/openmw/mwlua/uibindings.cpp index 04282f74ca..282892213a 100644 --- a/apps/openmw/mwlua/uibindings.cpp +++ b/apps/openmw/mwlua/uibindings.cpp @@ -141,7 +141,7 @@ namespace MWLua luaManager->addAction([wm, obj = obj.as()] { wm->setConsoleSelectedObject(obj.ptr()); }); } }; - api["content"] = LuaUi::Content::loadConstructor(context.mLua); + api["content"] = LuaUi::loadContentConstructor(context.mLua); api["create"] = [context](const sol::table& layout) { auto element = LuaUi::Element::make(layout); context.mLuaManager->addAction(std::make_unique(UiAction::CREATE, element, context.mLua)); diff --git a/apps/openmw_test_suite/lua/test_ui_content.cpp b/apps/openmw_test_suite/lua/test_ui_content.cpp index 13ee9b6a7d..bf33bbd697 100644 --- a/apps/openmw_test_suite/lua/test_ui_content.cpp +++ b/apps/openmw_test_suite/lua/test_ui_content.cpp @@ -15,15 +15,15 @@ namespace LuaUiContentTest() { mLuaState.addInternalLibSearchPath("resources/lua_libs"); - mNew = LuaUi::Content::loadConstructor(&mLuaState); + mNew = LuaUi::loadContentConstructor(&mLuaState); } - LuaUi::Content::View makeContent(sol::table source) + LuaUi::ContentView makeContent(sol::table source) { auto result = mNew.call(source); if (result.get_type() != sol::type::table) throw std::logic_error("Expected table"); - return LuaUi::Content::View(result.get()); + return LuaUi::ContentView(result.get()); } sol::table makeTable() { return sol::table(mLuaState.sol(), sol::create); } @@ -53,7 +53,7 @@ namespace table.add(makeTable()); table.add(makeTable()); table.add(makeTable()); - LuaUi::Content::View content = makeContent(table); + LuaUi::ContentView content = makeContent(table); EXPECT_EQ(content.size(), 3); } @@ -63,7 +63,7 @@ namespace table.add(makeTable()); table.add(makeTable()); table.add(makeTable()); - LuaUi::Content::View content = makeContent(table); + LuaUi::ContentView content = makeContent(table); content.insert(2, makeTable("inserted")); EXPECT_EQ(content.size(), 4); auto inserted = content.at("inserted"); @@ -77,7 +77,7 @@ namespace auto table = makeTable(); table.add(makeTable()); table.add(makeTable()); - LuaUi::Content::View content = makeContent(table); + LuaUi::ContentView content = makeContent(table); sol::table t = makeTable(); EXPECT_ANY_THROW(content.assign(3, t)); } @@ -96,7 +96,7 @@ namespace auto table = makeTable(); table.add(makeTable()); table.add(makeTable("a")); - LuaUi::Content::View content = makeContent(table); + LuaUi::ContentView content = makeContent(table); EXPECT_NO_THROW(content.at("a")); content.remove("a"); EXPECT_EQ(content.size(), 1); @@ -116,7 +116,7 @@ namespace table.add(makeTable()); table.add(makeTable()); table.add(makeTable()); - LuaUi::Content::View content = makeContent(table); + LuaUi::ContentView content = makeContent(table); auto child = makeTable(); content.assign(2, child); EXPECT_EQ(content.indexOf(child).value(), 2); @@ -126,7 +126,7 @@ namespace TEST_F(LuaUiContentTest, BoundsChecks) { auto table = makeTable(); - LuaUi::Content::View content = makeContent(table); + LuaUi::ContentView content = makeContent(table); EXPECT_ANY_THROW(content.at(0)); EXPECT_EQ(content.size(), 0); content.assign(content.size(), makeTable()); diff --git a/components/lua_ui/content.cpp b/components/lua_ui/content.cpp index fd1b56d427..2e1d4ca0c4 100644 --- a/components/lua_ui/content.cpp +++ b/components/lua_ui/content.cpp @@ -1,8 +1,8 @@ #include "content.hpp" -namespace LuaUi::Content +namespace LuaUi { - sol::protected_function loadConstructor(LuaUtil::LuaState* state) + sol::protected_function loadContentConstructor(LuaUtil::LuaState* state) { sol::function loader = state->loadInternalLib("content"); sol::set_environment(state->newInternalLibEnvironment(), loader); @@ -11,4 +11,12 @@ namespace LuaUi::Content throw std::logic_error("Expected function"); return metatable["new"].get(); } + + bool isValidContent(const sol::object& object) + { + if (object.get_type() != sol::type::table) + return false; + sol::table table = object; + return table.traverse_get>(sol::metatable_key, "__Content").value_or(false); + } } diff --git a/components/lua_ui/content.hpp b/components/lua_ui/content.hpp index b29bf82818..2caa1ff8dc 100644 --- a/components/lua_ui/content.hpp +++ b/components/lua_ui/content.hpp @@ -8,29 +8,23 @@ #include -namespace LuaUi::Content +namespace LuaUi { - sol::protected_function loadConstructor(LuaUtil::LuaState* state); + sol::protected_function loadContentConstructor(LuaUtil::LuaState* state); - class View + bool isValidContent(const sol::object& object); + + class ContentView { public: // accepts only Lua tables returned by ui.content - explicit View(sol::table table) + explicit ContentView(sol::table table) : mTable(std::move(table)) { - if (!isValid(mTable)) + if (!isValidContent(mTable)) throw std::domain_error("Expected a Content table"); } - static bool isValid(const sol::object& object) - { - if (object.get_type() != sol::type::table) - return false; - sol::table table = object; - return table.traverse_get>(sol::metatable_key, "__Content").value_or(false); - } - size_t size() const { return mTable.size(); } void assign(std::string_view name, const sol::table& table) diff --git a/components/lua_ui/element.cpp b/components/lua_ui/element.cpp index c3fb9c2450..91d1acc433 100644 --- a/components/lua_ui/element.cpp +++ b/components/lua_ui/element.cpp @@ -63,9 +63,7 @@ namespace LuaUi destroyWidget(w); return result; } - if (!Content::View::isValid(contentObj)) - throw std::logic_error("Layout content field must be a openmw.ui.content"); - Content::View content(contentObj.as()); + ContentView content(contentObj.as()); result.resize(content.size()); size_t minSize = std::min(children.size(), content.size()); for (size_t i = 0; i < minSize; i++)