· 6 years ago · Oct 30, 2019, 12:04 AM
1local version = "1.0"
2
3function startup()
4
5 print("ChamAPI> Loading Version: "..version)
6
7 -- Load API "aeslua"
8 if not fs.exists("aeslua") then
9 print("ChamAPI> \"aeslua\" not found! Retrieving via wget!")
10 shell.run("wget","https://git.io/aeslua","aeslua")
11 end
12 os.loadAPI("aeslua")
13 print("ChamAPI> \"aeslua\" successfully loaded!")
14end
15
16-- > Most of these functions below are from the Touchpoint API, mainly in the Term class and the new(), setupLabel() functions below all with modifications with variable names, features, etc.
17-- > https://pastebin.com/pFHeia96
18local function setupLabel(buttonLen, minY, maxY, name)
19 local labelTable = {}
20 if type(name) == "table" then
21 for i = 1, #name do
22 labelTable[i] = name[i]
23 end
24 name = name.label
25 elseif type(name) == "string" then
26 local buttonText = string.sub(name, 1, buttonLen - 2)
27 if #buttonText < #name then
28 buttonText = " "..buttonText.." "
29 else
30 local labelLine = string.rep(" ", math.floor((buttonLen - #buttonText) / 2))..buttonText
31 buttonText = labelLine..string.rep(" ", buttonLen - #labelLine)
32 end
33 for i = 1, maxY - minY + 1 do
34 if maxY == minY or i == math.floor((maxY - minY) / 2) + 1 then
35 labelTable[i] = buttonText
36 else
37 labelTable[i] = string.rep(" ", buttonLen)
38 end
39 end
40 end
41 return labelTable, name
42end
43
44local Term = {
45
46 draw = function(self)
47 term = self.mon
48 term.clear()
49 for id, compoData in pairs(self.compoList) do
50 if not compoData.fixed then
51 term.setCursorPos(compoData.x1,compoData.fixed and compoData.y1 or compoData.y1 - self.scrollY)
52 term.setTextColor(compoData.color)
53 term.setBackgroundColor(compoData.bgColor)
54 if not compoData.hidden then
55 if compoData.label then
56 for i = compoData.y1, compoData.y2 do
57 term.setCursorPos(compoData.x1, compoData.fixed and i or i - self.scrollY)
58 term.write(compoData.label[i - compoData.y1 + 1])
59 end
60 else
61
62 end
63 end
64 end
65 end
66 for id, compoData in pairs(self.compoList) do
67 if compoData.fixed then
68 term.setCursorPos(compoData.x1,compoData.fixed and compoData.y1 or compoData.y1 - self.scrollY)
69 term.setTextColor(compoData.color)
70 term.setBackgroundColor(compoData.bgColor)
71 if not compoData.hidden then
72 if compoData.label then
73 for i = compoData.y1, compoData.y2 do
74 term.setCursorPos(compoData.x1, compoData.fixed and i or i - self.scrollY)
75 term.write(compoData.label[i - compoData.y1 + 1])
76 end
77 else
78 self:updateTextWrap()
79 for i = compoData.y1, #compoData.wrapLines + compoData.y1 do
80 term.setCursorPos(compoData.x1, compoData.fixed and i or i - self.scrollY)
81 term.write(compoData.wrapLines[i - compoData.y1 + 1])
82 end
83 end
84 end
85 end
86 end
87 term.setBackgroundColor(self.monColor)
88 end,
89
90 addText = function(self, id, text, x, y, color, bgColor)
91 if not (self or id) then
92 error("Too few arguments", 2)
93 end
94
95 self.compoList[id] = {
96 wrapLines = {},
97 text = text or "",
98 x1 = x,
99 y1 = y,
100 x2 = x,
101 y2 = y,
102 color = color or colors.white,
103 bgColor = bgColor or colors.black,
104 hidden = false,
105 fixed = false
106 }
107
108 self:updateScrollbar()
109 end,
110
111 addButton = function(self, id, text, func, x1, y1, x2, y2, color, bgColor)
112 if not (self or id or func or x1 or y1 or x2 or y2) then
113 error("Too few arguments", 2)
114 end
115
116 label, newText = setupLabel(x2 - x1, y1, y2, text or "")
117
118 self.compoList[id] = {
119 label = label,
120 text = newText,
121 func = func,
122 x1 = x1,
123 y1 = y1,
124 x2 = x2,
125 y2 = y2,
126 color = color or colors.white,
127 bgColor = bgColor or colors.black,
128 hidden = false,
129 fixed = false
130 }
131
132 self:updateClickMap()
133 self:updateScrollbar()
134 end,
135
136 hide = function(self,id)
137 self.compoList[id].hidden = true
138 self:updateClickMap()
139 self:updateScrollbar()
140 end,
141
142 hideAll = function(self)
143 for id, compoData in pairs(self.compoList) do
144 compoData.hidden = true
145 end
146 self:updateClickMap()
147 self:updateScrollbar()
148 end,
149
150 show = function(self,id)
151 self.compoList[id].hidden = false
152 self:updateClickMap()
153 self:updateScrollbar()
154 end,
155
156 showAll = function(self)
157 for id, compoData in pairs(self.compoList) do
158 compoData.hidden = false
159 end
160 self:updateClickMap()
161 self:updateScrollbar()
162 end,
163
164 fix = function(self, id)
165 self.compoList[id].fixed = true
166 self:updateClickMap()
167 self:updateScrollbar()
168 end,
169
170 unfix = function(self, id)
171 self.compoList[id].fixed = false
172 self:updateClickMap()
173 self:updateScrollbar()
174 end,
175
176 scroll = function(self,n)
177 self.scrollY = self.scrollY + n
178 self:updateClickMap()
179 end,
180
181 setColor = function(self,id,color)
182 self.compoList[id].color = color
183 end,
184
185 setBGColor = function(self, id, color)
186 self.compoList[id].bgColor = color
187 end,
188
189 setMonColor = function(self, color)
190 self.monColor = color
191 end,
192
193 showScrollbar = function(self, hideValue)
194 if not hideValue then
195 self.compoList[2000].hidden = true
196 if self.compoList[4000]~=nil then
197 self.compoList[4000].hidden = true
198 end
199 self.scrollbarShown = false
200 else
201 self.compoList[2000].hidden = false
202 if self.compoList[4000]~=nil then
203 self.compoList[4000].hidden = false
204 end
205 self.scrollbarShown = true
206 end
207 end,
208
209 updateClickMap = function(self)
210
211 local w = self.mon.getSize()
212
213 for i in ipairs(self.clickMap) do
214 self.clickMap[i] = {}
215 self.fixMap[i] = {}
216 end
217
218 for id, compoData in pairs(self.compoList) do
219 if compoData.label and not compoData.fixed and not compoData.hidden then
220 for i=compoData.x1,compoData.x2 do
221 for j = compoData.y1 - self.scrollY, compoData.y2 - self.scrollY do
222 if j>0 and j<=w then
223 self.clickMap[i][j] = id
224 end
225 end
226 end
227 end
228 end
229 for id, compoData in pairs(self.compoList) do
230 if compoData.label and compoData.fixed and not compoData.hidden then
231 for i=compoData.x1,compoData.x2 do
232 for j = compoData.y1, compoData.y2 do
233 if j>0 and j<=w then
234 self.clickMap[i][j] = id
235 self.fixMap[i][j] = true
236 end
237 end
238 end
239 end
240 end
241 end,
242
243 updateScrollbar = function(self)
244 local curMaxY = 0
245 for id, compoData in pairs(self.compoList) do
246 if not compoData.fixed and not compoData.hidden then
247 local iMaxY = (compoData.y2==nil) and compoData.y1 or compoData.y2
248 if curMaxY < iMaxY then
249 curMaxY = iMaxY
250 end
251 end
252 end
253
254 local _,h = self.mon.getSize()
255
256 self.hiddenY = curMaxY - h
257
258 if self.hiddenY <= 0 then
259 self:showScrollbar(false)
260 self.scrollY = 0
261 else
262 self:showScrollbar(true)
263 end
264 end,
265
266 updateTextWrap = function(self)
267 local term = self.mon
268 for id, compoData in pairs(self.compoList) do
269 if compoData.lines and not compoData.hidden then
270 local w = term.getSize()
271 local maxLength = scrollBarshown and -3 or 0 + w
272 term.write(compoData.text)
273 compoData.wrapLines = {compoData.text}
274 while #compoData.wrapLines[#compoData.wrapLines]>maxLength do
275 local i = #compoData.wrapLines[#compoData.wrapLines]
276 while (string.sub(compoData.wrapLines[#compoData.wrapLines],i,i)~=" " and i>maxLength) or i==1 do
277 i = i - 1
278 end
279 table.insert()
280 end
281 end
282 end
283 end,
284
285 run = function(self)
286 while true do
287 self:draw()
288 local event = {self:handleEvents(os.pullEvent(self.side=="term" and "mouse_click" or "monitor_touch"))}
289
290 if event[1]=="button_click" then
291 self.compoList[event[2]].func()
292 end
293
294 end
295 end,
296
297 handleEvents = function(self, ...)
298 local event = {...}
299
300 if #event == 0 then
301 event = {os.pullEvent()}
302 end
303
304 if (self.side=="term" and event[1]=="mouse_click") or (self.side ~= "term" and event[1]=="monitor_touch" and event[2]==self.side) then
305 local clicked = self.clickMap[event[3]][event[4]]
306 if clicked and self.compoList[clicked] and not self.compoList[clicked].hidden then
307 return "button_click", clicked
308 end
309 end
310
311 if (self.side=="term" and event[1]=="key") then
312 if (event[2]==200 or event[2]==203) then
313 return "key", -1
314 end
315 if (event[2]==205 or event[2]==208) then
316 return "key", 1
317 end
318 end
319
320 return unpack(event)
321 end
322}
323
324function new(monSide, monColor)
325 local termInstance = {
326 side = monSide or "term",
327 mon = monSide and peripheral.wrap(monSide) or term.current(),
328 monColor = monColor or colors.black,
329
330 maxY = 0,
331 scrollY = 0,
332 hiddenY = 0,
333 scrollbarShown = false,
334
335 fixMap = {},
336 compoList = {},
337 clickMap = {}
338 }
339
340 if termInstance.mon~=term.current() then
341 termInstance.mon.setTextScale(0.5)
342 end
343
344 local w, h = termInstance.mon.getSize()
345
346 for i = 1, w do
347 termInstance.fixMap[i] = {}
348 termInstance.clickMap[i] = {}
349 end
350 setmetatable(termInstance, {__index = Term})
351
352 termInstance:addButton(2000,string.char(24),function()termInstance:scroll(-2)end,w-3,1,w,4,colors.blue,colors.lightGray)
353 termInstance:addButton(4000,string.char(25),function()termInstance:scroll(2)end,w-3,h-3,w,h,colors.blue,colors.lightGray)
354
355 termInstance:hide(2000)
356 termInstance:hide(4000)
357
358 termInstance:fix(2000)
359 termInstance:fix(4000)
360
361 return termInstance
362end
363
364startup()