csmit195

Glasses Power View

Sep 30th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.47 KB | None | 0 0
  1. local glass = peripheral.wrap('bottom')
  2. local power = peripheral.wrap('left')
  3.  
  4. -- Glasses
  5. local startX = 20
  6. local startY = 5
  7.  
  8. local powerID = 'IC2:fluidUuMatter'
  9.  
  10. function init()
  11.   if ( checkErrors() ) then
  12.     error('error found')
  13.   end
  14.   glassesLoop()
  15. end
  16.  
  17. function glassesLoop()
  18.   while true do
  19.     glass.clear()
  20.  
  21.     -- Power
  22.     local powerUsed = power.getEnergyStored()
  23.     local powerMax = power.getMaxEnergyStored()
  24.     local powerPercentage = ( ( powerUsed / powerMax ) * 100 )
  25.     local text = 'POWER: ' .. round(powerPercentage, 2)
  26.  
  27.     addChargeProgress(startX+1, startY+1, powerID, nil, powerUsed, powerMax, 0.75, 0x7f7f7f, 0.5, 0x4B0082, 1, 0.25, 0.5, 3, text)
  28.  
  29.     glass.sync()
  30.     sleep(1)
  31.   end
  32. end
  33.  
  34. function checkErrors()
  35.   return false
  36. end
  37.  
  38. function addChargeProgress(x, y, id, meta, amt, max_amt, n_a, s_rgb, s_a, rgb, a, tb_a, e_a, e_int, text)
  39.     local w = getStringWidth(text) + 34
  40.   local h = 18
  41.   glass.addBox(x + 1, y + 1, w - 2, h - 2, rgb, a)
  42.   local i = x + 1 + e_a
  43.   while i <= x + w - 2 do
  44.       glass.addBox(i, y + 1, 1, h - 2, 0x000000, e_a)
  45.       i = i + e_int
  46.   end
  47.   glass.addBox(x + 1, y + 1, w - 2, 3, 0x000000, tb_a)
  48.   glass.addBox(x + 1, y + h - 4, w - 2, 3, 0x000000, tb_a)
  49.   glass.addBox(x, y, w, 1, s_rgb, s_a)
  50.   glass.addBox(x, y + h - 1, w, 1, s_rgb, s_a)
  51.   glass.addBox(x, y + 1, 1, h - 2, s_rgb, s_a)
  52.   glass.addBox(x + w - 1, y + 1, 1, h - 2, s_rgb, s_a)
  53.   local amt_prog = math.ceil(amt / max_amt * w - 2)
  54.   glass.addBox(x + 1 + amt_prog, y + 1, w - 2 - amt_prog, h - 2, 0x000000, n_a)
  55.   glass.addIcon(x + 1, y + 1, id, meta)
  56.   glass.addText(x + 22, y + 5, text)
  57. end
  58.  
  59. function getStringWidth(text)
  60.   local w = 0
  61.   local def = 5
  62.   local w1 = "i!,.:;|"
  63.   local w2 = "'l"
  64.   local w3 = " t"
  65.   local w4 = "*{}()\"<>f"
  66.   local w6 = "~"
  67.   for i = 1, string.len(text) do
  68.       local c = string.sub(text, i, i)
  69.       if string.find(w1, c) ~= nil then
  70.           w = w + 1
  71.       elseif string.find(w2, c) ~= nil then
  72.           w = w + 2
  73.       elseif string.find(w3, c) ~= nil then
  74.           w = w + 3
  75.       elseif string.find(w4, c) ~= nil then
  76.           w = w + 4
  77.       elseif string.find(w6, c) ~= nil then
  78.           w = w + 6
  79.       else
  80.           w = w + 5
  81.       end
  82.       if i < string.len(text) - 1 then
  83.           w = w + 1
  84.       end
  85.   end
  86.   return w
  87. end
  88.  
  89. function round(num, numDecimalPlaces)
  90.   local mult = 10^(numDecimalPlaces or 0)
  91.   return math.floor(num * mult + 0.5) / mult
  92. end
  93.  
  94. init()
Advertisement
Add Comment
Please, Sign In to add comment