From 59b602c6bf12e1710552bc93ce7de43ce2a96a4d Mon Sep 17 00:00:00 2001 From: schwiti6190 <58079399+schwiti6190@users.noreply.github.com> Date: Thu, 30 Dec 2021 19:04:53 +0100 Subject: [PATCH] Could fix #128 --- .../ai/parameters/AIParameterSettingList.lua | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/scripts/ai/parameters/AIParameterSettingList.lua b/scripts/ai/parameters/AIParameterSettingList.lua index 635132a52..02d7c280c 100644 --- a/scripts/ai/parameters/AIParameterSettingList.lua +++ b/scripts/ai/parameters/AIParameterSettingList.lua @@ -66,7 +66,7 @@ function AIParameterSettingList.getAreaText(value) return g_i18n:formatArea(value, 1, true) end -AIParameterSettingList.UNITS = { +AIParameterSettingList.UNITS_TEXTS = { AIParameterSettingList.getSpeedText, --- km/h AIParameterSettingList.getDistanceText, --- m AIParameterSettingList.getAreaText, --- ha/arcs @@ -87,7 +87,7 @@ function AIParameterSettingList:generateValues(values,texts,min,max,inc,textStr, for i=min,max,inc do table.insert(values,i) local value = MathUtil.round(i,2) - local text = unit and AIParameterSettingList.UNITS[unit] and AIParameterSettingList.UNITS[unit](value) or tostring(value) + local text = unit and AIParameterSettingList.UNITS_TEXTS[unit] and AIParameterSettingList.UNITS_TEXTS[unit](value) or tostring(value) local text = textStr and string.format(textStr,value) or text table.insert(texts,text) end @@ -98,7 +98,7 @@ function AIParameterSettingList:enrichTexts(unit) for i,value in ipairs(self.values) do local text = tostring(value) if unit then - text = text..AIParameterSettingList.UNITS[unit](value) + text = text..AIParameterSettingList.UNITS_TEXTS[unit](value) end self.texts[i] = text end @@ -146,6 +146,23 @@ function AIParameterSettingList:validateCurrentValue() self:setToIx(new) end +--- Refresh the texts, if it depends on a changeable measurement unit. +--- For all units that are not an SI unit ... +function AIParameterSettingList:validateTexts() + local unit = self.data.unit + if unit then + local unitStrFunc = AIParameterSettingList.UNITS_TEXTS[unit] + local fixedTexts = {} + for ix,value in ipairs(self.values) do + local value = MathUtil.round(value,2) + local text = unitStrFunc(value) + local text = self.data.textStr and string.format(self.data.textStr,value) or text + fixedTexts[ix] = text + end + self.texts = fixedTexts + end +end + function AIParameterSettingList:getDebugString() -- replace % as this string goes through multiple formats (%% does not seem to work and I have no time to figure it out return string.format('%s: %s', self.name, string.gsub(self.texts[self.current], '%%', 'percent')) @@ -256,6 +273,7 @@ end function AIParameterSettingList:setGuiElement(guiElement) self:validateCurrentValue() + self:validateTexts() self.guiElement = guiElement self.guiElement.target = self self.guiElement.onClickCallback = function(setting,state,element)