SuperMcBrent

drawLib

Feb 24th, 2023 (edited)
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.48 KB | Gaming | 0 0
  1. -- Drawing Functions
  2. function DrawTitle(x, y, text, color_txt, color_bg, monitor)
  3.     monitor.setBackgroundColor(color_bg)
  4.     monitor.setTextColor(color_txt)
  5.     monitor.setCursorPos(x, y)
  6.     monitor.write(text)
  7.     monitor.setCursorPos(1, 1)
  8.     monitor.setBackgroundColor(colors.black)
  9. end
  10.  
  11. function DrawLine(x, y, length, size, color_bar, monitor)
  12.     for yPos = y, y + size - 1 do
  13.         monitor.setBackgroundColor(color_bar)
  14.         monitor.setCursorPos(x, yPos)
  15.         monitor.write(string.rep(" ", length))
  16.         monitor.setBackgroundColor(colors.black)
  17.     end
  18. end
  19.  
  20. function DrawProg(x, y, length, size, minVal, maxVal, color_bar, color_bg, monitor)
  21.     DrawLine(x, y, length, size, color_bg, monitor)
  22.     local barSize = math.floor((minVal / maxVal) * length)
  23.     DrawLine(x, y, barSize, size, color_bar, monitor)
  24. end
  25.  
  26. function DrawContent(x, y, height, width, minVal, maxVal, color_bar, color_bg, monitor)
  27.     DrawGraph(x, y, height, width, color_bg, monitor)
  28.     local a = math.floor((minVal / maxVal) * height)
  29.     DrawGraph(x, y, a, width, color_bar, monitor)
  30. end
  31.  
  32. function DrawGraph(x, y, height, width, color_bar, monitor)
  33.     DrawLine(x, y - height + 1, width, height, color_bar, monitor)
  34. end
  35.  
  36. function PutTime(x, y, monitor, bool, color)
  37.     monitor.setBackgroundColor(color)
  38.     monitor.setCursorPos(x, y)
  39.     time = textutils.formatTime(os.time(), bool)
  40.     if os.time() < 10.000 then monitor.write("0" .. time) else monitor.write(time) end
  41.     monitor.setBackgroundColor(colors.black)
  42. end
  43.  
  44. function ClearBox(x, y, h, l, monitor, color)
  45.     monitor.setBackgroundColor(color)
  46.     for i = y, y + h - 1 do
  47.         monitor.setCursorPos(x, i)
  48.         monitor.write(string.rep(" ", l))
  49.     end
  50.     monitor.setBackgroundColor(colors.black)
  51. end
  52.  
  53. function Empty(monitor)
  54.     monitor.setBackgroundColor(colors.black)
  55.     monitor.clear()
  56.     monitor.setCursorPos(1, 1)
  57. end
  58.  
  59. ---- BIG numbers
  60.  
  61. local digits = {
  62.     ["0"] = {
  63.         { 1, 1, 1, 1, 1 },
  64.         { 1, 0, 0, 0, 1 },
  65.         { 1, 0, 0, 1, 1 },
  66.         { 1, 0, 1, 0, 1 },
  67.         { 1, 1, 0, 0, 1 },
  68.         { 1, 0, 0, 0, 1 },
  69.         { 1, 1, 1, 1, 1 },
  70.     },
  71.     ["1"] = {
  72.         { 0, 0, 1, 0, 0 },
  73.         { 0, 1, 1, 0, 0 },
  74.         { 1, 0, 1, 0, 0 },
  75.         { 0, 0, 1, 0, 0 },
  76.         { 0, 0, 1, 0, 0 },
  77.         { 0, 0, 1, 0, 0 },
  78.         { 1, 1, 1, 1, 1 },
  79.     },
  80.     ["2"] = {
  81.         { 1, 1, 1, 1, 0 },
  82.         { 0, 0, 0, 0, 1 },
  83.         { 0, 0, 0, 0, 1 },
  84.         { 0, 1, 1, 1, 0 },
  85.         { 1, 0, 0, 0, 0 },
  86.         { 1, 0, 0, 0, 0 },
  87.         { 1, 1, 1, 1, 1 },
  88.     },
  89.     ["3"] = {
  90.         { 1, 1, 1, 1, 0 },
  91.         { 0, 0, 0, 0, 1 },
  92.         { 0, 0, 0, 0, 1 },
  93.         { 0, 1, 1, 1, 0 },
  94.         { 0, 0, 0, 0, 1 },
  95.         { 0, 0, 0, 0, 1 },
  96.         { 1, 1, 1, 1, 0 },
  97.     },
  98.     ["4"] = {
  99.         { 1, 0, 0, 1, 0 },
  100.         { 1, 0, 0, 1, 0 },
  101.         { 1, 0, 0, 1, 0 },
  102.         { 1, 1, 1, 1, 1 },
  103.         { 0, 0, 0, 1, 0 },
  104.         { 0, 0, 0, 1, 0 },
  105.         { 0, 0, 0, 1, 0 },
  106.     },
  107.     ["5"] = {
  108.         { 1, 1, 1, 1, 1 },
  109.         { 1, 0, 0, 0, 0 },
  110.         { 1, 1, 1, 1, 0 },
  111.         { 0, 0, 0, 0, 1 },
  112.         { 0, 0, 0, 0, 1 },
  113.         { 1, 0, 0, 0, 1 },
  114.         { 0, 1, 1, 1, 0 },
  115.     },
  116.     ["6"] = {
  117.         { 0, 1, 1, 1, 0 },
  118.         { 1, 0, 0, 0, 0 },
  119.         { 1, 0, 0, 0, 0 },
  120.         { 1, 1, 1, 1, 0 },
  121.         { 1, 0, 0, 0, 1 },
  122.         { 1, 0, 0, 0, 1 },
  123.         { 0, 1, 1, 1, 0 },
  124.     },
  125.     ["7"] = {
  126.         { 1, 1, 1, 1, 1 },
  127.         { 0, 0, 0, 0, 1 },
  128.         { 0, 0, 0, 1, 0 },
  129.         { 0, 0, 1, 0, 0 },
  130.         { 0, 1, 0, 0, 0 },
  131.         { 1, 0, 0, 0, 0 },
  132.         { 1, 0, 0, 0, 0 },
  133.     },
  134.     ["8"] = {
  135.         { 0, 1, 1, 1, 0 },
  136.         { 1, 0, 0, 0, 1 },
  137.         { 1, 0, 0, 0, 1 },
  138.         { 0, 1, 1, 1, 0 },
  139.         { 1, 0, 0, 0, 1 },
  140.         { 1, 0, 0, 0, 1 },
  141.         { 0, 1, 1, 1, 0 },
  142.     },
  143.     ["9"] = {
  144.         { 0, 1, 1, 1, 0 },
  145.         { 1, 0, 0, 0, 1 },
  146.         { 1, 0, 0, 0, 1 },
  147.         { 0, 1, 1, 1, 1 },
  148.         { 0, 0, 0, 0, 1 },
  149.         { 0, 0, 0, 0, 1 },
  150.         { 0, 1, 1, 1, 0 },
  151.     },
  152.     ["%"] = {
  153.         { 1, 1, 0, 0, 1 },
  154.         { 1, 1, 0, 1, 1 },
  155.         { 0, 0, 0, 1, 0 },
  156.         { 0, 0, 1, 0, 0 },
  157.         { 0, 1, 0, 0, 0 },
  158.         { 1, 1, 0, 1, 1 },
  159.         { 1, 0, 0, 1, 1 },
  160.     }
  161. }
  162.  
  163. local function drawPixel(x, y, color, monitor)
  164.     local oldBg = monitor.getBackgroundColor()
  165.     monitor.setCursorPos(x, y)
  166.     monitor.setBackgroundColor(color)
  167.     monitor.write(" ")
  168.     monitor.setBackgroundColor(oldBg)
  169. end
  170.  
  171. function drawDigit(x, y, char, color, monitor)
  172.     local scale = 1
  173.     color = color or colors.white
  174.     local shape = digits[char]
  175.     if not shape then return end
  176.  
  177.     for row = 1, #shape do
  178.         for col = 1, #shape[row] do
  179.             if shape[row][col] == 1 then
  180.                 for dy = 0, scale - 1 do
  181.                     for dx = 0, scale - 1 do
  182.                         drawPixel(x + (col - 1) * scale + dx, y + (row - 1) * scale + dy, color, monitor)
  183.                     end
  184.                 end
  185.             end
  186.         end
  187.     end
  188. end
  189.  
  190. function drawNumber(x, y, number, color, spacing, padleft, monitor)
  191.     spacing = spacing or 1
  192.     local text
  193.     if padleft == false then
  194.         text = tostring(number) .. "%"
  195.     else
  196.         text = string.format("%03d", number) .. "%"
  197.     end
  198.  
  199.     local scale = 1
  200.     local offset = 0
  201.     for i = 1, #text do
  202.         local c = text:sub(i, i)
  203.         drawDigit(x + offset, y, c, color, monitor)
  204.         offset = offset + (5 * scale) + spacing
  205.     end
  206. end
  207.  
  208. function drawCircle(left, top, width, color, fill, monitor)
  209.     color = color or colors.white
  210.     fill = fill ~= false
  211.     local aspect = 1.5
  212.     local height = math.floor(width / aspect)
  213.     local radiusX = width / 2
  214.     local radiusY = height / 2
  215.     local cx = left + radiusX
  216.     local cy = top + radiusY
  217.  
  218.     for yy = 0, height do
  219.         for xx = 0, width do
  220.             local dx = (xx - radiusX)
  221.             local dy = (yy - radiusY) * aspect
  222.             local dist = math.sqrt(dx * dx + dy * dy)
  223.             if fill then
  224.                 if dist <= radiusX then drawPixel(left + xx, top + yy, color, monitor) end
  225.             else
  226.                 if math.abs(dist - radiusX) < 0.5 then drawPixel(left + xx, top + yy, color, monitor) end
  227.             end
  228.         end
  229.     end
  230. end
  231.  
  232. --- rainbow colors
  233.  
  234. local rainbowColors = {
  235.     colors.red,
  236.     colors.orange,
  237.     colors.yellow,
  238.     colors.lime,
  239.     colors.green,
  240.     colors.cyan,
  241.     colors.lightBlue,
  242.     colors.blue,
  243.     colors.purple,
  244.     colors.magenta,
  245.     colors.pink,
  246. }
  247.  
  248. function rainbow(n)
  249.     -- n = seconds per color step
  250.     local t = os.clock() / n
  251.     local idx = math.floor(t % #rainbowColors) + 1
  252.     return rainbowColors[idx]
  253. end
  254.  
  255. return {
  256.     id = "draw",
  257.     name = "Drawing Library",
  258.     alias = "draw",
  259.     dependencies = {},
  260.     drawTitle = function(...) return DrawTitle(...) end,
  261.     drawLine = function(...) return DrawLine(...) end,
  262.     drawProg = function(...) return DrawProg(...) end,
  263.     drawContent = function(...) return DrawContent(...) end,
  264.     drawGraph = function(...) return DrawGraph(...) end,
  265.     drawNumber = function(...) return drawNumber(...) end,
  266.     drawCircle = function(...) return drawCircle(...) end,
  267.     putTime = function(...) return PutTime(...) end,
  268.     rainbow = function(...) return rainbow(...) end,
  269.     clearBox = function(...) return ClearBox(...) end,
  270.     empty = function(...) return Empty(...) end,
  271. }
  272.  
Advertisement
Add Comment
Please, Sign In to add comment