From cf84386cc27bdb24aafe3a4ab1b24b9450d4d915 Mon Sep 17 00:00:00 2001 From: uramer Date: Sat, 11 Nov 2023 14:07:36 +0100 Subject: [PATCH] Use Element Content children for Settings UI --- components/lua_ui/adapter.cpp | 14 +++++++++---- components/lua_ui/container.hpp | 1 + components/lua_ui/element.cpp | 35 ++------------------------------- components/lua_ui/element.hpp | 5 ----- 4 files changed, 13 insertions(+), 42 deletions(-) diff --git a/components/lua_ui/adapter.cpp b/components/lua_ui/adapter.cpp index 6db9420398..c9afd4db0d 100644 --- a/components/lua_ui/adapter.cpp +++ b/components/lua_ui/adapter.cpp @@ -44,14 +44,20 @@ namespace LuaUi void LuaAdapter::attachElement() { - if (mElement.get()) - mElement->attachToWidget(mContainer); + if (!mElement.get()) + return; + if (!mElement->mRoot) + throw std::logic_error("Attempting to use a destroyed UI Element"); + mContainer->setChildren({ mElement->mRoot }); + mElement->mRoot->updateCoord(); + mContainer->updateCoord(); } void LuaAdapter::detachElement() { - if (mElement.get()) - mElement->detachFromWidget(); + mContainer->setChildren({}); + if (mElement && mElement->mRoot) + mElement->mRoot->widget()->detachFromWidget(); mElement = nullptr; } } diff --git a/components/lua_ui/container.hpp b/components/lua_ui/container.hpp index 79d3cd8fa8..16f19d3c12 100644 --- a/components/lua_ui/container.hpp +++ b/components/lua_ui/container.hpp @@ -9,6 +9,7 @@ namespace LuaUi { MYGUI_RTTI_DERIVED(LuaContainer) + public: MyGUI::IntSize calculateSize() override; void updateCoord() override; diff --git a/components/lua_ui/element.cpp b/components/lua_ui/element.cpp index cc75a258d5..013e6fc85d 100644 --- a/components/lua_ui/element.cpp +++ b/components/lua_ui/element.cpp @@ -53,6 +53,8 @@ namespace LuaUi { if (!ext->isRoot()) destroyWidget(ext); + else + ext->widget()->detachFromWidget(); } void detachElements(LuaUi::WidgetExtension* ext) @@ -235,7 +237,6 @@ namespace LuaUi Element::Element(sol::table layout) : mRoot(nullptr) - , mAttachedTo(nullptr) , mLayout(std::move(layout)) , mLayer() , mUpdate(false) @@ -257,7 +258,6 @@ namespace LuaUi { mRoot = createWidget(layout(), 0); mLayer = setLayer(mRoot, layout()); - updateAttachment(); updateRootCoord(mRoot); } } @@ -281,7 +281,6 @@ namespace LuaUi updateWidget(mRoot, layout(), 0); } mLayer = setLayer(mRoot, layout()); - updateAttachment(); updateRootCoord(mRoot); } mUpdate = false; @@ -297,34 +296,4 @@ namespace LuaUi } sAllElements.erase(this); } - - void Element::attachToWidget(WidgetExtension* w) - { - if (mAttachedTo) - throw std::logic_error("A UI element can't be attached to two widgets at once"); - mAttachedTo = w; - updateAttachment(); - } - - void Element::detachFromWidget() - { - if (mRoot) - mRoot->widget()->detachFromWidget(); - if (mAttachedTo) - mAttachedTo->setChildren({}); - mAttachedTo = nullptr; - } - - void Element::updateAttachment() - { - if (!mRoot) - return; - if (mAttachedTo) - { - if (!mLayer.empty()) - Log(Debug::Warning) << "Ignoring element's layer " << mLayer << " because it's attached to a widget"; - mAttachedTo->setChildren({ mRoot }); - mAttachedTo->updateCoord(); - } - } } diff --git a/components/lua_ui/element.hpp b/components/lua_ui/element.hpp index b57af92fee..5aadb1beab 100644 --- a/components/lua_ui/element.hpp +++ b/components/lua_ui/element.hpp @@ -17,7 +17,6 @@ namespace LuaUi } WidgetExtension* mRoot; - WidgetExtension* mAttachedTo; sol::object mLayout; std::string mLayer; bool mUpdate; @@ -31,14 +30,10 @@ namespace LuaUi friend void clearUserInterface(); - void attachToWidget(WidgetExtension* w); - void detachFromWidget(); - private: Element(sol::table layout); sol::table layout() { return LuaUtil::cast(mLayout); } static std::map> sAllElements; - void updateAttachment(); }; }