1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-05 15:55:45 +00:00

Wrap Lua settings widgets into an Adapter widget

This commit is contained in:
uramer 2022-01-29 23:06:43 +01:00
parent db9e734a6a
commit 086a7d9bc5
13 changed files with 168 additions and 44 deletions

View File

@ -242,9 +242,9 @@ namespace MWGui
getWidget(mScriptList, "ScriptList");
getWidget(mScriptBox, "ScriptBox");
getWidget(mScriptView, "ScriptView");
getWidget(mScriptAdapter, "ScriptAdapter");
getWidget(mScriptDisabled, "ScriptDisabled");
getWidget(mScriptDescription, "ScriptDescription");
mScriptChild = nullptr;
#ifndef WIN32
// hide gamma controls since it currently does not work under Linux
@ -732,8 +732,7 @@ namespace MWGui
void SettingsWindow::renderScriptSettings()
{
if (mCurrentPage >= 0)
LuaUi::attachToWidget(mCurrentPage);
mScriptAdapter->detach();
mCurrentPage = -1;
mScriptList->removeAllItems();
mScriptView->setCanvasSize({0, 0});
@ -766,18 +765,14 @@ namespace MWGui
void SettingsWindow::onScriptListSelection(MyGUI::ListBox*, size_t index)
{
if (mCurrentPage >= 0)
LuaUi::attachToWidget(mCurrentPage);
mScriptAdapter->detach();
mCurrentPage = -1;
if (index < mScriptList->getItemCount())
{
mCurrentPage = *mScriptList->getItemDataAt<size_t>(index);
LuaUi::attachToWidget(mCurrentPage, mScriptView);
LuaUi::attachPageAt(mCurrentPage, mScriptAdapter);
}
mScriptChild = mScriptView->getChildCount() > 0 ? mScriptView->getChildAt(0) : nullptr;
MyGUI::IntSize canvasSize = mScriptChild ? mScriptChild->getSize() : MyGUI::IntSize();
if (mScriptChild)
mScriptChild->setVisible(mScriptView->getVisible());
MyGUI::IntSize canvasSize = mScriptAdapter->getSize();
mScriptView->setCanvasSize(canvasSize);
}
@ -787,14 +782,13 @@ namespace MWGui
{
mScriptDescription->setVisible(false);
mScriptView->setVisible(true);
if (mScriptChild)
mScriptChild->setVisible(true);
return;
}
size_t page = *mScriptList->getItemDataAt<size_t>(index);
mScriptDescription->setCaption(LuaUi::scriptSettingsPageAt(page).mDescription);
mScriptDescription->setVisible(true);
mScriptView->setVisible(false);
else {
size_t page = *mScriptList->getItemDataAt<size_t>(index);
mScriptDescription->setCaption(LuaUi::scriptSettingsPageAt(page).mDescription);
mScriptDescription->setVisible(true);
mScriptView->setVisible(false);
}
}
void SettingsWindow::onRebindAction(MyGUI::Widget* _sender)

View File

@ -1,6 +1,8 @@
#ifndef MWGUI_SETTINGS_H
#define MWGUI_SETTINGS_H
#include <components/lua_ui/adapter.hpp>
#include "windowbase.hpp"
namespace MWGui
@ -48,14 +50,11 @@ namespace MWGui
MyGUI::ListBox* mScriptList;
MyGUI::Widget* mScriptBox;
MyGUI::ScrollView* mScriptView;
LuaUi::LuaAdapter* mScriptAdapter;
MyGUI::EditBox* mScriptDisabled;
MyGUI::EditBox* mScriptDescription;
int mCurrentPage;
// only necessary to work around a MyGUI bug
// adding a child to an invisible widget doesn't make the child invisible
MyGUI::Widget* mScriptChild;
void onTabChanged(MyGUI::TabControl* _sender, size_t index);
void onOkButtonClicked(MyGUI::Widget* _sender);
void onTextureFilteringChanged(MyGUI::ComboBox* _sender, size_t pos);

View File

@ -166,7 +166,7 @@ add_component_dir (queries
add_component_dir (lua_ui
properties widget element util layers content scriptsettings
text textedit window image
adapter text textedit window image
)

View File

@ -0,0 +1,65 @@
#include "adapter.hpp"
#include <MyGUI_Gui.h>
#include "element.hpp"
namespace LuaUi
{
namespace
{
sol::state luaState;
}
LuaAdapter::LuaAdapter()
: mElement(nullptr)
, mContent(nullptr)
{
MyGUI::Widget* widget = MyGUI::Gui::getInstancePtr()->createWidgetT(
"LuaWidget", "",
MyGUI::IntCoord(), MyGUI::Align::Default,
std::string(), "");
mContent = dynamic_cast<WidgetExtension*>(widget);
if (!mContent)
throw std::runtime_error("Invalid widget!");
mContent->initialize(luaState, widget);
mContent->onSizeChange([this](MyGUI::IntSize size)
{
setSize(size);
});
mContent->widget()->attachToWidget(this);
}
void LuaAdapter::attach(const std::shared_ptr<Element>& element)
{
detachElement();
mElement = element;
attachElement();
setSize(mContent->widget()->getSize());
// workaround for MyGUI bug
// parent visibility doesn't affect added children
setVisible(!getVisible());
setVisible(!getVisible());
}
void LuaAdapter::detach()
{
detachElement();
setSize({ 0, 0 });
}
void LuaAdapter::attachElement()
{
if (mElement.get())
mElement->attachToWidget(mContent);
}
void LuaAdapter::detachElement()
{
if (mElement.get())
mElement->detachFromWidget();
mElement = nullptr;
}
}

View File

@ -0,0 +1,29 @@
#ifndef OPENMW_LUAUI_ADAPTER
#define OPENMW_LUAUI_ADAPTER
#include <MyGUI_Widget.h>
namespace LuaUi
{
class WidgetExtension;
struct Element;
class LuaAdapter : public MyGUI::Widget
{
MYGUI_RTTI_DERIVED(LuaAdapter)
public:
LuaAdapter();
void attach(const std::shared_ptr<Element>& element);
void detach();
bool empty() { return mElement.get() == nullptr; }
private:
WidgetExtension* mContent;
std::shared_ptr<Element> mElement;
void attachElement();
void detachElement();
};
}
#endif // !OPENMW_LUAUI_ADAPTER

View File

@ -4,6 +4,7 @@
#include "content.hpp"
#include "util.hpp"
#include "widget.hpp"
namespace LuaUi
{
@ -209,14 +210,26 @@ namespace LuaUi
sAllElements.erase(this);
}
void Element::attachToWidget(MyGUI::Widget* w)
void Element::attachToWidget(WidgetExtension* w)
{
if (mAttachedTo && 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->onSizeChange({});
mRoot->widget()->detachFromWidget();
}
if (mAttachedTo)
mAttachedTo->setChildren({});
mAttachedTo = nullptr;
}
void Element::updateAttachment()
{
if (!mRoot)
@ -225,18 +238,17 @@ namespace LuaUi
{
if (!mLayer.empty())
Log(Debug::Warning) << "Ignoring element's layer " << mLayer << " because it's attached to a widget";
if (mRoot->widget()->getParent() != mAttachedTo)
mAttachedTo->setChildren({ mRoot });
auto callback = [this](MyGUI::IntSize size)
{
mRoot->widget()->attachToWidget(mAttachedTo);
mRoot->updateCoord();
}
}
else
{
if (mRoot->widget()->getParent() != nullptr)
{
mRoot->widget()->detachFromWidget();
}
if (!mAttachedTo)
return;
mAttachedTo->setForcedSize(mRoot->widget()->getSize());
mAttachedTo->updateCoord();
};
mRoot->onSizeChange(callback);
mRoot->updateCoord();
callback(mRoot->widget()->getSize());
}
}
}

View File

@ -10,7 +10,7 @@ namespace LuaUi
static std::shared_ptr<Element> make(sol::table layout);
WidgetExtension* mRoot;
MyGUI::Widget* mAttachedTo;
WidgetExtension* mAttachedTo;
sol::table mLayout;
std::string mLayer;
bool mUpdate;
@ -24,7 +24,8 @@ namespace LuaUi
friend void clearUserInterface();
void attachToWidget(MyGUI::Widget* w = nullptr);
void attachToWidget(WidgetExtension* w);
void detachFromWidget();
private:
Element(sol::table layout);

View File

@ -1,8 +1,10 @@
#include "scriptsettings.hpp"
#include <map>
#include <sol/sol.hpp>
#include "element.hpp"
#include "adapter.hpp"
namespace LuaUi
{
@ -19,7 +21,7 @@ namespace LuaUi
if (!element.get())
Log(Debug::Warning) << "A script settings page has no UI element assigned";
return {
name, description, element.get()
name, description, element
};
}
}
@ -44,13 +46,14 @@ namespace LuaUi
allPages.clear();
}
void attachToWidget(size_t index, MyGUI::Widget* widget)
void attachPageAt(size_t index, LuaAdapter* adapter)
{
if (index < allPages.size())
{
ScriptSettingsPage page = parse(allPages[index]);
if (page.mElement)
page.mElement->attachToWidget(widget);
adapter->detach();
if (page.mElement.get())
adapter->attach(page.mElement);
}
}
}

View File

@ -1,7 +1,6 @@
#ifndef OPENMW_LUAUI_SCRIPTSETTINGS
#define OPENMW_LUAUI_SCRIPTSETTINGS
#include <vector>
#include <string>
#include <string_view>
@ -9,18 +8,19 @@
namespace LuaUi
{
class LuaAdapter;
struct Element;
struct ScriptSettingsPage
{
std::string mName;
std::string mDescription;
Element* mElement; // TODO: figure out if this can lead to use after free
std::shared_ptr<Element> mElement;
};
size_t scriptSettingsPageCount();
ScriptSettingsPage scriptSettingsPageAt(size_t index);
void registerSettingsPage(const sol::table& options);
void clearSettings();
void attachToWidget(size_t index, MyGUI::Widget* widget = nullptr);
void attachPageAt(size_t index, LuaAdapter* adapter);
}
#endif // !OPENMW_LUAUI_SCRIPTSETTINGS

View File

@ -2,6 +2,7 @@
#include <MyGUI_FactoryManager.h>
#include "adapter.hpp"
#include "widget.hpp"
#include "text.hpp"
#include "textedit.hpp"
@ -16,6 +17,7 @@ namespace LuaUi
void registerAllWidgets()
{
MyGUI::FactoryManager::getInstance().registerFactory<LuaAdapter>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaWidget>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaText>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaTextEdit>("Widget");

View File

@ -194,6 +194,11 @@ namespace LuaUi
mForcedCoord = offset;
}
void WidgetExtension::setForcedSize(const MyGUI::IntSize& size)
{
mForcedCoord = size;
}
void WidgetExtension::updateCoord()
{
MyGUI::IntCoord oldCoord = mWidget->getCoord();
@ -202,7 +207,11 @@ namespace LuaUi
if (oldCoord != newCoord)
mWidget->setCoord(newCoord);
if (oldCoord.size() != newCoord.size())
{
updateChildrenCoord();
if (mOnSizeChange.has_value())
mOnSizeChange.value()(newCoord.size());
}
}
void WidgetExtension::setProperties(sol::object props)

View File

@ -2,6 +2,7 @@
#define OPENMW_LUAUI_WIDGET
#include <map>
#include <functional>
#include <MyGUI_Widget.h>
#include <sol/sol.hpp>
@ -44,6 +45,7 @@ namespace LuaUi
MyGUI::IntCoord forcedCoord();
void setForcedCoord(const MyGUI::IntCoord& offset);
void setForcedSize(const MyGUI::IntSize& size);
void updateCoord();
const sol::table& getLayout() { return mLayout; }
@ -55,6 +57,11 @@ namespace LuaUi
return parseExternal(mExternal, name, defaultValue);
}
void onSizeChange(const std::optional<std::function<void(MyGUI::IntSize)>>& callback)
{
mOnSizeChange = callback;
}
protected:
virtual void initialize();
sol::table makeTable() const;
@ -119,6 +126,8 @@ namespace LuaUi
void mouseRelease(MyGUI::Widget*, int, int, MyGUI::MouseButton);
void focusGain(MyGUI::Widget*, MyGUI::Widget*);
void focusLoss(MyGUI::Widget*, MyGUI::Widget*);
std::optional<std::function<void(MyGUI::IntSize)>> mOnSizeChange;
};
class LuaWidget : public MyGUI::Widget, public WidgetExtension

View File

@ -664,6 +664,7 @@
<Widget name="ScriptView" type="ScrollView" skin="MW_ScrollViewVH" position="2 2 246 246" align="Stretch">
<Property key="CanvasAlign" value="Left Top"/>
<Property key="CanvasSize" value="246 246"/>
<Widget name="ScriptAdapter" type="LuaAdapter" position="0 0 246 246"/>
</Widget>
<Widget name="ScriptDescription" type = "EditBox" skin="SandText" position="2 2 246 246" align="Stretch">
<Property key="Visible" value="false"/>