-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmenu
89 lines (86 loc) · 2.79 KB
/
menu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
local unpack = unpack or table.unpack
local function drawMenu(menuTable, cursor, offset, colors)
local xlim, ylim = term.getSize()
local sT = term.isColor() and colors[1] or colors.black
local sB = term.isColor() and colors[2] or colors.white
local nT = term.isColor() and colors[3] or colors.white
local nB = term.isColor() and colors[4] or colors.black
if xlim < 3 or (ylim < 3 and #menuTable > 2) then
return nil, "Not enought space to draw menu!"
end
term.setBackgroundColor(nB)
term.setTextColor(nT)
term.clear()
offset = offset or 1
for i = 1, math.min(ylim, #menuTable) do
term.setCursorPos(1, i)
if i == 1 and offset > 1 then
term.write(" /\\")
elseif i == ylim and #menuTable > offset + ylim - 1 then
term.write(" \\/")
else
if i == (cursor - offset + 1) then
term.setTextColor(sT)
term.setBackgroundColor(sB)
else
term.setTextColor(nT)
term.setBackgroundColor(nB)
end
local text = string.sub(menuTable[offset + i - 1], 1, xlim - 2)
term.write(" "..text..string.rep(" ", xlim - #text - 1))
end
end
end
function select(menuTable, selText, selBack, normText, normBack)
local colors = {selText or colors.white, selBack or colors.blue, normText or colors.white, normBack or colors.gray}
local cursor, offset = 1, 1
drawMenu(menuTable, cursor, offset, colors)
while true do
local e, p1, p2, p3 = os.pullEvent()
if e == "key" then
--up
if p1 == 200 then
if cursor - offset + 1 > 2 or (cursor > 1 and offset == 1) then
cursor = cursor - 1
drawMenu(menuTable, cursor, offset, colors)
elseif cursor - offset + 1 == 2 and offset > 1 then
offset = offset - 1
cursor = cursor - 1
drawMenu(menuTable, cursor, offset, colors)
end
--down
elseif p1 == 208 then
_, ylim = term.getSize()
if cursor < #menuTable and (cursor - offset + 1 < ylim - 1 or #menuTable <= ylim or (cursor == #menuTable - 1 and cursor - offset + 1 == ylim - 1)) then
cursor = cursor + 1
drawMenu(menuTable, cursor, offset, colors)
elseif cursor < #menuTable and cursor - offset + 1 == ylim - 1 then
offset = offset + 1
cursor = cursor + 1
drawMenu(menuTable, cursor, offset, colors)
end
elseif p1 == 28 then
return cursor
end
elseif e == "mouse_click" and p1 == 1 then
if p3 <= #menuTable then
_, ylim = term.getSize()
if offset > 1 and p3 == 1 then
--scroll up.
offset = offset - 1
cursor = offset + 1
drawMenu(menuTable, cursor, offset, colors)
elseif offset + ylim - 1 < #menuTable and p3 == ylim then
--scroll down.
offset = offset + 1
cursor = offset + ylim - 2
drawMenu(menuTable, cursor, offset, colors)
else
return p3 + offset - 1
end
end
elseif e == "term_resize" then
drawMenu(menuTable, cursor, offset, colors)
end
end
end