switch72

Untitled

Apr 27th, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.83 KB | None | 0 0
  1. local versionMajor = "2"
  2. local versionMinor = "5"
  3.  
  4.  
  5. local component = require("component")
  6. local gpu = component.gpu
  7. local event = require("event")
  8. local computer = require("computer")
  9.  
  10. local screenWidth, screenHeight = gpu.getResolution()
  11.  
  12. local gui = {}
  13.  
  14. local colorScreenBackground = 0xC0C0C0
  15. local colorScreenForeground = 0x000000
  16. local colorTopLineBackground = 0x0000FF
  17. local colorTopLineForeground = 0xFFFFFF
  18. local colorBottomLineBackground = 0x0000FF
  19. local colorBottomLineForeground = 0xFFFFFF
  20. local colorFrameBackground = 0xC0C0C0
  21. local colorFrameForeground = 0x000000
  22. local colorButtonBackground = 0x0000FF
  23. local colorButtonForeground = 0xFFFFFF
  24. local colorButtonClickedBackground = 0x00FF00
  25. local colorButtonClickedForeground = 0xFFFFFF
  26. local colorButtonDisabledBackground = 0x000000
  27. local colorButtonDisabledForeground = 0xFFFFFF
  28. local colorTextBackground = 0x000000
  29. local colorTextForeground = 0xFFFF00
  30. local colorInputBackground = 0x0000FF
  31. local colorInputForeground = 0xFFFFFF
  32. local colorProgressBackground = 0x000000
  33. local colorProgressForeground = 0x00FF00
  34. local colorProgressNumberForeground = 0xFFFF00
  35. local colorListBackground = 0x0000FF
  36. local colorListForeground = 0xFFFFFF
  37. local colorListActiveBackground = 0x00FF00
  38. local colorListActiveForeground = 0xFFFF00
  39. local colorListDisabledBackground = 0x000000
  40. local colorListDisabledForeground = 0xFFFF00
  41. local colorVProgressBackground = 0x000000
  42. local colorVProgressForeground = 0x00FF00
  43. local colorVSliderBackground = 0x000000
  44. local colorVSliderForeground = 0x00FF00
  45. local colorChartBackground = 0x000000
  46. local colorChartForeground = 0x00FF00
  47.  
  48. local displayed = false
  49.  
  50.  
  51. function gui.Version()
  52. return versionMajor .. "." .. versionMinor, versionMajor, versionMinor
  53. end
  54.  
  55. function gui.checkVersion(major, minor)
  56. if major > tonumber(versionMajor) then
  57. compGui = gui.newGui("center", "center", 40, 9, true, nil, 0xFF0000, 0xFFFF00)
  58. gui.displayGui(compGui)
  59. gui.newLabel(compGui, "center", 1, "!Wrong Gui version!")
  60. gui.newLabel(compGui, "center", 3, string.format("Need version %d.%d",major, minor))
  61. gui.newLabel(compGui, "center", 5, string.format("Installed version is v %d.%d",versionMajor, versionMinor))
  62. gui.newHLine(compGui, 1, 6, 38)
  63. gui.newButton(compGui, "center", 7, "exit", gui.exit)
  64. while true do
  65. gui.runGui(compGui)
  66. end
  67. else
  68. if minor > tonumber(versionMinor) then
  69. compGui = gui.newGui("center", "center", 40, 9, true, nil, 0xFF0000, 0xFFFF00)
  70. gui.displayGui(compGui)
  71. gui.newLabel(compGui, "center", 1, "!Wrong Gui version!")
  72. gui.newLabel(compGui, "center", 3, string.format("Need version %d.%d",major, minor))
  73. gui.newLabel(compGui, "center", 5, string.format("Installed version is v %d.%d",versionMajor, versionMinor))
  74. gui.newHLine(compGui, 1, 6, 38)
  75. gui.newButton(compGui, "center", 7, "exit", gui.exit)
  76. while true do
  77. gui.runGui(compGui)
  78. end
  79. end
  80. end
  81. end
  82.  
  83. function gui.clearScreen()
  84. gpu.setBackground(colorScreenBackground)
  85. gpu.setForeground(colorScreenForeground)
  86. gpu.fill(1, 1, screenWidth, screenHeight, " ")
  87.  
  88. gpu.setBackground(colorTopLineBackground)
  89. gpu.setForeground(colorTopLineForeground)
  90. gpu.fill(1, 1, screenWidth, 1, " ")
  91.  
  92. gpu.setBackground(colorBottomLineBackground)
  93. gpu.setForeground(colorBottomLineForeground)
  94. gpu.fill(1, screenHeight, screenWidth, 1, " ")
  95. end
  96.  
  97. function gui.setTop(text)
  98. gpu.setBackground(colorTopLineBackground)
  99. gpu.setForeground(colorTopLineForeground)
  100. gpu.set( (screenWidth / 2) - (string.len(text) / 2), 1, text)
  101. end
  102.  
  103. function gui.setBottom(text)
  104. gpu.setBackground(colorBottomLineBackground)
  105. gpu.setForeground(colorBottomLineForeground)
  106. gpu.set( (screenWidth / 2) - (string.len(text) / 2), screenHeight, text)
  107. end
  108.  
  109. local function saveBackground(x,y,w,h)
  110. local buffer = {}
  111. for i = x,x + w do
  112. for j = y,y + h do
  113. local ch,fc,bc = gpu.get(i,j)
  114. local tmp = {i,j,ch,fc,bc}
  115. table.insert(buffer, tmp)
  116. end
  117. end
  118. return buffer
  119. end
  120.  
  121.  
  122. local function restoreBackground(buff)
  123. for k,v in pairs(buff) do
  124. gpu.setBackground(v[5])
  125. gpu.setForeground(v[4])
  126. gpu.set(v[1], v[2], v[3])
  127. end
  128. end
  129.  
  130. function gui.closeGui(guiID)
  131. -- print("restoring" .. guiID.num)
  132. -- os.sleep(1)
  133. restoreBackground(guiID.buffer)
  134. guiID.new = true
  135. end
  136.  
  137.  
  138. -- displays the gui frame, if set or just clears the display area
  139. local function _displayFrame(guiID)
  140. if guiID.new == true then
  141. -- print("saving" .. guiID.num)
  142. guiID.buffer = saveBackground(guiID.x, guiID.y, guiID.width, guiID.height)
  143. guiID.new = false
  144. -- os.sleep(1)
  145. end
  146. gpu.setBackground(guiID.bg)
  147. gpu.setForeground(guiID.fg)
  148. gpu.fill(guiID.x, guiID.y, guiID.width, guiID.height, " ")
  149. if guiID.frame == true then
  150. gpu.fill(guiID.x, guiID.y, 1, guiID.height, "║")
  151. gpu.fill(guiID.x + guiID.width - 1, guiID.y, 1, guiID.height, "║")
  152. gpu.fill(guiID.x, guiID.y, guiID.width, 1, "═")
  153. gpu.fill(guiID.x, guiID.y + guiID.height - 1, guiID.width, 1, "═")
  154. gpu.set(guiID.x, guiID.y, "╔")
  155. gpu.set(guiID.x + guiID.width - 1 , guiID.y, "╗")
  156. gpu.set(guiID.x, guiID.y + guiID.height - 1 , "╚")
  157. gpu.set(guiID.x + guiID.width - 1 , guiID.y + guiID.height - 1, "╝")
  158. if guiID.text then
  159. gpu.set(guiID.x + math.floor((guiID.width/2)) - math.floor((string.len(guiID.text)/2)), guiID.y, guiID.text)
  160. end
  161. end
  162. end
  163.  
  164. -- displays a frame
  165. local function _displayAFrame(guiID, frameID)
  166. if guiID[frameID].visible == true then
  167. gpu.setBackground(guiID.bg)
  168. gpu.setForeground(guiID.fg)
  169. gpu.fill(guiID[frameID].x, guiID[frameID].y, 1, guiID[frameID].height, "║")
  170. gpu.fill(guiID[frameID].x + guiID[frameID].width - 1, guiID[frameID].y, 1, guiID[frameID].height, "║")
  171. gpu.fill(guiID[frameID].x, guiID[frameID].y, guiID[frameID].width, 1, "═")
  172. gpu.fill(guiID[frameID].x, guiID[frameID].y + guiID[frameID].height - 1, guiID[frameID].width, 1, "═")
  173. gpu.set(guiID[frameID].x, guiID[frameID].y, "╔")
  174. gpu.set(guiID[frameID].x + guiID[frameID].width - 1 , guiID[frameID].y, "╗")
  175. gpu.set(guiID[frameID].x, guiID[frameID].y + guiID[frameID].height - 1 , "╚")
  176. gpu.set(guiID[frameID].x + guiID[frameID].width - 1 , guiID[frameID].y + guiID[frameID].height - 1, "╝")
  177. if guiID[frameID].text then
  178. gpu.set(guiID[frameID].x + math.floor((guiID[frameID].width/2)) - math.floor((string.len(guiID[frameID].text)/2)+1), guiID[frameID].y, "╡" .. guiID[frameID].text .. "┝")
  179. end
  180. end
  181. end
  182.  
  183. --display a horizontal line
  184. local function _displayHLine(guiID, lineID)
  185. gpu.setBackground(guiID.bg)
  186. gpu.setForeground(guiID.fg)
  187. gpu.fill(guiID[lineID].x, guiID[lineID].y, guiID[lineID].width, 1, "═")
  188. end
  189.  
  190. -- displays a checkbox
  191. local function _displayCheckbox(guiID, checkboxID)
  192. if guiID[checkboxID].visible == true then
  193. gpu.setBackground(guiID.bg)
  194. gpu.setForeground(guiID.fg)
  195. local x = 0
  196. x =guiID.x + guiID[checkboxID].x
  197. if guiID[checkboxID].status == true then
  198. gpu.set(x, guiID[checkboxID].y, "[√]")
  199. else
  200. gpu.set(x, guiID[checkboxID].y, "[ ]")
  201. end
  202. end
  203. end
  204.  
  205. -- displays a radio button
  206. local function _displayRadio(guiID, radioID)
  207. if guiID[radioID].visible == true then
  208. gpu.setBackground(guiID.bg)
  209. gpu.setForeground(guiID.fg)
  210. local x = 0
  211. x =guiID.x + guiID[radioID].x
  212. if guiID[radioID].status == true then
  213. gpu.set(x, guiID[radioID].y, "(x)")
  214. else
  215. gpu.set(x, guiID[radioID].y, "( )")
  216. end
  217. end
  218. end
  219.  
  220. -- displays a label
  221. local function _displayLabel(guiID, labelID)
  222. if guiID[labelID].visible == true then
  223. gpu.setBackground(guiID[labelID].bg)
  224. gpu.setForeground(guiID[labelID].fg)
  225. local x = 0
  226. if guiID[labelID].x == "center" then
  227. x = guiID.x + math.floor((guiID.width / 2)) - math.floor((string.len(guiID[labelID].text)) / 2)
  228. else
  229. x =guiID.x + guiID[labelID].x
  230. end
  231. gpu.fill(x, guiID[labelID].y, guiID[labelID].l , 1, " ")
  232. gpu.set(x, guiID[labelID].y, guiID[labelID].text)
  233. end
  234. end
  235.  
  236. -- displays a time label
  237. local function _displayTimeLabel(guiID, labelID)
  238. if guiID[labelID].visible == true then
  239. gpu.setBackground(guiID[labelID].bg)
  240. gpu.setForeground(guiID[labelID].fg)
  241. local x = guiID.x + guiID[labelID].x
  242. gpu.set(x, guiID[labelID].y, os.date("%H:%M", os.time()))
  243. end
  244. end
  245.  
  246. -- displays a date label
  247. local function _displayDateLabel(guiID, labelID)
  248. if guiID[labelID].visible == true then
  249. gpu.setBackground(guiID[labelID].bg)
  250. gpu.setForeground(guiID[labelID].fg)
  251. local x = guiID.x + guiID[labelID].x
  252. if guiID[labelID].frm == false then
  253. gpu.set(x, guiID[labelID].y, os.date("%d/%m/%Y"))
  254. elseif guiID[labelID].frm == true then
  255. gpu.set(x, guiID[labelID].y, os.date("%A %d. %B %Y"))
  256. end
  257. end
  258. end
  259.  
  260. local function splitWords(Lines, limit)
  261. while #Lines[#Lines] > limit do
  262. Lines[#Lines+1] = Lines[#Lines]:sub(limit+1)
  263. Lines[#Lines-1] = Lines[#Lines-1]:sub(1,limit)
  264. end
  265. end
  266.  
  267. local function wrap(str, limit)
  268. local Lines, here, limit, found = {}, 1, limit or 72, str:find("(%s+)()(%S+)()")
  269.  
  270. if found then
  271. Lines[1] = string.sub(str,1,found-1) -- Put the first word of the string in the first index of the table.
  272. else Lines[1] = str end
  273.  
  274. str:gsub("(%s+)()(%S+)()",
  275. function(sp, st, word, fi) -- Function gets called once for every space found.
  276. splitWords(Lines, limit)
  277.  
  278. if fi-here > limit then
  279. here = st
  280. Lines[#Lines+1] = word -- If at the end of a line, start a new table index...
  281. else Lines[#Lines] = Lines[#Lines].." "..word end -- ... otherwise add to the current table index.
  282. end)
  283.  
  284. splitWords(Lines, limit)
  285.  
  286. return Lines
  287. end
  288.  
  289. -- displays a multi line label
  290. local function _displayMultiLineLabel(guiID, labelID)
  291. if guiID[labelID].visible == true then
  292. gpu.setBackground(guiID[labelID].bg)
  293. gpu.setForeground(guiID[labelID].fg)
  294. gpu.fill(guiID[labelID].x, guiID[labelID].y, guiID[labelID].w, guiID[labelID].h, " ")
  295. local text = wrap(guiID[labelID].text, guiID[labelID].w)
  296. for i = 1, #text do
  297. gpu.set(guiID[labelID].x, guiID[labelID].y + i, text[i])
  298. end
  299. end
  300. end
  301.  
  302. -- displays a button
  303. local function _displayButton(guiID, buttonID)
  304. if guiID[buttonID].visible == true then
  305. if guiID[buttonID].active == true then
  306. gpu.setBackground(colorButtonClickedBackground)
  307. gpu.setForeground(colorButtonClickedForeground)
  308. elseif guiID[buttonID].enabled == false then
  309. gpu.setBackground(colorButtonDisabledBackground)
  310. gpu.setForeground(colorButtonDisabledForeground)
  311. else
  312. gpu.setBackground(colorButtonBackground)
  313. gpu.setForeground(colorButtonForeground)
  314. end
  315. local x = 0
  316. if guiID[buttonID].x == "center" then
  317. x = guiID.x + math.floor((guiID.width / 2)) - math.floor((guiID[buttonID].lenght / 2))
  318. else
  319. x = guiID.x + guiID[buttonID].x
  320. end
  321. gpu.fill(x, guiID[buttonID].y, guiID[buttonID].lenght, 1, " ")
  322. gpu.set(x, guiID[buttonID].y, guiID[buttonID].text)
  323. end
  324. end
  325.  
  326. -- displays a text
  327. local function _displayText(guiID, textID)
  328. if guiID[textID].visible == true then
  329. gpu.setBackground(colorTextBackground)
  330. gpu.setForeground(colorTextForeground)
  331. local x = 0
  332. if guiID[textID].x == "center" then
  333. x = guiID.x + math.floor((guiID.width / 2)) - math.floor((guiID[textID].fieldLenght) / 2)
  334. else
  335. x = guiID.x + guiID[textID].x
  336. end
  337. gpu.fill(x, guiID[textID].y , guiID[textID].fieldLenght, 1, " ")
  338. tmpStr = guiID[textID].text
  339. if guiID[textID].hide == true then
  340. tmpStr = ""
  341. for i = 1, string.len(guiID[textID].text) do
  342. tmpStr = tmpStr .."*"
  343. end
  344. end
  345. gpu.set(x, guiID[textID].y, string.sub(tmpStr, 1, guiID[textID].fieldLenght))
  346. end
  347. end
  348.  
  349. -- displays a vertical slider
  350. local function _displayVslider(guiID, sliderID)
  351. if guiID[sliderID].visible == true then
  352. gpu.setBackground(colorVSliderBackground)
  353. gpu.setForeground(colorVSliderForeground)
  354. local x = 0
  355. x = guiID.x + guiID[sliderID].x
  356. gpu.fill(x, guiID[sliderID].y , guiID[sliderID].lenght + 2, 1, " ")
  357. gpu.setBackground(colorButtonBackground)
  358. gpu.setForeground(colorButtonForeground)
  359. gpu.set(x, guiID[sliderID].y, "-")
  360. gpu.set(x + guiID[sliderID].lenght + 1, guiID[sliderID].y, "+")
  361. x = x + 1
  362. local proz = math.floor(100 / guiID[sliderID].max * guiID[sliderID].value)
  363. if proz > 100 then
  364. proz = 100
  365. if guiID[sliderID].func then
  366. guiID[sliderID].func(guiID, sliderID)
  367. end
  368. end
  369. local pos = math.floor(guiID[sliderID].lenght / 100 * proz)
  370. gpu.setBackground(colorVSliderForeground)
  371. gpu.setForeground(colorVSliderBackground)
  372. gpu.fill(x, guiID[sliderID].y , pos, 1, " ")
  373. gpu.setBackground(colorVSliderBackground)
  374. gpu.setForeground(colorVSliderForeground)
  375. gpu.fill(x + pos, guiID[sliderID].y , guiID[sliderID].lenght - pos, 1, " ")
  376. end
  377. end
  378.  
  379. -- displays a progress bar
  380. local function _displayProgress(guiID, progressID)
  381. if guiID[progressID].visible == true then
  382. gpu.setBackground(colorProgressForeground)
  383. gpu.setForeground(colorProgressBackground)
  384. local x = 0
  385. if guiID[progressID].x == "center" then
  386. x = guiID.x + math.floor((guiID.width / 2)) - math.floor((guiID[progressID].lenght) / 2)
  387. else
  388. x = guiID.x + guiID[progressID].x
  389. end
  390. local proz = math.floor(100 / guiID[progressID].max * guiID[progressID].value)
  391. if proz > 100 then
  392. proz = 100
  393. if guiID[progressID].finished == false and guiID[progressID].func then
  394. guiID[progressID].func(guiID, progressID)
  395. end
  396. guiID[progressID].finished = true
  397. end
  398. local pos = math.floor(guiID[progressID].lenght / 100 * proz)
  399. gpu.fill(x, guiID[progressID].y , pos, 1, " ")
  400. gpu.setBackground(colorProgressBackground)
  401. gpu.setForeground(colorProgressForeground)
  402. gpu.fill(x + pos, guiID[progressID].y , guiID[progressID].lenght - pos, 1, " ")
  403. gpu.setBackground(guiID.bg)
  404. gpu.setForeground(guiID.fg)
  405. if guiID[progressID].displayNumber == true then
  406. gpu.fill(x, guiID[progressID].y - 1, guiID[progressID].lenght, 1, " ")
  407. gpu.set(x + (math.floor(guiID[progressID].lenght / 2)) - 1, guiID[progressID].y - 1, proz .. "%")
  408. end
  409. end
  410. end
  411.  
  412. -- displays a vertical progress bar
  413. local function _displayVProgress(guiID, progressID)
  414. if guiID[progressID].visible == true then
  415. local x = 0
  416. if guiID[progressID].x == "center" then
  417. x = guiID.x + math.floor((guiID.width / 2)) - math.floor((guiID[progressID].lenght) / 2)
  418. else
  419. x = guiID.x + guiID[progressID].x
  420. end
  421. local proz = math.floor(100 / guiID[progressID].max * guiID[progressID].value)
  422. if proz > 100 then
  423. proz = 100
  424. if guiID[progressID].finished == false and guiID[progressID].func then
  425. guiID[progressID].func(guiID, progressID)
  426. end
  427. guiID[progressID].finished = true
  428. end
  429. local pos = math.floor(guiID[progressID].lenght / 100 * proz)
  430. for i = 1, guiID[progressID].width do
  431. if guiID[progressID].direction == 0 then
  432. gpu.setBackground(colorProgressForeground)
  433. gpu.setForeground(colorProgressBackground)
  434. gpu.fill(x+i-1, guiID[progressID].y , 1, pos, " ")
  435. gpu.setBackground(colorProgressBackground)
  436. gpu.setForeground(colorProgressForeground)
  437. gpu.fill(x+i-1, guiID[progressID].y + pos, 1, guiID[progressID].lenght - pos, " ")
  438. end
  439. if guiID[progressID].direction == 1 then
  440. gpu.setBackground(colorProgressBackground)
  441. gpu.setForeground(colorProgressForeground)
  442. gpu.fill(x+i-1, guiID[progressID].y, 1, guiID[progressID].lenght, " ")
  443. gpu.setBackground(colorProgressForeground)
  444. gpu.setForeground(colorProgressBackground)
  445. gpu.fill(x+i-1, guiID[progressID].y + guiID[progressID].lenght - pos , 1, pos, " ")
  446. end
  447. end
  448. end
  449. end
  450.  
  451. -- display list
  452. local function _displayList(guiID, listID)
  453. if guiID[listID].visible == true then
  454. if guiID[listID].enabled == true then
  455. gpu.setBackground(colorListBackground)
  456. gpu.setForeground(colorListForeground)
  457. else
  458. gpu.setBackground(colorListDisabledBackground)
  459. gpu.setForeground(colorListDisabledForeground)
  460. end
  461. gpu.fill(guiID[listID].x, guiID[listID].y, guiID[listID].width, guiID[listID].height, " ")
  462. gpu.fill(guiID[listID].x, guiID[listID].y, guiID[listID].width, 1, "═")
  463. if guiID[listID].text then
  464. gpu.set( guiID[listID].x + (guiID[listID].width/2) - (string.len(guiID[listID].text)/2), guiID[listID].y, "╡" .. guiID[listID].text .. "┝")
  465. end
  466. if guiID[listID].active + guiID[listID].height - 3 > #guiID[listID].entries then
  467. l = #guiID[listID].entries
  468. else
  469. l = guiID[listID].active + guiID[listID].height - 3
  470. end
  471. gpu.fill(guiID[listID].x, guiID[listID].y +guiID[listID].height - 1, guiID[listID].width, 1, "═")
  472. gpu.set(guiID[listID].x, guiID[listID].y + guiID[listID].height - 1, "[<]")
  473. gpu.set(guiID[listID].x + guiID[listID].width - 3, guiID[listID].y + guiID[listID].height - 1, "[>]")
  474. for v = guiID[listID].active, l do
  475. if v == guiID[listID].selected then
  476. gpu.setBackground(colorListActiveBackground)
  477. gpu.setForeground(colorListActiveForeground)
  478. else
  479. if guiID[listID].enabled == true then
  480. gpu.setBackground(colorListBackground)
  481. gpu.setForeground(colorListForeground)
  482. else
  483. gpu.setBackground(colorListDisabledBackground)
  484. gpu.setForeground(colorListDisabledForeground)
  485. end
  486. end
  487. gpu.fill(guiID[listID].x, guiID[listID].y + v - guiID[listID].active + 1, guiID[listID].width, 1 , " ")
  488. gpu.set(guiID[listID].x + 1, guiID[listID].y + v - guiID[listID].active + 1, guiID[listID].entries[v] )
  489. end
  490. end
  491. end
  492.  
  493. -- displays a chart
  494. local function _displayChart(guiID, chartID)
  495. if guiID[chartID].visible == true then
  496. gpu.setBackground(colorChartBackground)
  497. gpu.setForeground(colorChartForeground)
  498. for x = 1, #guiID[chartID].data do
  499. local proz = math.floor(100 / guiID[chartID].max * guiID[chartID].data[x])
  500. local dotPos = guiID[chartID].height - math.floor( guiID[chartID].height / guiID[chartID].max * guiID[chartID].data[x])
  501. for y = 1, guiID[chartID].height do
  502. if dotPos == y then
  503. gpu.setBackground(colorChartForeground)
  504. else
  505. gpu.setBackground(colorChartBackground)
  506. end
  507. gpu.set(x + guiID[chartID].x, y + guiID[chartID].y, " ")
  508.  
  509. end
  510. end
  511. end
  512. end
  513.  
  514. -- display the gui and all widgets
  515. function gui.displayGui(guiID)
  516.  
  517. _displayFrame(guiID)
  518.  
  519. for i = 1, #guiID do
  520. if guiID[i].type == "label" then
  521. _displayLabel(guiID, i)
  522. elseif guiID[i].type == "multiLineLabel" then
  523. _displayMultiLineLabel(guiID, i)
  524. elseif guiID[i].type == "button" then
  525. _displayButton(guiID, i)
  526. elseif guiID[i].type == "text" then
  527. _displayText(guiID, i)
  528. elseif guiID[i].type == "progress" then
  529. _displayProgress(guiID, i)
  530. elseif guiID[i].type == "vprogress" then
  531. _displayVProgress(guiID, i)
  532. elseif guiID[i].type == "list" then
  533. _displayList(guiID, i)
  534. elseif guiID[i].type == "frame" then
  535. _displayAFrame(guiID, i)
  536. elseif guiID[i].type == "hline" then
  537. _displayHLine(guiID, i)
  538. elseif guiID[i].type == "checkbox" then
  539. _displayCheckbox(guiID, i)
  540. elseif guiID[i].type == "radio" then
  541. _displayRadio(guiID, i)
  542. elseif guiID[i].type == "vslider" then
  543. _displayVslider(guiID, i)
  544. elseif guiID[i].type == "chart" then
  545. _displayChart(guiID, i)
  546. end
  547. end
  548. end
  549.  
  550. function gui.displayWidget(guiID, widgetID)
  551.  
  552. if guiID[widgetID].type == "label" then
  553. _displayLabel(guiID, widgetID)
  554. elseif guiID[widgetID].type == "multiLineLabel" then
  555. _displayMultiLineLabel(guiID, widgetID)
  556. elseif guiID[widgetID].type == "button" then
  557. _displayButton(guiID, widgetID)
  558. elseif guiID[widgetID].type == "text" then
  559. _displayText(guiID, widgetID)
  560. elseif guiID[widgetID].type == "progress" then
  561. _displayProgress(guiID, widgetID)
  562. elseif guiID[widgetID].type == "vprogress" then
  563. _displayVProgress(guiID, widgetID)
  564. elseif guiID[widgetID].type == "list" then
  565. _displayList(guiID, widgetID)
  566. elseif guiID[widgetID].type == "frame" then
  567. _displayAFrame(guiID, widgetID)
  568. elseif guiID[widgetID].type == "hline" then
  569. _displayHLine(guiID, widgetID)
  570. elseif guiID[widgetID].type == "checkbox" then
  571. _displayCheckbox(guiID, widgetID)
  572. elseif guiID[widgetID].type == "radio" then
  573. _displayRadio(guiID, widgetID)
  574. elseif guiID[widgetID].type == "vslider" then
  575. _displayVslider(guiID, widgetID)
  576. elseif guiID[widgetID].type == "chart" then
  577. _displayChart(guiID, widgetID)
  578. end
  579. end
  580.  
  581. function gui.exit()
  582. gpu.setBackground(0x000000)
  583. gpu.setForeground(0xFFFFFF)
  584. gpu.fill(1, 1, screenWidth, screenHeight, " ")
  585. os.exit()
  586. end
  587.  
  588. local guiCounter = 0
  589. -- need to be called first to setup a new dialog
  590. function gui.newGui(x, y, w, h, frame, text, bg, fg)
  591. local tmpTable = {}
  592. tmpTable["type"] = "gui"
  593. if x == "center" then
  594. tmpTable["x"] = math.floor((screenWidth / 2) - (w / 2))
  595. else
  596. tmpTable["x"] = x
  597. end
  598. if y == "center" then
  599. tmpTable["y"] = math.floor((screenHeight / 2) - (h / 2))
  600. else
  601. tmpTable["y"] = y
  602. end
  603. tmpTable["bg"] = bg or colorFrameBackground
  604. tmpTable["fg"] = fg or colorFrameForeground
  605. tmpTable["width"] = w
  606. tmpTable["height"] = h
  607. tmpTable["frame"] = frame
  608. if text then
  609. tmpTable["text"] = "╡" .. text .. "┝"
  610. end
  611. tmpTable["buffer"] = {}
  612. tmpTable["num"] = guiCounter
  613. guiCounter = guiCounter + 1
  614. tmpTable["new"] = true
  615. displayed = false
  616. return tmpTable
  617. end
  618.  
  619. -- checkbox
  620. function gui.newCheckbox(guiID, x, y, status, func)
  621. local tmpTable = {}
  622. tmpTable["type"] = "checkbox"
  623. tmpTable["status"] = status or false
  624. tmpTable["y"] = y + guiID.y
  625. tmpTable["visible"] = true
  626. tmpTable["enabled"] = true
  627. tmpTable["x"] = x
  628. tmpTable["func"] = func
  629. table.insert(guiID, tmpTable)
  630. return #guiID
  631. end
  632.  
  633. -- radio button
  634. function gui.newRadio(guiID, x, y, func)
  635. local tmpTable = {}
  636. tmpTable["type"] = "radio"
  637. tmpTable["status"] = false
  638. tmpTable["y"] = y + guiID.y
  639. tmpTable["visible"] = true
  640. tmpTable["enabled"] = true
  641. tmpTable["x"] = x
  642. tmpTable["func"] = func
  643. table.insert(guiID, tmpTable)
  644. return #guiID
  645. end
  646.  
  647. -- label
  648. function gui.newLabel(guiID, x, y, text, bg, fg, l)
  649. local tmpTable = {}
  650. tmpTable["type"] = "label"
  651. tmpTable["y"] = y + guiID.y
  652. tmpTable["text"] = text
  653. tmpTable["lenght"] = string.len(text)
  654. tmpTable["bg"] = bg or guiID.bg
  655. tmpTable["fg"] = fg or guiID.fg
  656. tmpTable["visible"] = true
  657. tmpTable["x"] = x
  658. tmpTable["l"] = l or string.len(text)
  659. table.insert(guiID, tmpTable)
  660. return #guiID
  661. end
  662.  
  663. -- time label
  664. function gui.newTimeLabel(guiID, x, y, bg, fg)
  665. local tmpTable = {}
  666. tmpTable["type"] = "timelabel"
  667. tmpTable["y"] = y + guiID.y
  668. tmpTable["bg"] = bg or guiID.bg
  669. tmpTable["fg"] = fg or guiID.fg
  670. tmpTable["visible"] = true
  671. tmpTable["x"] = x
  672. table.insert(guiID, tmpTable)
  673. return #guiID
  674. end
  675.  
  676. -- date label
  677. function gui.newDateLabel(guiID, x, y, bg, fg, frm)
  678. local tmpTable = {}
  679. tmpTable["type"] = "datelabel"
  680. tmpTable["y"] = y + guiID.y
  681. tmpTable["bg"] = bg or guiID.bg
  682. tmpTable["fg"] = fg or guiID.fg
  683. tmpTable["visible"] = true
  684. tmpTable["x"] = x
  685. tmpTable["frm"] = frm or false
  686. table.insert(guiID, tmpTable)
  687. return #guiID
  688. end
  689.  
  690. -- multi line label
  691. function gui.newMultiLineLabel(guiID, x, y, w, h, text, bg, fg)
  692. local tmpTable = {}
  693. tmpTable["type"] = "multiLineLabel"
  694. tmpTable["y"] = y + guiID.y
  695. tmpTable["text"] = text
  696. tmpTable["bg"] = bg or guiID.bg
  697. tmpTable["fg"] = fg or guiID.fg
  698. tmpTable["visible"] = true
  699. tmpTable["x"] = x + guiID.x
  700. tmpTable["w"] = w
  701. tmpTable["h"] = h
  702. table.insert(guiID, tmpTable)
  703. return #guiID
  704. end
  705.  
  706. -- button
  707. function gui.newButton(guiID, x, y, text, func)
  708. local tmpTable = {}
  709. tmpTable["type"] = "button"
  710. tmpTable["y"] = y + guiID.y
  711. tmpTable["text"] = "[" .. text .. "]"
  712. tmpTable["lenght"] = string.len(tmpTable.text)
  713. tmpTable["visible"] = true
  714. tmpTable["enabled"] = true
  715. tmpTable["active"] = false
  716. tmpTable["func"] = func
  717. tmpTable["x"] = x
  718. table.insert(guiID, tmpTable)
  719. return #guiID
  720. end
  721.  
  722. -- text input field
  723. function gui.newText(guiID, x, y, lenght, text, func, fieldLenght, hide)
  724. local tmpTable = {}
  725. tmpTable["type"] = "text"
  726. tmpTable["x"] = x
  727. tmpTable["y"] = y + guiID.y
  728. tmpTable["text"] = text
  729. tmpTable["lenght"] = lenght
  730. tmpTable["visible"] = true
  731. tmpTable["enabled"] = true
  732. tmpTable["func"] = func
  733. if fieldLenght then
  734. tmpTable["fieldLenght"] = fieldLenght
  735. else
  736. tmpTable["fieldLenght"] = lenght
  737. end
  738. table.insert(guiID, tmpTable)
  739. tmpTable["hide"] = hide or false
  740. return #guiID
  741. end
  742.  
  743. -- progressbar
  744. function gui.newProgress(guiID, x, y, lenght, maxValue, value, func, number)
  745. local tmpTable = {}
  746. tmpTable["type"] = "progress"
  747. tmpTable["x"] = x
  748. tmpTable["y"] = y + guiID.y
  749. tmpTable["lenght"] = lenght
  750. tmpTable["visible"] = true
  751. tmpTable["enabled"] = true
  752. tmpTable["max"] = maxValue
  753. tmpTable["value"] = value
  754. tmpTable["func"] = func
  755. tmpTable["finished"] = false
  756. tmpTable["displayNumber"] = number or false
  757. table.insert(guiID, tmpTable)
  758. return #guiID
  759. end
  760.  
  761. -- vertical progress
  762. function gui.newVProgress(guiID, x, y, lenght, width, maxValue, value, func, direction)
  763. local tmpTable = {}
  764. tmpTable["type"] = "vprogress"
  765. tmpTable["x"] = x
  766. tmpTable["y"] = y + guiID.y
  767. tmpTable["lenght"] = lenght
  768. tmpTable["width"] = width
  769. tmpTable["visible"] = true
  770. tmpTable["enabled"] = true
  771. tmpTable["max"] = maxValue
  772. tmpTable["value"] = value
  773. tmpTable["func"] = func
  774. tmpTable["direction"] = direction or 0
  775. tmpTable["finished"] = false
  776. table.insert(guiID, tmpTable)
  777. return #guiID
  778. end
  779.  
  780. -- vertical slider
  781. function gui.newVSlider(guiID, x, y, lenght, min, max, value, func)
  782. local tmpTable = {}
  783. tmpTable["type"] = "vslider"
  784. tmpTable["x"] = x
  785. tmpTable["y"] = y + guiID.y
  786. tmpTable["lenght"] = lenght
  787. tmpTable["visible"] = true
  788. tmpTable["enabled"] = true
  789. tmpTable["min"] = min
  790. tmpTable["max"] = max
  791. tmpTable["value"] = value
  792. tmpTable["func"] = func
  793. table.insert(guiID, tmpTable)
  794. return #guiID
  795. end
  796.  
  797. -- list
  798. function gui.newList(guiID, x, y, width, height, tab, func, text)
  799. local tmpTable = {}
  800. tmpTable["type"] = "list"
  801. tmpTable["x"] = x + guiID.x
  802. tmpTable["y"] = y + guiID.y
  803. tmpTable["width"] = width
  804. tmpTable["height"] = height
  805. tmpTable["visible"] = true
  806. tmpTable["enabled"] = true
  807. tmpTable["func"] = func
  808. tmpTable["selected"] = 1
  809. tmpTable["active"] = 1
  810. tmpTable["entries"] = tab
  811. tmpTable["text"] = text
  812. table.insert(guiID, tmpTable)
  813. return #guiID
  814. end
  815.  
  816. --frame
  817. function gui.newFrame(guiID, x, y, width, height, text)
  818. local tmpTable = {}
  819. tmpTable["type"] = "frame"
  820. tmpTable["x"] = x + guiID.x
  821. tmpTable["y"] = y + guiID.y
  822. tmpTable["width"] = width
  823. tmpTable["height"] = height
  824. tmpTable["visible"] = true
  825. tmpTable["enabled"] = true
  826. tmpTable["text"] = text
  827. table.insert(guiID, tmpTable)
  828. return #guiID
  829. end
  830.  
  831. -- hline
  832. function gui.newHLine(guiID, x, y, width)
  833. local tmpTable = {}
  834. tmpTable["type"] = "hline"
  835. tmpTable["x"] = x + guiID.x
  836. tmpTable["y"] = y + guiID.y
  837. tmpTable["width"] = width
  838. tmpTable["visible"] = true
  839. tmpTable["enabled"] = true
  840. table.insert(guiID, tmpTable)
  841. return #guiID
  842. end
  843.  
  844. -- chart
  845. function gui.newChart(guiID, x, y, minValue, maxValue, data, lenght, height, bg, fg)
  846. local tmpTable = {}
  847. tmpTable["type"] = "chart"
  848. tmpTable["y"] = y + guiID.y
  849. tmpTable["lenght"] = lenght
  850. tmpTable["height"] = height
  851. tmpTable["bg"] = bg or guiID.bg
  852. tmpTable["fg"] = fg or guiID.fg
  853. tmpTable["visible"] = true
  854. tmpTable["x"] = x + guiID.x
  855. tmpTable["data"] = data
  856. tmpTable["min"] = minValue
  857. tmpTable["max"] = maxValue
  858. table.insert(guiID, tmpTable)
  859. return #guiID
  860. end
  861.  
  862. function gui.getSelected(guiID, listID)
  863. return guiID[listID].selected, guiID[listID].entries[guiID[listID].selected]
  864. end
  865.  
  866. function gui.setSelected(guiID, listID, selection)
  867. if selection<= #guiID[listID].entries then
  868. guiID[listID].selected = selection
  869. _displayList(guiID, listID)
  870. end
  871. end
  872.  
  873. function gui.setMax(guiID, widgetID, maxValue)
  874. guiID[widgetID].max = maxValue
  875. _displayProgress(guiID, widgetID)
  876. end
  877.  
  878. function gui.setChartData(guiID, chartID, data)
  879. guiID[chartID].data = data
  880. _displayChart(guiID, chartID)
  881. end
  882.  
  883. function gui.setValue(guiID, widgetID, value)
  884. guiID[widgetID].value = value
  885. if guiID[widgetID].type == "progress" then
  886. _displayProgress(guiID, widgetID)
  887. end
  888. if guiID[widgetID].type == "vprogress" then
  889. _displayVProgress(guiID, widgetID)
  890. end
  891. if guiID[widgetID].type == "vslider" then
  892. _displayVslider(guiID, widgetID)
  893. end
  894. end
  895.  
  896. function gui.resetProgress(guiID, progressID)
  897. guiID[progressID].finished = false
  898. _displayProgress(guiID, progressID)
  899. end
  900.  
  901. -- sets the text of a widget
  902. function gui.setText(guiID, widgetID, text, refresh)
  903. guiID[widgetID].text = text
  904. if guiID[widgetID].type == "text" then
  905. if refresh == nil or refresh == true then
  906. _displayText(guiID, widgetID)
  907. end
  908. end
  909. if guiID[widgetID].type == "label" then
  910. if refresh == nil or refresh == true then
  911. _displayLabel(guiID, widgetID)
  912. end
  913. end
  914. if guiID[widgetID].type == "multiLineLabel" then
  915. if refresh == nil or refresh == true then
  916. _displayMultiLineLabel(guiID, widgetID)
  917. end
  918. end
  919. -- gui.displayGui(guiID)
  920. end
  921.  
  922. function gui.getText(guiID, widgetID)
  923. return guiID[widgetID].text
  924. end
  925.  
  926. function gui.getCheckboxStatus(guiID, widgetID)
  927. return guiID[widgetID].status
  928. end
  929.  
  930. function gui.setEnable(guiID, widgetID, state, refresh)
  931. guiID[widgetID].enabled = state
  932. if refresh == nil then
  933. gui.displayGui(guiID)
  934. end
  935. if refresh == true then
  936. gui.displayWidget(guiID, widgetID)
  937. end
  938. end
  939.  
  940. function gui.setPosition(guiID, widgetID, x, y, refresh)
  941. guiID[widgetID].x = x
  942. guiID[widgetID].y = y
  943. if refresh == nil then
  944. gui.displayGui(guiID)
  945. end
  946. if refresh == true then
  947. gui.displayWidget(guiID, widgetID)
  948. end
  949. end
  950.  
  951. function gui.setSize(guiID, widgetID, w, h, refresh)
  952. guiID[widgetID].width = w
  953. guiID[widgetID].height = h
  954. guiID[widgetID].w = w
  955. guiID[widgetID].h = h
  956. guiID[widgetID].lenght = w
  957. if refresh == nil then
  958. gui.displayGui(guiID)
  959. end
  960. if refresh == true then
  961. gui.displayWidget(guiID, widgetID)
  962. end
  963. end
  964.  
  965. function gui.setVisible(guiID, widgetID, state, refresh)
  966. if state == false then
  967. guiID[widgetID].visible = state
  968. guiID[widgetID].enabled = state
  969. elseif state == true then
  970. guiID[widgetID].visible = state
  971. end
  972. if refresh == nil then
  973. gui.displayGui(guiID)
  974. end
  975. if refresh == true then
  976. gui.displayWidget(guiID, widgetID)
  977. end
  978. end
  979.  
  980. function gui.setBackground(guiID, widgetID, color)
  981. guiID[widgetID].bg = color
  982. if guiID[widgetID].type == "label" then
  983. _displayLabel(guiID, widgetID)
  984. end
  985. end
  986. function gui.setForeground(guiID, widgetID, color)
  987. guiID[widgetID].fg = color
  988. if guiID[widgetID].type == "label" then
  989. _displayLabel(guiID, widgetID)
  990. end
  991. end
  992.  
  993. function gui.clearList(guiID, listID)
  994. guiID[listID].entries = {}
  995. end
  996.  
  997. function gui.insertList(guiID, listID, value)
  998. table.insert(guiID[listID].entries, value)
  999. _displayList(guiID, listID)
  1000. end
  1001.  
  1002. function gui.removeList(guiID, listID, entry)
  1003. table.remove(guiID[listID].entries, entry)
  1004. _displayList(guiID, listID)
  1005. end
  1006.  
  1007. function gui.renameList(guiID, listID, entry, newName)
  1008. guiID[listID].entries[entry] = newName
  1009. _displayList(guiID, listID)
  1010. end
  1011.  
  1012. function gui.getRadio(guiID)
  1013. for i = 1, #guiID do
  1014. if guiID[i].type == "radio" then
  1015. if guiID[i].status == true then
  1016. return i
  1017. end
  1018. end
  1019. end
  1020. return -1
  1021. end
  1022.  
  1023. function gui.setRadio(guiID, radioID)
  1024. for i = 1, #guiID do
  1025. if guiID[i].type == "radio" then
  1026. guiID[i].status = false
  1027. end
  1028. end
  1029. guiID[radioID].status = true
  1030. return -1
  1031. end
  1032.  
  1033. function gui.setCheckbox(guiID, checkboxID, status)
  1034. guiID[checkboxID].status = status
  1035. end
  1036.  
  1037. local function runInput(guiID, textID)
  1038. local inputText = guiID[textID].text
  1039. gpu.setBackground(colorInputBackground)
  1040. gpu.setForeground(colorInputForeground)
  1041.  
  1042. local x = 0
  1043. if guiID[textID].x == "center" then
  1044. x = guiID.x + math.floor((guiID.width / 2)) - math.floor((guiID[textID].fieldLenght) / 2)
  1045. else
  1046. x =guiID.x + guiID[textID].x
  1047. end
  1048.  
  1049. local loopRunning = true
  1050. while loopRunning == true do
  1051. gpu.fill(x, guiID[textID].y, guiID[textID].fieldLenght, 1, " ")
  1052. tmpStr = inputText
  1053. if guiID[textID].hide == true then
  1054. tmpStr = ""
  1055. for i = 1, string.len(inputText) do
  1056. tmpStr = tmpStr .."*"
  1057. end
  1058. end
  1059. if string.len(tmpStr) + 1 > guiID[textID].fieldLenght then
  1060. tmpStr = string.sub(tmpStr, string.len(tmpStr) - guiID[textID].fieldLenght + 2, string.len(tmpStr))
  1061. end
  1062. gpu.set(x, guiID[textID].y, tmpStr .. "_")
  1063. local e, _, character, code = event.pullMultiple(0.1, "key_down", "touch")
  1064. if e == "key_down" then
  1065. if character == 8 then -- backspace
  1066. inputText = string.sub(inputText, 1, string.len(inputText) - 1)
  1067. elseif character == 13 then -- return
  1068. guiID[textID].text = inputText
  1069. if guiID[textID].func then
  1070. guiID[textID].func(guiID, textID, inputText)
  1071. end
  1072. loopRunning = false
  1073. elseif character > 31 and character < 128 and string.len(inputText) < guiID[textID].lenght then
  1074. inputText = inputText .. string.char(character)
  1075. end
  1076. elseif e == "touch" then
  1077. if character < x or character > (x + guiID[textID].fieldLenght) or guiID[textID].y ~= code then
  1078. guiID[textID].text = inputText
  1079. _displayText(guiID, textID)
  1080. if guiID[textID].func then
  1081. guiID[textID].func(guiID, textID, inputText)
  1082. end
  1083. loopRunning = false
  1084. computer.pushSignal("touch", _, character, code)
  1085. end
  1086. end
  1087. end
  1088. end
  1089.  
  1090.  
  1091. function gui.runGui(guiID)
  1092. if displayed == false then
  1093. displayed = true
  1094. gui.displayGui(guiID)
  1095. end
  1096.  
  1097. -- events with out touch
  1098. for i = 1, #guiID do
  1099. if guiID[i].type == "timelabel" then
  1100. _displayTimeLabel(guiID, i)
  1101. elseif guiID[i].type == "datelabel" then
  1102. _displayDateLabel(guiID, i)
  1103. end
  1104. end
  1105.  
  1106. local ix = 0
  1107. local e, _, x, y, button = event.pull(0.1, "touch")
  1108. if e == nil then
  1109. return false
  1110. end
  1111. for i = 1, #guiID do
  1112. if guiID[i].type == "button" then
  1113. if guiID[i].x == "center" then
  1114. ix = guiID.x + math.floor((guiID.width / 2)) - math.floor((guiID[i].lenght / 2))
  1115. else
  1116. ix = guiID.x + guiID[i].x
  1117. end
  1118. if x >= ix and x < (ix + guiID[i].lenght) and guiID[i].y == y then
  1119. if guiID[i].func and guiID[i].enabled == true then
  1120. guiID[i].active = true
  1121. gui.displayGui(guiID)
  1122. os.sleep(0.05)
  1123. guiID[i].active = false
  1124. gui.displayGui(guiID)
  1125. guiID[i].func(guiID, i)
  1126. end
  1127. end
  1128. elseif guiID[i].type == "timelabel" then
  1129. _displayTimeLabel(guiID, i)
  1130. elseif guiID[i].type == "checkbox" then
  1131. ix = guiID.x + guiID[i].x + 1
  1132. if x == ix and guiID[i].y == y then
  1133. if guiID[i].enabled == true then
  1134. if guiID[i].status == true then
  1135. guiID[i].status = false
  1136. else
  1137. guiID[i].status = true
  1138. end
  1139. if guiID[i].func then
  1140. guiID[i].func(guiID, i, guiID[i].status)
  1141. end
  1142. _displayCheckbox(guiID, i)
  1143. end
  1144. end
  1145. elseif guiID[i].type == "radio" then
  1146. ix = guiID.x + guiID[i].x + 1
  1147. if x == ix and guiID[i].y == y then
  1148. if guiID[i].enabled == true then
  1149. for c = 1, #guiID do
  1150. if guiID[c].type == "radio" then
  1151. guiID[c].status = false
  1152. if guiID[i].func then
  1153. guiID[i].func(guiID, i, guiID[i].status)
  1154. end
  1155. _displayRadio(guiID, c)
  1156. end
  1157. end
  1158. guiID[i].status = true
  1159. if guiID[i].func then
  1160. guiID[i].func(guiID, i, guiID[i].status)
  1161. end
  1162. _displayRadio(guiID, i)
  1163. end
  1164. end
  1165. elseif guiID[i].type == "text" then
  1166. if guiID[i].x == "center" then
  1167. ix = guiID.x + math.floor((guiID.width / 2)) - math.floor((guiID[i].lenght / 2))
  1168. else
  1169. ix = guiID.x + guiID[i].x
  1170. end
  1171. if x >= ix and x < (ix + guiID[i].fieldLenght) and guiID[i].y == y then
  1172. if guiID[i].enabled == true then
  1173. runInput(guiID, i)
  1174. end
  1175. end
  1176. elseif guiID[i].type == "list" and guiID[i].enabled == true then
  1177. if x == guiID[i].x +1 and y == guiID[i].y + guiID[i].height - 1 then
  1178. guiID[i].active = guiID[i].active - guiID[i].height + 2
  1179. if guiID[i].active < 1 then
  1180. guiID[i].active = 1
  1181. end
  1182. gpu.setBackground(colorListActiveBackground)
  1183. gpu.setForeground(colorListActiveForeground)
  1184. gpu.set(guiID[i].x, guiID[i].y + guiID[i].height - 1, "[<]")
  1185. guiID[i].selected = guiID[i].active
  1186. -- _displayList(guiID, i)
  1187.  
  1188. if guiID[i].func then
  1189. gpu.setBackground(colorButtonClickedBackground)
  1190. gpu.setForeground(colorButtonClickedForeground)
  1191. gpu.set(guiID[i].x, guiID[i].y + guiID[i].height - 1, "[<]")
  1192. os.sleep(0.05)
  1193. gpu.setBackground(colorListBackground)
  1194. gpu.setForeground(colorListForeground)
  1195. gpu.set(guiID[i].x, guiID[i].y + guiID[i].height - 1, "[<]")
  1196. guiID[i].func(guiID, i, guiID[i].selected, guiID[i].entries[guiID[i].selected])
  1197. end
  1198. end
  1199. if x == guiID[i].x + guiID[i].width - 2 and y == guiID[i].y + guiID[i].height - 1 then
  1200. if guiID[i].active + guiID[i].height - 2 < #guiID[i].entries + 1 then
  1201. guiID[i].active = guiID[i].active + guiID[i].height - 2
  1202. guiID[i].selected = guiID[i].active
  1203. end
  1204. gpu.setBackground(colorListActiveBackground)
  1205. gpu.setForeground(colorListActiveForeground)
  1206. gpu.set(guiID[i].x + guiID[i].width - 3, guiID[i].y + guiID[i].height - 1, "[>]")
  1207. -- _displayList(guiID, i)
  1208.  
  1209. if guiID[i].func then
  1210. gpu.setBackground(colorButtonClickedBackground)
  1211. gpu.setForeground(colorButtonClickedForeground)
  1212. gpu.set(guiID[i].x + guiID[i].width - 3, guiID[i].y + guiID[i].height - 1, "[>]")
  1213. os.sleep(0.05)
  1214. gpu.setBackground(colorListBackground)
  1215. gpu.setForeground(colorListForeground)
  1216. gpu.set(guiID[i].x + guiID[i].width - 3, guiID[i].y + guiID[i].height - 1, "[>]")
  1217. guiID[i].func(guiID, i, guiID[i].selected, guiID[i].entries[guiID[i].selected])
  1218. end
  1219. end
  1220. if x > guiID[i].x - 1 and x < guiID[i].x + guiID[i].width and y > guiID[i].y and y < guiID[i].y + guiID[i].height - 1 then
  1221. if guiID[i].active + y - guiID[i].y - 1 <= #guiID[i].entries then
  1222. guiID[i].selected = guiID[i].active + y - guiID[i].y - 1
  1223. -- _displayList(guiID, i)
  1224.  
  1225. if guiID[i].func then
  1226. guiID[i].func(guiID, i, guiID[i].selected, guiID[i].entries[guiID[i].selected])
  1227. end
  1228. end
  1229. end
  1230. _displayList(guiID, i)
  1231. elseif guiID[i].type == "chart" and guiID[i].enabled == true then
  1232. _displayChart(guiID, i)
  1233. elseif guiID[i].type == "vslider" and guiID[i].enabled == true then
  1234. ix = guiID.x + guiID[i].x
  1235. if x == ix and y == guiID[i].y and guiID[i].value > guiID[i].min then
  1236. v = guiID[i].value - 1
  1237. gui.setValue(guiID, i, v)
  1238. elseif x == ix + guiID[i].lenght and y == guiID[i].y and guiID[i].value < guiID[i].max then
  1239. v = guiID[i].value + 1
  1240. gui.setValue(guiID, i, v)
  1241. end
  1242. if guiID[i].func then
  1243. guiID[i].func(guiID, i, guiID[i].value)
  1244. end
  1245. _displayVslider(guiID, i)
  1246. end
  1247. end
  1248.  
  1249. -- gui.displayGui(guiID)
  1250. end
  1251.  
  1252. errorGui = gui.newGui("center", "center", 40, 10, true, "ERROR", 0xFF0000, 0xFFFF00)
  1253. errorMsgLabel1 = gui.newLabel(errorGui, "center", 3, "")
  1254. errorMsgLabel2 = gui.newLabel(errorGui, "center", 4, "")
  1255. errorMsgLabel3 = gui.newLabel(errorGui, "center", 5, "")
  1256. errorButton = gui.newButton(errorGui, "center", 8, "exit", gui.exit)
  1257.  
  1258. function gui.showError(msg1, msg2, msg3)
  1259. gui.displayGui(errorGui)
  1260. gui.setText(errorGui, errorMsgLabel1, msg1 or "")
  1261. gui.setText(errorGui, errorMsgLabel2, msg2 or "")
  1262. gui.setText(errorGui, errorMsgLabel3, msg3 or "")
  1263. while true do
  1264. gui.runGui(errorGui)
  1265. end
  1266. gui.closeGui(errorGui)
  1267. end
  1268.  
  1269.  
  1270. function gui.checkHardware(comp, msg)
  1271. if component.isAvailable(comp) == false then
  1272. compGui = gui.newGui("center", "center", 40, 8, true, nil, 0xFF0000, 0xFFFF00)
  1273. gui.displayGui(compGui)
  1274. gui.newLabel(compGui, "center", 1, "!Component missing!")
  1275. gui.newLabel(compGui, "center", 3, msg)
  1276. gui.newHLine(compGui, 1, 5, 38)
  1277. gui.newButton(compGui, "center", 6, "exit", gui.exit)
  1278. while true do
  1279. gui.runGui(compGui)
  1280. end
  1281. end
  1282. end
  1283.  
  1284.  
  1285. local msgRunning = true
  1286. function msgCallback()
  1287. msgRunning = false
  1288. end
  1289.  
  1290. msgGui = gui.newGui("center", "center", 40, 10, true, "Info")
  1291. msgLabel1 = gui.newLabel(msgGui, "center", 3, "")
  1292. msgLabel2 = gui.newLabel(msgGui, "center", 4, "")
  1293. msgLabel3 = gui.newLabel(msgGui, "center", 5, "")
  1294. msgButton = gui.newButton(msgGui, "center", 8, "ok", msgCallback)
  1295.  
  1296. function gui.showMsg(msg1, msg2, msg3)
  1297. gui.displayGui(msgGui)
  1298. gui.setText(msgGui, msgLabel1, msg1 or "")
  1299. gui.setText(msgGui, msgLabel2, msg2 or "")
  1300. gui.setText(msgGui, msgLabel3, msg3 or "")
  1301. msgRunning = true
  1302. while msgRunning == true do
  1303. gui.runGui(msgGui)
  1304. end
  1305. gui.closeGui(msgGui)
  1306. end
  1307.  
  1308.  
  1309. local yesNoRunning = true
  1310. local yesNoValue = false
  1311.  
  1312. local function yesNoCallbackYes()
  1313. yesNoRunning = false
  1314. yesNoValue = true
  1315. end
  1316. local function yesNoCallbackNo()
  1317. yesNoRunning = false
  1318. yesNoValue = false
  1319. end
  1320.  
  1321. yesNoGui = gui.newGui("center", "center", 40, 10, true, "Question")
  1322. yesNoMsgLabel1 = gui.newLabel(yesNoGui, "center", 3, "")
  1323. yesNoMsgLabel2 = gui.newLabel(yesNoGui, "center", 4, "")
  1324. yesNoMsgLabel3 = gui.newLabel(yesNoGui, "center", 5, "")
  1325. yesNoYesButton = gui.newButton(yesNoGui, 3, 8, "yes", yesNoCallbackYes)
  1326. yesNoNoButton = gui.newButton(yesNoGui, 33, 8, "no", yesNoCallbackNo)
  1327.  
  1328.  
  1329. function gui.getYesNo(msg1, msg2, msg3)
  1330. yesNoRunning = true
  1331. gui.displayGui(yesNoGui)
  1332. gui.setText(yesNoGui, yesNoMsgLabel1, msg1 or "")
  1333. gui.setText(yesNoGui, yesNoMsgLabel2, msg2 or "")
  1334. gui.setText(yesNoGui, yesNoMsgLabel3, msg3 or "")
  1335. while yesNoRunning == true do
  1336. gui.runGui(yesNoGui)
  1337. end
  1338. gui.closeGui(yesNoGui)
  1339. return yesNoValue
  1340. end
  1341.  
  1342.  
  1343.  
  1344.  
  1345. -- File handling
  1346.  
  1347. function gui.splitString(str, sep)
  1348. local sep, fields = sep or ":", {}
  1349. local pattern = string.format("([^%s]+)", sep)
  1350. str:gsub(pattern, function(c) fields[#fields+1] = c end)
  1351. return fields
  1352. end
  1353.  
  1354.  
  1355.  
  1356.  
  1357. local function convert( chars, dist, inv )
  1358. return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  1359. end
  1360.  
  1361. function gui.string2key(str)
  1362. tmpTable = {}
  1363. for i = 1, string.len(str) do
  1364. tmpTable[i] = string.byte(str,i)
  1365. end
  1366. while #tmpTable < 5 do
  1367. table.insert(tmpTable,100)
  1368. end
  1369. return tmpTable
  1370. end
  1371.  
  1372.  
  1373. function gui.crypt(str, k, inv)
  1374. if not k then
  1375. k = {1,2,3,4,5}
  1376. end
  1377. while #k < 5 do
  1378. table.insert(k,100)
  1379. end
  1380. local enc= "";
  1381. for i=1,#str do
  1382. if(#str-k[#k] >= i or not inv)then
  1383. for inc=0,3 do
  1384. if(i%4 == inc)then
  1385. enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  1386. break;
  1387. end
  1388. end
  1389. end
  1390. end
  1391. if(not inv)then
  1392. for i=1,k[#k] do
  1393. enc = enc .. string.char(math.random(32,126));
  1394. end
  1395. end
  1396. return enc;
  1397. end
  1398.  
  1399. --// exportstring( string )
  1400. --// returns a "Lua" portable version of the string
  1401. function exportstring( s )
  1402. s = string.format( "%q",s )
  1403. -- to replace
  1404. s = string.gsub( s,"\\\n","\\n" )
  1405. s = string.gsub( s,"\r","\\r" )
  1406. s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  1407. return s
  1408. end
  1409. --// The Save Function
  1410. function gui.saveTable(tbl,filename )
  1411. local charS,charE = " ","\n"
  1412. local file,err
  1413. -- create a pseudo file that writes to a string and return the string
  1414. if not filename then
  1415. file = { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
  1416. charS,charE = "",""
  1417. -- write table to tmpfile
  1418. elseif filename == true or filename == 1 then
  1419. charS,charE,file = "","",io.tmpfile()
  1420. -- write table to file
  1421. -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
  1422. else
  1423. file,err = io.open( filename, "w" )
  1424. if err then
  1425. print ("Gui-lib: Error saving table " .. filename .." -> " .. err)
  1426. return _,err
  1427. end
  1428. end
  1429. -- initiate variables for save procedure
  1430. local tables,lookup = { tbl },{ [tbl] = 1 }
  1431. file:write( "return {"..charE )
  1432. for idx,t in ipairs( tables ) do
  1433. if filename and filename ~= true and filename ~= 1 then
  1434. file:write( "-- Table: {"..idx.."}"..charE )
  1435. end
  1436. file:write( "{"..charE )
  1437. local thandled = {}
  1438. for i,v in ipairs( t ) do
  1439. thandled[i] = true
  1440. -- escape functions and userdata
  1441. if type( v ) ~= "userdata" then
  1442. -- only handle value
  1443. if type( v ) == "table" then
  1444. if not lookup[v] then
  1445. table.insert( tables, v )
  1446. lookup[v] = #tables
  1447. end
  1448. file:write( charS.."{"..lookup[v].."},"..charE )
  1449. elseif type( v ) == "function" then
  1450. file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
  1451. else
  1452. local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  1453. file:write( charS..value..","..charE )
  1454. end
  1455. end
  1456. end
  1457. for i,v in pairs( t ) do
  1458. -- escape functions and userdata
  1459. if (not thandled[i]) and type( v ) ~= "userdata" then
  1460. -- handle index
  1461. if type( i ) == "table" then
  1462. if not lookup[i] then
  1463. table.insert( tables,i )
  1464. lookup[i] = #tables
  1465. end
  1466. file:write( charS.."[{"..lookup[i].."}]=" )
  1467. else
  1468. local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
  1469. file:write( charS..index.."=" )
  1470. end
  1471. -- handle value
  1472. if type( v ) == "table" then
  1473. if not lookup[v] then
  1474. table.insert( tables,v )
  1475. lookup[v] = #tables
  1476. end
  1477. file:write( "{"..lookup[v].."},"..charE )
  1478. elseif type( v ) == "function" then
  1479. file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
  1480. else
  1481. local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  1482. file:write( value..","..charE )
  1483. end
  1484. end
  1485. end
  1486. file:write( "},"..charE )
  1487. end
  1488. file:write( "}" )
  1489. -- Return Values
  1490. -- return stringtable from string
  1491. if not filename then
  1492. -- set marker for stringtable
  1493. return file.str.."--|"
  1494. -- return stringttable from file
  1495. elseif filename == true or filename == 1 then
  1496. file:seek ( "set" )
  1497. -- no need to close file, it gets closed and removed automatically
  1498. -- set marker for stringtable
  1499. return file:read( "*a" ).."--|"
  1500. -- close file and return 1
  1501. else
  1502. file:close()
  1503. return 1
  1504. end
  1505. end
  1506.  
  1507. --// The Load Function
  1508. function gui.loadTable( sfile )
  1509. local tables, err, _
  1510.  
  1511. -- catch marker for stringtable
  1512. if string.sub( sfile,-3,-1 ) == "--|" then
  1513. tables,err = loadstring( sfile )
  1514. else
  1515. tables,err = loadfile( sfile )
  1516. end
  1517. if err then
  1518. print("Gui-lib: Error loading table " ..sfile .. " -> " ..err)
  1519. return _,err
  1520. end
  1521. tables = tables()
  1522. for idx = 1,#tables do
  1523. local tolinkv,tolinki = {},{}
  1524. for i,v in pairs( tables[idx] ) do
  1525. if type( v ) == "table" and tables[v[1]] then
  1526. table.insert( tolinkv,{ i,tables[v[1]] } )
  1527. end
  1528. if type( i ) == "table" and tables[i[1]] then
  1529. table.insert( tolinki,{ i,tables[i[1]] } )
  1530. end
  1531. end
  1532. -- link values, first due to possible changes of indices
  1533. for _,v in ipairs( tolinkv ) do
  1534. tables[idx][v[1]] = v[2]
  1535. end
  1536. -- link indices
  1537. for _,v in ipairs( tolinki ) do
  1538. tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
  1539. end
  1540. end
  1541. return tables[1]
  1542. end
  1543.  
  1544. function gui.sepString(str)
  1545. tmpTable = {}
  1546. for i = 1, string.len(str) do
  1547. tmpTable[i] = string.char(string.byte(str,i))
  1548. end
  1549. return tmpTable
  1550. end
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557. return gui
Advertisement
Add Comment
Please, Sign In to add comment