mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-21 22:41:35 +00:00
Merge branch 'lua_ui_reset_properties' into 'master'
Correctly set UI properties to defaults when passed nil See merge request OpenMW/openmw!1439
This commit is contained in:
commit
f10202dbbe
11 changed files with 112 additions and 126 deletions
|
@ -162,7 +162,8 @@ add_component_dir (queries
|
||||||
)
|
)
|
||||||
|
|
||||||
add_component_dir (lua_ui
|
add_component_dir (lua_ui
|
||||||
widget widgetlist element content text textedit window
|
widget widgetlist element content
|
||||||
|
text textedit window
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -24,18 +24,7 @@ namespace LuaUi
|
||||||
|
|
||||||
void setProperties(LuaUi::WidgetExtension* ext, const sol::table& layout)
|
void setProperties(LuaUi::WidgetExtension* ext, const sol::table& layout)
|
||||||
{
|
{
|
||||||
auto props = layout.get<sol::optional<sol::table>>("props");
|
ext->setProperties(layout.get<sol::object>("props"));
|
||||||
if (props.has_value())
|
|
||||||
{
|
|
||||||
props.value().for_each([ext](const sol::object& key, const sol::object& value)
|
|
||||||
{
|
|
||||||
if (key.is<std::string_view>())
|
|
||||||
ext->setProperty(key.as<std::string_view>(), value);
|
|
||||||
else
|
|
||||||
Log(Debug::Warning) << "UI property key must be a string";
|
|
||||||
});
|
|
||||||
ext->updateCoord();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setEventCallbacks(LuaUi::WidgetExtension* ext, const sol::table& layout)
|
void setEventCallbacks(LuaUi::WidgetExtension* ext, const sol::table& layout)
|
||||||
|
|
64
components/lua_ui/properties.hpp
Normal file
64
components/lua_ui/properties.hpp
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
#ifndef OPENMW_LUAUI_PROPERTIES
|
||||||
|
#define OPENMW_LUAUI_PROPERTIES
|
||||||
|
|
||||||
|
#include <sol/sol.hpp>
|
||||||
|
#include <MyGUI_Types.h>
|
||||||
|
#include <osg/Vec2>
|
||||||
|
|
||||||
|
#include <components/lua/luastate.hpp>
|
||||||
|
|
||||||
|
namespace LuaUi
|
||||||
|
{
|
||||||
|
template <typename T>
|
||||||
|
sol::optional<T> getProperty(sol::object from, std::string_view field) {
|
||||||
|
sol::object value = LuaUtil::getFieldOrNil(from, field);
|
||||||
|
if (value == sol::nil)
|
||||||
|
return sol::nullopt;
|
||||||
|
if (value.is<T>())
|
||||||
|
return value.as<T>();
|
||||||
|
std::string error("Property \"");
|
||||||
|
error += field;
|
||||||
|
error += "\" has an invalid value \"";
|
||||||
|
error += LuaUtil::toString(value);
|
||||||
|
error += "\"";
|
||||||
|
throw std::logic_error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T parseProperty(sol::object from, std::string_view field, const T& defaultValue)
|
||||||
|
{
|
||||||
|
sol::optional<T> opt = getProperty<T>(from, field);
|
||||||
|
if (opt.has_value())
|
||||||
|
return opt.value();
|
||||||
|
else
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
MyGUI::types::TPoint<T> parseProperty(
|
||||||
|
sol::object from,
|
||||||
|
std::string_view field,
|
||||||
|
const MyGUI::types::TPoint<T>& defaultValue)
|
||||||
|
{
|
||||||
|
auto v = getProperty<osg::Vec2f>(from, field);
|
||||||
|
if (v.has_value())
|
||||||
|
return MyGUI::types::TPoint<T>(v.value().x(), v.value().y());
|
||||||
|
else
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
MyGUI::types::TSize<T> parseProperty(
|
||||||
|
sol::object from,
|
||||||
|
std::string_view field,
|
||||||
|
const MyGUI::types::TSize<T>& defaultValue)
|
||||||
|
{
|
||||||
|
auto v = getProperty<osg::Vec2f>(from, field);
|
||||||
|
if (v.has_value())
|
||||||
|
return MyGUI::types::TSize<T>(v.value().x(), v.value().y());
|
||||||
|
else
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !OPENMW_LUAUI_PROPERTIES
|
|
@ -12,25 +12,11 @@ namespace LuaUi
|
||||||
WidgetExtension::initialize();
|
WidgetExtension::initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LuaText::setPropertyRaw(std::string_view name, sol::object value)
|
void LuaText::setProperties(sol::object props)
|
||||||
{
|
{
|
||||||
if (name == "caption")
|
setCaption(parseProperty(props, "caption", std::string()));
|
||||||
{
|
mAutoSized = parseProperty(props, "autoSize", true);
|
||||||
if (!value.is<std::string>())
|
WidgetExtension::setProperties(props);
|
||||||
return false;
|
|
||||||
setCaption(value.as<std::string>());
|
|
||||||
}
|
|
||||||
else if (name == "autoSize")
|
|
||||||
{
|
|
||||||
if (!value.is<bool>())
|
|
||||||
return false;
|
|
||||||
mAutoSized = value.as<bool>();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return WidgetExtension::setPropertyRaw(name, value);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MyGUI::IntSize LuaText::calculateSize()
|
MyGUI::IntSize LuaText::calculateSize()
|
||||||
|
|
|
@ -14,13 +14,13 @@ namespace LuaUi
|
||||||
public:
|
public:
|
||||||
LuaText();
|
LuaText();
|
||||||
virtual void initialize() override;
|
virtual void initialize() override;
|
||||||
|
virtual void setProperties(sol::object) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool mAutoSized;
|
bool mAutoSized;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual MyGUI::IntSize calculateSize() override;
|
virtual MyGUI::IntSize calculateSize() override;
|
||||||
bool setPropertyRaw(std::string_view name, sol::object value) override;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,18 +2,9 @@
|
||||||
|
|
||||||
namespace LuaUi
|
namespace LuaUi
|
||||||
{
|
{
|
||||||
bool LuaTextEdit::setPropertyRaw(std::string_view name, sol::object value)
|
void LuaTextEdit::setProperties(sol::object props)
|
||||||
{
|
{
|
||||||
if (name == "caption")
|
setCaption(parseProperty(props, "caption", std::string()));
|
||||||
{
|
WidgetExtension::setProperties(props);
|
||||||
if (!value.is<std::string>())
|
|
||||||
return false;
|
|
||||||
setCaption(value.as<std::string>());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return WidgetExtension::setPropertyRaw(name, value);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,7 @@ namespace LuaUi
|
||||||
{
|
{
|
||||||
MYGUI_RTTI_DERIVED(LuaTextEdit)
|
MYGUI_RTTI_DERIVED(LuaTextEdit)
|
||||||
|
|
||||||
protected:
|
virtual void setProperties(sol::object) override;
|
||||||
bool setPropertyRaw(std::string_view name, sol::object value) override;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -142,19 +142,12 @@ namespace LuaUi
|
||||||
mCallbacks.clear();
|
mCallbacks.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetExtension::setProperty(std::string_view name, sol::object value)
|
MyGUI::IntCoord WidgetExtension::forcedCoord()
|
||||||
{
|
|
||||||
if (!setPropertyRaw(name, value))
|
|
||||||
Log(Debug::Error) << "Invalid value of property " << name
|
|
||||||
<< ": " << LuaUtil::toString(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
MyGUI::IntCoord WidgetExtension::forcedOffset()
|
|
||||||
{
|
{
|
||||||
return mForcedCoord;
|
return mForcedCoord;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetExtension::setForcedOffset(const MyGUI::IntCoord& offset)
|
void WidgetExtension::setForcedCoord(const MyGUI::IntCoord& offset)
|
||||||
{
|
{
|
||||||
mForcedCoord = offset;
|
mForcedCoord = offset;
|
||||||
}
|
}
|
||||||
|
@ -164,55 +157,16 @@ namespace LuaUi
|
||||||
mWidget->setCoord(calculateCoord());
|
mWidget->setCoord(calculateCoord());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WidgetExtension::setPropertyRaw(std::string_view name, sol::object value)
|
void WidgetExtension::setProperties(sol::object props)
|
||||||
{
|
{
|
||||||
if (name == "position")
|
mAbsoluteCoord = parseProperty(props, "position", MyGUI::IntPoint());
|
||||||
{
|
mAbsoluteCoord = parseProperty(props, "size", MyGUI::IntSize());
|
||||||
if (!value.is<osg::Vec2f>())
|
mRelativeCoord = parseProperty(props, "relativePosition", MyGUI::FloatPoint());
|
||||||
return false;
|
mRelativeCoord = parseProperty(props, "relativeSize", MyGUI::FloatSize());
|
||||||
auto v = value.as<osg::Vec2f>();
|
mAnchor = parseProperty(props, "anchor", MyGUI::FloatSize());
|
||||||
mAbsoluteCoord.left = v.x();
|
mWidget->setVisible(parseProperty(props, "visible", true));
|
||||||
mAbsoluteCoord.top = v.y();
|
|
||||||
}
|
updateCoord();
|
||||||
else if (name == "size")
|
|
||||||
{
|
|
||||||
if (!value.is<osg::Vec2f>())
|
|
||||||
return false;
|
|
||||||
auto v = value.as<osg::Vec2f>();
|
|
||||||
mAbsoluteCoord.width = v.x();
|
|
||||||
mAbsoluteCoord.height = v.y();
|
|
||||||
}
|
|
||||||
else if (name == "relativePosition")
|
|
||||||
{
|
|
||||||
if (!value.is<osg::Vec2f>())
|
|
||||||
return false;
|
|
||||||
auto v = value.as<osg::Vec2f>();
|
|
||||||
mRelativeCoord.left = v.x();
|
|
||||||
mRelativeCoord.top = v.y();
|
|
||||||
}
|
|
||||||
else if (name == "relativeSize")
|
|
||||||
{
|
|
||||||
if (!value.is<osg::Vec2f>())
|
|
||||||
return false;
|
|
||||||
auto v = value.as<osg::Vec2f>();
|
|
||||||
mRelativeCoord.width = v.x();
|
|
||||||
mRelativeCoord.height = v.y();
|
|
||||||
}
|
|
||||||
else if (name == "anchor")
|
|
||||||
{
|
|
||||||
if (!value.is<osg::Vec2f>())
|
|
||||||
return false;
|
|
||||||
auto v = value.as<osg::Vec2f>();
|
|
||||||
mAnchor.width = v.x();
|
|
||||||
mAnchor.height = v.y();
|
|
||||||
}
|
|
||||||
else if (name == "visible")
|
|
||||||
{
|
|
||||||
if (!value.is<bool>())
|
|
||||||
return false;
|
|
||||||
mWidget->setVisible(value.as<bool>());
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetExtension::updateChildrenCoord(MyGUI::Widget* _widget)
|
void WidgetExtension::updateChildrenCoord(MyGUI::Widget* _widget)
|
||||||
|
|
|
@ -5,10 +5,11 @@
|
||||||
|
|
||||||
#include <MyGUI_Widget.h>
|
#include <MyGUI_Widget.h>
|
||||||
#include <sol/sol.hpp>
|
#include <sol/sol.hpp>
|
||||||
#include <osg/Vec2>
|
|
||||||
|
|
||||||
#include <components/lua/scriptscontainer.hpp>
|
#include <components/lua/scriptscontainer.hpp>
|
||||||
|
|
||||||
|
#include "properties.hpp"
|
||||||
|
|
||||||
namespace LuaUi
|
namespace LuaUi
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
|
@ -36,17 +37,16 @@ namespace LuaUi
|
||||||
void setCallback(const std::string&, const LuaUtil::Callback&);
|
void setCallback(const std::string&, const LuaUtil::Callback&);
|
||||||
void clearCallbacks();
|
void clearCallbacks();
|
||||||
|
|
||||||
void setProperty(std::string_view, sol::object value);
|
virtual void setProperties(sol::object);
|
||||||
|
|
||||||
MyGUI::IntCoord forcedOffset();
|
MyGUI::IntCoord forcedCoord();
|
||||||
void setForcedOffset(const MyGUI::IntCoord& offset);
|
void setForcedCoord(const MyGUI::IntCoord& offset);
|
||||||
void updateCoord();
|
void updateCoord();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
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;
|
||||||
virtual bool setPropertyRaw(std::string_view name, sol::object value);
|
|
||||||
virtual void initialize();
|
virtual void initialize();
|
||||||
virtual void deinitialize();
|
virtual void deinitialize();
|
||||||
virtual MyGUI::IntSize calculateSize();
|
virtual MyGUI::IntSize calculateSize();
|
||||||
|
@ -55,9 +55,14 @@ namespace LuaUi
|
||||||
|
|
||||||
void triggerEvent(std::string_view name, const sol::object& argument) const;
|
void triggerEvent(std::string_view name, const sol::object& argument) const;
|
||||||
|
|
||||||
|
// offsets the position and size, used only in C++ widget code
|
||||||
MyGUI::IntCoord mForcedCoord;
|
MyGUI::IntCoord mForcedCoord;
|
||||||
|
// position and size in pixels
|
||||||
MyGUI::IntCoord mAbsoluteCoord;
|
MyGUI::IntCoord mAbsoluteCoord;
|
||||||
|
// position and size as a ratio of parent size
|
||||||
MyGUI::FloatCoord mRelativeCoord;
|
MyGUI::FloatCoord mRelativeCoord;
|
||||||
|
// negative position offset as a ratio of this widget's size
|
||||||
|
// used in combination with relative coord to align the widget, e. g. center it
|
||||||
MyGUI::FloatSize mAnchor;
|
MyGUI::FloatSize mAnchor;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -8,6 +8,7 @@ namespace LuaUi
|
||||||
: mCaption()
|
: mCaption()
|
||||||
, mPreviousMouse()
|
, mPreviousMouse()
|
||||||
, mChangeScale()
|
, mChangeScale()
|
||||||
|
, mMoveResize()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void LuaWindow::initialize()
|
void LuaWindow::initialize()
|
||||||
|
@ -43,20 +44,13 @@ namespace LuaUi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LuaWindow::setPropertyRaw(std::string_view name, sol::object value)
|
void LuaWindow::setProperties(sol::object props)
|
||||||
{
|
{
|
||||||
if (name == "caption")
|
if (mCaption)
|
||||||
{
|
mCaption->setCaption(parseProperty(props, "caption", std::string()));
|
||||||
if (!value.is<std::string>())
|
mMoveResize = MyGUI::IntCoord();
|
||||||
return false;
|
setForcedCoord(mMoveResize);
|
||||||
if (mCaption)
|
WidgetExtension::setProperties(props);
|
||||||
mCaption->setCaption(value.as<std::string>());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return WidgetExtension::setPropertyRaw(name, value);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaWindow::notifyMousePress(MyGUI::Widget* sender, int left, int top, MyGUI::MouseButton id)
|
void LuaWindow::notifyMousePress(MyGUI::Widget* sender, int left, int top, MyGUI::MouseButton id)
|
||||||
|
@ -84,9 +78,11 @@ namespace LuaUi
|
||||||
change.width *= (left - mPreviousMouse.left);
|
change.width *= (left - mPreviousMouse.left);
|
||||||
change.height *= (top - mPreviousMouse.top);
|
change.height *= (top - mPreviousMouse.top);
|
||||||
|
|
||||||
setForcedOffset(forcedOffset() + change.size());
|
mMoveResize = mMoveResize + change.size();
|
||||||
MyGUI::IntPoint positionOffset = change.point() + getPosition() - calculateCoord().point();
|
setForcedCoord(mMoveResize);
|
||||||
setForcedOffset(forcedOffset() + positionOffset);
|
// position can change based on size changes
|
||||||
|
mMoveResize = mMoveResize + change.point() + getPosition() - calculateCoord().point();
|
||||||
|
setForcedCoord(mMoveResize);
|
||||||
updateCoord();
|
updateCoord();
|
||||||
|
|
||||||
mPreviousMouse.left = left;
|
mPreviousMouse.left = left;
|
||||||
|
|
|
@ -15,6 +15,7 @@ namespace LuaUi
|
||||||
|
|
||||||
public:
|
public:
|
||||||
LuaWindow();
|
LuaWindow();
|
||||||
|
virtual void setProperties(sol::object) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// \todo replace with LuaText when skins are properly implemented
|
// \todo replace with LuaText when skins are properly implemented
|
||||||
|
@ -22,12 +23,12 @@ namespace LuaUi
|
||||||
MyGUI::IntPoint mPreviousMouse;
|
MyGUI::IntPoint mPreviousMouse;
|
||||||
MyGUI::IntCoord mChangeScale;
|
MyGUI::IntCoord mChangeScale;
|
||||||
|
|
||||||
|
MyGUI::IntCoord mMoveResize;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void initialize() override;
|
virtual void initialize() override;
|
||||||
virtual void deinitialize() override;
|
virtual void deinitialize() override;
|
||||||
|
|
||||||
bool setPropertyRaw(std::string_view name, sol::object value) override;
|
|
||||||
|
|
||||||
void notifyMousePress(MyGUI::Widget*, int, int, MyGUI::MouseButton);
|
void notifyMousePress(MyGUI::Widget*, int, int, MyGUI::MouseButton);
|
||||||
void notifyMouseDrag(MyGUI::Widget*, int, int, MyGUI::MouseButton);
|
void notifyMouseDrag(MyGUI::Widget*, int, int, MyGUI::MouseButton);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue