Update and document Lua Text and TextEdit widget types, fix some issues with Lua UI

C++20
uramer 3 years ago committed by Petr Mikheev
parent 7f4d4c0d70
commit 581c3f4882

@ -3,6 +3,7 @@
#include <components/lua_ui/layers.hpp>
#include <components/lua_ui/content.hpp>
#include <components/lua_ui/registerscriptsettings.hpp>
#include <components/lua_ui/alignment.hpp>
#include "context.hpp"
#include "actions.hpp"
@ -239,6 +240,12 @@ namespace MWLua
typeTable.set(it.second, it.first);
api["TYPE"] = LuaUtil::makeReadOnly(typeTable);
api["ALIGNMENT"] = LuaUtil::makeReadOnly(context.mLua->tableFromPairs<std::string_view, LuaUi::Alignment>({
{ "Start", LuaUi::Alignment::Start },
{ "Center", LuaUi::Alignment::Center },
{ "End", LuaUi::Alignment::End }
}));
api["registerSettingsPage"] = &LuaUi::registerSettingsPage;
return LuaUtil::makeReadOnly(api);

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

@ -0,0 +1,18 @@
#include "alignment.hpp"
namespace LuaUi
{
MyGUI::Align alignmentToMyGui(Alignment horizontal, Alignment vertical)
{
MyGUI::Align align(MyGUI::Align::Center);
if (horizontal == Alignment::Start)
align |= MyGUI::Align::Left;
if (horizontal == Alignment::End)
align |= MyGUI::Align::Right;
if (horizontal == Alignment::Start)
align |= MyGUI::Align::Top;
if (horizontal == Alignment::End)
align |= MyGUI::Align::Bottom;
return align;
}
}

@ -0,0 +1,18 @@
#ifndef OPENMW_LUAUI_ALIGNMENT
#define OPENMW_LUAUI_ALIGNMENT
#include <MyGUI_Align.h>
namespace LuaUi
{
enum class Alignment
{
Start = 0,
Center = 1,
End = 2
};
MyGUI::Align alignmentToMyGui(Alignment horizontal, Alignment vertical);
}
#endif // !OPENMW_LUAUI_PROPERTIES

@ -10,8 +10,8 @@ namespace LuaUi
MYGUI_RTTI_DERIVED(LuaContainer)
protected:
virtual void updateChildren() override;
virtual MyGUI::IntSize childScalingSize() override;
void updateChildren() override;
MyGUI::IntSize childScalingSize() override;
private:
void updateSizeToFit();

@ -130,6 +130,8 @@ 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->setLayout(layout);
ext->setExternal(layout.get<sol::object>(LayoutKeys::external));
setTemplate(ext, layout.get<sol::object>(LayoutKeys::templateLayout));

@ -19,9 +19,9 @@ namespace LuaUi
// mCoord could be zero, prevent division by 0
// use arbitrary large numbers to prevent performance issues
if (mTileSize.width == 0)
if (mTileSize.width <= 0)
mTileSize.width = 1e7;
if (mTileSize.height == 0)
if (mTileSize.height <= 0)
mTileSize.height = 1e7;
}

@ -29,7 +29,7 @@ namespace LuaUi
LuaImage();
protected:
virtual void updateProperties() override;
void updateProperties() override;
LuaTileRect* mTileRect;
};
}

@ -6,6 +6,7 @@
#include <osg/Vec2>
#include <components/lua/luastate.hpp>
#include <components/misc/color.hpp>
namespace LuaUi
{
@ -18,6 +19,12 @@ namespace LuaUi
std::is_same<T, MyGUI::FloatSize>();
}
template <typename T>
constexpr bool isMyGuiColor()
{
return std::is_same<T, MyGUI::Colour>();
}
template <typename T, typename LuaT>
sol::optional<T> parseValue(
sol::object table,
@ -41,6 +48,8 @@ namespace LuaUi
LuaT luaT = opt.as<LuaT>();
if constexpr (isMyGuiVector<T>())
return T(luaT.x(), luaT.y());
else if constexpr (isMyGuiColor<T>())
return T(luaT.r(), luaT.g(), luaT.b(), luaT.a());
else
return luaT;
}
@ -53,6 +62,8 @@ namespace LuaUi
{
if constexpr (isMyGuiVector<T>())
return parseValue<T, osg::Vec2f>(table, field, errorPrefix);
else if constexpr (isMyGuiColor<T>())
return parseValue<T, Misc::Color>(table, field, errorPrefix);
else
return parseValue<T, T>(table, field, errorPrefix);
}

