From e65c9464522571139d96d4f954a7d0715a662d06 Mon Sep 17 00:00:00 2001 From: uramer Date: Thu, 2 Dec 2021 21:52:29 +0000 Subject: [PATCH] Add a data field to Lua UI layouts --- components/lua_ui/element.cpp | 7 +++++++ components/lua_ui/widget.cpp | 5 ++++- components/lua_ui/widget.hpp | 5 ++++- docs/source/reference/lua-scripting/user_interface.rst | 8 ++++++++ docs/source/reference/lua-scripting/widgets/widget.rst | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/components/lua_ui/element.cpp b/components/lua_ui/element.cpp index 1a1b38e4dc..63ae0e7d5e 100644 --- a/components/lua_ui/element.cpp +++ b/components/lua_ui/element.cpp @@ -46,6 +46,11 @@ namespace LuaUi } } + void setLayout(LuaUi::WidgetExtension* ext, const sol::table& layout) + { + ext->setLayout(layout); + } + LuaUi::WidgetExtension* createWidget(const sol::table& layout, LuaUi::WidgetExtension* parent) { std::string type = widgetType(layout); @@ -72,6 +77,7 @@ namespace LuaUi setEventCallbacks(ext, layout); setProperties(ext, layout); + setLayout(ext, layout); Content cont = content(layout); for (size_t i = 0; i < cont.size(); i++) @@ -90,6 +96,7 @@ namespace LuaUi { setEventCallbacks(ext, layout); setProperties(ext, layout); + setLayout(ext, layout); Content newContent = content(layout); diff --git a/components/lua_ui/widget.cpp b/components/lua_ui/widget.cpp index ef876e8a06..5653659287 100644 --- a/components/lua_ui/widget.cpp +++ b/components/lua_ui/widget.cpp @@ -13,13 +13,16 @@ namespace LuaUi , mAbsoluteCoord() , mRelativeCoord() , mAnchor() + , mLua{ nullptr } + , mWidget{ nullptr } + , mLayout{ sol::nil } {} void WidgetExtension::triggerEvent(std::string_view name, const sol::object& argument = sol::nil) const { auto it = mCallbacks.find(name); if (it != mCallbacks.end()) - it->second(argument); + it->second(argument, mLayout); } void WidgetExtension::create(lua_State* lua, MyGUI::Widget* self) diff --git a/components/lua_ui/widget.hpp b/components/lua_ui/widget.hpp index 360c6483ba..99d430f71c 100644 --- a/components/lua_ui/widget.hpp +++ b/components/lua_ui/widget.hpp @@ -43,6 +43,8 @@ namespace LuaUi void setForcedCoord(const MyGUI::IntCoord& offset); void updateCoord(); + void setLayout(const sol::table& layout) { mLayout = layout; } + protected: sol::table makeTable() const; sol::object keyEvent(MyGUI::KeyCode) const; @@ -67,11 +69,12 @@ namespace LuaUi private: // use lua_State* instead of sol::state_view because MyGUI requires a default constructor - lua_State* mLua; + lua_State* mLua; MyGUI::Widget* mWidget; std::vector mContent; std::map> mCallbacks; + sol::table mLayout; void updateChildrenCoord(MyGUI::Widget*); diff --git a/docs/source/reference/lua-scripting/user_interface.rst b/docs/source/reference/lua-scripting/user_interface.rst index 5b733a8fe9..88233dceb9 100644 --- a/docs/source/reference/lua-scripting/user_interface.rst +++ b/docs/source/reference/lua-scripting/user_interface.rst @@ -46,6 +46,14 @@ A container holding all the widget's children. It has a few important difference .. TODO: Talk about skins/templates here when they are ready +Events +------ + +| A table mapping event names to `openmw.async.callback` s. +| When an event triggers, the callback is called with two arguments: + an event-specific value, and that widget's layout table. +| See the Widget type pages for information on what events exist, and which first argument they pass. + Widget types ------------ diff --git a/docs/source/reference/lua-scripting/widgets/widget.rst b/docs/source/reference/lua-scripting/widgets/widget.rst index 51d6d17203..49058ee278 100644 --- a/docs/source/reference/lua-scripting/widgets/widget.rst +++ b/docs/source/reference/lua-scripting/widgets/widget.rst @@ -41,7 +41,7 @@ Events :widths: 20 20 60 * - name - - type + - first argument type - description * - keyPress - `KeyboardEvent <../openmw_input.html##(KeyboardEvent)>`_