84 lines
3.6 KiB
Lua
84 lines
3.6 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. Evaluering højst hvert CHAIN_EVALUATE_MS (visning, handler ikke).
|
|
|
|
SmartPickupFrame.CHAIN_EVALUATE_MS = 5000
|
|
|
|
-- Spillets ur i ms; uden g_time evalueres ved hver opbygning (der kun sker hvert CHAIN_REBUILD_TICKS s).
|
|
local function nowMs()
|
|
return type(g_time) == "number" and g_time or nil
|
|
end
|
|
|
|
function SmartPickupFrame:chainLast(adEnv, force)
|
|
local now = nowMs()
|
|
local due = force or ADChain.last == nil or now == nil or self.chainEvaluatedAt == nil
|
|
or now - self.chainEvaluatedAt >= SmartPickupFrame.CHAIN_EVALUATE_MS
|
|
if due then
|
|
ADChain.evaluate(adEnv)
|
|
self.chainEvaluatedAt = now
|
|
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.
|
|
function SmartPickupFrame:onChainAction(action)
|
|
if action.kind == "approve" then
|
|
ADChain.approve(action.fieldId)
|
|
elseif action.kind == "dismiss" then
|
|
ADChain.dismiss(action.fieldId)
|
|
elseif action.kind == "pause" or action.kind == "resume" then
|
|
ADChain.setPaused(action.rootKey, action.kind == "pause")
|
|
end
|
|
local adEnv = self.getAdEnv()
|
|
if adEnv ~= nil then self:chainLast(adEnv, true) end
|
|
self:requestRebuild()
|
|
end
|
|
|
|
-- Række med prik + c1..c5 (flowListPrefab); knappen er c5 (klik i c5 = handling, som Flows-listen).
|
|
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 = ADFlowView.LEVEL_COLORS[entry.level or "running"]
|
|
if dot ~= nil and dot.setImageColor ~= nil and color ~= nil then
|
|
dot:setImageColor(nil, unpackFn(color))
|
|
-- rækker uden egen status (forslag/afvis/auto) får ingen prik — kun rod og noder har status
|
|
if entry.kind ~= "root" and entry.kind ~= "node" and dot.setVisible ~= nil then dot:setVisible(false) end
|
|
end
|
|
if cells[4] ~= nil and cells[4].setTextColor ~= nil and color ~= nil and entry.level ~= "running" then
|
|
cells[4]:setTextColor(unpackFn(color))
|
|
end
|
|
if entry.action ~= nil then
|
|
local action, actionCell = entry.action, cells[5]
|
|
self:makeRowClickable(row, function(frame)
|
|
local onButton = frame.lastClickX == nil or actionCell == nil or actionCell.absPosition == nil
|
|
or frame.lastClickX >= actionCell.absPosition[1]
|
|
if onButton then frame:onChainAction(action) end
|
|
end)
|
|
end
|
|
return row
|
|
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)
|
|
for _, entry in ipairs(rows) do
|
|
if entry.kind == "info" or entry.kind == "empty" then
|
|
self:addTextRow(entry.text)
|
|
else
|
|
self:addChainRow(entry)
|
|
end
|
|
end
|
|
end
|