mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-05 15:55:45 +00:00
Correctly set UI properties to defaults when passed nil
This commit is contained in:
parent
9d7a53b701
commit
e9f8c34372
@ -162,7 +162,8 @@ add_component_dir (queries
|
||||
)
|
||||
|
||||
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)
|
||||
{
|
||||
auto props = layout.get<sol::optional<sol::table>>("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();
|
||||
}
|
||||
ext->setProperties(layout.get<sol::object>("props"));
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
bool LuaText::setPropertyRaw(std::string_view name, sol::object value)
|
||||
void LuaText::setProperties(sol::object props)
|
||||
{
|
||||
if (name == "caption")
|
||||
{
|
||||
if (!value.is<std::string>())
|
||||
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;
|
||||
setCaption(parseProperty(props, "caption", std::string()));
|
||||
mAutoSized = parseProperty(props, "autoSize", true);
|
||||
WidgetExtension::setProperties(props);
|
||||
}
|
||||
|
||||
MyGUI::IntSize LuaText::calculateSize()
|
||||
|
@ -14,13 +14,13 @@ namespace LuaUi
|
||||
public:
|
||||
LuaText();
|
||||
virtual void initialize() override;
|
||||
virtual void setProperties(sol::object) override;
|
||||
|
||||
private:
|
||||
bool mAutoSized;
|
||||
|
||||
protected:
|
||||
virtual MyGUI::IntSize calculateSize() override;
|
||||
bool setPropertyRaw(std::string_view name, sol::object value) override;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -2,18 +2,9 @@
|
||||
|
||||
namespace LuaUi
|
||||
{
|
||||
bool LuaTextEdit::setPropertyRaw(std::string_view name, sol::object value)
|
||||
void LuaTextEdit::setProperties(sol::object props)
|
||||
{
|
||||
if (name == "caption")
|
||||
{
|
||||
if (!value.is<std::string>())
|
||||
return false;
|
||||
setCaption(value.as<std::string>());
|
||||
}
|
||||
else
|
||||
{
|
||||
return WidgetExtension::setPropertyRaw(name, value);
|
||||
}
|
||||
return true;
|
||||
setCaption(parseProperty(props, "caption", std::string()));
|
||||
WidgetExtension::setProperties(props);
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,7 @@ namespace LuaUi
|
||||
{
|
||||
MYGUI_RTTI_DERIVED(LuaTextEdit)
|
||||
|
||||
protected:
|
||||
bool setPropertyRaw(std::string_view name, sol::object value) override;
|
||||
virtual void setProperties(sol::object) override;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -142,19 +142,12 @@ namespace LuaUi
|
||||
mCallbacks.clear();
|
||||
}
|
||||
|
||||
void WidgetExtension::setProperty(std::string_view name, sol::object value)
|
||||
{
|
||||
if (!setPropertyRaw(name, value))
|
||||
Log(Debug::Error) << "Invalid value of property " << name
|
||||
<< ": " << LuaUtil::toString(value);
|
||||
}
|
||||
|
||||
MyGUI::IntCoord WidgetExtension::forcedOffset()
|
||||
MyGUI::IntCoord WidgetExtension::forcedCoord()
|
||||
{
|
||||
return mForcedCoord;
|
||||
}
|
||||
|
||||
void WidgetExtension::setForcedOffset(const MyGUI::IntCoord& offset)
|
||||
void WidgetExtension::setForcedCoord(const MyGUI::IntCoord& offset)
|
||||
{
|
||||
mForcedCoord = offset;
|
||||
}
|
||||
@ -164,55 +157,16 @@ namespace LuaUi
|
||||
mWidget->setCoord(calculateCoord());
|
||||
}
|
||||
|
||||
bool WidgetExtension::setPropertyRaw(std::string_view name, sol::object value)
|
||||
void WidgetExtension::setProperties(sol::object props)
|
||||
{
|
||||
if (name == "position")
|
||||
{
|
||||
if (!value.is<osg::Vec2f>())
|
||||
return false;
|
||||
auto v = value.as<osg::Vec2f>();
|
||||
mAbsoluteCoord.left = v.x();
|
||||
mAbsoluteCoord.top = v.y();
|
||||
}
|
||||
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;
|
||||
mAbsoluteCoord = parseProperty(props, "position", MyGUI::IntPoint());
|
||||
mAbsoluteCoord = parseProperty(props, "size", MyGUI::IntSize());
|
||||
mRelativeCoord = parseProperty(props, "relativePosition", MyGUI::FloatPoint());
|
||||
mRelativeCoord = parseProperty(props, "relativeSize", MyGUI::FloatSize());
|
||||
mAnchor = parseProperty(props, "anchor", MyGUI::FloatSize());
|
||||
mWidget->setVisible(parseProperty(props, "visible", true));
|
||||
|
||||
updateCoord();
|
||||
}
|
||||
|
||||
void WidgetExtension::updateChildrenCoord(MyGUI::Widget* _widget)
|
||||
|
@ -5,10 +5,11 @@
|
||||
|
||||
#include <MyGUI_Widget.h>
|
||||
#include <sol/sol.hpp>
|
||||
#include <osg/Vec2>
|
||||
|
||||
#include <components/lua/scriptscontainer.hpp>
|
||||
|
||||
#include "properties.hpp"
|
||||
|
||||
namespace LuaUi
|
||||
{
|
||||
/*
|
||||
@ -36,17 +37,16 @@ namespace LuaUi
|
||||
void setCallback(const std::string&, const LuaUtil::Callback&);
|
||||
void clearCallbacks();
|
||||
|
||||
void setProperty(std::string_view, sol::object value);
|
||||
virtual void setProperties(sol::object);
|
||||
|
||||
MyGUI::IntCoord forcedOffset();
|
||||
void setForcedOffset(const MyGUI::IntCoord& offset);
|
||||
MyGUI::IntCoord forcedCoord();
|
||||
void setForcedCoord(const MyGUI::IntCoord& offset);
|
||||
void updateCoord();
|
||||
|
||||
protected:
|
||||
sol::table makeTable() const;
|
||||
sol::object keyEvent(MyGUI::KeyCode) 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 deinitialize();
|
||||
virtual MyGUI::IntSize calculateSize();
|
||||
@ -55,9 +55,14 @@ namespace LuaUi
|
||||
|
||||
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;
|
||||
// position and size in pixels
|
||||
MyGUI::IntCoord mAbsoluteCoord;
|
||||
// position and size as a ratio of parent size
|
||||
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;
|
||||
|
||||
private:
|
||||
|
@ -8,6 +8,7 @@ namespace LuaUi
|
||||
: mCaption()
|
||||
, mPreviousMouse()
|
||||
, mChangeScale()
|
||||
, mMoveResize()
|
||||
{}
|
||||
|
||||
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 (!value.is<std::string>())
|
||||
return false;
|
||||
if (mCaption)
|
||||
mCaption->setCaption(value.as<std::string>());
|
||||
}
|
||||
else
|
||||
{
|
||||
return WidgetExtension::setPropertyRaw(name, value);
|
||||
}
|
||||
return true;
|
||||
if (mCaption)
|
||||
mCaption->setCaption(parseProperty(props, "caption", std::string()));
|
||||
mMoveResize = MyGUI::IntCoord();
|
||||
setForcedCoord(mMoveResize);
|
||||
WidgetExtension::setProperties(props);
|
||||
}
|
||||
|
||||
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.height *= (top - mPreviousMouse.top);
|
||||
|
||||
setForcedOffset(forcedOffset() + change.size());
|
||||
MyGUI::IntPoint positionOffset = change.point() + getPosition() - calculateCoord().point();
|
||||
setForcedOffset(forcedOffset() + positionOffset);
|
||||
mMoveResize = mMoveResize + change.size();
|
||||
setForcedCoord(mMoveResize);
|
||||
// position can change based on size changes
|
||||
mMoveResize = mMoveResize + change.point() + getPosition() - calculateCoord().point();
|
||||
setForcedCoord(mMoveResize);
|
||||
updateCoord();
|
||||
|
||||
mPreviousMouse.left = left;
|
||||
|
@ -15,6 +15,7 @@ namespace LuaUi
|
||||
|
||||
public:
|
||||
LuaWindow();
|
||||
virtual void setProperties(sol::object) override;
|
||||
|
||||
private:
|
||||
// \todo replace with LuaText when skins are properly implemented
|
||||
@ -22,12 +23,12 @@ namespace LuaUi
|
||||
MyGUI::IntPoint mPreviousMouse;
|
||||
MyGUI::IntCoord mChangeScale;
|
||||
|
||||
MyGUI::IntCoord mMoveResize;
|
||||
|
||||
protected:
|
||||
virtual void initialize() override;
|
||||
virtual void deinitialize() override;
|
||||
|
||||
bool setPropertyRaw(std::string_view name, sol::object value) override;
|
||||
|
||||
void notifyMousePress(MyGUI::Widget*, int, int, MyGUI::MouseButton);
|
||||
void notifyMouseDrag(MyGUI::Widget*, int, int, MyGUI::MouseButton);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user