From fc50724f5c2306bc060bb500839b04263ebd98eb Mon Sep 17 00:00:00 2001 From: uramer Date: Tue, 8 Feb 2022 13:38:42 +0100 Subject: [PATCH] Render text and images correctly in templates with slots --- apps/openmw/mwlua/uibindings.cpp | 2 +- components/lua_ui/element.cpp | 3 +-- components/lua_ui/image.cpp | 2 +- components/lua_ui/image.hpp | 4 +--- components/lua_ui/text.cpp | 2 +- components/lua_ui/textedit.cpp | 2 +- components/lua_ui/widget.cpp | 21 +++++++++++++++++---- components/lua_ui/widget.hpp | 4 +++- files/mygui/openmw_lua.xml | 10 ++++------ 9 files changed, 30 insertions(+), 20 deletions(-) diff --git a/apps/openmw/mwlua/uibindings.cpp b/apps/openmw/mwlua/uibindings.cpp index 0572365b57..031bb67af0 100644 --- a/apps/openmw/mwlua/uibindings.cpp +++ b/apps/openmw/mwlua/uibindings.cpp @@ -283,7 +283,7 @@ namespace MWLua { LuaUi::TextureData data; sol::object path = LuaUtil::getFieldOrNil(options, "path"); - if (path.is() and !path.as().empty()) + if (path.is() && !path.as().empty()) data.mPath = path.as(); else throw sol::error("Invalid texture path"); diff --git a/components/lua_ui/element.cpp b/components/lua_ui/element.cpp index d2074ecfc2..47dd0a3d38 100644 --- a/components/lua_ui/element.cpp +++ b/components/lua_ui/element.cpp @@ -130,8 +130,7 @@ namespace LuaUi void updateWidget(WidgetExtension* ext, const sol::table& layout) { - ext->resetSlot(); // otherwise if template gets changed, all non-template children will get destroyed - + ext->reset(); ext->setLayout(layout); ext->setExternal(layout.get(LayoutKeys::external)); setTemplate(ext, layout.get(LayoutKeys::templateLayout)); diff --git a/components/lua_ui/image.cpp b/components/lua_ui/image.cpp index 9dab34868e..4a25416835 100644 --- a/components/lua_ui/image.cpp +++ b/components/lua_ui/image.cpp @@ -27,7 +27,7 @@ namespace LuaUi mTileSize.height = 1e7; } - LuaImage::LuaImage() + void LuaImage::initialize() { changeWidgetSkin("LuaImage"); mTileRect = dynamic_cast(getSubWidgetMain()); diff --git a/components/lua_ui/image.hpp b/components/lua_ui/image.hpp index e841e55fdb..f7df102440 100644 --- a/components/lua_ui/image.hpp +++ b/components/lua_ui/image.hpp @@ -25,10 +25,8 @@ namespace LuaUi { MYGUI_RTTI_DERIVED(LuaImage) - public: - LuaImage(); - protected: + void initialize() override; void updateProperties() override; LuaTileRect* mTileRect; }; diff --git a/components/lua_ui/text.cpp b/components/lua_ui/text.cpp index 3a56ad68af..9ec31834f4 100644 --- a/components/lua_ui/text.cpp +++ b/components/lua_ui/text.cpp @@ -10,7 +10,7 @@ namespace LuaUi void LuaText::initialize() { - changeWidgetSkin("SandText"); + changeWidgetSkin("LuaText"); setEditStatic(true); setVisibleHScroll(false); setVisibleVScroll(false); diff --git a/components/lua_ui/textedit.cpp b/components/lua_ui/textedit.cpp index ac44872f21..5cb47ec34a 100644 --- a/components/lua_ui/textedit.cpp +++ b/components/lua_ui/textedit.cpp @@ -6,7 +6,7 @@ namespace LuaUi { void LuaTextEdit::initialize() { - changeWidgetSkin("LuaTextEdit"); + changeWidgetSkin("LuaText"); eventEditTextChange += MyGUI::newDelegate(this, &LuaTextEdit::textChange); diff --git a/components/lua_ui/widget.cpp b/components/lua_ui/widget.cpp index 4364a0c362..96a6d096bd 100644 --- a/components/lua_ui/widget.cpp +++ b/components/lua_ui/widget.cpp @@ -24,9 +24,8 @@ namespace LuaUi { mLua = lua; mWidget = self; - updateTemplate(); - initialize(); + updateTemplate(); } void WidgetExtension::initialize() @@ -72,6 +71,13 @@ namespace LuaUi w->deinitialize(); } + void WidgetExtension::reset() + { + // detach all children from the slot widget, in case it gets destroyed + for (auto& w: mChildren) + w->widget()->detachFromWidget(); + } + void WidgetExtension::attach(WidgetExtension* ext) { ext->mParent = this; @@ -182,8 +188,15 @@ namespace LuaUi else mSlot = slot->mSlot; if (mSlot != oldSlot) - for (WidgetExtension* w : mChildren) - attach(w); + { + MyGUI::IntSize slotSize = mSlot->widget()->getSize(); + MyGUI::IntPoint slotPosition = mSlot->widget()->getAbsolutePosition() - widget()->getAbsolutePosition(); + MyGUI::IntCoord slotCoord(slotPosition, slotSize); + if (mWidget->getSubWidgetMain()) + mWidget->getSubWidgetMain()->setCoord(slotCoord); + if (mWidget->getSubWidgetText()) + mWidget->getSubWidgetText()->setCoord(slotCoord); + } } void WidgetExtension::setCallback(const std::string& name, const LuaUtil::Callback& callback) diff --git a/components/lua_ui/widget.hpp b/components/lua_ui/widget.hpp index 9037443676..827993d47d 100644 --- a/components/lua_ui/widget.hpp +++ b/components/lua_ui/widget.hpp @@ -28,6 +28,9 @@ namespace LuaUi virtual void deinitialize(); MyGUI::Widget* widget() const { return mWidget; } + WidgetExtension* slot() const { return mSlot; } + + void reset(); const std::vector& children() { return mChildren; } void setChildren(const std::vector&); @@ -50,7 +53,6 @@ namespace LuaUi const sol::table& getLayout() { return mLayout; } void setLayout(const sol::table& layout) { mLayout = layout; } - void resetSlot() { mSlot = this; } template T externalValue(std::string_view name, const T& defaultValue) diff --git a/files/mygui/openmw_lua.xml b/files/mygui/openmw_lua.xml index cb0af2b4a9..588fc53014 100644 --- a/files/mygui/openmw_lua.xml +++ b/files/mygui/openmw_lua.xml @@ -4,12 +4,10 @@ - - - - - - + + + + \ No newline at end of file