@ -1,18 +1,40 @@
#include "text.hpp"
#include "alignment.hpp"
namespace LuaUi
{
LuaText::LuaText()
: mAutoSized(true)
{}
void LuaText::initialize()
{
changeWidgetSkin("NormalText");
changeWidgetSkin("SandText");
setEditStatic(true);
setVisibleHScroll(false);
setVisibleVScroll(false);
WidgetExtension::initialize();
}
void LuaText::updateProperties()
{
setCaption(propertyValue("caption", std::string()));
mAutoSized = propertyValue("autoSize", true);
setCaption(propertyValue("text", std::string()));
setFontHeight(propertyValue("textSize", 10));
setTextColour(propertyValue("textColor", MyGUI::Colour(0, 0, 0, 1)));
setEditMultiLine(propertyValue("multiline", false));
setEditWordWrap(propertyValue("wordWrap", false));
Alignment horizontal(propertyValue("textAlignH", Alignment::Start));
Alignment vertical(propertyValue("textAlignV", Alignment::Start));
setTextAlign(alignmentToMyGui(horizontal, vertical));
setTextShadow(propertyValue("textShadow", false));
setTextShadowColour(propertyValue("textShadowColor", MyGUI::Colour(0, 0, 0, 1)));
WidgetExtension::updateProperties();
}

@ -1,26 +1,27 @@
#ifndef OPENMW_LUAUI_TEXT
#define OPENMW_LUAUI_TEXT
#include <MyGUI_TextBox.h>
#include <MyGUI_EditBox.h>
#include "widget.hpp"
namespace LuaUi
{
class LuaText : public MyGUI::TextBox, public WidgetExtension
class LuaText : public MyGUI::EditBox, public WidgetExtension
{
MYGUI_RTTI_DERIVED(LuaText)
public:
LuaText();
virtual void updateProperties() override;
void initialize() override;
void updateProperties() override;
void setCaption(const MyGUI::UString& caption) override;
private:
bool mAutoSized;
protected:
virtual MyGUI::IntSize calculateSize() override;
MyGUI::IntSize calculateSize() override;
};
}

@ -1,11 +1,43 @@
#include "textedit.hpp"
#include "alignment.hpp"
namespace LuaUi
{
void LuaTextEdit::initialize()
{
changeWidgetSkin("LuaTextEdit");
eventEditTextChange += MyGUI::newDelegate(this, &LuaTextEdit::textChange);
WidgetExtension::initialize();
}
void LuaTextEdit::deinitialize()
{
eventEditTextChange -= MyGUI::newDelegate(this, &LuaTextEdit::textChange);
WidgetExtension::deinitialize();
}
void LuaTextEdit::updateProperties()
{
setCaption(propertyValue("caption", std::string()));
setCaption(propertyValue("text", std::string()));
setFontHeight(propertyValue("textSize", 10));
setTextColour(propertyValue("textColor", MyGUI::Colour(0, 0, 0, 1)));
setEditMultiLine(propertyValue("multiline", false));
setEditWordWrap(propertyValue("wordWrap", false));
Alignment horizontal(propertyValue("textAlignH", Alignment::Start));
Alignment vertical(propertyValue("textAlignV", Alignment::Start));
setTextAlign(alignmentToMyGui(horizontal, vertical));
setEditStatic(propertyValue("readOnly", false));
WidgetExtension::updateProperties();
}
void LuaTextEdit::textChange(MyGUI::EditBox*)
{
triggerEvent("textChanged", sol::make_object(lua(), getCaption().asUTF8()));
}
}

@ -11,7 +11,13 @@ namespace LuaUi
{
MYGUI_RTTI_DERIVED(LuaTextEdit)
virtual void updateProperties() override;
protected:
void initialize() override;
void deinitialize() override;
void updateProperties() override;
private:
void textChange(MyGUI::EditBox*);
};
}

@ -10,14 +10,13 @@
namespace LuaUi
{
WidgetExtension::WidgetExtension()
: mForcedCoord()
, mAbsoluteCoord()
, mRelativeCoord()
, mAnchor()
, mLua(nullptr)
: mLua(nullptr)
, mWidget(nullptr)
, mSlot(this)
, mLayout(sol::nil)
, mProperties(sol::nil)
, mTemplateProperties(sol::nil)
, mExternal(sol::nil)
, mParent(nullptr)
{}
@ -80,16 +79,22 @@ namespace LuaUi
ext->updateCoord();
}
WidgetExtension* WidgetExtension::findFirst(std::string_view flagName)
void WidgetExtension::attachTemplate(WidgetExtension* ext)
{
ext->widget()->attachToWidget(widget());
ext->updateCoord();
}
WidgetExtension* WidgetExtension::findDeep(std::string_view flagName)
{
if (externalValue(flagName, false))
return this;
for (WidgetExtension* w : mChildren)
{
WidgetExtension* result = w->findFirst(flagName);
WidgetExtension* result = w->findDeep(flagName);
if (result != nullptr)
return result;
}
if (externalValue(flagName, false))
return this;
return nullptr;
}
@ -101,11 +106,11 @@ namespace LuaUi
w->findAll(flagName, result);
}
WidgetExtension* WidgetExtension::findFirstInTemplates(std::string_view flagName)
WidgetExtension* WidgetExtension::findDeepInTemplates(std::string_view flagName)
{
for (WidgetExtension* w : mTemplateChildren)
{
WidgetExtension* result = w->findFirst(flagName);
WidgetExtension* result = w->findDeep(flagName);
if (result != nullptr)
return result;
}
@ -163,7 +168,7 @@ namespace LuaUi
for (size_t i = 0; i < children.size(); ++i)
{
mTemplateChildren[i] = children[i];
mTemplateChildren[i]->widget()->attachToWidget(mWidget);
attachTemplate(mTemplateChildren[i]);
}
updateTemplate();
}
@ -171,9 +176,11 @@ namespace LuaUi
void WidgetExtension::updateTemplate()
{
WidgetExtension* oldSlot = mSlot;
mSlot = findFirstInTemplates("slot");
if (mSlot == nullptr)
WidgetExtension* slot = findDeepInTemplates("slot");
if (slot == nullptr)
mSlot = this;
else
mSlot = slot->mSlot;
if (mSlot != oldSlot)
for (WidgetExtension* w : mChildren)
attach(w);
@ -281,7 +288,7 @@ namespace LuaUi
MyGUI::IntSize WidgetExtension::childScalingSize()
{
return widget()->getSize();
return mSlot->widget()->getSize();
}
void WidgetExtension::triggerEvent(std::string_view name, const sol::object& argument = sol::nil) const

@ -50,6 +50,7 @@ namespace LuaUi
const sol::table& getLayout() { return mLayout; }
void setLayout(const sol::table& layout) { mLayout = layout; }
void resetSlot() { mSlot = this; }
template <typename T>
T externalValue(std::string_view name, const T& defaultValue)
@ -80,13 +81,14 @@ namespace LuaUi
return parseProperty(mProperties, mTemplateProperties, name, defaultValue);
}
WidgetExtension* findFirstInTemplates(std::string_view flagName);
WidgetExtension* findDeepInTemplates(std::string_view flagName);
std::vector<WidgetExtension*> findAllInTemplates(std::string_view flagName);
virtual void updateTemplate();
virtual void updateProperties();
virtual void updateChildren() {};
lua_State* lua() { return mLua; }
void triggerEvent(std::string_view name, const sol::object& argument) const;
// offsets the position and size, used only in C++ widget code
@ -114,8 +116,9 @@ namespace LuaUi
WidgetExtension* mParent;
void attach(WidgetExtension* ext);
void attachTemplate(WidgetExtension* ext);
WidgetExtension* findFirst(std::string_view name);
WidgetExtension* findDeep(std::string_view name);
void findAll(std::string_view flagName, std::vector<WidgetExtension*>& result);
void updateChildrenCoord();

@ -5,10 +5,7 @@
namespace LuaUi
{
LuaWindow::LuaWindow()
: mCaption()
, mPreviousMouse()
, mChangeScale()
, mMoveResize()
: mCaption(nullptr)
{}
void LuaWindow::updateTemplate()
@ -20,7 +17,7 @@ namespace LuaUi
}
mActionWidgets.clear();
WidgetExtension* captionWidget = findFirstInTemplates("caption");
WidgetExtension* captionWidget = findDeepInTemplates("caption");
mCaption = dynamic_cast<LuaText*>(captionWidget);
if (mCaption)

