Skip to content

Commit

Permalink
Could fix #128
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Dec 30, 2021
1 parent e43cc5f commit 59b602c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions scripts/ai/parameters/AIParameterSettingList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 59b602c

Please sign in to comment.