Ryouga

powermeter

Mar 31st, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.28 KB | None | 0 0
  1. local mon = peripheral.wrap("right")
  2. mon.setTextScale(0.5)
  3. mon.setBackgroundColor(colors.black)
  4. mon.setTextColor(colors.white)
  5.  
  6. function label(x, y, text)
  7.    mon.setCursorPos(x, y)
  8.    mon.write(text)
  9. end
  10.  
  11. function formatPercent(str, fp)
  12.   str = tostring(str)
  13.   local dec = string.find(str, "%.")
  14.  
  15.   if fp > 4 then
  16.     fp = 4
  17.   end
  18.  
  19.   if dec then
  20.    
  21.     local wNum = string.sub(str,1,dec-1)
  22.     local fNum = string.sub(str,dec+1,#str)
  23.    
  24.     if #fNum >= fp then
  25.       return (wNum .. "." .. string.sub(fNum, 1, fp) .. "%")
  26.     else
  27.       return (wNum .. "." .. fNum .. "%")
  28.     end
  29.  
  30.   else
  31.     return (str.."%")
  32.   end
  33. end
  34.  
  35. function float(str, fp)
  36.   str = tostring(str)
  37.   local dec = string.find(str, "%.")
  38.  
  39.   if fp > 4 then
  40.     fp = 4
  41.   end
  42.  
  43.   if dec then
  44.    
  45.     local wNum = string.sub(str,1,dec-1)
  46.     local fNum = string.sub(str,dec+1,#str)
  47.    
  48.     if #fNum >= fp then
  49.       return tonumber(wNum .. "." .. string.sub(fNum, 1, fp))
  50.     else
  51.       return tonumber(wNum .. "." .. fNum)
  52.     end
  53.  
  54.   else
  55.     return tonumber(str)
  56.   end
  57. end
  58.  
  59.  
  60.  
  61. function drawMeter(x,y,width,height,clr, label,iNum,maxNum)
  62.  
  63.   --
  64.   -- drawMeter(x pos, y pos, width, height, border color, meter label, current energy, max energy)
  65.   --
  66.  
  67.   local oldBGColor = mon.getBackgroundColor()
  68.   local label_start = x+3
  69.  
  70.   fpct = float(((iNum/maxNum) * 100),4)
  71.  
  72.   label = label .. " - " .. formatPercent(fpct,4)
  73.  
  74.   local label_end = label_start+#label+2
  75.  
  76.   --Height must be 3 or greater
  77.   --Set to 3 instead of failing the render
  78.   if height < 3 then
  79.     height = 3
  80.   end
  81.  
  82.   height=height-1
  83.   mon.setBackgroundColor(clr)
  84.  
  85.   for i=x, x+width do
  86.  
  87.     if i >= label_start and i < label_end then
  88.       -- Moved out of the loop
  89.         --mon.setBackgroundColor(oldBGColor)
  90.         --mon.setCursorPos(label_start,y)
  91.         --mon.write(" " .. label .. " ")
  92.     else
  93.       mon.setBackgroundColor(clr)
  94.       mon.setCursorPos(i,y)
  95.       mon.write(" ")
  96.     end
  97.  
  98.     mon.setBackgroundColor(clr)
  99.     mon.setCursorPos(i,y+height)
  100.     mon.write(" ")
  101.  
  102.   end
  103.  
  104.   for i=y, (y+(height)) do
  105.     mon.setCursorPos(x,i)
  106.     mon.write(" ")
  107.     mon.setCursorPos(x+width, i)
  108.     mon.write(" ")
  109.   end
  110.  
  111.   --
  112.   -- Write the label
  113.   --
  114.     mon.setBackgroundColor(oldBGColor)
  115.     mon.setCursorPos(label_start,y)
  116.     mon.write(" " .. label .. " ")
  117.  
  118.   -- Adjusted info for progress bar
  119.     fx1 = x+1
  120.     fy1 = y+1
  121.     fw = width - 2
  122.     fx2 = fx1+fw
  123.     fy2 = fy1+(height-2)
  124.    
  125.   -- Counter and Percent for pixel rendering
  126.     fc = 0
  127.     fp = 0
  128.  
  129.     for i = fx1, fx2 do
  130.  
  131.       fp = float( (fc / fw)*100 , 4)
  132.      
  133.       fc = fc+1
  134.       fbl = 1
  135.       for j = fy1, fy2 do
  136.  
  137.         if (fp > fpct and fp ~= 1) or fpct == 0 then
  138.           mon.setBackgroundColor(colors.black)
  139.           mon.setCursorPos(i,j)
  140.           mon.write(" ")
  141.          
  142.           -- break loop after writing a few 'empty' bars
  143.           -- removes artifacts and decreases time in the loop
  144.           if fbl == 3 then
  145.             break
  146.           end
  147.           fbl = fbl+1
  148.         else
  149.           -- Progress bar colors
  150.           if fpct <= 5 then
  151.             mon.setBackgroundColor(colors.red)
  152.             mon.setTextColor(colors.red)
  153.  
  154.           elseif fpct <= 10 and fpct > 5 then
  155.             mon.setBackgroundColor(colors.red)
  156.             mon.setTextColor(colors.black)
  157.  
  158.           elseif fpct <= 17.5 and fpct > 10 then
  159.             mon.setBackgroundColor(colors.red)
  160.             mon.setTextColor(colors.orange)
  161.  
  162.           elseif fpct <= 25 and fpct > 17.5 then
  163.             mon.setBackgroundColor(colors.red)
  164.             mon.setTextColor(colors.yellow)
  165.  
  166.           elseif fpct <= 37.5 and fpct > 25 then
  167.             mon.setBackgroundColor(colors.orange)
  168.             mon.setTextColor(colors.red)
  169.  
  170.           elseif fpct <= 50 and fpct > 37.5 then
  171.             mon.setBackgroundColor(colors.orange)
  172.             mon.setTextColor(colors.yellow)
  173.  
  174.           elseif fpct <= 67.5 and fpct > 50 then
  175.             mon.setBackgroundColor(colors.yellow)
  176.             mon.setTextColor(colors.orange)
  177.  
  178.           elseif fpct <= 75 and fpct > 67.5 then
  179.             mon.setBackgroundColor(colors.yellow)
  180.             mon.setTextColor(colors.lime)
  181.  
  182.           elseif fpct <= 87.5 and fpct > 75 then
  183.             mon.setBackgroundColor(colors.lime)
  184.             mon.setTextColor(colors.yellow)
  185.  
  186.           elseif fpct < 100 and fpct > 87.5 then
  187.             mon.setBackgroundColor(colors.lime)
  188.             mon.setTextColor(colors.white)
  189.  
  190.           elseif fpct == 100 then
  191.             mon.setBackgroundColor(colors.lime)
  192.             mon.setTextColor(colors.lime)
  193.           end
  194.           -- End Progress bar colors
  195.  
  196.           mon.setCursorPos(i,j)
  197.           --mon.write(" ")
  198.  
  199.           --Random Chars because I could
  200.           --Serves no real purpose
  201.           mwn = math.random(1,4)
  202.           if mwn == 1 then
  203.             mon.write("*")
  204.           elseif mwn == 2 then
  205.             mon.write("+")
  206.           elseif mwn == 3 then
  207.             mon.write("\\")
  208.           elseif mwn == 4 then
  209.             mon.write("/")
  210.           end
  211.           --End random chars
  212.          
  213.         end
  214.  
  215.       end
  216.      
  217.     end
  218.  
  219.     mon.setTextColor(colors.white)
  220.     mon.setBackgroundColor(colors.black)
  221.  
  222. end -- drawMeter
Advertisement
Add Comment
Please, Sign In to add comment