Merge branch 'lua_settings_ui' into 'master'

Lua settings UI

Closes #6454

See merge request OpenMW/openmw!1595
This commit is contained in:
psi29a 2022-02-02 21:04:18 +00:00
commit cf8d49bb9a
19 changed files with 551 additions and 28 deletions

View file

@ -165,8 +165,9 @@ add_component_dir (queries
)
add_component_dir (lua_ui
registerscriptsettings scriptsettings
properties widget element util layers content
text textedit window image
adapter text textedit window image container
)

View file

@ -0,0 +1,60 @@
#include "adapter.hpp"
#include <MyGUI_Gui.h>
#include "element.hpp"
#include "container.hpp"
namespace LuaUi
{
namespace
{
sol::state luaState;
}
LuaAdapter::LuaAdapter()
: mElement(nullptr)
, mContainer(nullptr)
{
mContainer = MyGUI::Gui::getInstancePtr()->createWidget<LuaContainer>(
"", MyGUI::IntCoord(), MyGUI::Align::Default, "", "");
mContainer->initialize(luaState, mContainer);
mContainer->onCoordChange([this](WidgetExtension* ext, MyGUI::IntCoord coord)
{
setSize(coord.size());
});
mContainer->widget()->attachToWidget(this);
}
void LuaAdapter::attach(const std::shared_ptr<Element>& element)
{
detachElement();
mElement = element;
attachElement();
setSize(mContainer->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(mContainer);
}
void LuaAdapter::detachElement()
{
if (mElement.get())
mElement->detachFromWidget();
mElement = nullptr;
}
}

View file

@ -0,0 +1,31 @@
#ifndef OPENMW_LUAUI_ADAPTER
#define OPENMW_LUAUI_ADAPTER
#include <memory>
#include <MyGUI_Widget.h>
namespace LuaUi
{
class LuaContainer;
struct Element;
class LuaAdapter : public MyGUI::Widget
{
MYGUI_RTTI_DERIVED(LuaAdapter)
public:
LuaAdapter();
void attach(const std::shared_ptr<Element>& element);
void detach();
private:
std::shared_ptr<Element> mElement;
LuaContainer* mContainer;
void attachElement();
void detachElement();
};
}
#endif // !OPENMW_LUAUI_ADAPTER

View file

@ -0,0 +1,35 @@
#include "container.hpp"
#include <algorithm>
namespace LuaUi
{
void LuaContainer::updateChildren()
{
WidgetExtension::updateChildren();
for (auto w : children())
{
w->onCoordChange([this](WidgetExtension* child, MyGUI::IntCoord coord)
{ updateSizeToFit(); });
}
updateSizeToFit();
}
MyGUI::IntSize LuaContainer::childScalingSize()
{
return MyGUI::IntSize();
}
void LuaContainer::updateSizeToFit()
{
MyGUI::IntSize size;
for (auto w : children())
{
MyGUI::IntCoord coord = w->widget()->getCoord();
size.width = std::max(size.width, coord.left + coord.width);
size.height = std::max(size.height, coord.top + coord.height);
}
setForcedSize(size);
updateCoord();
}
}

View file

@ -0,0 +1,21 @@
#ifndef OPENMW_LUAUI_CONTAINER
#define OPENMW_LUAUI_CONTAINER
#include "widget.hpp"
namespace LuaUi
{
class LuaContainer : public WidgetExtension, public MyGUI::Widget
{
MYGUI_RTTI_DERIVED(LuaContainer)
protected:
virtual void updateChildren() override;
virtual MyGUI::IntSize childScalingSize() override;
private:
void updateSizeToFit();
};
}
#endif // !OPENMW_LUAUI_CONTAINER

View file

@ -4,6 +4,7 @@
#include "content.hpp"
#include "util.hpp"
#include "widget.hpp"
namespace LuaUi
{
@ -138,7 +139,7 @@ namespace LuaUi
ext->setChildren(updateContent(ext->children(), layout.get<sol::object>(LayoutKeys::content)));
}
void setLayer(WidgetExtension* ext, const sol::table& layout)
std::string setLayer(WidgetExtension* ext, const sol::table& layout)
{
MyGUI::ILayer* layerNode = ext->widget()->getLayer();
std::string currentLayer = layerNode ? layerNode->getName() : std::string();
@ -149,15 +150,18 @@ namespace LuaUi
{
MyGUI::LayerManager::getInstance().attachToLayerNode(newLayer, ext->widget());
}
return newLayer;
}
std::map<Element*, std::shared_ptr<Element>> Element::sAllElements;
Element::Element(sol::table layout)
: mRoot{ nullptr }
, mLayout{ std::move(layout) }
, mUpdate{ false }
, mDestroy{ false }
: mRoot(nullptr)
, mAttachedTo(nullptr)
, mLayout(std::move(layout))
, mLayer()
, mUpdate(false)
, mDestroy(false)
{}
@ -174,7 +178,8 @@ namespace LuaUi
if (!mRoot)
{
mRoot = createWidget(mLayout);
setLayer(mRoot, mLayout);
mLayer = setLayer(mRoot, mLayout);
updateAttachment();
}
}
@ -182,8 +187,17 @@ namespace LuaUi
{
if (mRoot && mUpdate)
{
updateWidget(mRoot, mLayout);
setLayer(mRoot, mLayout);
if (mRoot->widget()->getTypeName() != widgetType(mLayout))
{
destroyWidget(mRoot);
mRoot = createWidget(mLayout);
}
else
{
updateWidget(mRoot, mLayout);
}
mLayer = setLayer(mRoot, mLayout);
updateAttachment();
}
mUpdate = false;
}
@ -195,4 +209,34 @@ namespace LuaUi
mRoot = nullptr;
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 });
mRoot->updateCoord();
}
}
}

View file

@ -9,8 +9,10 @@ namespace LuaUi
{
static std::shared_ptr<Element> make(sol::table layout);
LuaUi::WidgetExtension* mRoot;
WidgetExtension* mRoot;
WidgetExtension* mAttachedTo;
sol::table mLayout;
std::string mLayer;
bool mUpdate;
bool mDestroy;
@ -22,9 +24,13 @@ namespace LuaUi
friend void clearUserInterface();
void attachToWidget(WidgetExtension* w);
void detachFromWidget();
private:
Element(sol::table layout);
static std::map<Element*, std::shared_ptr<Element>> sAllElements;
void updateAttachment();
};
}

View file

@ -0,0 +1,13 @@
#ifndef OPENMW_LUAUI_REGISTERSCRIPTSETTINGS
#define OPENMW_LUAUI_REGISTERSCRIPTSETTINGS
#include <sol/sol.hpp>
namespace LuaUi
{
// implemented in scriptsettings.cpp
void registerSettingsPage(const sol::table& options);
void clearSettings();
}
#endif // !OPENMW_LUAUI_REGISTERSCRIPTSETTINGS

View file

@ -0,0 +1,60 @@
#include "scriptsettings.hpp"
#include <map>
#include <sol/sol.hpp>
#include "registerscriptsettings.hpp"
#include "element.hpp"
#include "adapter.hpp"
namespace LuaUi
{
namespace
{
std::vector<sol::table> allPages;
ScriptSettingsPage parse(const sol::table& options)
{
auto name = options.get_or("name", std::string());
auto searchHints = options.get_or("searchHints", std::string());
auto element = options.get_or<std::shared_ptr<LuaUi::Element>>("element", nullptr);
if (name.empty())
Log(Debug::Warning) << "A script settings page has an empty name";
if (!element.get())
Log(Debug::Warning) << "A script settings page has no UI element assigned";
return {
name, searchHints, element
};
}
}
size_t scriptSettingsPageCount()
{
return allPages.size();
}
ScriptSettingsPage scriptSettingsPageAt(size_t index)
{
return parse(allPages[index]);
}
void registerSettingsPage(const sol::table& options)
{
allPages.push_back(options);
}
void clearSettings()
{
allPages.clear();
}
void attachPageAt(size_t index, LuaAdapter* adapter)
{
if (index < allPages.size())
{
ScriptSettingsPage page = parse(allPages[index]);
adapter->detach();
if (page.mElement.get())
adapter->attach(page.mElement);
}
}
}

View file

@ -0,0 +1,25 @@
#ifndef OPENMW_LUAUI_SCRIPTSETTINGS
#define OPENMW_LUAUI_SCRIPTSETTINGS
#include <string>
#include <string_view>
#include <memory>
#include <MyGUI_Widget.h>
namespace LuaUi
{
class LuaAdapter;
struct Element;
struct ScriptSettingsPage
{
std::string mName;
std::string mSearchHints;
std::shared_ptr<Element> mElement;
};
size_t scriptSettingsPageCount();
ScriptSettingsPage scriptSettingsPageAt(size_t index);
void attachPageAt(size_t index, LuaAdapter* adapter);
}
#endif // !OPENMW_LUAUI_SCRIPTSETTINGS

View file

@ -2,24 +2,29 @@
#include <MyGUI_FactoryManager.h>
#include "adapter.hpp"
#include "widget.hpp"
#include "text.hpp"
#include "textedit.hpp"
#include "window.hpp"
#include "image.hpp"
#include "container.hpp"
#include "element.hpp"
#include "registerscriptsettings.hpp"
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");
MyGUI::FactoryManager::getInstance().registerFactory<LuaWindow>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaImage>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaContainer>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaTileRect>("BasisSkin");
}
@ -37,6 +42,7 @@ namespace LuaUi
void clearUserInterface()
{
clearSettings();
while (!Element::sAllElements.empty())
Element::sAllElements.begin()->second->destroy();
}

View file

@ -14,10 +14,11 @@ namespace LuaUi
, mAbsoluteCoord()
, mRelativeCoord()
, mAnchor()
, mLua{ nullptr }
, mWidget{ nullptr }
, mLua(nullptr)
, mWidget(nullptr)
, mSlot(this)
, mLayout{ sol::nil }
, mLayout(sol::nil)
, mParent(nullptr)
{}
void WidgetExtension::initialize(lua_State* lua, MyGUI::Widget* self)
@ -64,6 +65,8 @@ namespace LuaUi
mWidget->eventKeySetFocus.clear();
mWidget->eventKeyLostFocus.clear();
mOnCoordChange.reset();
for (WidgetExtension* w : mChildren)
w->deinitialize();
for (WidgetExtension* w : mTemplateChildren)
@ -72,6 +75,7 @@ namespace LuaUi
void WidgetExtension::attach(WidgetExtension* ext)
{
ext->mParent = this;
ext->widget()->attachToWidget(mSlot->widget());
ext->updateCoord();
}
@ -150,6 +154,7 @@ namespace LuaUi
mChildren[i] = children[i];
attach(mChildren[i]);
}
updateChildren();
}
void WidgetExtension::setTemplateChildren(const std::vector<WidgetExtension*>& children)
@ -194,6 +199,11 @@ namespace LuaUi
mForcedCoord = offset;
}
void WidgetExtension::setForcedSize(const MyGUI::IntSize& size)
{
mForcedCoord = size;
}
void WidgetExtension::updateCoord()
{
MyGUI::IntCoord oldCoord = mWidget->getCoord();
@ -203,6 +213,8 @@ namespace LuaUi
mWidget->setCoord(newCoord);
if (oldCoord.size() != newCoord.size())
updateChildrenCoord();
if (oldCoord != newCoord && mOnCoordChange.has_value())
mOnCoordChange.value()(this, newCoord);
}
void WidgetExtension::setProperties(sol::object props)
@ -231,23 +243,31 @@ namespace LuaUi
w->updateCoord();
}
MyGUI::IntSize WidgetExtension::parentSize()
{
if (mParent)
return mParent->childScalingSize();
else
return widget()->getParentSize();
}
MyGUI::IntSize WidgetExtension::calculateSize()
{
const MyGUI::IntSize& parentSize = mWidget->getParentSize();
MyGUI::IntSize pSize = parentSize();
MyGUI::IntSize newSize;
newSize = mAbsoluteCoord.size() + mForcedCoord.size();
newSize.width += mRelativeCoord.width * parentSize.width;
newSize.height += mRelativeCoord.height * parentSize.height;
newSize.width += mRelativeCoord.width * pSize.width;
newSize.height += mRelativeCoord.height * pSize.height;
return newSize;
}
MyGUI::IntPoint WidgetExtension::calculatePosition(const MyGUI::IntSize& size)
{
const MyGUI::IntSize& parentSize = mWidget->getParentSize();
MyGUI::IntSize pSize = parentSize();
MyGUI::IntPoint newPosition;
newPosition = mAbsoluteCoord.point() + mForcedCoord.point();
newPosition.left += mRelativeCoord.left * parentSize.width - mAnchor.width * size.width;
newPosition.top += mRelativeCoord.top * parentSize.height - mAnchor.height * size.height;
newPosition.left += mRelativeCoord.left * pSize.width - mAnchor.width * size.width;
newPosition.top += mRelativeCoord.top * pSize.height - mAnchor.height * size.height;
return newPosition;
}
@ -259,6 +279,11 @@ namespace LuaUi
return newCoord;
}
MyGUI::IntSize WidgetExtension::childScalingSize()
{
return widget()->getSize();
}
void WidgetExtension::triggerEvent(std::string_view name, const sol::object& argument = sol::nil) const
{
auto it = mCallbacks.find(name);

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,15 +57,22 @@ namespace LuaUi
return parseExternal(mExternal, name, defaultValue);
}
void onCoordChange(const std::optional<std::function<void(WidgetExtension*, MyGUI::IntCoord)>>& callback)
{
mOnCoordChange = callback;
}
protected:
virtual void initialize();
sol::table makeTable() const;
sol::object keyEvent(MyGUI::KeyCode) const;
sol::object mouseEvent(int left, int top, MyGUI::MouseButton button) const;
MyGUI::IntSize parentSize();
virtual MyGUI::IntSize calculateSize();
virtual MyGUI::IntPoint calculatePosition(const MyGUI::IntSize& size);
MyGUI::IntCoord calculateCoord();
virtual MyGUI::IntSize childScalingSize();
template<typename T>
T propertyValue(std::string_view name, const T& defaultValue)
@ -76,6 +85,7 @@ namespace LuaUi
virtual void updateTemplate();
virtual void updateProperties();
virtual void updateChildren() {};
void triggerEvent(std::string_view name, const sol::object& argument) const;
@ -101,6 +111,7 @@ namespace LuaUi
sol::object mProperties;
sol::object mTemplateProperties;
sol::object mExternal;
WidgetExtension* mParent;
void attach(WidgetExtension* ext);
@ -119,6 +130,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(WidgetExtension*, MyGUI::IntCoord)>> mOnCoordChange;
};
class LuaWidget : public MyGUI::Widget, public WidgetExtension