diff --git a/apps/openmw/mwlua/luamanagerimp.cpp b/apps/openmw/mwlua/luamanagerimp.cpp index 3319e975cc..716612f745 100644 --- a/apps/openmw/mwlua/luamanagerimp.cpp +++ b/apps/openmw/mwlua/luamanagerimp.cpp @@ -26,7 +26,10 @@ namespace MWLua { - LuaManager::LuaManager(const VFS::Manager* vfs, const std::string& libsDir) : mLua(vfs, &mConfiguration), mI18n(vfs, &mLua) + LuaManager::LuaManager(const VFS::Manager* vfs, const std::string& libsDir) + : mLua(vfs, &mConfiguration) + , mUiResourceManager(vfs) + , mI18n(vfs, &mLua) { Log(Debug::Info) << "Lua version: " << LuaUtil::getLuaVersion(); mLua.addInternalLibSearchPath(libsDir); @@ -37,8 +40,6 @@ namespace MWLua mLocalLoader = createUserdataSerializer(true, mWorldView.getObjectRegistry(), &mContentFileMapping); mGlobalScripts.setSerializer(mGlobalSerializer.get()); - - mUiResourceManager = std::make_unique(vfs); } void LuaManager::initConfiguration() @@ -250,6 +251,7 @@ namespace MWLua void LuaManager::clear() { LuaUi::clearUserInterface(); + mUiResourceManager.clear(); mActiveLocalScripts.clear(); mLocalEvents.clear(); mGlobalEvents.clear(); @@ -472,7 +474,8 @@ namespace MWLua { Log(Debug::Info) << "Reload Lua"; - LuaUi::clearUserInterface(); + LuaUi::clearUserInterface(); + mUiResourceManager.clear(); mLua.dropScriptCache(); initConfiguration(); diff --git a/apps/openmw/mwlua/luamanagerimp.hpp b/apps/openmw/mwlua/luamanagerimp.hpp index fecb9478e0..fed6dc9dc7 100644 --- a/apps/openmw/mwlua/luamanagerimp.hpp +++ b/apps/openmw/mwlua/luamanagerimp.hpp @@ -91,18 +91,17 @@ namespace MWLua return [this, c](Arg arg) { this->queueCallback(c, sol::make_object(c.mFunc.lua_state(), arg)); }; } - LuaUi::ResourceManager* uiResourceManager() { return mUiResourceManager.get(); } + LuaUi::ResourceManager* uiResourceManager() { return &mUiResourceManager; } private: void initConfiguration(); LocalScripts* createLocalScripts(const MWWorld::Ptr& ptr, ESM::LuaScriptCfg::Flags); - std::unique_ptr mUiResourceManager; - bool mInitialized = false; bool mGlobalScriptsStarted = false; LuaUtil::ScriptsConfiguration mConfiguration; LuaUtil::LuaState mLua; + LuaUi::ResourceManager mUiResourceManager; LuaUtil::I18nManager mI18n; sol::table mNearbyPackage; sol::table mUserInterfacePackage; diff --git a/apps/openmw/mwlua/uibindings.cpp b/apps/openmw/mwlua/uibindings.cpp index 031bb67af0..b384994654 100644 --- a/apps/openmw/mwlua/uibindings.cpp +++ b/apps/openmw/mwlua/uibindings.cpp @@ -78,10 +78,10 @@ namespace MWLua std::shared_ptr mElement; }; - class LayerAction final : public Action + class InsertLayerAction final : public Action { public: - LayerAction(std::string_view name, std::string_view afterName, + InsertLayerAction(std::string_view name, std::string_view afterName, LuaUi::Layers::Options options, LuaUtil::LuaState* state) : Action(state) , mName(name) @@ -247,7 +247,7 @@ namespace MWLua { LuaUi::Layers::Options options; options.mInteractive = LuaUtil::getValueOrDefault(LuaUtil::getFieldOrNil(opt, "interactive"), true); - context.mLuaManager->addAction(std::make_unique(name, afterName, options, context.mLua)); + context.mLuaManager->addAction(std::make_unique(name, afterName, options, context.mLua)); }; { auto pairs = [layers](const sol::object&) @@ -283,10 +283,10 @@ namespace MWLua { LuaUi::TextureData data; sol::object path = LuaUtil::getFieldOrNil(options, "path"); - if (path.is() && !path.as().empty()) + if (path.is()) data.mPath = path.as(); - else - throw sol::error("Invalid texture path"); + if (data.mPath.empty()) + throw std::logic_error("Invalid texture path"); sol::object offset = LuaUtil::getFieldOrNil(options, "offset"); if (offset.is()) data.mOffset = offset.as(); diff --git a/components/lua_ui/image.cpp b/components/lua_ui/image.cpp index 4a25416835..69bccac1ed 100644 --- a/components/lua_ui/image.cpp +++ b/components/lua_ui/image.cpp @@ -39,13 +39,12 @@ namespace LuaUi MyGUI::IntCoord atlasCoord; if (resource) { - auto& data = resource->data(); atlasCoord = MyGUI::IntCoord( - static_cast(data.mOffset.x()), - static_cast(data.mOffset.y()), - static_cast(data.mSize.x()), - static_cast(data.mSize.y())); - setImageTexture(data.mPath); + static_cast(resource->mOffset.x()), + static_cast(resource->mOffset.y()), + static_cast(resource->mSize.x()), + static_cast(resource->mSize.y())); + setImageTexture(resource->mPath); } bool tileH = propertyValue("tileH", false); diff --git a/components/lua_ui/resources.cpp b/components/lua_ui/resources.cpp index 92318a8a46..605c9ba58b 100644 --- a/components/lua_ui/resources.cpp +++ b/components/lua_ui/resources.cpp @@ -7,8 +7,8 @@ namespace LuaUi { std::shared_ptr ResourceManager::registerTexture(TextureData data) { - std::string normalizedPath = vfs()->normalizeFilename(data.mPath); - if (!vfs()->exists(normalizedPath)) + std::string normalizedPath = mVfs->normalizeFilename(data.mPath); + if (!mVfs->exists(normalizedPath)) { auto error = Misc::StringUtils::format("Texture with path \"%s\" doesn't exist", data.mPath); throw std::logic_error(error); @@ -19,4 +19,9 @@ namespace LuaUi list.push_back(std::make_shared(data)); return list.back(); } + + void ResourceManager::clear() + { + mTextures.clear(); + } } diff --git a/components/lua_ui/resources.hpp b/components/lua_ui/resources.hpp index da9d566273..cabcd63bf4 100644 --- a/components/lua_ui/resources.hpp +++ b/components/lua_ui/resources.hpp @@ -22,18 +22,8 @@ namespace LuaUi osg::Vec2f mSize; }; - class TextureResource - { - public: - TextureResource(TextureData data) - : mData(data) - {} - - const TextureData& data() { return mData; } - - private: - TextureData mData; - }; + // will have more/different data when automated atlasing is supported + using TextureResource = TextureData; class ResourceManager { @@ -43,10 +33,9 @@ namespace LuaUi {} std::shared_ptr registerTexture(TextureData data); + void clear(); private: - const VFS::Manager* vfs() const { return mVfs; } - const VFS::Manager* mVfs; using TextureResources = std::vector>; std::unordered_map mTextures;