mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-25 06:35:30 +00:00
Merge branch 'addutils' into 'master'
Add remap and round to lua utils See merge request OpenMW/openmw!2605
This commit is contained in:
commit
ca48b778c3
@ -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…
x
Reference in New Issue
Block a user