mirror of
https://github.com/OpenMW/openmw.git
synced 2025-03-31 14:07:35 +00:00
Get rid of the LuaUI::Content namespace
This commit is contained in:
parent
d24c506b0e
commit
e96681151c
5 changed files with 28 additions and 28 deletions
|
@ -141,7 +141,7 @@ namespace MWLua
|
|||
luaManager->addAction([wm, obj = obj.as<LObject>()] { 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>(UiAction::CREATE, element, context.mLua));
|
||||
|
|
|
@ -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<sol::table>());
|
||||
return LuaUi::ContentView(result.get<sol::table>());
|
||||
}
|
||||
|
||||
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());
|
||||
|
|
|
@ -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<sol::protected_function>();
|
||||
}
|
||||
|
||||
bool isValidContent(const sol::object& object)
|
||||
{
|
||||
if (object.get_type() != sol::type::table)
|
||||
return false;
|
||||
sol::table table = object;
|
||||
return table.traverse_get<sol::optional<bool>>(sol::metatable_key, "__Content").value_or(false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,29 +8,23 @@
|
|||
|
||||
#include <components/lua/luastate.hpp>
|
||||
|
||||
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::optional<bool>>(sol::metatable_key, "__Content").value_or(false);
|
||||
}
|
||||
|
||||
size_t size() const { return mTable.size(); }
|
||||
|
||||
void assign(std::string_view name, const sol::table& table)
|
||||
|
|
|
@ -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<sol::table>());
|
||||
ContentView content(contentObj.as<sol::table>());
|
||||
result.resize(content.size());
|
||||
size_t minSize = std::min(children.size(), content.size());
|
||||
for (size_t i = 0; i < minSize; i++)
|
||||
|
|
Loading…
Reference in a new issue