Files
fs25-adsmartpickup/FS25_ADSmartPickup/gui/SmartPickupSettingsTab.lua
T

200 lines
9.3 KiB
Lua

-- Smart Pickup — fanen Indstillinger
-- Moddens justeringer (ADSettings), delt i sektioner (ADSettings.SECTIONS, vælger øverst): é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 — i sektionen fra ADSettings.SECTION_OF, ellers i den første.
-- Klik går til onClickSettingsRow.
SmartPickupFrame.SETTINGS_ROW_KINDS = {setGlobal = true, setBuilding = true, setBarnOnly = true, lockTractor = true, settingsSection = true}
local function choiceTexts(spec, i18n, withGlobal)
local texts = {}
if withGlobal then table.insert(texts, i18n:getText("spu_set_asGlobal")) end
for index, value in ipairs(spec.choices or {}) do
-- valg med navne (fx pop-up/statusvindue) i stedet for et tal
local labelKey = spec.labels ~= nil and spec.labels[index] or nil
table.insert(texts, labelKey ~= nil and i18n:getText(labelKey) or 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
-- Stalde til pr.-stald-rækkerne: rene stalde, og stalde med dyr (også dem med indbygget silo, der er klassificeret som
-- silo). Fast rækkefølge også for ens navne (sted, så id — review)
local function collectBarns(adEnv)
local barns, animalBarns = {}, {}
for _, building in ipairs(ADBuildings.list(adEnv, ADUtil.farmId()) or {}) do
if building.kind == "husbandry" then table.insert(barns, building) end
if building.kind == "husbandry" or (building.placeable ~= nil and building.placeable.spec_husbandryAnimals ~= nil) then
table.insert(animalBarns, building)
end
end
local function byLabel(a, b)
local la, lb = ADBuildings.labelWithPlace(a), ADBuildings.labelWithPlace(b)
if la ~= lb then return la < lb end
return tostring(a.id) < tostring(b.id)
end
table.sort(barns, byLabel)
table.sort(animalBarns, byLabel)
return barns, animalBarns
end
-- Sektionsvælgeren øverst (brugerønske 20/10: fanen deles op)
function SmartPickupFrame:getSettingsSection()
return ADSettings.validSection(self.settingsSection)
end
function SmartPickupFrame:addSettingsSectionRow(section)
local texts, state = {}, 1
for index, name in ipairs(ADSettings.SECTIONS) do
table.insert(texts, self.i18n:getText("spu_set_section_" .. name))
if name == section then state = index end
end
local option = self:addFieldSiloChoiceRow("settingsSection", self.i18n:getText("spu_set_sectionTitle"), texts, state)
self.rowData[option] = {kind = "settingsSection"}
end
-- Gælder hele gården: sektionens nøgler uden barnOnly
function SmartPickupFrame:addSettingsGlobalRows(keys)
local i18n, anyGlobal, anyOverride = self.i18n, false, false
for _, key in ipairs(keys) do
local spec = ADSettings.KEYS[key]
if not spec.barnOnly then
if not anyGlobal then self:addTextRow(i18n:getText("spu_set_globalTitle")) end
anyGlobal = true
anyOverride = anyOverride or spec.perBuilding == true
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
end
-- hjælpen om "som globalt" kun når sektionen har pr.-stald-rækker der kan afvige
if anyOverride then self:addTextRow(i18n:getText("spu_set_globalHelp")) end
end
-- Pr. stald: sektionens nøgler med perBuilding. Spredningen vises kun for stalde, der sælger (skjul det irrelevante)
function SmartPickupFrame:addSettingsBarnRows(keys, barns, animalBarns)
local i18n = self.i18n
for _, key in ipairs(keys) do
local spec = ADSettings.KEYS[key]
local list = {}
for _, barn in ipairs(spec.animalBarns and animalBarns or barns) do
if (key ~= "sellReadySpreadMonths" and key ~= "sellReadyTarget") or ADSettings.getOwn("sellReadyAnimals", barn.id) == 1 then table.insert(list, barn) end
end
if spec.perBuilding and #list > 0 then
self:addTextRow(string.format(i18n:getText("spu_set_perBarnTitle"), keyTitle(i18n, key)))
for _, barn in ipairs(list) do
local own = ADSettings.getOwn(key, barn.id)
if spec.barnOnly then
-- kun staldens egen værdi (ingen "som global"): valgene direkte, intet valgt = standard (fra)
local option = self:addFieldSiloChoiceRow("setBuilding", ADBuildings.labelWithPlace(barn), choiceTexts(spec, i18n, false), choiceIndex(spec, own))
self.rowData[option] = {kind = "setBarnOnly", key = key, buildingId = barn.id}
else
local state = own ~= nil and (choiceIndex(spec, own) + 1) or 1
local option = self:addFieldSiloChoiceRow("setBuilding", ADBuildings.labelWithPlace(barn), choiceTexts(spec, i18n, true), state)
self.rowData[option] = {kind = "setBuilding", key = key, buildingId = barn.id}
end
end
end
end
end
function SmartPickupFrame:fillSettingsRows(adEnv)
if ADSettings == nil then
self:addTextRow("ADSettings mangler")
return
end
-- hele manualen med søgning (brugerønske 20/10)
if self.openHelpView ~= nil then
self:addLinkRow(self.i18n:getText("spu_help_openManual"), function(frame) frame:openHelpView({mode = "toc"}) end)
end
local section = self:getSettingsSection()
self:addSettingsSectionRow(section)
local keys = ADSettings.sectionKeys(section)
self:addSettingsGlobalRows(keys)
local barns, animalBarns = collectBarns(adEnv)
self:addSettingsBarnRows(keys, barns, animalBarns)
if section == "animals" then self:fillLivestockTractorRows(adEnv, animalBarns) end
end
-- Fast traktor til slagtedyr pr. stald (kun stalde med salget slået til): "ledig traktor" eller en bestemt traktor
function SmartPickupFrame:fillLivestockTractorRows(adEnv, animalBarns)
if ADLivestock == nil or ADLivestock.setLock == nil then return end
local i18n = self.i18n
local selling = {}
for _, barn in ipairs(animalBarns) do
if ADSettings.getOwn("sellReadyAnimals", barn.id) == 1 then table.insert(selling, barn) end
end
if #selling == 0 then return end
local tractors = {}
for _, vehicle in ipairs(ADRunsController.getFarmVehicles(adEnv) or {}) do
local id = ADRunsController.getVehicleId(vehicle)
if id ~= nil and vehicle.spec_attacherJoints ~= nil and vehicle.spec_combine == nil and vehicle.spec_dischargeable == nil then
table.insert(tractors, {id = id, name = tostring(ADRuns.vehicleLabel(vehicle))})
end
end
table.sort(tractors, function(a, b) return a.name < b.name end)
self:addTextRow(i18n:getText("spu_set_livestockTractorTitle"))
for _, barn in ipairs(selling) do
local texts, ids, state = {i18n:getText("spu_set_livestockTractorAny")}, {false}, 1
local locked = ADLivestock.getLock(barn.id)
for _, tractor in ipairs(tractors) do
table.insert(texts, tractor.name)
table.insert(ids, tractor.id)
if tractor.id == locked then state = #texts end
end
-- låst traktor findes ikke længere (solgt/ukendt): vis det, så låsen kan ses og slås fra (review)
if locked ~= nil and state == 1 then
table.insert(texts, i18n:getText("spu_set_livestockTractorMissing"))
table.insert(ids, locked)
state = #texts
end
local option = self:addFieldSiloChoiceRow("setBuilding", ADBuildings.labelWithPlace(barn), texts, state)
self.rowData[option] = {kind = "lockTractor", buildingId = barn.id, ids = ids}
end
end
function SmartPickupFrame:onClickSettingsRow(state, data)
self:safe(function(frame)
if data.kind == "settingsSection" then
frame.settingsSection = ADSettings.SECTIONS[state]
frame:requestRebuild()
return
end
if data.kind == "lockTractor" then
local id = data.ids[state]
ADLivestock.setLock(data.buildingId, id ~= false and id or nil)
pcall(ADLivestock.save)
frame:requestRebuild()
return
end
local spec = ADSettings.KEYS[data.key]
if spec == nil then return end
if data.kind == "setBarnOnly" then
ADSettings.set(data.key, spec.choices[state], data.buildingId)
elseif 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