feat: fanen Indstillinger (ADSettings globalt + pr. stald) (v1.62.140)
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
-- Smart Pickup — fanen Indstillinger
|
||||
-- Moddens justeringer (ADSettings): én række pr. nøgle globalt, og pr. stald en række der kan afvige fra det globale
|
||||
-- ("som globalt" = ingen egen værdi). Nye nøgler i ADSettings.KEYS (med choices) dukker op af sig selv.
|
||||
-- Klik går til onClickSettingsRow.
|
||||
|
||||
SmartPickupFrame.SETTINGS_ROW_KINDS = {setGlobal = true, setBuilding = true}
|
||||
|
||||
local function choiceTexts(spec, i18n, withGlobal)
|
||||
local texts = {}
|
||||
if withGlobal then table.insert(texts, i18n:getText("spu_set_asGlobal")) end
|
||||
for _, value in ipairs(spec.choices or {}) do
|
||||
table.insert(texts, string.format(spec.format or "%s", tostring(value)))
|
||||
end
|
||||
return texts
|
||||
end
|
||||
|
||||
local function choiceIndex(spec, value)
|
||||
local best, bestDiff = 1, math.huge
|
||||
for index, choice in ipairs(spec.choices or {}) do
|
||||
local diff = math.abs(choice - (value or spec.default))
|
||||
if diff < bestDiff then best, bestDiff = index, diff end
|
||||
end
|
||||
return best
|
||||
end
|
||||
|
||||
local function keyTitle(i18n, key)
|
||||
local text = i18n:getText("spu_set_" .. key)
|
||||
if text == nil or text == "" or text == ("spu_set_" .. key) then return key end
|
||||
return text
|
||||
end
|
||||
|
||||
local function sortedKeys()
|
||||
local keys = {}
|
||||
for key, spec in pairs(ADSettings.KEYS) do
|
||||
if spec.choices ~= nil then table.insert(keys, key) end
|
||||
end
|
||||
table.sort(keys)
|
||||
return keys
|
||||
end
|
||||
|
||||
function SmartPickupFrame:fillSettingsRows(adEnv)
|
||||
local i18n = self.i18n
|
||||
if ADSettings == nil then
|
||||
self:addTextRow("ADSettings mangler")
|
||||
return
|
||||
end
|
||||
local keys = sortedKeys()
|
||||
self:addTextRow(i18n:getText("spu_set_globalTitle"))
|
||||
for _, key in ipairs(keys) do
|
||||
local spec = ADSettings.KEYS[key]
|
||||
local option = self:addFieldSiloChoiceRow("setGlobal", keyTitle(i18n, key), choiceTexts(spec, i18n, false), choiceIndex(spec, ADSettings.get(key)))
|
||||
self.rowData[option] = {kind = "setGlobal", key = key}
|
||||
end
|
||||
self:addTextRow(i18n:getText("spu_set_globalHelp"))
|
||||
|
||||
-- pr. stald: kun nøgler med perBuilding = true
|
||||
local farmId = g_currentMission:getFarmId()
|
||||
local barns = {}
|
||||
for _, building in ipairs(ADBuildings.list(adEnv, farmId) or {}) do
|
||||
if building.kind == "husbandry" then table.insert(barns, building) end
|
||||
end
|
||||
table.sort(barns, function(a, b) return tostring(a.name) < tostring(b.name) end)
|
||||
for _, key in ipairs(keys) do
|
||||
local spec = ADSettings.KEYS[key]
|
||||
if spec.perBuilding and #barns > 0 then
|
||||
self:addTextRow(string.format(i18n:getText("spu_set_perBarnTitle"), keyTitle(i18n, key)))
|
||||
for _, barn in ipairs(barns) do
|
||||
local own = ADSettings.getOwn(key, barn.id)
|
||||
local state = own ~= nil and (choiceIndex(spec, own) + 1) or 1
|
||||
local option = self:addFieldSiloChoiceRow("setBuilding", tostring(barn.name), choiceTexts(spec, i18n, true), state)
|
||||
self.rowData[option] = {kind = "setBuilding", key = key, buildingId = barn.id}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function SmartPickupFrame:onClickSettingsRow(state, data)
|
||||
self:safe(function(frame)
|
||||
local spec = ADSettings.KEYS[data.key]
|
||||
if spec == nil then return end
|
||||
if data.kind == "setGlobal" then
|
||||
ADSettings.set(data.key, spec.choices[state], nil)
|
||||
elseif data.kind == "setBuilding" then
|
||||
if state == 1 then
|
||||
ADSettings.clear(data.key, data.buildingId)
|
||||
else
|
||||
ADSettings.set(data.key, spec.choices[state - 1], data.buildingId)
|
||||
end
|
||||
end
|
||||
pcall(ADSettings.save)
|
||||
frame:requestRebuild()
|
||||
end)
|
||||
end
|
||||
Reference in New Issue
Block a user