mast3rillusion

sliders.lua

Jul 27th, 2021 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1.     sliders = {}
  2.     monitor = nil
  3.  
  4.     function setMonitor(side)
  5.         monitor = peripheral.wrap(side)
  6.         if not (monitor.isColor()) then
  7.             error("Monitor Doesn't Support Colors")
  8.         end
  9.     end
  10.  
  11.     function createSlider(name, x, y, length, height, sliderColor, barColor)
  12.  
  13.         -- Checks if name is not unique
  14.         for k,v in pairs(sliders) do
  15.             if name == k then
  16.                 error(name .. " already exist!")
  17.             end
  18.         end
  19.  
  20.         -- Optional values are put
  21.         if sliderColor == nil then
  22.             sliderColor = colors.gray
  23.         end
  24.         if barColor == nil then
  25.             barColor = colors.white
  26.         end
  27.  
  28.         -- fills in values
  29.         sliders[name] = {}
  30.         sliders[name]["x"] = x
  31.         sliders[name]["y"] = y
  32.         sliders[name]["length"] = length
  33.         sliders[name]["height"] = height
  34.         sliders[name]["sliderColor"] = sliderColor
  35.         sliders[name]["barColor"] = barColor
  36.         sliders[name]["value"] = 0
  37.         sliders[name]["textColor"] = colors.black
  38.     end
  39.  
  40.     -- This functions sets the value of the slider.
  41.     function updateSlider(name, value)
  42.         if value > 100 then
  43.             value = 100
  44.             -- error( name ": Value can not be over 100!")
  45.         elseif value < 0 then
  46.             error( name ": Value can not be under 0!")
  47.         end
  48.  
  49.         for k,v in pairs(sliders) do
  50.             if k == name then
  51.                 sliders[name]["value"] = value
  52.             end
  53.         end
  54.     end
  55.  
  56.     function draw(name, drawText)
  57.  
  58.         -- Sets default monitor to the term and checks if it supports color (is an advanced machine)
  59.  
  60.         if monitor == nil then
  61.             monitor = term
  62.             if not (monitor.isColor()) then
  63.                 error("Monitor doesn't support Colors")
  64.             end
  65.         end
  66.  
  67.         -- Checks if the arguments are set, if not do default
  68.         if not (type(name) == "table") then
  69.             if not (type(name) == "string") then
  70.                 name = {}
  71.                 for k,v in pairs(sliders) do
  72.                     table.insert(name,k)
  73.                 end
  74.             else
  75.                 name = {name}
  76.             end
  77.         end
  78.  
  79.         if drawText == nil then
  80.             drawText = true;
  81.         end
  82.  
  83.         for k,v in pairs(sliders) do
  84.             for s = 0, #name+1 do
  85.                 if k == name[s] then
  86.  
  87.                     percentDraw = v.length * (v.value / 100)
  88.                         for yPos = v.y, v.y + v.height - 1 do
  89.  
  90.                             monitor.setBackgroundColor(v.barColor)
  91.                             monitor.setCursorPos(v.x, yPos)
  92.                             monitor.write(string.rep(" ", v.length))
  93.  
  94.                             monitor.setCursorPos(v.x, yPos)
  95.                             monitor.setBackgroundColor(v.sliderColor)
  96.                             monitor.write(string.rep(" ", percentDraw))
  97.  
  98.                             if drawText == true then
  99.                                 local textX = math.floor(v.x + (v.length/2)-1)
  100.                                 local textY = math.floor(v.y + v.height - (v.height/2))
  101.                                 local text = math.floor(v.value) .. "%"
  102.  
  103.                                 monitor.setCursorPos(textX,textY)
  104.                                 monitor.setTextColor(v.textColor)
  105.  
  106.                                 for i = 0, #text do
  107.                                     if percentDraw >= v.length/2 and i == 1 then
  108.                                         monitor.setBackgroundColor(v.sliderColor)
  109.                                         monitor.write(string.sub(text,i,i))
  110.                                     elseif percentDraw >= math.floor(v.length/2+1) and i <= 2 then
  111.                                         monitor.setBackgroundColor(v.sliderColor)
  112.                                         monitor.write(string.sub(text,i,i))
  113.                                     elseif percentDraw >= math.floor(v.length/2+2) and i <= 3 then
  114.                                         monitor.setBackgroundColor(v.sliderColor)
  115.                                         monitor.write(string.sub(text,i,i))
  116.                                     elseif percentDraw >= math.floor(v.length/2+3) and i <= 4 then
  117.                                         monitor.setBackgroundColor(v.sliderColor)
  118.                                         monitor.write(string.sub(text,i,i))
  119.                                     else
  120.                                         monitor.setBackgroundColor(v.barColor)
  121.                                         monitor.write(string.sub(text,i,i))
  122.                                     end
  123.                                 end
  124.                             end
  125.                         end
  126.                     end
  127.                 end
  128.  
  129.             monitor.setBackgroundColor(colors.black)
  130.             monitor.setTextColor(colors.white)
  131.             monitor.setCursorPos(1,1)
  132.  
  133.         end
  134.     end
Add Comment
Please, Sign In to add comment