joserobjr

gui.lua

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