minecraft-cc-tools/libs/ui/CountersSelector.lua

160 lines
4.0 KiB
Lua

local utils = require('libs/utils')
local function noTitleFn()
return ""
end
local savedTextColor = nil
local savedBackgroundColor = nil
local function saveTermConfig()
savedTextColor = term.getTextColor()
savedBackgroundColor = term.getBackgroundColor()
end
local function restoreTermConfig()
term.setTextColor(savedTextColor)
term.setBackgroundColor(savedBackgroundColor)
end
local function getTotalCount(countersMap)
local total = 0
for _, counter in pairs(countersMap) do
total = total + (counter or 0)
end
return total
end
local function dropN(t, n)
local result = {}
for k,v in pairs(t) do
if n == 0 then
result[k] = v
else
n = n - 1
end
end
return result
end
local function takeN(t, n)
local result = {}
for k,v in pairs(t) do
if n == 0 then
break
end
result[k] = v
n = n - 1
end
return result
end
local TITLE_MARGIN = 1
local function renderCountersMap(countersMap, selectedCounter, titleFn)
term.setCursorPos(1, 1)
local _, height = term.getSize()
-- local nbCounters = utils.sizeof(countersMap)
-- local totalCount = getTotalCount(countersMap)
local topMargin = 0
local bottomMargin = 0
if titleFn ~= noTitleFn then
term.clearLine()
term.write('custom title: ' .. titleFn(countersMap, selectedCounter))
topMargin = TITLE_MARGIN
end
local availableHeight = height - topMargin - bottomMargin
local selectedPage = math.floor(selectedCounter / availableHeight) + 1
-- local totalPages = (nbCounters % availableHeight) + 1
local nbElementsToOmit = (selectedPage - 1) * availableHeight
local displayedCounters = takeN(dropN(countersMap, nbElementsToOmit), availableHeight)
local cursorYIndex = 1 + topMargin
for k,v in pairs(displayedCounters) do
term.setCursorPos(1, cursorYIndex)
term.clearLine()
if k == selectedCounter then
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
else
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
end
term.write(tostring(v.name) .. ' ' .. tostring(v.count))
cursorYIndex = cursorYIndex + 1
end
end
local function CountersSelector(initialCountersMap, config)
local countersMap = utils.shallowClone(initialCountersMap)
local counterMax = config.counterMax
local titleFn = config.titleFn or noTitleFn
if not counterMax then
error('no counterMax found in CountersSelector config')
end
if utils.sizeof(countersMap) == 0 then
error('empty countersMap provided')
end
saveTermConfig()
term.clear()
local selectedCounter = 1
local nbCounters = utils.sizeof(countersMap)
local globalCounter = getTotalCount(countersMap)
if globalCounter > counterMax then
error('counter cannot be greater than the counterMax')
end
local shouldContinue = true
while shouldContinue do
renderCountersMap(countersMap, selectedCounter, titleFn)
local _, keyPressed, isHeld = os.pullEvent('key')
if keyPressed == keys.up then
selectedCounter = math.max(1, selectedCounter - 1)
elseif keyPressed == keys.down then
selectedCounter = math.min(nbCounters, selectedCounter + 1)
elseif keyPressed == keys.left and globalCounter > 0 then
local counterPayload = countersMap[selectedCounter]
if counterPayload and counterPayload.count and counterPayload.count > 0 then
countersMap[selectedCounter] = {
count = counterPayload.count - 1,
name = counterPayload.name
}
globalCounter = globalCounter - 1
end
elseif keyPressed == keys.right and globalCounter < counterMax then
local counterPayload = countersMap[selectedCounter]
if counterPayload and counterPayload.count then
countersMap[selectedCounter] = {
count = counterPayload.count + 1,
name = counterPayload.name
}
globalCounter = globalCounter + 1
end
elseif keyPressed == keys.enter then
shouldContinue = false
end
end
restoreTermConfig()
return countersMap, selectedCounter
end
return CountersSelector