· 5 years ago · Nov 22, 2020, 09:34 PM
1--This is not mine, in case anyone stumbles across this unlisted paste. I just made a few edits.
2--The original author can be found here: http://www.computercraft.info/forums2/index.php?/topic/18977-guiapi-simple-powerful-buttonsdialogue-boxes-text-boxes-and-more/
3
4-- GUI API
5
6black = colors.black
7white = colors.white
8lightBlue = colors.lightBlue
9green = colors.green
10yellow = colors.yellow
11blue = colors.blue
12purple = colors.purple
13magenta = colors.magenta
14lime = colors.lime
15orange = colors.orange
16red = colors.red
17brown = colors.brown
18cyan = colors.cyan
19pink = colors.pink
20grey = colors.gray
21gray = colors.gray
22lightGray = colors.lightGray
23lightGrey = colors.lightGray
24
25-- Buttons
26
27buttonList = {}
28Buttons = {}
29Buttons.__index = Buttons
30
31function createButton( name, func )
32 button = {}
33 setmetatable(button,Buttons)
34 button.name = name
35 button.action = func
36 return button
37end
38
39function formatString(num)
40 local newstring = string.format("%04d", num * 100)
41 return string.sub(newstring,1,2).."."..string.sub(newstring,3,4).."%"
42end
43
44function Buttons:toggle( newColor,sec )
45
46 self:draw( self.x,self.y,self.width,newColor,self.tcolor)
47
48 if sec ~= nil then
49 sleep(sec)
50 self:draw( self.x,self.y,self.width,self.color,self.tcolor )
51 end
52end
53
54
55function Buttons:draw( x,y,width,color,tcolor )
56
57 table.insert(buttonList,{self.name,x,y,width,self.action})
58
59 self.x = x
60 self.y = y
61 self.width = width
62 if self.tcolor == nil then
63 self.color = color
64 end
65 self.tcolor = tcolor
66
67 for i = 1,width do
68 paintutils.drawLine(x, y + i, x + #self.name + 1, y + i, color)
69 end
70
71 term.setCursorPos(x,y + math.ceil(width/2))
72 term.setTextColor(tcolor)
73 term.setBackgroundColor(color)
74 term.write(" "..self.name.." ")
75end
76
77function Buttons:trigger()
78 buttonList[i][5]()
79end
80
81function Buttons:remove()
82 for i = 1,#buttonList do
83 if self.name == buttonList[i][1] then
84 table.remove(buttonList,i)
85 end
86 end
87 self = nil
88end
89
90function detect( x,y,trigger )
91 for i = 1,#buttonList do
92 if x >= buttonList[i][2] and x <= buttonList[i][2] + #buttonList[i][1] and y >= buttonList[i][3] and y <= buttonList[i][3] + buttonList[i][4] then
93 if trigger == true then
94 buttonList[i][5]()
95 end
96 return buttonList[i][1]
97 end
98 end
99end
100
101-- Progress Bars
102
103barList = {}
104Bars = {}
105Bars.__index = Bars
106
107function createBar( name )
108 bar = {}
109 setmetatable(bar,Bars)
110 bar.name = name
111 return bar
112end
113
114function Bars:setup( x,y,length,color,pcolor,disp,dispbcolor,tcolor )
115 self.x,self.y,self.length,self.color,self.pcolor,self.disp,self.dispbcolor,self.tcolor = x,y,length,color,pcolor,disp,dispbcolor,tcolor
116end
117
118function Bars:draw( x,y,length,color,pcolor,disp,dispbcolor,tcolor )
119
120 if self.x == nil and x ~= nil then
121 self.x,self.y,self.length,self.color,self.pcolor,self.disp,self.dispbcolor,self.tcolor = x,y,length,color,pcolor,disp,dispbcolor,tcolor
122 end
123
124 self.percent = 0
125 term.setCursorPos(x,y)
126 paintutils.drawLine(x,y,x+ length,y,color)
127 term.setCursorPos(x,y)
128 if self.percent > 0 then
129 paintutils.drawLine(x,y,x + length*(self.percent/100),y,pcolor)
130 end
131
132 percentString = formatString(self.percent)
133 --percentString = tostring(self.percent)
134 if disp == true then
135 term.setCursorPos(math.max(x + math.ceil(length/2) - math.ceil(#percentString/2),0) + 1,y-1)
136 term.setBackgroundColor(dispbcolor)
137 term.setTextColor(tcolor)
138 write(percentString.." ")
139 end
140end
141
142function Bars:update( percent )
143 if self.percent > 0 then
144 paintutils.drawLine(self.x,self.y,self.x + self.length*(self.percent/100),self.y,self.pcolor)
145 end
146 self.percent = percent
147 term.setCursorPos(self.x,self.y)
148 paintutils.drawLine(self.x + self.length*(self.percent/100) + 1,self.y,self.x + self.length,self.y,self.color)
149 term.setCursorPos(self.x,self.y)
150
151 percentString = formatString(self.percent)
152 if self.disp == true then
153 term.setCursorPos(math.max(self.x + math.ceil(self.length/2) - math.ceil(#percentString/2),0) + 1,self.y-1)
154 term.setBackgroundColor(self.dispbcolor)
155 term.setTextColor(self.tcolor)
156 write(percentString)
157 end
158end
159
160-- Textboxs
161
162function newTextBox(x,y,length,bcolor,tcolor)
163 typed = {}
164 alphabet = "abcdefghijklmnopqrstuvwxyz"
165
166 term.setBackgroundColor(bcolor)
167 paintutils.drawLine(x, y, x + length - 1, y, bcolor)
168 term.setCursorPos(x, y)
169 term.setTextColor(tcolor)
170 term.write("_")
171
172 typing = true
173 term.setCursorPos(x,y)
174 while typing do
175 event,key = os.pullEvent()
176 if event == "key" then
177 if key >= 0 and key <= 11 then
178 table.insert(typed,key)
179 elseif keys.getName(key) == "enter" then
180 typing = false
181 return string.gsub(table.concat(typed,""),"space"," ")
182 elseif keys.getName(key) == "space" then
183 table.insert(typed,keys.getName(key))
184 elseif keys.getName(key) == "period" then
185 table.insert(typed,".")
186 elseif keys.getName(key) == "comma" then
187 table.insert(typed,",")
188 elseif keys.getName(key) == "backspace" then
189 table.remove(typed,#typed)
190 else
191 key = keys.getName(key)
192 if string.find(alphabet,key) ~= nil then
193 table.insert(typed,key)
194 end
195 end
196 if #typed > length then
197 table.remove(typed,#typed)
198 else
199 cx,cy = term.getCursorPos()
200 term.setBackgroundColor(bcolor)
201 paintutils.drawLine(x, y, x + length - 1, y, bcolor)
202 term.setCursorPos(x,y)
203 term.write(string.gsub(table.concat(typed,""),"space"," "))
204 if cx < x + length then
205 term.write("_")
206 end
207 end
208 end
209 end
210end
211
212-- Boxes
213
214Boxs = {}
215Boxs.__index = Boxs
216
217function createDialogueBox( title,body,boxType )
218 boxes = {}
219 setmetatable(boxes,Boxs)
220 boxes.title = title
221 boxes.body = body
222 boxes.boxType = boxType
223 return boxes
224end
225
226function Boxs:draw( x,y,width,color,bcolor,tcolor )
227 ret = nil
228 self.width = width
229 self.x = x
230 self.y = y
231
232 if self.boxType == "yn" then -- YN Box
233
234 if type(self.body) ~= "table" then
235 paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
236 term.setCursorPos(x,y)
237 term.setTextColor(tcolor)
238 write(self.title)
239
240 self.len = #self.body
241
242 for i = 1,width do
243 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
244 end
245
246 term.setCursorPos(x + 1, y + 2)
247 term.write(self.body)
248
249
250 term.setCursorPos(x + 1,y + width)
251 term.setTextColor(tcolor)
252 term.setBackgroundColor(green)
253 write(" Yes ")
254
255 term.setCursorPos(x + len - 4,y + width)
256 term.setBackgroundColor(red)
257 write(" No ")
258
259 repeat
260 event,click,cx,cy = os.pullEvent("mouse_click")
261
262 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
263 ret = true
264 elseif cx >= x + len - 5 and cx <= x + len - 1 and cy == y + width then
265 ret = false
266 end
267 until ret ~= nil
268
269 else
270
271 len = 0
272 for i = 1,#self.body do
273 if #self.body[i] > len then
274 len = #self.body[i]
275 end
276 end
277
278 paintutils.drawLine(x, y, x + len + 1, y, bcolor)
279 term.setCursorPos(x,y)
280 term.setTextColor(tcolor)
281 write(self.title)
282
283 for i = 1,width do
284 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
285 end
286
287 for i = 1,#self.body do
288 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
289 term.write(self.body[i])
290 end
291
292 term.setCursorPos(x + 1,y + width)
293 term.setTextColor(tcolor)
294 term.setBackgroundColor(green)
295 write(" Yes ")
296
297 term.setCursorPos(x + len - 4,y + width)
298 term.setBackgroundColor(red)
299 write(" No ")
300
301 repeat
302 event,click,cx,cy = os.pullEvent("mouse_click")
303
304 if cx >= x + 1 and cx <= x + 5 and cy == y + width then
305 ret = true
306 elseif cx >= x + len - 5 and cx <= x + len - 1 and cy == y + width then
307 ret = false
308 end
309 until ret ~= nil
310
311
312 self.len = len
313 end
314
315 elseif self.boxType == "ok" then -- Ok Box
316 if type(self.body) ~= "table" then
317
318 paintutils.drawLine(x, y, x + #self.body + 1, y, bcolor)
319 term.setCursorPos(x,y)
320 term.setTextColor(tcolor)
321 write(self.title)
322 self.len = #self.body
323
324 for i = 1,width do
325 paintutils.drawLine(x, y + i, x + #self.body, y + i, color)
326 end
327
328 term.setCursorPos(x + 1, y + 2)
329 term.write(self.body)
330
331 term.setCursorPos(x + (self.len/2) - 1,y + width)
332 term.setTextColor(tcolor)
333 term.setBackgroundColor(green)
334 write(" Ok ")
335
336
337 repeat
338 event,click,cx,cy = os.pullEvent("mouse_click")
339
340 if cx > x + (self.len/2 - 4) and cx < x + (self.len/2) and cy == y + width then
341 ret = true
342 end
343 until ret == true
344
345 else
346
347 len = 0
348 for i = 1,#self.body do
349 if #self.body[i] > len then
350 len = #self.body[i]
351 end
352 end
353 self.len = len
354
355 paintutils.drawLine(x, y, x + len + 1, y, bcolor)
356 term.setCursorPos(x,y)
357 term.setTextColor(tcolor)
358 write(self.title)
359
360 for i = 1,width do
361 paintutils.drawLine(x, y + i, x + len + 1, y + i, color)
362 end
363
364 for i = 1,#self.body do
365 term.setCursorPos(x + (len/2 - #self.body[i]/2) + 1, y + i + 1)
366 term.write(self.body[i])
367 end
368
369 term.setCursorPos(x + (self.len/2) - 1,y + width)
370 term.setTextColor(tcolor)
371 term.setBackgroundColor(green)
372 write(" Ok ")
373
374 repeat
375 event,click,cx,cy = os.pullEvent("mouse_click")
376
377 if cx > x + (len/2 - 4) and cx < x + (len/2) and cy == y + width then
378 ret = true
379 end
380 until ret == true
381 end
382 end
383 return ret
384end
385
386function Boxs:clear( color )
387 paintutils.drawLine(self.x, self.y, self.x + self.len + 1, self.y, color)
388 for i = 1,self.width do
389 paintutils.drawLine(self.x, self.y + i, self.x + self.len + 1, self.y + i, color)
390 end
391end