· 6 years ago · Mar 22, 2020, 11:08 PM
1os.loadAPI("/rom/apis/colors")
2
3--[[ Variables ]]--
4local offset = 0
5local sel = 1
6local printThanks = false
7
8local bgColour, textColour, selectedBgColour, selectedTextColour
9
10if term.isColour() then
11bgColour = colors.white
12textColour = colors.gray
13selectedBgColour = colors.lightBlue
14selectedTextColour = colors.white
15else
16bgColour = colors.white
17textColour = colors.black
18selectedBgColour = colors.black
19selectedTextColour = colors.white
20end
21
22local maxX, maxY = term.getSize()
23local running
24
25--[[ Functions ]]--
26
27clear = function()
28term.clear()
29term.setCursorPos(1,1)
30end
31
32local function centerWrite(txt)
33local x, y = term.getCursorPos()
34term.setCursorPos(math.floor(maxX/2-#tostring(txt)/2),y)
35write(txt)
36end
37
38local function redraw(tbl, sel, offset)
39term.setBackgroundColour(bgColour)
40clear()
41for i=1, maxY do
42 if tbl[i] ~= nil then
43 term.setCursorPos(1, i)
44 if (i+offset) == sel then
45 term.setBackgroundColour(selectedBgColour)
46 term.clearLine()
47 term.setTextColour(selectedTextColour)
48 centerWrite("[ "..tbl[i + offset].text.." ]")
49 else
50 term.setBackgroundColour(bgColour)
51 term.clearLine()
52 term.setTextColour(textColour)
53 centerWrite(tbl[i + offset].text)
54 end
55 end
56end
57end
58
59local function checkTable(tbl)
60for i,v in ipairs(tbl) do
61 if v.handler == nil or type(v.handler) ~= "function" then
62 if term.isColour() then
63 term.setTextColour(colors.red)
64 end
65 print("Menu item \""..i.."\" has no valid handler!")
66 local txt = textutils.serialize(tostring(v.handler))
67 error("handler = "..txt, 0)
68 elseif v.text == nil then
69 if term.isColour() then
70 term.setTextColour(colors.red)
71 end
72 print("Menu item \""..i.."\" has no text!")
73 local txt = textutils.serialize(tostring(v.text))
74 error("text = "..txt, 0)
75 end
76end
77end
78
79runMenu = function(tbl)
80if type(tbl) ~= "table" then
81 error("Invalid arguments!\nUsage: menuApi.runMenu(menu_table)", 0)
82elseif #tbl < 2 then
83 error("Not enough items in menu!\nAt least 2 items are required!", 0)
84end
85
86checkTable(tbl)
87
88running = true
89while running do
90 term.setCursorBlink(false)
91 os.queueEvent("")
92 os.pullEvent()
93 redraw(tbl, sel, offset)
94 local ev = {os.pullEvent()}
95 if ev[1] == "key" then
96 if ev[2] == keys.up then
97 if sel > 1 then sel = sel - 1 end
98 if offset > 0 then offset = offset - 1 end
99 elseif ev[2] == keys.down then
100 if sel < #tbl then sel = sel + 1 end
101 if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
102 elseif ev[2] == keys.enter then
103 term.setBackgroundColour(colors.black)
104 term.setTextColour(colors.white)
105 clear()
106 tbl[sel].handler()
107 end
108 elseif ev[1] == "mouse_scroll" then
109 if ev[2] == -1 then
110 if sel > 1 then sel = sel - 1 end
111 if offset > 0 then offset = offset - 1 end
112 elseif ev[2] == 1 then
113 if sel < #tbl then sel = sel + 1 end
114 if offset < math.max(#tbl - maxY, 0) then offset = offset + 1 end
115 end
116 elseif ev[1] == "mouse_click" then
117 if tbl[(ev[4] + offset)] ~= nil then
118 sel = ev[4] + offset
119 redraw(tbl, sel, offset)
120 sleep(.1)
121 term.setBackgroundColour(colors.black)
122 term.setTextColour(colors.white)
123 clear()
124 tbl[(ev[4] + offset)].handler()
125 end
126 end
127end
128end
129
130stopMenu = function()
131running = false
132term.setBackgroundColour(colors.black)
133if printThanks == true then
134 if term.isColour() == true then
135 term.setTextColour(colors.yellow)
136 end
137 clear()
138 centerWrite("Thank you for using HD's menu api.")
139 print("")
140else
141 clear()
142end
143end
144
145listMethods = function()
146local tmp = {}
147for i,v in pairs(menuApi) do
148 table.insert(tmp, i.."()")
149end
150textutils.pagedTabulate(tmp)
151local tmp = nil
152end
153
154local function isColour(color)
155if term.isColour() then
156 if type(color) == "string" then
157 if colors[color] ~= nil then
158 return {true, colors[color]}
159 else
160 return false
161 end
162 elseif type(color) == "number" then
163 if color >= 1 and color <= colors.black then
164 return {true, color}
165 else
166 return false
167 end
168 else
169 return false
170 end
171else
172 return false
173end
174end
175
176setBackgroundColour = function(color)
177local tmp = isColour(color)
178if tmp[1] then
179 bgColour = tmp[2]
180end
181end
182
183setBarColour = function(color)
184local tmp = isColour(color)
185if tmp[1] then
186 selectedBgColour = tmp[2]
187end
188end
189
190setTextColour = function(color)
191local tmp = isColour(color)
192if tmp[1] then
193 textColour = tmp[2]
194end
195end
196
197setBarTextColour = function(color)
198local tmp = isColour(color)
199if tmp[1] then
200 selectedTextColour = tmp[2]
201end
202end
203
204setBarTextColor = setBarTextColour
205setTextColor = setTextColour
206setBarColor = setBarColour
207setBackgroundColor = setBackgroundColour