· 4 years ago · Sep 07, 2021, 01:44 AM
1-- Button API
2-- Version: 1.1
3-- Tested in: computercraft 1.58 (Minecraft 1.6.4)
4-- By: Termanater13 (of the computercraft forums)
5-- Link: http://www.computercraft.info/forums2/index.php?/topic/17346-button-api/
6-- Description: Turn your advanced monitor into a touch screen with
7-- with fully customizable buttons and labels.
8-- Support: if you are having any issues with This API, use the link
9-- and follow the directions there.
10-- Credits: Direwolf20 for the button API he made that made me want
11-- to make my own, the display code is this is based on his code
12-- since ones I tried would not work.
13
14-- set variables for API
15local buttonData = {}
16local buttonFunc = {}
17local mon = {}
18local chk = {}
19new = {}
20e = {}
21e.s={}
22get = {}
23-- initioal settings for buttonSetting
24local buttonSetting = {}
25buttonSetting.back = {}
26buttonSetting.back.on = colors.lime
27buttonSetting.back.off = colors.red
28buttonSetting.back.non = colors.black
29buttonSetting.text = {}
30buttonSetting.text.on = colors.white
31buttonSetting.text.off = colors.white
32buttonSetting.text.non = colors.white
33buttonSetting.isTerm = false
34buttonSetting.scale = 1
35buttonSetting.size = {}
36buttonSetting.size.x = nil
37buttonSetting.size.y = nil
38-- set Side for the button to be displayed
39function wrap (side)
40 if side == "term" then
41 mon = term
42 buttonSetting.isTerm = true
43 buttonSetting.size.x, buttonSetting.size.y = 51, 19
44 else
45 mon = peripheral.wrap(side)
46 buttonSetting.size.x, buttonSetting.size.y = mon.getSize()
47 end
48end
49-- find coordinates middle value
50local function fx (l,h,t)
51 local n = string.len(t)
52 local f = h-l-n
53 if (f%2) == 0 then
54 return (f/2)
55 else
56 return (math.floor(f/2)+1)
57 end
58end
59local function fy (l,h)
60 return math.floor((h+l)/2)
61end
62-- check functions
63function chk.color(color)
64 if type(color) == "number" then
65 for i = 0,15,1 do
66 if color == (2^i) then
67 return color
68 end
69 end
70 elseif type(color) == "string" then
71 color = string.lower(color)
72 if color == "lightgray" or color == "light grey" then
73 color = "lightGray"
74 end
75 if color == "lightblue" or color == "light blue" then
76 color = "lightBlue"
77 end
78 if colors[color] then
79 return colors[color]
80 end
81 else
82 return false
83 end
84end
85-- make a new button
86function new.b (id,text,xmin,xmax,ymin,ymax,func,argpass)
87 buttonData[id]={}
88 buttonData[id].x={}
89 buttonData[id].x.min = xmin
90 buttonData[id].x.max = xmax
91 buttonData[id].y={}
92 buttonData[id].y.min = ymin
93 buttonData[id].y.max = ymax
94 buttonData[id].text = {}
95 buttonData[id].text.on = nil
96 buttonData[id].text.off = nil
97 buttonData[id].text.show = text
98 buttonData[id].back = {}
99 buttonData[id].back.on = nil
100 buttonData[id].back.off = nil
101 buttonData[id].display = true
102 buttonData[id].click = true
103 buttonData[id].hidden = false
104 buttonData[id].label = "B"
105 buttonData[id].status = false
106 buttonFunc[id] = {}
107 buttonFunc[id].func = func or nil
108 buttonFunc[id].argpass = argpass or nil
109end
110function new.l (id,text,x,y,func,argpass)
111 buttonData[id]={}
112 buttonData[id].x={}
113 buttonData[id].x.min = x
114 buttonData[id].x.max = x + string.len(text)
115 buttonData[id].y={}
116 buttonData[id].y.min = y
117 buttonData[id].y.max = y
118 buttonData[id].text = {}
119 buttonData[id].text.on = nil
120 buttonData[id].text.off = nil
121 buttonData[id].text.show = text
122 buttonData[id].back = {}
123 buttonData[id].back.on = nil
124 buttonData[id].back.off = nil
125 buttonData[id].display = true
126 buttonData[id].click = false
127 buttonData[id].hidden = false
128 buttonData[id].label = "L"
129 buttonData[id].status = false
130 buttonFunc[id] = {}
131 buttonFunc[id].func = func or nil
132 buttonFunc[id].argpass = argpass or nil
133end
134-- edit commands to directly set the values
135function e.color (id, T, on, off)
136 T = string.lower(T)
137 if T == "back" or T == "background" then
138 buttonData[id].back.on = chk.color(on)
139 buttonData[id].back.off = chk.color(off)
140 elseif T == "text" then
141 buttonData[id].text.on = chk.color(on)
142 buttonData[id].text.off = chk.color(off)
143 end
144end
145function e.x (id, T, value)
146 if type(T) == "number" then
147 if T > value then
148 value, T = T, value
149 end
150 buttonData[id].x.min = T
151 buttonData[id].x.max = value
152 elseif type(T) == "string" then
153 if string.lower(T) == "min" then
154 buttonData[id].x.min = value
155 elseif string.lower(T) == "max" then
156 buttonData[id].x.max = value
157 end
158 end
159end
160function e.y (id, T, value)
161 if type(T) == "number" then
162 if T > value then
163 value, T = T, value
164 end
165 buttonData[id].x.min = T
166 buttonData[id].x.max = value
167 elseif type(T) == "string" then
168 if string.lower(T) == "min" then
169 buttonData[id].x.min = value
170 elseif string.lower(T) == "max" then
171 buttonData[id].x.max = value
172 end
173 end
174end
175function e.text (id, value)
176 if type(value) == "string" then
177 buttonData[id].text.show = value
178 end
179end
180function e.label (id, value)
181 value = string.lower(value)
182 if value == "b" or value == "button" then
183 buttonData[id].label = "B"
184 elseif value == "l" or value == "label" then
185 buttonData[id].label = "L"
186 end
187end
188function e.func (id, value)
189 if type(value) == "function" then
190 buttonFunc[id].func = value
191 end
192end
193function e.argpass(id, value)
194 buttonFunc[id].argpass = argpass
195end
196-- edit commands to set or toggle values
197function e.display (id, value)
198 if type(value) == "string" then
199 if string.lower(value) == "on" or string.lower(value) == "yes" then
200 value = true
201 elseif string.lower(value) == "off" or string.lower(value) == "no" then
202 value = false
203 end
204 end
205 if value == nil or type(value) ~= "boolean" then
206 buttonData[id].display = not buttonData[id].display
207 elseif type(value) == "boolean" then
208 buttonData[id].display = value
209 end
210end
211function e.click (id, value)
212 if type(value) == "string" then
213 if string.lower(value) == "on" or string.lower(value) == "yes" then
214 value = true
215 elseif string.lower(value) == "off" or string.lower(value) == "no" then
216 value = false
217 end
218 end
219 if value == nil or type(value) ~= "boolean" then
220 buttonData[id].click = not buttonData[id].click
221 elseif type(value) == "boolean" then
222 buttonData[id].click = value
223 end
224end
225function e.status (id, value)
226 if type(value) == "string" then
227 if string.lower(value) == "on" or string.lower(value) == "yes" then
228 value = true
229 elseif string.lower(value) == "off" or string.lower(value) == "no" then
230 value = false
231 end
232 end
233 if value == nil or type(value) ~= "boolean" then
234 buttonData[id].status = not buttonData[id].status
235 elseif type(value) == "boolean" then
236 buttonData[id].status = value
237 end
238end
239function e.hidden (id, value)
240 if type(value) == "string" then
241 if string.lower(value) == "on" or string.lower(value) == "yes" then
242 value = true
243 elseif string.lower(value) == "off" or string.lower(value) == "no" then
244 value = false
245 end
246 end
247 if value == nil or type(value) ~= "boolean" then
248 buttonData[id].hidden = not buttonData[id].hidden
249 elseif type(value) == "boolean" then
250 buttonData[id].hidden = value
251 end
252end
253-- used to edit the settings
254function e.s.color(T, S, color)
255 T = string.lower(T)
256 S = string.lower(S)
257 local test = {}
258 if T == "back" or T == "background" or T == "text" then
259 test.t = true
260 if T == "background" then
261 T = "back"
262 end
263 end
264 if S == "on" or S == "Off" or S == "non" then
265 test.s = true
266 end
267 if test.t and test.s and chk.color(color) then
268 buttonSetting[T][S] = chk.color(color)
269 end
270end
271function e.s.scale(scale)
272 if not buttonSetting.isTerm then
273 buttonSetting.scale = scale
274 mon.setTextScale(scale)
275 end
276end
277-- used to get data for buttons
278function get.b (id)
279 if id == nil then
280 return buttonData
281 else
282 return buttonData[id]
283 end
284end
285get.l = get.b
286function get.s ()
287 return buttonSetting
288end
289function get.f (id)
290 return buttonFunc[id]
291end
292-- delet the selected button from table
293function delete (id)
294 buttonData[id] = nil
295 buttonFunc[id] = nil
296end
297-- color select
298local function colorselect(value, T)
299 if value.status then
300 if value.label == "B" then
301 if value[T].on == nil then
302 return buttonSetting[T].on
303 else
304 return value[T].on
305 end
306 elseif value.label == "L" then
307 if value[T].on == nil then
308 return buttonSetting[T].non
309 else
310 return value[T].on
311 end
312 end
313 else
314 if value.label == "B" then
315 if value[T].off == nil then
316 return buttonSetting[T].off
317 else
318 return value[T].off
319 end
320 elseif value.label == "L" then
321 if value[T].off == nil then
322 return buttonSetting[T].non
323 else
324 return value[T].off
325 end
326 end
327 end
328end
329-- display code
330function display ()
331 mon.setBackgroundColor(buttonSetting.back.non)
332 mon.setTextColor(buttonSetting.text.non)
333 mon.clear()
334 for key, value in pairs(buttonData) do
335 if value.display then
336 ys = fy(value.y.min,value.y.max)
337 xs = fx(value.x.min,value.x.max,value.text.show)
338 mon.setBackgroundColor(colorselect(value, "back"))
339 mon.setTextColor(colorselect(value, "text"))
340 for y = value.y.min, value.y.max, 1 do
341 mon.setCursorPos(value.x.min, y)
342 if y == ys then
343 for x = 0, (value.x.max-value.x.min-string.len(value.text.show)+1), 1 do
344 if x == xs then
345 mon.write(value.text.show)
346 else
347 mon.write(" ")
348 end
349 end
350 else
351 for x = 0, (value.x.max-value.x.min), 1 do
352 mon.write(" ")
353 end
354 end
355 end
356 end
357 end
358 mon.setBackgroundColor(buttonSetting.back.non)
359 mon.setTextColor(buttonSetting.text.non)
360end
361-- check if button is clicked
362function check(x,y)
363 for key, value in pairs(buttonData) do
364 if value.y.min <= y and y <= value.y.max then
365 if value.x.min <= x and x <= value.x.max then
366 if ((value.hidden and value.click) or (value.display and value.click)) and buttonFunc[key].func ~= nil then
367 if buttonFunc[key].argpass ~= nil then
368 buttonFunc[key].func(buttonFunc[key].argpass)
369 else
370 buttonFunc[key].func()
371 end
372 end
373 end
374 end
375 end
376end
377-- cycle command (not recommended but works)
378function cycle ()
379 local cychk = {}
380 while true do
381 display()
382 cychk["event"],cychk["side"],cychk["x"],cychk["y"] = os.pullEvent()
383 if cychk.event == "monitor_touch" or cychk.event == "mouse_click" then
384 check(cychk["x"],cychk["y"])
385 end
386 end
387end