bobmarley12345

ComputerCraftSimpleGUIFunctions

Oct 7th, 2020 (edited)
875
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- by ther/carrot xd
  2. -- Monitor is required, but you could set it to term
  3. -- if you want it to draw stuff to the terminal screen
  4. -- so... Monitor = term
  5. MonitorSide = "left"
  6. Monitor = peripheral.wrap(MonitorSide)
  7.  
  8. function ClearMonitor()
  9.     Monitor.setTextColor(colours.black)
  10.     Monitor.setBackgroundColor(colours.black)
  11.     Monitor.clear()
  12.     Monitor.setCursorPos(1,1)
  13. end
  14.  
  15. function DrawText(xPos, yPos, text, textColour, backgroundColour)
  16.     Monitor.setBackgroundColor(backgroundColour)
  17.     Monitor.setTextColor(textColour)
  18.     Monitor.setCursorPos(xPos,yPos)
  19.     Monitor.write(text)
  20. end
  21.  
  22. function DrawCenteredHorizontal(xPos, yPos, width, text, textColour, backgroundColour)
  23.     -- width could be the size of your monitor, or anything else. this means it
  24.     -- doesn't have to stretch your entire monitor's if you want. imagine
  25.     -- it as a rectangle and the text is in the center.
  26.     local textLength = string.len(text)
  27.     local centerX = (width / 2) - (textLength / 2)
  28.     DrawText(xPos + centerX, yPos, text, textColour, backgroundColour)
  29. end
  30.  
  31. function DrawLineH(xPos, yPos, lineLength, colour)
  32.     Monitor.setBackgroundColor(colour)
  33.     Monitor.setTextColor(colour)
  34.     Monitor.setCursorPos(xPos,yPos)
  35.     Monitor.write(string.rep(" ", lineLength))
  36. end
  37.  
  38. function DrawLineV(xPos, yPos, width, lineHeight, colour)
  39.     Monitor.setBackgroundColor(colour)
  40.     for i = 1, lineHeight, 1 do
  41.         Monitor.setCursorPos(xPos, yPos + (i - 1))
  42.         Monitor.write(string.rep(" ", width))
  43.     end
  44. end
  45.  
  46. function DrawSquare(xPos, yPos, nWidth, nHeight, backgroundColour)
  47.     Monitor.setBackgroundColor(backgroundColour)
  48.     for i = 1, math.ceil(nHeight), 1 do
  49.         Monitor.setCursorPos(xPos, yPos + (i - 1))
  50.         Monitor.write(string.rep(" ", nWidth))
  51.     end
  52. end
  53.  
  54. function ProgressBar(xPos, yPos, width, value, maxValue, backgroundColour, progressColour)
  55.     DrawLineH(xPos, yPos, width, backgroundColour) --backgoround bar
  56.     local barSize = math.floor((value/maxValue) * width)
  57.     DrawLineH(xPos, yPos, barSize, progressColour) --progress so far
  58. end
  59.  
  60. function GroupBoxHeader(xPos, yPos, width, header, textColour, backgroundColour)
  61.     local headerLength = string.len(header)
  62.     local repeatChars = width - 4 - headerLength
  63.  
  64.     if (repeatChars < 0) then
  65.         repeatChars = 0
  66.     end
  67.  
  68.     local head = "--- " .. header .. " " .. string.rep("-", repeatChars)
  69.     DrawText(xPos, yPos, head, textColour, backgroundColour)
  70. end
  71.  
  72. function GroupBoxFooter(xPos, yPos, width, textColour, backgroundColour)
  73.     if (width < 0) then
  74.         width = 0
  75.     end
  76.     local head = string.rep("-", width)
  77.     DrawText(xPos, yPos, head, textColour, backgroundColour)
  78. end
  79.  
  80. function GroupBox(xPos, yPos, width, height, header, textColour, headerBackgroundColour)
  81.     GroupBoxHeader(xPos, yPos, width, header, textColour, headerBackgroundColour)
  82.     GroupBoxFooter(xPos, yPos + height + 1, width, textColour, headerBackgroundColour)
  83. end
RAW Paste Data