--Selection menu made by MKlegoman357
local function selectionMenu(TableSelections, NumberKey, limit)
local xO, yO = term.getCursorPos()
local x, y = term.getCursorPos()
local xm, ym = term.getCursorPos()
local w, h = term.getSize()
local selection = 1
local pos = 1
local enable = false
limit = limit or #TableSelections
local function scroll ()
if selection < pos then
pos = selection
elseif selection > pos + limit - 1 then
pos = selection - limit + 1
end
end
while true do
x, y = xO + 1, yO
term.setCursorPos(x, y)
scroll()
for n = pos, getMin(#TableSelections, limit + pos - 1) do
resetColors()
if TableSelections[n] == "" then
term.setCursorPos(x, y)
term.write(TableSelections[n])
y = y + 1
elseif TableSelections[n] then
term.setCursorPos(x, y)
if selection == n then
term.setBackgroundColor(conBColor(colors.blue))
term.setTextColor(conTColor(colors.yellow))
end
term.write(TableSelections[n])
y = y + 1
end
end
resetColors()
x, y = xO, yO
term.setCursorPos(x, y)
if #TableSelections > 1 then
local b = limit < #TableSelections
local limit = getMin(#TableSelections, limit)
term.setBackgroundColor(conBColor(colors.blue))
term.setTextColor(conTColor(colors.yellow))
local start = getMax(round(pos / #TableSelections * limit), 1)
local finish = getMin(start + getMax(round(limit / #TableSelections * limit), 1), limit)
for n = start, finish do
term.setCursorPos(x, y + n - 1)
term.write(" ")
end
end
local event, p1, x, y = os.pullEvent()
if event == "key" then
if p1 == keys.down then
repeat
selection = selection + 1
until TableSelections[selection] ~= "" or selection <= 1
elseif p1 == keys.up then
repeat
selection = selection - 1
until TableSelections[selection] ~= "" or selection >= #TableSelections
elseif p1 == keys.enter then
return selection
elseif NumberKey and p1 == NumberKey then
return selection, true
else
x, y = xO, yO
term.setCursorPos(x, y)
end
elseif event == "mouse_scroll" then
if p1 < 0 then
repeat
selection = selection - 1
until TableSelections[selection] ~= "" or selection >= #TableSelections
else
repeat
selection = selection + 1
until TableSelections[selection] ~= "" or selection <= 1
end
elseif event == "mouse_click" then
local xw, yw = xO, yO
xp, yp = xO, yO
local ok = false
enable = false
for n = pos, getMin(#TableSelections, limit + pos - 1) do
if y == yw and x >= xw + 1 and x <= xw + #TableSelections[n] then
selection = n
ok = true
break
end
yw = yw + 1
end
if not ok then
local limit = getMin(#TableSelections, limit)
local start = getMax(round(pos / #TableSelections * (limit)), 1)
local finish = getMin(start + getMax(round(limit / #TableSelections * limit), 1), limit)
for n = start, finish do
term.setCursorPos(xp, yp + n - 1)
term.write(" ")
if x == xp and y == yp + n - 1 then
enable = true
break
end
end
end
if ok == true and p1 == 1 then
return selection
elseif p1 == 2 then
return selection, true
end
elseif event == "mouse_drag" and enable == true then
local moved = clamp(y - ym, -1, 1)
if moved > 0 then
local limit = getMin(#TableSelections, limit)
local start = getMax(round(pos / #TableSelections * limit), 1)
local finish = getMin(start + getMax(round(limit / #TableSelections * limit), 1), limit) + 1
local temp = round(finish / limit * #TableSelections)
selection = temp
else
local limit = getMin(#TableSelections, limit)
local start = getMax(round(pos / #TableSelections * limit), 1) - 1
local temp = round(start / limit * #TableSelections)
selection = temp
end
xm, ym = x, y
end
x, y = xO, yO
term.setCursorPos(x, y)
for n = pos, getMin(#TableSelections, limit + pos - 1) do
if TableSelections[n] then
resetColors()
term.setCursorPos(x, y)
term.write(string.rep(" ", #TableSelections[n] + 1))
y = y + 1
end
end
selection = clamp(selection, 1, #TableSelections)
end
end