Advertisement
Wyvern67

GUI Api

Jul 11th, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.06 KB | None | 0 0
  1. wyvernsPeripherals = {}
  2. wyvernsCtrl = {}
  3. wyvernsState = {}
  4. checkboxScale = 5
  5.  
  6. function pulse(side, delay)
  7.     if delay == nil then delay = 1 end
  8.     redstone.setOutput(side, true)
  9.     sleep(delay)
  10.     redstone.setOutput(side, false)
  11. end
  12.  
  13. function wget(url, dir)
  14.     local page=http.get(url)
  15.     if not page then return false end
  16.     local cFile = page.readAll()
  17.     page.close()
  18.  
  19.     local fHandle = fs.open(dir, "w")
  20.     if fHandle == nil then return false end
  21.     local ret = fHandle.write(cFile)
  22.     if ret == nil then return false end
  23.     ret = fHandle.close()
  24. end
  25.  
  26. function mprint(data)
  27.     local m=loadPeripheral("monitor")
  28.     local x,y = m.getCursorPos()
  29.     m.write(data)
  30.     local sizeX, sizeY = m.getSize()
  31.     if y<sizeY then m.setCursorPos(1, y+1) end
  32.     if y>=sizeY then m.scroll(1) m.setCursorPos(1, sizeY) end
  33. end
  34.  
  35. function printFunctions(side)
  36.     local methods = peripheral.getMethods(side)
  37.     for i,v in pairs(methods) do
  38.         print(peripheral.getType(side).."."..v.."()")
  39.     end
  40. end
  41.  
  42. -- ##Buttons part##
  43.  
  44. --  #Local functions#
  45. --  Registering
  46. local function registerCtrlButton(id, text, left, top, width, height, edges)
  47.     wyvernsCtrl[id]={[1]="Button",[2]=text,[3]=left,[4]=top,[5]=width,[6]=height,[7]=edges}
  48. end
  49. local function registerCtrlToggle(id, text, left, top, width, height, edges, state)
  50.     wyvernsCtrl[id]={[1]="Toggle",[2]=text,[3]=left,[4]=top,[5]=width,[6]=height,[7]=edges,[8]=state}
  51. end
  52. local function registerCtrlCheckbox(id, left, top)
  53.     wyvernsCtrl[id]={[1]="Checkbox",[3]=left, [4]=top, [5]=checkboxScale, [6]=checkboxScale}
  54. end
  55. local function registerCtrlLabel(id, text, left, top, width, height, textColor, bgColor)
  56.     wyvernsCtrl[id]={[1]="Label",[2]=text,[3]=left,[4]=top,[5]=width,[6]=height,[7]=textColor,[8]=bgColor}
  57. end
  58.  
  59. local function loadCtrl(id, shift)
  60.     if shift == nil then shift = 1 end -- The function won't return any parameter below the shift, this allow me to add global parameters whenever I want
  61.     if type(wyvernsCtrl[id]) ~= "table" then print("Error: invalid id ("..id..").") end
  62.     return wyvernsCtrl[id][1+shift], wyvernsCtrl[id][2+shift], wyvernsCtrl[id][3+shift], wyvernsCtrl[id][4+shift], wyvernsCtrl[id][5+shift], wyvernsCtrl[id][6+shift], wyvernsCtrl[id][7+shift], wyvernsCtrl[id][8+shift], wyvernsCtrl[id][9+shift]
  63. end
  64.  
  65. local function getBackgroundColor()
  66.     return backgroundColor
  67. end
  68.  
  69. --   Drawing
  70. local function drawEdges(left, top, width, height)
  71.     for i=0, height do -- Draw the left edge
  72.         drawPixel(left, top+i, "white")
  73.     end
  74.     for i=0, height do -- Draw the right edge
  75.         drawPixel(left+width, top+i, "gray")
  76.     end
  77.     for i=0, width-1 do -- Draw the top edge
  78.         drawPixel(left+i, top, "white")
  79.     end
  80.     for i=0, width do -- Draw the bottom edge
  81.         drawPixel(left+i, top+height, "gray")
  82.     end
  83. end
  84.  
  85. local function drawInvertedEdges(left, top, width, height)
  86.     for i=0, height do -- Draw the left edge
  87.         drawPixel(left, top+i, "gray")
  88.     end
  89.     for i=0, height do -- Draw the right edge
  90.         drawPixel(left+width, top+i, "white")
  91.     end
  92.     for i=0, width-1 do -- Draw the top edge
  93.         drawPixel(left+i, top, "gray")
  94.     end
  95.     for i=0, width do -- Draw the bottom edge
  96.         drawPixel(left+i, top+height, "white")
  97.     end
  98. end
  99.  
  100. -- ##Public functions##
  101. function registerPeripheral(side)
  102.     wyvernsPeripherals[peripheral.getType(side)] = peripheral.wrap(side)
  103. end
  104.  
  105. function registerDistantPeripheral(name)
  106.     wyvernsPeripherals[peripheral.getType(name)] = peripheral.wrap(name)
  107. end
  108.  
  109. function loadPeripheral(peripheralType)
  110.     return wyvernsPeripherals[peripheralType]
  111. end
  112.  
  113. function setBackgroundColor(color)
  114.     local m=loadPeripheral("monitor")
  115.     m.setBackgroundColor(colors[color])
  116.     local x,y=m.getSize()
  117.     for i=1,y do
  118.         for v=1,x do
  119.             m.setCursorPos(v,i)
  120.             m.write(" ")
  121.         end
  122.     end
  123.     backgroundColor = color
  124. end
  125.  
  126. function changeResolution(newResolution)
  127.     local m=loadPeripheral("monitor")
  128.     if newResolution > 5 then
  129.         newResolution = 5
  130.         print("Resolution can't be greater than 5. Automatically set to max.")
  131.     elseif newResolution < 0.5 then
  132.         newResolution = 0.5
  133.         print("Resolution can't be lower than 5. Automatically set to min.")
  134.     end
  135.     m.setTextScale(newResolution)
  136. end
  137.  
  138. function drawPixel(x,y,color)
  139.     local m=loadPeripheral("monitor")
  140.     m.setCursorPos(x,y)
  141.     m.setBackgroundColor(colors[color])
  142.     m.write(" ")
  143.     m.setBackgroundColor(colors.white)
  144. end
  145.  
  146. function guiCtrlGetType(id)
  147.     local cType = loadCtrl(id, 0)
  148.     return cType
  149. end
  150.  
  151. function guiCtrlGetState(id)
  152.     local cType = loadCtrl(id, 0)
  153.     if cType == "Toggle" or cType == "Checkbox" then
  154.         return wyvernsState[id]
  155.     end
  156. end
  157.  
  158. function guiCtrlSetState(id, state)
  159.     local m=loadPeripheral("monitor")
  160.     local left, top, width, height = loadCtrl(id, 2)
  161.    
  162.     if state == true then
  163.         color = "lime"
  164.         wyvernsState[id] = true
  165.     else
  166.         color = "red"
  167.         wyvernsState[id] = false
  168.     end
  169.    
  170.     if guiCtrlGetType(id) == "Toggle" then
  171.         for i=1, width-3 do
  172.             drawPixel(left+1+i, top+1+height/2, color)
  173.         end
  174.     elseif guiCtrlGetType(id) == "Checkbox" then
  175.         for v=1, height-1 do
  176.             for i=1, width-1 do
  177.                 drawPixel(left+i, top+v, color)
  178.             end
  179.         end
  180.         -- if state == true then drawInvertedEdges(left, top, width, height) end
  181.         -- if state == false then drawEdges(left, top, width, height) end
  182.         drawEdges(left, top, width, height)
  183.     end
  184. end
  185.  
  186. function guiCreateButton(text, left, top, width, height, color, edges)
  187.     if edges == nil then edges = true end
  188.     if color == nil then color = "lightGray" end
  189.     local m=loadPeripheral("monitor")
  190.     for v=0, height do
  191.         for i=0, width do
  192.             drawPixel(left+i, top+v, color)
  193.         end
  194.     end
  195.     if edges == true then drawEdges(left, top, width, height) end
  196.     m.setCursorPos(left+1-(string.len(text)/2)+width/2, top+height/2)
  197.     m.setTextColor(colors.black)
  198.     m.setBackgroundColor(colors[color])
  199.     m.write(text)
  200.     registerCtrlButton(#wyvernsCtrl+1, text, left, top, width, height)
  201.     return #wyvernsCtrl -- Return the ID of the button
  202. end
  203.  
  204. function guiCreateToggle(text, left, top, width, height, color, edges, state)
  205.     if edges == nil then edges = true end
  206.     if color == nil then color = "lightGray" end
  207.     if state == nil then state = false end
  208.     local m=loadPeripheral("monitor")
  209.     for v=0, height do
  210.         for i=0, width do
  211.             drawPixel(left+i, top+v, color)
  212.         end
  213.     end
  214.     if edges == true then drawEdges(left, top, width, height) end
  215.     m.setCursorPos(left+1-(string.len(text)/2)+width/2, top+height/2)
  216.     m.setTextColor(colors.black)
  217.     m.setBackgroundColor(colors[color])
  218.     m.write(text)
  219.     registerCtrlToggle(#wyvernsCtrl+1, text, left, top, width, height, state)
  220.     guiCtrlSetState(#wyvernsCtrl, state) -- As I just added it to the registry there's no +1 anymore
  221.     return #wyvernsCtrl -- Return the ID of the button
  222. end
  223.  
  224. function guiCreateCheckbox(left, top, state)
  225.     if state == nil then state = false end
  226.     if state == true then color = "lime" end
  227.     if state == false then color = "red" end
  228.     local m=loadPeripheral("monitor")
  229.     --[[
  230.     for v=0, checkboxScale do
  231.         for i=0, checkboxScale do
  232.             drawPixel(left+i, top+v, color)
  233.         end
  234.     end
  235.     ]]
  236.     registerCtrlCheckbox(#wyvernsCtrl+1, left, top)
  237.     guiCtrlSetState(#wyvernsCtrl, state)
  238.     return #wyvernsCtrl
  239. end
  240.  
  241. function guiCreateLabel(text, left, top, width, height, textColor, bgColor)
  242.     if width == nil then width = string.len(text) end
  243.     if height == nil then height = 1 end
  244.     if textColor == nil then textColor = "black" end
  245.     if bgColor == nil then bgColor = getBackgroundColor() end
  246.     local m=loadPeripheral("monitor")
  247.    
  248.     m.setCursorPos(left, top)
  249.     m.setTextColor(colors[textColor])
  250.     m.setBackgroundColor(colors[bgColor])
  251.     m.write(text)
  252.    
  253.     registerCtrlLabel(#wyvernsCtrl+1, text, left, top, width, height, textColor, bgColor)
  254.     return #wyvernsCtrl
  255. end
  256.  
  257. function guiCreateProgressBar(curVal, maxVal, left, top, width, height, fgColor, bgColor)
  258.     if fgColor == nil then fgColor = "lime" end
  259.     if bgColor == nil then bgColor = "green" end
  260.     local m=loadPeripheral("monitor")
  261.     local progress = curVal/maxVal*width
  262.  
  263.     for v=0, height-1 do
  264.         for h=0, width-1 do
  265.             if h <= progress then
  266.                 color=fgColor
  267.             else
  268.                 color=bgColor
  269.             end
  270.             drawPixel(left+h,top+v,color)
  271.         end
  272.     end
  273. end
  274.  
  275. function guiCreateAdvProgressBar(curVal, maxVal, lowVal, left, top, width, height, fgColor, bgColor, lowColor)
  276.     if fgColor == nil then fgColor = "lime" end
  277.     if bgColor == nil then bgColor = "green" end
  278.     if lowColor == nil then lowColor = "red" end
  279.     if curVal <= lowVal then fgColor = lowColor end
  280.     local m = loadPeripheral("monitor")
  281.     local progress = curVal/maxVal*width
  282.  
  283.     for v=0,height-1 do
  284.         for h=0,width-1 do
  285.             if h <= progress then
  286.                 color=fgColor
  287.             else
  288.                 color=bgColor
  289.             end
  290.             drawPixel(left+h,top+v,color)
  291.         end
  292.     end
  293. end
  294.  
  295. function guiGetMsg()
  296.     local _,side,x,y = os.pullEvent("monitor_touch")
  297.     for i=1, #wyvernsCtrl do
  298.         local name, left, top, width, height = loadCtrl(i)
  299.         if x <= left+width and x >= left then
  300.             if y <= top+height and y >= top then
  301.                 -- He clicked on a button
  302.                 if guiCtrlGetType(i) == "Toggle" or guiCtrlGetType(i) == "Checkbox" then
  303.                     if guiCtrlGetState(i) == false then
  304.                         guiCtrlSetState(i, true)
  305.                     else
  306.                         guiCtrlSetState(i, false)
  307.                     end
  308.                 end
  309.                 return i, name, 0
  310.             end
  311.         end
  312.     end
  313.     return -1, x, y
  314. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement