1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-31 10:20:13 +00:00

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

@ -1,18 +1,20 @@
#include "settingswindow.hpp" #include "settingswindow.hpp"
#include <regex>
#include <iomanip>
#include <numeric>
#include <array>
#include <MyGUI_ScrollBar.h> #include <MyGUI_ScrollBar.h>
#include <MyGUI_Window.h> #include <MyGUI_Window.h>
#include <MyGUI_ComboBox.h> #include <MyGUI_ComboBox.h>
#include <MyGUI_ScrollView.h> #include <MyGUI_ScrollView.h>
#include <MyGUI_Gui.h> #include <MyGUI_Gui.h>
#include <MyGUI_TabControl.h> #include <MyGUI_TabControl.h>
#include <MyGUI_TabItem.h>
#include <SDL_video.h> #include <SDL_video.h>
#include <iomanip>
#include <numeric>
#include <array>
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/misc/stringops.hpp> #include <components/misc/stringops.hpp>
#include <components/misc/constants.hpp> #include <components/misc/constants.hpp>
@ -21,6 +23,7 @@
#include <components/resource/resourcesystem.hpp> #include <components/resource/resourcesystem.hpp>
#include <components/resource/scenemanager.hpp> #include <components/resource/scenemanager.hpp>
#include <components/sceneutil/lightmanager.hpp> #include <components/sceneutil/lightmanager.hpp>
#include <components/lua_ui/scriptsettings.hpp>
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp" #include "../mwbase/world.hpp"
@ -33,7 +36,6 @@
namespace namespace
{ {
std::string textureMipmappingToStr(const std::string& val) std::string textureMipmappingToStr(const std::string& val)
{ {
if (val == "linear") return "Trilinear"; if (val == "linear") return "Trilinear";
@ -205,9 +207,9 @@ namespace MWGui
} }
} }
SettingsWindow::SettingsWindow() : SettingsWindow::SettingsWindow() : WindowBase("openmw_settings_window.layout")
WindowBase("openmw_settings_window.layout"), , mKeyboardMode(true)
mKeyboardMode(true) , mCurrentPage(-1)
{ {
bool terrain = Settings::Manager::getBool("distant terrain", "Terrain"); bool terrain = Settings::Manager::getBool("distant terrain", "Terrain");
const std::string widgetName = terrain ? "RenderingDistanceSlider" : "LargeRenderingDistanceSlider"; const std::string widgetName = terrain ? "RenderingDistanceSlider" : "LargeRenderingDistanceSlider";
@ -236,6 +238,12 @@ namespace MWGui
getWidget(mLightingMethodButton, "LightingMethodButton"); getWidget(mLightingMethodButton, "LightingMethodButton");
getWidget(mLightsResetButton, "LightsResetButton"); getWidget(mLightsResetButton, "LightsResetButton");
getWidget(mMaxLights, "MaxLights"); getWidget(mMaxLights, "MaxLights");
getWidget(mScriptFilter, "ScriptFilter");
getWidget(mScriptList, "ScriptList");
getWidget(mScriptBox, "ScriptBox");
getWidget(mScriptView, "ScriptView");
getWidget(mScriptAdapter, "ScriptAdapter");
getWidget(mScriptDisabled, "ScriptDisabled");
#ifndef WIN32 #ifndef WIN32
// hide gamma controls since it currently does not work under Linux // hide gamma controls since it currently does not work under Linux
@ -321,6 +329,9 @@ namespace MWGui
mKeyboardSwitch->setStateSelected(true); mKeyboardSwitch->setStateSelected(true);
mControllerSwitch->setStateSelected(false); mControllerSwitch->setStateSelected(false);
mScriptFilter->eventEditTextChange += MyGUI::newDelegate(this, &SettingsWindow::onScriptFilterChange);
mScriptList->eventListMouseItemActivate += MyGUI::newDelegate(this, &SettingsWindow::onScriptListSelection);
} }
void SettingsWindow::onTabChanged(MyGUI::TabControl* /*_sender*/, size_t /*index*/) void SettingsWindow::onTabChanged(MyGUI::TabControl* /*_sender*/, size_t /*index*/)
@ -699,6 +710,118 @@ namespace MWGui
mControlsBox->setVisibleVScroll(true); mControlsBox->setVisibleVScroll(true);
} }
void SettingsWindow::resizeScriptSettings()
{
constexpr int minListWidth = 150;
constexpr float relativeListWidth = 0.2f;
constexpr int padding = 2;
constexpr int outerPadding = padding * 2;
MyGUI::IntSize parentSize = mScriptFilter->getParent()->getClientCoord().size();
int listWidth = std::max(minListWidth, static_cast<int>(parentSize.width * relativeListWidth));
int filterHeight = mScriptFilter->getSize().height;
int listHeight = parentSize.height - mScriptList->getPosition().top - outerPadding;
mScriptFilter->setSize({ listWidth, filterHeight });
mScriptList->setSize({ listWidth, listHeight });
mScriptBox->setPosition({ listWidth + padding, 0 });
mScriptBox->setSize({ parentSize.width - listWidth - padding, parentSize.height - outerPadding });
mScriptDisabled->setPosition({0, 0});
mScriptDisabled->setSize(parentSize);
}
namespace
{
std::string escapeRegex(const std::string& str)
{
static const std::regex specialChars(R"r([\^\.\[\$\(\)\|\*\+\?\{])r", std::regex_constants::extended);
return std::regex_replace(str, specialChars, R"(\$&)");
}
std::regex wordSearch(const std::string& query)
{
static const std::regex wordsRegex(R"([^[:space:]]+)", std::regex_constants::extended);
auto wordsBegin = std::sregex_iterator(query.begin(), query.end(), wordsRegex);
auto wordsEnd = std::sregex_iterator();
std::string searchRegex("(");
for (auto it = wordsBegin; it != wordsEnd; ++it)
{
if (it != wordsBegin)
searchRegex += '|';
searchRegex += escapeRegex(query.substr(it->position(), it->length()));
}
searchRegex += ')';
// query had only whitespace characters
if (searchRegex == "()")
searchRegex = "^(.*)$";
return std::regex(searchRegex, std::regex_constants::extended | std::regex_constants::icase);
}
double weightedSearch(const std::regex& regex, const std::string& text)
{
std::smatch matches;
std::regex_search(text, matches, regex);
// need a signed value, so cast to double (not an integer type to guarantee no overflow)
return static_cast<double>(matches.size());
}
}
void SettingsWindow::renderScriptSettings()
{
mScriptAdapter->detach();
mCurrentPage = -1;
mScriptList->removeAllItems();
mScriptView->setCanvasSize({0, 0});
struct WeightedPage {
size_t mIndex;
std::string mName;
double mNameWeight;
double mHintWeight;
constexpr auto tie() const { return std::tie(mNameWeight, mHintWeight, mName); }
constexpr bool operator<(const WeightedPage& rhs) const { return tie() < rhs.tie(); }
};
std::regex searchRegex = wordSearch(mScriptFilter->getCaption());
std::vector<WeightedPage> weightedPages;
weightedPages.reserve(LuaUi::scriptSettingsPageCount());
for (size_t i = 0; i < LuaUi::scriptSettingsPageCount(); ++i)
{
LuaUi::ScriptSettingsPage page = LuaUi::scriptSettingsPageAt(i);
double nameWeight = weightedSearch(searchRegex, page.mName);
double hintWeight = weightedSearch(searchRegex, page.mSearchHints);
if ((nameWeight + hintWeight) > 0)
weightedPages.push_back({ i, page.mName, -nameWeight, -hintWeight });
}
std::sort(weightedPages.begin(), weightedPages.end());
for (const WeightedPage& weightedPage : weightedPages)
mScriptList->addItem(weightedPage.mName, weightedPage.mIndex);
// Hide script settings tab when the game world isn't loaded and scripts couldn't add their settings
bool disabled = LuaUi::scriptSettingsPageCount() == 0;
mScriptDisabled->setVisible(disabled);
mScriptFilter->setVisible(!disabled);
mScriptList->setVisible(!disabled);
mScriptBox->setVisible(!disabled);
}
void SettingsWindow::onScriptFilterChange(MyGUI::EditBox*)
{
renderScriptSettings();
}
void SettingsWindow::onScriptListSelection(MyGUI::ListBox*, size_t index)
{
mScriptAdapter->detach();
mCurrentPage = -1;
if (index < mScriptList->getItemCount())
{
mCurrentPage = *mScriptList->getItemDataAt<size_t>(index);
LuaUi::attachPageAt(mCurrentPage, mScriptAdapter);
}
mScriptView->setCanvasSize(mScriptAdapter->getSize());
}
void SettingsWindow::onRebindAction(MyGUI::Widget* _sender) void SettingsWindow::onRebindAction(MyGUI::Widget* _sender)
{ {
int actionId = *_sender->getUserData<int>(); int actionId = *_sender->getUserData<int>();
@ -744,12 +867,15 @@ namespace MWGui
updateControlsBox(); updateControlsBox();
updateLightSettings(); updateLightSettings();
resetScrollbars(); resetScrollbars();
renderScriptSettings();
resizeScriptSettings();
MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mOkButton); MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mOkButton);
} }
void SettingsWindow::onWindowResize(MyGUI::Window *_sender) void SettingsWindow::onWindowResize(MyGUI::Window *_sender)
{ {
layoutControlsBox(); layoutControlsBox();
resizeScriptSettings();
} }
void SettingsWindow::computeMinimumWindowSize() void SettingsWindow::computeMinimumWindowSize()

View File

@ -1,6 +1,8 @@
#ifndef MWGUI_SETTINGS_H #ifndef MWGUI_SETTINGS_H
#define MWGUI_SETTINGS_H #define MWGUI_SETTINGS_H
#include <components/lua_ui/adapter.hpp>
#include "windowbase.hpp" #include "windowbase.hpp"
namespace MWGui namespace MWGui
@ -44,6 +46,14 @@ namespace MWGui
MyGUI::Button* mControllerSwitch; MyGUI::Button* mControllerSwitch;
bool mKeyboardMode; //if true, setting up the keyboard. Otherwise, it's controller bool mKeyboardMode; //if true, setting up the keyboard. Otherwise, it's controller
MyGUI::EditBox* mScriptFilter;
MyGUI::ListBox* mScriptList;
MyGUI::Widget* mScriptBox;
MyGUI::ScrollView* mScriptView;
LuaUi::LuaAdapter* mScriptAdapter;
MyGUI::EditBox* mScriptDisabled;
int mCurrentPage;
void onTabChanged(MyGUI::TabControl* _sender, size_t index); void onTabChanged(MyGUI::TabControl* _sender, size_t index);
void onOkButtonClicked(MyGUI::Widget* _sender); void onOkButtonClicked(MyGUI::Widget* _sender);
void onTextureFilteringChanged(MyGUI::ComboBox* _sender, size_t pos); void onTextureFilteringChanged(MyGUI::ComboBox* _sender, size_t pos);
@ -71,12 +81,17 @@ namespace MWGui
void onWindowResize(MyGUI::Window* _sender); void onWindowResize(MyGUI::Window* _sender);
void onScriptFilterChange(MyGUI::EditBox*);
void onScriptListSelection(MyGUI::ListBox*, size_t index);
void apply(); void apply();
void configureWidgets(MyGUI::Widget* widget, bool init); void configureWidgets(MyGUI::Widget* widget, bool init);
void updateSliderLabel(MyGUI::ScrollBar* scroller, const std::string& value); void updateSliderLabel(MyGUI::ScrollBar* scroller, const std::string& value);
void layoutControlsBox(); void layoutControlsBox();
void resizeScriptSettings();
void renderScriptSettings();
void computeMinimumWindowSize(); void computeMinimumWindowSize();