@ -14,8 +14,8 @@ namespace LuaUi
public:
LuaWindow();
virtual void updateTemplate() override;
virtual void updateProperties() override;
void updateTemplate() override;
void updateProperties() override;
private:
LuaText* mCaption;

@ -1,11 +1,6 @@
User interface reference
========================
.. toctree::
:hidden:
widgets/widget
Layouts
-------
@ -79,103 +74,57 @@ Events
Widget types
------------
.. list-table::
:widths: 30 70
.. toctree::
:maxdepth: 1
* - :ref:`Widget`
- Base widget type, all the other widget inherit its properties and events.
* - `Text`
- Displays text.
* - EditText
- Accepts text input from the user.
* - Window
- Can be moved and resized by the user.
Widget: Base widget type, all the other widgets inherit its properties and events. <widgets/widget>
Text: Displays text. <widgets/text>
TextEdit: Accepts text input from the user. <widgets/textedit>
Example
-------
*scripts/requirePassword.lua*
*scripts/clock.lua*
.. code-block:: Lua
local core = require('openmw.core')
local async = require('openmw.async')
local ui = require('openmw.ui')
local v2 = require('openmw.util').vector2
local layout = {
layers = 'Windows',
type = ui.TYPE.Window,
template = { skin = 'MW_Window' }, -- TODO: replace all skins here when they are re-implemented in Lua
props = {
size = v2(200, 250),
-- put the window in the middle of the screen
relativePosition = v2(0.5, 0.5),
anchor = v2(0.5, 0.5),
},
content = ui.content {
{
type = ui.TYPE.Text,
template = { skin = 'SandText' },
props = {
caption = 'Input password',
relativePosition = v2(0.5, 0),
anchor = v2(0.5, 0),
},
},
{
name = 'input',
type = ui.TYPE.TextEdit,
template = { skin = "MW_TextEdit" },
props = {
caption = '',
relativePosition = v2(0.5, 0.5),
anchor = v2(0.5, 0.5),
size = v2(125, 50),
},
events = {}
},
{
name = 'submit',
type = ui.TYPE.Text, -- TODO: replace with button when implemented
template = { skin = "MW_Button" },
props = {
caption = 'Submit',
-- position at the bottom
relativePosition = v2(0.5, 1.0),
anchor = v2(0.5, 1.0),
autoSize = false,
size = v2(75, 50),
},
events = {},
},
},
}
local element = nil
local input = layout.content.input
-- TODO: replace with a better event when TextEdit is finished
input.events.textInput = async:callback(function(text)
input.props.caption = input.props.caption .. text
end)
local submit = layout.content.submit
submit.events.mouseClick = async:callback(function()
if input.props.caption == 'very secret password' then
if element then
element:destroy()
end
else
print('wrong password', input.props.caption)
core.quit()
end
end)
element = ui.create(layout)
*requirePassword.omwscripts*
local ui = require('openmw.ui')
local util = require('openmw.util')
local calendar = require('openmw_aux.calendar')
local time = require('openmw_aux.time')
local element = ui.create {
-- important not to forget the layer
-- by default widgets are not attached to any layer and are not visible
layer = 'HUD',
type = ui.TYPE.Text,
props = {
-- position in the top right corner
relativePosition = util.vector2(1, 0),
-- position is for the top left corner of the widget by default
-- change it to align exactly to the top right corner of the screen
anchor = util.vector2(1, 0),
text = calendar.formatGameTime('%H:%M'),
textSize = 24,
-- default black text color isn't always visible
textColor = util.color.rgb(0, 1, 0),
},
}
local function updateTime()
-- formatGameTime uses current time by default
-- otherwise we could get it by calling `core.getGameTime()`
element.layout.props.text = calendar.formatGameTime('%H:%M')
-- the layout changes won't affect the widget unless we request an update
element:update()
end
-- we are showing game time in hours and minutes
-- so no need to update more often than ones a game minute
time.runRepeatedly(updateTime, 1 * time.minute, { type = time.GameTime })
*clock.omwscripts*
::
PLAYER: scripts/requirePassword.lua
PLAYER: scripts/clock.lua

@ -0,0 +1,44 @@
Text Widget
===========
Properties
----------
.. list-table::
:header-rows: 1
:widths: 20 20 60
* - name
- type (default value)
- description
* - autoSize
- boolean (true)
- | Adjusts this widget's size to fit the text exactly.
| Ignores `size` and `relativeSize`.
* - text
- string ('')
- The text to display.
* - textSize
- number (10)
- The size of the text.
* - textColor
- util.color (0, 0, 0, 1)
- The color of the text.
* - multiline
- boolean (false)
- Whether to render text on multiple lines.
* - wordWrap
- boolean (false)
- Whether to break text into lines to fit the widget's width.
* - textAlignH
- ui.ALIGNMENT (Start)
- Horizontal alignment of the text.
* - textAlignV
- ui.ALIGNMENT (Start)
- Vertical alignment of the text.
* - textShadow
- boolean (false)
- Whether to render a shadow behind the text.
* - textShadowColor
- util.color (0, 0, 0, 1)
- The color of the text shadow.

@ -0,0 +1,51 @@
TextEdit Widget
===============
Properties
----------
.. list-table::
:header-rows: 1
:widths: 20 20 60
* - name
- type (default value)
- description
* - text
- string ('')
- The text to display.
* - textSize
- number (10)
- The size of the text.
* - textColor
- util.color (0, 0, 0, 1)
- The color of the text.
* - multiline
- boolean (false)
- Whether to render text on multiple lines.
* - wordWrap
- boolean (false)
- Whether to break text into lines to fit the widget's width.
* - textAlignH
- ui.ALIGNMENT (Start)
- Horizontal alignment of the text.
* - textAlignV
- ui.ALIGNMENT (Start)
- Vertical alignment of the text.
* - readOnly
- boolean (false)
- Whether the text can be edited.
Events
------
.. list-table::
:header-rows: 1
:widths: 20 20 60
* - name
- first argument type
- description
* - textChanged
- string
- Displayed text changed (e. g. by user input)

@ -6,18 +6,32 @@
-- local ui = require('openmw.ui')
---
-- @field [parent=#ui] #WIDGET_TYPE WIDGET_TYPE
-- Widget types
-- @field [parent=#ui] #TYPE TYPE
---
-- Alignment values (left to right, top to bottom)
-- @field [parent=#ui] #ALIGNMENT ALIGNMENT
---
-- Tools for working with layers
-- @field [parent=#ui] #Layers layers
---
-- @type WIDGET_TYPE
-- @field [parent=#WIDGET_TYPE] Widget Base widget type
-- @field [parent=#WIDGET_TYPE] Text Display text
-- @field [parent=#WIDGET_TYPE] TextEdit Accepts user text input
-- @field [parent=#WIDGET_TYPE] Window Can be moved and resized by the user
-- All available widget types
-- @type TYPE
-- @field Widget Base widget type
-- @field Text Display text
-- @field TextEdit Accepts user text input
-- @field Window Can be moved and resized by the user
---
-- Alignment values (details depend on the specific property).
-- For horizontal alignment the order is left to right, for vertical alignment the order is top to bottom.
-- @type ALIGNMENT
-- @field Start
-- @field Center
-- @field End
---
-- Shows given message at the bottom of the screen.

@ -43,6 +43,7 @@ set(MYGUI_FILES
openmw_journal.layout
openmw_journal.skin.xml
openmw_layers.xml
openmw_lua.xml
openmw_list.skin.xml
openmw_mainmenu.layout
openmw_mainmenu.skin.xml

@ -15,8 +15,4 @@
<Resource type="ResourceSkin" name="ImageBox" size="16 16">
<BasisSkin type="MainSkin" offset="0 0 16 16"/>
</Resource>
<Resource type="ResourceSkin" name="LuaImage">
<BasisSkin type="LuaTileRect"/>
</Resource>
</MyGUI>

@ -5,6 +5,7 @@
<List file="openmw_layers.xml" />
<List file="openmw_pointer.xml" />
<List file="openmw_settings.xml" />
<List file="openmw_lua.xml" />
</MyGUI>
</MyGUI>

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<MyGUI type="Resource" version="1.1">
<Resource type="ResourceSkin" name="LuaImage">
<BasisSkin type="LuaTileRect"/>
</Resource>
<Resource type="ResourceSkin" name="LuaTextEdit" size="512 20">
<Property key="FontName" value="Default"/>
<Property key="TextAlign" value="Left VCenter"/>
<Property key="TextColour" value="#{fontcolour=normal}"/>
<Child type="TextBox" skin="MW_TextEditClient" offset="0 0 502 20" align="Stretch" name="Client"/>
</Resource>
</MyGUI>
Loading…
Cancel
Save