Advertisement
dorky106

OpenComputersUI

Mar 6th, 2020
427
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local gpu = component.gpu
  3. local event = require("event")
  4. local term = require("term")
  5.  
  6. local screenWidth, screenHeight = gpu.getResolution()
  7.  
  8. local gui = {}
  9. local white = 0xFFFFFF
  10. local black = 0x000000
  11. local red = 0xFF0000
  12. local green = 0x00FF00
  13. local blue = 0x0000FF
  14.  
  15. function gui.NewProgressbar(layout, bgcolour, fgcolour, x, y, width, height, value, maxvalue, direction)
  16.     local tmpTable = {}
  17.     tmpTable["Type"] = "Progressbar"
  18.     tmpTable["BGColour"] = bgcolour
  19.     tmpTable["FGColour"] = fgcolour
  20.     tmpTable["X"] = x
  21.     tmpTable["Y"] = y
  22.     tmpTable["Width"] = width
  23.     tmpTable["Height"] = height
  24.     tmpTable["Value"] = value
  25.     tmpTable["MaxValue"] = maxvalue
  26.     tmpTable["Direction"] = direction or 0
  27.     table.insert(layout, tmpTable)
  28.     return #layout
  29. end
  30.  
  31. function gui.NewLabel(layout, bgcolour, fgcolour, x, y, txt, length)
  32.     local tmpTable = {}
  33.     tmpTable["Type"] = "Label"
  34.     tmpTable["BGColour"] = bgcolour
  35.     tmpTable["FGColour"] = fgcolour
  36.     tmpTable["X"] = x
  37.     tmpTable["Y"] = y
  38.     tmpTable["Text"] = txt
  39.     tmpTable["Length"] = length
  40.     table.insert(layout, tmpTable)
  41.     return #layout
  42. end
  43.  
  44. function gui.SetValue(layout, widgetID, newvalue)
  45.     layout[widgetID].Value = newvalue
  46. end
  47.  
  48. function gui.SetText(layout, widgetID, newtxt)
  49.     layout[widgetID].Text = newtxt
  50. end
  51.  
  52. function gui.NewUI(x, y, w, h, bg, fg)
  53.     local tmpTable = {}
  54.     tmpTable["Type"] = "UI"
  55.     tmpTable["X"] = x
  56.     tmpTable["Y"] = y
  57.     tmpTable["Width"] = w
  58.     tmpTable["Height"] = h
  59.     tmpTable["Background"] = bg
  60.     tmpTable["Foreground"] = fg
  61.     return tmpTable
  62. end
  63.  
  64. function gui.ClearScreen()
  65.     gpu.setBackground(black)
  66.     gpu.setForeground(white)
  67.     gpu.fill(1, 1, screenWidth, screenHeight, " ")
  68.     term.clear()
  69. end
  70.  
  71. local function _displayProgressbar(progressbar)
  72.     gpu.setBackground(progressbar.BGColour)
  73.     gpu.setForeground(progressbar.FGColour)
  74.     gpu.fill(progressbar.X, progressbar.Y, progressbar.Width, progressbar.Height, " ")
  75.    
  76.     term.setCursor(1, 1)
  77.    
  78.     if progressbar.Direction == 0 then
  79.         local percent = math.floor(progressbar.Value / progressbar.MaxValue * progressbar.Height)
  80.         gpu.setBackground(progressbar.FGColour)
  81.         gpu.setForeground(progressbar.BGColour)
  82.         gpu.fill(progressbar.X, progressbar.Y+progressbar.Height-percent, progressbar.Width, percent, " ")
  83.     end
  84. end
  85.  
  86. local function _displayLabel(label)
  87.     gpu.setBackground(label.BGColour)
  88.     gpu.setForeground(label.FGColour)
  89.     gpu.fill(label.X, label.Y, label.Length, 1, " ")
  90.     gpu.set(label.X, label.Y, label.Text)
  91. end
  92.  
  93. function gui.RunUI(layout)
  94.     if layout.Type == "UI" then
  95.         for k, v in ipairs(layout) do
  96.             if v.Type == "Progressbar" then
  97.                 _displayProgressbar(v)
  98.             elseif v.Type == "Label" then
  99.                 _displayLabel(v)
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. return gui
Advertisement
RAW Paste Data Copied
Advertisement