bobmarley12345

gui-lib-carrot

Mar 2nd, 2022 (edited)
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. local MONITOR = nil
  2.  
  3. function setMonitor(mon)
  4.     MONITOR = mon
  5. end
  6.  
  7. function getMonitor()
  8.     return MONITOR
  9. end
  10.  
  11. function string.jsub(str, startIndex, endIndex)
  12.     if (endIndex == nil) then
  13.         endIndex = string.len(str) + 1
  14.     end
  15.    
  16.     return string.sub(str, startIndex, endIndex - 1)
  17. end
  18.  
  19. function drawText(x, y, text, foreground, background)
  20.     MONITOR.setCursorPos(x, y)
  21.     if (foreground ~= nil) then
  22.         MONITOR.setTextColor(foreground)
  23.     end
  24.     if (background ~= nil) then
  25.         MONITOR.setBackgroundColor(background)
  26.     end
  27.  
  28.     MONITOR.write(text)
  29. end
  30.  
  31. function drawBox(x, y, w, h, background, character)
  32.     if (background ~= nil) then
  33.         MONITOR.setBackgroundColor(background)
  34.         MONITOR.setTextColor(background)
  35.     end
  36.  
  37.     if (character == nil) then
  38.         character = " "
  39.     end
  40.  
  41.     local y2 = y + h - 1
  42.     for i = y, y2 do
  43.         MONITOR.setCursorPos(x, i)
  44.         MONITOR.write(string.rep(character, w))
  45.     end
  46. end
  47.  
  48. function drawRect2d(x1, y1, x2, y2, background, character)
  49.     if (background ~= nil) then
  50.         MONITOR.setBackgroundColor(background)
  51.         MONITOR.setTextColor(background)
  52.     end
  53.  
  54.     if (character == nil) then
  55.         character = " "
  56.     end
  57.  
  58.     local w = x2 - x1 + 1
  59.     for y = y1, y2 do
  60.         MONITOR.setCursorPos(x1, y)
  61.         MONITOR.write(string.rep(character, w))
  62.     end
  63. end
  64.  
  65. -- Draws a vertical line at the given x and y coords, with the given height, in the given colour
  66. function drawLineV(x, y, h, background, character)
  67.     if (background ~= nil) then
  68.         MONITOR.setBackgroundColor(background)
  69.         MONITOR.setTextColor(background)
  70.     end
  71.  
  72.     if (character == nil) then
  73.         character = " "
  74.     end
  75.  
  76.     for i = y, h do
  77.         MONITOR.setCursorPos(x, i)
  78.         MONITOR.write(character)
  79.     end
  80. end
  81.  
  82. function drawLineH(x, y, w, background, character)
  83.     if (character == nil) then
  84.         character = " "
  85.     end
  86.  
  87.     drawText(x, y, string.rep(character, w), background, background)
  88. end
  89.  
  90. function drawProgressBar(x, y, w, h, min, max, val, foreground, background)
  91.     drawBox(x, y, w, h, background)
  92.     drawBox(x, y, math.floor((val / (max - min)) * w), h, foreground)
  93. end
  94.  
  95. function clearMonitor()
  96.     MONITOR.setBackgroundColor(colours.black)
  97.     MONITOR.setTextColor(colours.white)
  98.     MONITOR.clear()
  99. end
  100.  
  101. function drawButton(btn, text, textColour, background)
  102.     local x = btn.x1
  103.     local y = btn.y1
  104.     drawRect2d(x, y, btn.x2, btn.y2, background)
  105.     local len = string.len(text)
  106.     if (len < 1) then
  107.         return
  108.     end
  109.  
  110.     local w = btn.x2 - btn.x1 + 1
  111.     local h = btn.y2 - btn.y1 + 1
  112.     local cX = math.floor(w / 2) - math.floor(len / 2)
  113.     local cY = math.ceil(h / 2)
  114.     drawText(cX, cY, text, textColour, background)
  115. end
Add Comment
Please, Sign In to add comment