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