View File

@ -2,6 +2,7 @@
#include <components/lua_ui/element.hpp> #include <components/lua_ui/element.hpp>
#include <components/lua_ui/layers.hpp> #include <components/lua_ui/layers.hpp>
#include <components/lua_ui/content.hpp> #include <components/lua_ui/content.hpp>
#include <components/lua_ui/registerscriptsettings.hpp>
#include "context.hpp" #include "context.hpp"
#include "actions.hpp" #include "actions.hpp"
@ -43,7 +44,7 @@ namespace MWLua
break; break;
} }
} }
catch (std::exception& e) catch (std::exception&)
{ {
// prevent any actions on a potentially corrupted widget // prevent any actions on a potentially corrupted widget
mElement->mRoot = nullptr; mElement->mRoot = nullptr;
@ -238,6 +239,8 @@ namespace MWLua
typeTable.set(it.second, it.first); typeTable.set(it.second, it.first);
api["TYPE"] = LuaUtil::makeReadOnly(typeTable); api["TYPE"] = LuaUtil::makeReadOnly(typeTable);
api["registerSettingsPage"] = &LuaUi::registerSettingsPage;
return LuaUtil::makeReadOnly(api); return LuaUtil::makeReadOnly(api);
} }
} }

View File

@ -165,8 +165,9 @@ add_component_dir (queries
) )
add_component_dir (lua_ui add_component_dir (lua_ui
registerscriptsettings scriptsettings
properties widget element util layers content 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 "content.hpp"
#include "util.hpp" #include "util.hpp"
#include "widget.hpp"
namespace LuaUi namespace LuaUi
{ {
@ -138,7 +139,7 @@ namespace LuaUi
ext->setChildren(updateContent(ext->children(), layout.get<sol::object>(LayoutKeys::content))); 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(); MyGUI::ILayer* layerNode = ext->widget()->getLayer();
std::string currentLayer = layerNode ? layerNode->getName() : std::string(); std::string currentLayer = layerNode ? layerNode->getName() : std::string();
@ -149,15 +150,18 @@ namespace LuaUi
{ {
MyGUI::LayerManager::getInstance().attachToLayerNode(newLayer, ext->widget()); MyGUI::LayerManager::getInstance().attachToLayerNode(newLayer, ext->widget());
} }
return newLayer;
} }
std::map<Element*, std::shared_ptr<Element>> Element::sAllElements; std::map<Element*, std::shared_ptr<Element>> Element::sAllElements;
Element::Element(sol::table layout) Element::Element(sol::table layout)
: mRoot{ nullptr } : mRoot(nullptr)
, mLayout{ std::move(layout) } , mAttachedTo(nullptr)
, mUpdate{ false } , mLayout(std::move(layout))
, mDestroy{ false } , mLayer()
, mUpdate(false)
, mDestroy(false)
{} {}
@ -174,7 +178,8 @@ namespace LuaUi
if (!mRoot) if (!mRoot)
{ {
mRoot = createWidget(mLayout); mRoot = createWidget(mLayout);
setLayer(mRoot, mLayout); mLayer = setLayer(mRoot, mLayout);
updateAttachment();
} }
} }
@ -182,8 +187,17 @@ namespace LuaUi
{ {
if (mRoot && mUpdate) if (mRoot && mUpdate)
{ {
updateWidget(mRoot, mLayout); if (mRoot->widget()->getTypeName() != widgetType(mLayout))
setLayer(mRoot, mLayout); {
destroyWidget(mRoot);
mRoot = createWidget(mLayout);
}
else
{
updateWidget(mRoot, mLayout);
}
mLayer = setLayer(mRoot, mLayout);
updateAttachment();
} }
mUpdate = false; mUpdate = false;
} }
@ -195,4 +209,34 @@ namespace LuaUi
mRoot = nullptr; mRoot = nullptr;
sAllElements.erase(this); 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); static std::shared_ptr<Element> make(sol::table layout);
LuaUi::WidgetExtension* mRoot; WidgetExtension* mRoot;
WidgetExtension* mAttachedTo;
sol::table mLayout; sol::table mLayout;
std::string mLayer;
bool mUpdate; bool mUpdate;
bool mDestroy; bool mDestroy;
@ -22,9 +24,13 @@ namespace LuaUi
friend void clearUserInterface(); friend void clearUserInterface();
void attachToWidget(WidgetExtension* w);
void detachFromWidget();
private: private:
Element(sol::table layout); Element(sol::table layout);
static std::map<Element*, std::shared_ptr<Element>> sAllElements; 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 <MyGUI_FactoryManager.h>
#include "adapter.hpp"
#include "widget.hpp" #include "widget.hpp"
#include "text.hpp" #include "text.hpp"
#include "textedit.hpp" #include "textedit.hpp"
#include "window.hpp" #include "window.hpp"
#include "image.hpp" #include "image.hpp"
#include "container.hpp"
#include "element.hpp" #include "element.hpp"
#include "registerscriptsettings.hpp"
namespace LuaUi namespace LuaUi
{ {
void registerAllWidgets() void registerAllWidgets()
{ {
MyGUI::FactoryManager::getInstance().registerFactory<LuaAdapter>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaWidget>("Widget"); MyGUI::FactoryManager::getInstance().registerFactory<LuaWidget>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaText>("Widget"); MyGUI::FactoryManager::getInstance().registerFactory<LuaText>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaTextEdit>("Widget"); MyGUI::FactoryManager::getInstance().registerFactory<LuaTextEdit>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaWindow>("Widget"); MyGUI::FactoryManager::getInstance().registerFactory<LuaWindow>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaImage>("Widget"); MyGUI::FactoryManager::getInstance().registerFactory<LuaImage>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaContainer>("Widget");
MyGUI::FactoryManager::getInstance().registerFactory<LuaTileRect>("BasisSkin"); MyGUI::FactoryManager::getInstance().registerFactory<LuaTileRect>("BasisSkin");
} }
@ -37,6 +42,7 @@ namespace LuaUi
void clearUserInterface() void clearUserInterface()
{ {
clearSettings();
while (!Element::sAllElements.empty()) while (!Element::sAllElements.empty())
Element::sAllElements.begin()->second->destroy(); Element::sAllElements.begin()->second->destroy();
} }

View File

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

View File

@ -2,6 +2,7 @@
#define OPENMW_LUAUI_WIDGET #define OPENMW_LUAUI_WIDGET
#include <map> #include <map>
#include <functional>
#include <MyGUI_Widget.h> #include <MyGUI_Widget.h>
#include <sol/sol.hpp> #include <sol/sol.hpp>
@ -44,6 +45,7 @@ namespace LuaUi
MyGUI::IntCoord forcedCoord(); MyGUI::IntCoord forcedCoord();
void setForcedCoord(const MyGUI::IntCoord& offset); void setForcedCoord(const MyGUI::IntCoord& offset);
void setForcedSize(const MyGUI::IntSize& size);
void updateCoord(); void updateCoord();
const sol::table& getLayout() { return mLayout; } const sol::table& getLayout() { return mLayout; }
@ -55,15 +57,22 @@ namespace LuaUi
return parseExternal(mExternal, name, defaultValue); return parseExternal(mExternal, name, defaultValue);
} }
void onCoordChange(const std::optional<std::function<void(WidgetExtension*, MyGUI::IntCoord)>>& callback)
{
mOnCoordChange = callback;
}
protected: protected:
virtual void initialize(); virtual void initialize();
sol::table makeTable() const; sol::table makeTable() const;
sol::object keyEvent(MyGUI::KeyCode) const; sol::object keyEvent(MyGUI::KeyCode) const;
sol::object mouseEvent(int left, int top, MyGUI::MouseButton button) const; sol::object mouseEvent(int left, int top, MyGUI::MouseButton button) const;
MyGUI::IntSize parentSize();
virtual MyGUI::IntSize calculateSize(); virtual MyGUI::IntSize calculateSize();
virtual MyGUI::IntPoint calculatePosition(const MyGUI::IntSize& size); virtual MyGUI::IntPoint calculatePosition(const MyGUI::IntSize& size);
MyGUI::IntCoord calculateCoord(); MyGUI::IntCoord calculateCoord();
virtual MyGUI::IntSize childScalingSize();
template<typename T> template<typename T>
T propertyValue(std::string_view name, const T& defaultValue) T propertyValue(std::string_view name, const T& defaultValue)
@ -76,6 +85,7 @@ namespace LuaUi
virtual void updateTemplate(); virtual void updateTemplate();
virtual void updateProperties(); virtual void updateProperties();
virtual void updateChildren() {};
void triggerEvent(std::string_view name, const sol::object& argument) const; void triggerEvent(std::string_view name, const sol::object& argument) const;
@ -101,6 +111,7 @@ namespace LuaUi
sol::object mProperties; sol::object mProperties;
sol::object mTemplateProperties; sol::object mTemplateProperties;
sol::object mExternal; sol::object mExternal;
WidgetExtension* mParent;
void attach(WidgetExtension* ext); void attach(WidgetExtension* ext);
@ -119,6 +130,8 @@ namespace LuaUi
void mouseRelease(MyGUI::Widget*, int, int, MyGUI::MouseButton); void mouseRelease(MyGUI::Widget*, int, int, MyGUI::MouseButton);
void focusGain(MyGUI::Widget*, MyGUI::Widget*); void focusGain(MyGUI::Widget*, MyGUI::Widget*);
void focusLoss(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 class LuaWidget : public MyGUI::Widget, public WidgetExtension

View File

@ -36,6 +36,11 @@
-- @param #Layout layout -- @param #Layout layout
-- @return #Element -- @return #Element
---
-- Adds a settings page to main menu setting's Scripts tab.
-- @function [parent=#ui] registerSettingsPage
-- @param #SettingsPage page
--- ---
-- Layout -- Layout
-- @type Layout -- @type Layout
@ -137,4 +142,11 @@
-- @field openmw.util#Vector2 offset Position of the mouse cursor relative to the widget -- @field openmw.util#Vector2 offset Position of the mouse cursor relative to the widget
-- @field #number button Mouse button which triggered the event (could be nil) -- @field #number button Mouse button which triggered the event (could be nil)
---
-- Settings page parameters, passed as an argument to ui.registerSettingsPage
-- @type SettingsPage
-- @field #string name Name of the page, displayed in the list, used for search
-- @field #string searchHints Additional keywords used in search, not displayed anywhere
-- @field #Element element The page's UI, which will be attached to the settings tab. The root widget has to have a fixed size (set `size` field in `props`, see Widget documentation, `relativeSize` is ignored)
return nil return nil

View File

@ -14,4 +14,11 @@
<Child type="ScrollBar" skin="MW_HScroll" offset="3 498 513 14" align="Left Bottom HStretch" name="HScroll"/> <Child type="ScrollBar" skin="MW_HScroll" offset="3 498 513 14" align="Left Bottom HStretch" name="HScroll"/>
</Resource> </Resource>
<Resource type="ResourceSkin" name="MW_ScrollViewVH" size="516 516">
<Property key="NeedKey" value="false"/>
<Child type="Widget" skin="" offset="0 0 502 502" align="Stretch" name="Client"/>
<Child type="ScrollBar" skin="MW_VScroll" offset="498 3 14 492" align="Right Top VStretch" name="VScroll"/>
<Child type="ScrollBar" skin="MW_HScroll" offset="3 498 492 14" align="Left Bottom HStretch" name="HScroll"/>
</Resource>
</MyGUI> </MyGUI>

View File

@ -656,6 +656,26 @@
--> -->
</Widget> </Widget>
</Widget> </Widget>
<Widget type="TabItem" skin="" position="4 32 360 308">
<Property key="Caption" value=" Scripts "/>
<Widget name="ScriptFilter" type="EditBox" skin="MW_TextEdit" position="0 0 150 40"/>
<Widget name="ScriptList" type="ListBox" skin="MW_List" position="0 40 150 268"/>
<Widget name="ScriptBox" type="Widget" skin="MW_Box" position="150 0 250 250" name="ScriptBox">
<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>
<Widget name="ScriptDisabled" type="EditBox" skin="SandText" position="0 0 360 308" align="Center">
<Property key="Visible" value="false"/>
<Property key="Caption" value="Scripts settings are only available when a game is loaded."/>
<Property key="TextAlign" value="Center" />
<Property key="MultiLine" value="true" />
<Property key="WordWrap" value="true" />
<Property key="Static" value="true"/>
</Widget>
</Widget>
</Widget> </Widget>
<Widget type="AutoSizedButton" skin="MW_Button" position="320 420 56 24" align="Right Bottom" name="OkButton"> <Widget type="AutoSizedButton" skin="MW_Button" position="320 420 56 24" align="Right Bottom" name="OkButton">
<Property key="ExpandDirection" value="Left"/> <Property key="ExpandDirection" value="Left"/>