Add remap and round to lua utils

7220-lua-add-a-general-purpose-lexical-parser
Kindi 2 years ago committed by Petr Mikheev
parent e575c25278
commit 07da7eddea

@ -212,11 +212,17 @@ namespace LuaUtil
transQType["inverse"] = [](const TransformQ& q) { return TransformQ{ q.mQ.inverse() }; };
// Utility functions
util["clamp"] = [](float value, float from, float to) { return std::clamp(value, from, to); };
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)
{

@ -4,6 +4,29 @@
-- @usage local util = require('openmw.util')
---
-- Rounds the given value to the nearest whole number.
-- @function [parent=#util] round
-- @param #number value
-- @return #number The rounded value.
-- @usage
-- local util = require('openmw.util')
-- local roundedValue = util.round(3.141592)
-- print(roundedValue) -- prints 3
---
-- Remaps the value from one range to another.
-- @function [parent=#util] remap
-- @param #number value
-- @param #number min
-- @param #number max
-- @param #number newMin
-- @param #number newMax
-- @return #number The remapped value.
-- @usage
-- local util = require('openmw.util')
-- local newValue = util.remap(3, 0, 10, 0, 100)
-- print(newValue) -- prints 30
---
-- Limits given value to the interval [`from`, `to`].

Loading…
Cancel
Save