From 07da7eddea5a6bbc0b32d723206bf084f09c0011 Mon Sep 17 00:00:00 2001 From: Kindi <2538602-Kuyondo@users.noreply.gitlab.com> Date: Fri, 20 Jan 2023 22:28:34 +0000 Subject: [PATCH] Add remap and round to lua utils --- components/lua/utilpackage.cpp | 8 +++++++- files/lua_api/openmw/util.lua | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/lua/utilpackage.cpp b/components/lua/utilpackage.cpp index 65431f3f6e..79cae93003 100644 --- a/components/lua/utilpackage.cpp +++ b/components/lua/utilpackage.cpp @@ -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` 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) { diff --git a/files/lua_api/openmw/util.lua b/files/lua_api/openmw/util.lua index 19eb3a82cd..e5ea351ff3 100644 --- a/files/lua_api/openmw/util.lua +++ b/files/lua_api/openmw/util.lua @@ -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`].