You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw/components/lua_ui/properties.hpp

91 lines
2.5 KiB
C++

#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
{
3 years ago
template<typename T>
constexpr bool isMyGuiVector() {
return
std::is_same<T, MyGUI::IntPoint>() ||
std::is_same<T, MyGUI::IntSize>() ||
std::is_same<T, MyGUI::FloatPoint>() ||
std::is_same<T, MyGUI::FloatSize>();
}
3 years ago
template <typename T, typename LuaT>
sol::optional<T> parseValue(
sol::object table,
std::string_view field,
std::string_view errorPrefix)
{
3 years ago
sol::object opt = LuaUtil::getFieldOrNil(table, field);
if (opt != sol::nil && !opt.is<LuaT>())
{
std::string error(errorPrefix);
error += " \"";
error += field;
error += "\" has an invalid value \"";
error += LuaUtil::toString(opt);
error += "\"";
throw std::logic_error(error);
}
if (!opt.is<LuaT>())
return sol::nullopt;
LuaT luaT = opt.as<LuaT>();
if constexpr (isMyGuiVector<T>())
return T(luaT.x(), luaT.y());
else
3 years ago
return luaT;
}
template <typename T>
3 years ago
sol::optional<T> parseValue(
sol::object table,
std::string_view field,
3 years ago
std::string_view errorPrefix)
{
3 years ago
if constexpr (isMyGuiVector<T>())
return parseValue<T, osg::Vec2f>(table, field, errorPrefix);
else
3 years ago
return parseValue<T, T>(table, field, errorPrefix);
}
template <typename T>
3 years ago
T parseProperty(
sol::object props,
sol::object templateProps,
std::string_view field,
3 years ago
const T& defaultValue)
{
3 years ago
auto propOptional = parseValue<T>(props, field, "Property");
auto templateOptional = parseValue<T>(templateProps, field, "Template property");
if (propOptional.has_value())
return propOptional.value();
else if (templateOptional.has_value())
return templateOptional.value();
else
return defaultValue;
}
3 years ago
template <typename T>
T parseExternal(
sol::object external,
std::string_view field,
const T& defaultValue)
{
auto optional = parseValue<T>(external, field, "External value");
return optional.value_or(defaultValue);
}
}
#endif // !OPENMW_LUAUI_PROPERTIES