mirror of
https://github.com/OpenMW/openmw.git
synced 2026-01-04 18:13:05 +00:00
move some util to lua
This commit is contained in:
parent
d40c4855b1
commit
32f59c16aa
3 changed files with 27 additions and 7 deletions
|
|
@ -61,6 +61,7 @@ add_component_dir (lua
|
|||
luastate scriptscontainer asyncpackage utilpackage serialization configuration l10n storage utf8
|
||||
shapes/box inputactions yamlloader scripttracker luastateptr
|
||||
)
|
||||
copy_resource_file("lua/util.lua" "${OPENMW_RESOURCES_ROOT}" "resources/lua_libs/util.lua")
|
||||
|
||||
add_component_dir (l10n
|
||||
messagebundles manager
|
||||
|
|
|
|||
21
components/lua/util.lua
Normal file
21
components/lua/util.lua
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
local M = {}
|
||||
|
||||
function M.remap(value, min, max, newMin, newMax)
|
||||
return newMin + (value - min) * (newMax - newMin) / (max - min)
|
||||
end
|
||||
|
||||
function M.round(value)
|
||||
return value >= 0 and math.floor(value + 0.5) or math.ceil(value - 0.5)
|
||||
end
|
||||
|
||||
function M.clamp(value, low, high)
|
||||
return value < low and low or (value > high and high or value)
|
||||
end
|
||||
|
||||
function M.normalizeAngle(angle)
|
||||
local fullTurns = angle / (2 * math.pi) + 0.5
|
||||
return (fullTurns - math.floor(fullTurns) - 0.5) * (2 * math.pi)
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -352,16 +352,14 @@ namespace LuaUtil
|
|||
return std::make_tuple(angles.z(), angles.y(), angles.x());
|
||||
};
|
||||
|
||||
sol::function luaUtilLoader = lua["loadInternalLib"]("util");
|
||||
sol::table utils = luaUtilLoader();
|
||||
for (const auto& [key, value] : utils)
|
||||
util[key.as<std::string>()] = value;
|
||||
|
||||
// Utility functions
|
||||
util["clamp"] = [](double value, double from, double to) { return std::clamp(value, from, to); };
|
||||
// NOTE: `util["clamp"] = std::clamp<float>` causes error 'AddressSanitizer: stack-use-after-scope'
|
||||
util["normalizeAngle"] = &Misc::normalizeAngle;
|
||||
util["makeReadOnly"] = [](const sol::table& tbl) { return makeReadOnly(tbl, /*strictIndex=*/false); };
|
||||
util["makeStrictReadOnly"] = [](const sol::table& tbl) { return makeReadOnly(tbl, /*strictIndex=*/true); };
|
||||
util["remap"] = [](double value, double min, double max, double newMin, double newMax) {
|
||||
return newMin + (value - min) * (newMax - newMin) / (max - min);
|
||||
};
|
||||
util["round"] = [](double value) { return round(value); };
|
||||
|
||||
if (lua["bit32"] != sol::nil)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue