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

118 lines
5.5 KiB
Lua

-- Smart Pickup — fanen Kæder
-- Produktionskæderne som træ: rod (produktion spilleren har tændt) -> varer -> råvarer, med kilde, behov pr.
-- måned og flowstatus; afgrødeforslag kan godkendes/afvises og en rod sættes på pause. Rækkerne bygges af
-- ADChainView (testet); her kun kloning, farve og klik. Et resultat yngre end CHAIN_MAX_AGE_MS (fra kædens eget
-- tick eller en tidligere visning) genbruges; ellers evalueres (visning, handler ikke). Klik evaluerer altid.
SmartPickupFrame.CHAIN_MAX_AGE_MS = 30000
function SmartPickupFrame:chainLast(adEnv, force)
local age = ADChain.lastAge ~= nil and ADChain.lastAge() or math.huge
if force or ADChain.last == nil or age >= SmartPickupFrame.CHAIN_MAX_AGE_MS then
ADChain.evaluate(adEnv)
end
return ADChain.last
end
function SmartPickupFrame:chainTitle(last, name)
local title = last ~= nil and last.titles ~= nil and last.titles[name] or nil
return title or self:fruitTitle(name)
end
-- Klik på en kæde-knap: udfør, evaluér straks (ny visning) og genopbyg. Et forslag der ikke længere findes
-- (approve/dismiss -> false) meldes i statuslinjen som i "Kræver handling".
function SmartPickupFrame:onChainAction(action)
local ok = true
if action.kind == "approve" then
ok = ADChain.approve(action.fieldId)
elseif action.kind == "dismiss" then
ok = ADChain.dismiss(action.fieldId)
elseif action.kind == "finalMode" then
ADChain.setFinalKeep(action.rootKey, action.keep)
elseif action.kind == "pause" or action.kind == "resume" then
ADChain.setPaused(action.rootKey, action.kind == "pause")
end
if ok == false then
local text = self.i18n:getText("spu_chain_gone")
self.statusOverride, self.statusOverrideTicks = text, SmartPickupFrame.STATUS_OVERRIDE_TICKS
if ADFieldJobs ~= nil and ADFieldJobs.notify ~= nil then ADFieldJobs.notify(text, false) end
end
local adEnv = self.getAdEnv()
if adEnv ~= nil then self:chainLast(adEnv, true) end
self:requestRebuild()
end
-- Klik på rodens linje uden for knappen: fold ud/sammen (huskes kun mens spillet kører).
function SmartPickupFrame:toggleChainRoot(rootKey)
self.chainExpanded = self.chainExpanded or {}
self.chainExpanded[rootKey] = not self.chainExpanded[rootKey] or nil
self:requestRebuild()
end
-- Hvilken celle blev der klikket i? Højeste celle hvis venstre kant ligger til venstre for klikket.
local function clickedCell(frame, cells)
if frame.lastClickX == nil then return nil end
for index = #cells, 1, -1 do
local cell = cells[index]
if cell ~= nil and cell.absPosition ~= nil and frame.lastClickX >= cell.absPosition[1] then return index end
end
return nil
end
-- Række med prik + c1..c5 (flowListPrefab). Knapper er celler (entry.actions[celle], som [Start]/[Stop] i
-- Flows-listen); et klik andetsteds på en rodlinje folder den ud/sammen.
function SmartPickupFrame:addChainRow(entry)
local unpackFn = table.unpack or unpack
local row = self:addRow(self.flowListPrefab, {kind = "chainRow"})
local cells = {}
for index = 1, 5 do cells[index] = row:getDescendantByName("c" .. index) end
SmartPickupFrame.setCells(cells, entry.cells)
local dot = row:getDescendantByName("dot")
local color = entry.level ~= nil and ADFlowView.LEVEL_COLORS[entry.level] or nil
if dot ~= nil and dot.setImageColor ~= nil and color ~= nil then dot:setImageColor(nil, unpackFn(color)) end
-- kun rod og varer har en status-prik (overskrift, forslag og auto-skift har ingen)
if dot ~= nil and dot.setVisible ~= nil and entry.kind ~= "root" and entry.kind ~= "node" then dot:setVisible(false) end
if cells[4] ~= nil and cells[4].setTextColor ~= nil and color ~= nil and entry.level ~= "running" and entry.kind ~= "suggest" then
cells[4]:setTextColor(unpackFn(color))
end
local actions, toggle = entry.actions or {}, entry.toggle
if next(actions) ~= nil or toggle ~= nil then
self:makeRowClickable(row, function(frame)
local action = actions[clickedCell(frame, cells) or 0]
if action ~= nil then
frame:onChainAction(action)
elseif toggle ~= nil then
frame:toggleChainRoot(toggle)
end
end)
end
return row
end
-- Manglende led: problemteksten som (ombrudt) tekstrække i rød under varelinjen.
function SmartPickupFrame:addChainProblemRow(entry)
local unpackFn = table.unpack or unpack
local color = ADFlowView.LEVEL_COLORS ~= nil and ADFlowView.LEVEL_COLORS[entry.level] or nil
for _, line in ipairs(ADFlowView.wrapText(entry.text, SmartPickupFrame.TEXT_ROW_CHARS)) do
local title = self:addRow(self.textPrefab, {kind = "text"}):getDescendantByName("title")
title:setText(line)
if color ~= nil and title.setTextColor ~= nil then title:setTextColor(unpackFn(color)) end
end
end
function SmartPickupFrame:fillChainRows(adEnv)
local i18n = self.i18n
local last = self:chainLast(adEnv, false)
local rows = ADChainView.rows(last, function(name) return self:chainTitle(last, name) end,
SmartPickupFrame.formatCompact, function(key) return i18n:getText(key) end, self.chainExpanded or {})
for _, entry in ipairs(rows) do
if entry.kind == "info" or entry.kind == "empty" then
self:addTextRow(entry.text)
elseif entry.kind == "problem" then
self:addChainProblemRow(entry)
else
self:addChainRow(entry)
end
end
end