Advertisement
pneisen

screentest2

Feb 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.70 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local math = require("math")
  4. local bit32 = require("bit32")
  5. local string = require("string")
  6. local gpu = component.gpu
  7. local width, height = gpu.getResolution()
  8.  
  9. function clear()
  10.   gpu.setBackground(0x000000)
  11.   local width, height = gpu.getResolution()
  12.   gpu.fill(1, 1, width, height, " ")
  13. end
  14.  
  15. function singleBorderBox(x, y, w, h)
  16.   gpu.fill(x, y, w, 1, "█")
  17.   gpu.fill(x, y + h - 1, w, 1, "█")
  18.   gpu.fill(x, y + 1, 1, h - 1, "█")
  19.   gpu.fill(x + w - 1, y + 1, 1, h - 1, "█")
  20. end
  21.  
  22. function doubleBorderBox(x, y, w, h)
  23.   singleBorderBox(x, y, w, h)
  24.   singleBorderBox(x + 2, y + 2, w - 4, h - 4)
  25. end
  26.  
  27.  
  28. function horizontalProgressBar(x, y, w, h, progress, color, showPercent)
  29.   -- 0 >= progress <= 1
  30.   singleBorderBox(x,y,w,h)
  31.  
  32.   local oldFG = gpu.getForeground()
  33.   gpu.setForeground(color)
  34.  
  35.   gpu.fill(x + 2, y + 2, math.floor(((w - 4) *  progress) + 0.5), h - 4, "█")
  36.  
  37.   if showPercent then
  38.     -- Figure out placement of the percent label
  39.     local percent = string.format("%.2f", progress * 100) .. "%"
  40.     local len = string.len(percent)
  41.     local labelX = math.floor((((w - 2) / 2) - (len / 2)) + 0.5) + x + 1
  42.     local labelY = math.floor(((h - 2) / 2) + 0.5) + y
  43.  
  44.     -- Display the label with appropriate coloring.      
  45.     for i = 1, len do
  46.       local c, fg, bg, _, _ = gpu.get(labelX + i - 1, labelY)
  47.       if c == "█" then
  48.         gpu.setForeground(bg)
  49.         gpu.setBackground(fg)
  50.         gpu.set(labelX + i - 1, labelY, string.sub(percent, i, i))
  51.         gpu.setBackground(bg)
  52.         gpu.setForeground(fg)
  53.       else
  54.         gpu.set(labelX + i - 1, labelY, string.sub(percent, i, i))    
  55.       end
  56.     end
  57.   end
  58.   gpu.setForeground(oldFG)
  59. end
  60.  
  61. function adjustShade(color, factor)
  62.   local red = bit32.band(16711680, color)
  63.   local green = bit32.band(65280, color)
  64.   local blue = bit32.band(255, color)
  65.  
  66.   red = red * factor
  67.   green = green * factor
  68.   blue = blue * factor
  69.  
  70.   local result = bit32.bor(blue, 0)
  71.   result = bit32.bor(green, result)
  72.   result = bit32.bor(red, result)
  73.   return result
  74. end
  75.  
  76. function maxAndMinFromArray(data)
  77.   local max = data[1]
  78.   local min = data[1]
  79.   if #data > 1 then
  80.     for i=2, #data do
  81.       if data[i] > max then
  82.         max = data[i]
  83.       end
  84.       if data[i] < min then
  85.         min = data[i]
  86.       end
  87.     end
  88.   end
  89.   return max, min
  90. end
  91.  
  92. function verticalBarChart(x, y, w, h, data, color)
  93.   -- Draw the axies
  94.   gpu.fill(x, y, 1, h, "█")
  95.   gpu.fill(x + 1, y + h - 1, w - 1, 1, "█")
  96.  
  97.   local max, min = maxAndMinFromArray(data)
  98.  
  99.   local oldFG = gpu.getForeground()
  100.   gpu.setForeground(color)
  101.  
  102.   -- Draw the bars
  103.   local numberOfBars = math.min(#data, math.floor((w - 2) / 2))
  104.  
  105.   for i=1, numberOfBars do
  106.     if i >= 2 then
  107.       gpu.setForeground(adjustShade(color, .75))
  108.     end
  109.     local heightOfBar = math.floor(((((h - 2)  * .95) / max) * data[i]) +0.5)
  110.     gpu.fill(x + w - (2 * i), (h - 2) - heightOfBar + y, 2, heightOfBar, "█")
  111.   end
  112.  
  113.   gpu.setForeground(oldFG)
  114. end
  115.  
  116. function format_int(number)
  117.   -- stolen from here: https://stackoverflow.com/questions/10989788/lua-format-integer
  118.   local i, j, minus, int, fraction = tostring(number):find('([-]?)(%d+)([.]?%d*)')
  119.  
  120.   -- reverse the int-string and append a comma to all blocks of 3 digits
  121.   int = int:reverse():gsub("(%d%d%d)", "%1,")
  122.  
  123.   -- reverse the int-string back remove an optional comma and put the
  124.   -- optional minus and fractional part back
  125.   return minus .. int:reverse():gsub("^,", "") .. fraction
  126. end
  127.  
  128. function condenseInt(number)
  129.   if number >= 1000000000 then
  130.     return string.format("%.2f", number / 1000000000) .. "B"
  131.   elseif number >=1000000 then
  132.     return string.format("%.2f", number / 1000000) .. "M"
  133.   elseif number >= 1000 then
  134.     return string.format("%.2f", number / 1000) .. "K"
  135.   else
  136.     return number
  137.   end
  138. end
  139.  
  140. local currentRF = 30000000000
  141. local RFStorage = 59300000000
  142. local supplyData = {1700000, 1700000, 1200000, 1200000, 0, 0, 0, 0, 1700000, 1700000, 1210000}
  143. local demandData = {13000,13000,25000,13000, 13000, 13000, 26000, 27000, 13000, 13000}
  144.  
  145. function updateScreen(currentRF, RFStorage, supplyData, demandData)
  146.   clear()
  147.   gpu.setBackground(0x000000)
  148.   gpu.setForeground(0x696969)
  149.   doubleBorderBox(1, 1, width, height)
  150.  
  151.   gpu.setForeground(0xD2D2D2)
  152.   horizontalProgressBar(6, 6, width - 10, 9, currentRF/RFStorage, 0xCC0000, true)
  153.   verticalBarChart(23, 19, 46, 23, supplyData, 0x00DB00)
  154.   verticalBarChart(91, 19, 46, 23, demandData, 0xCC0000)
  155.  
  156.   gpu.setForeground(0xFFFFFF)
  157.  
  158.   local rf = "Redstone Flux: " .. format_int(currentRF) .. " / " .. format_int(RFStorage)
  159.   gpu.set(math.floor(((width - #rf) / 2) + 0.5), 5, rf)
  160.  
  161.   local supplyTitle = "RF Supply"
  162.   gpu.set(math.floor(((46 - #supplyTitle) / 2) + 0.5 + 23), 18, supplyTitle)
  163.  
  164.   local demandTitle = "RF Demand"
  165.   gpu.set(math.floor(((46 - #demandTitle) / 2) + 0.5 + 91), 18, demandTitle)
  166.  
  167.   local currentSupply = supplyData[1]
  168.   local maxSupply, minSupply = maxAndMinFromArray(supplyData)
  169.   gpu.set(34, 43, "Current Supply: " .. condenseInt(currentSupply) .. " RF/t")
  170.   gpu.set(34, 44, "Max Supply: " .. condenseInt(maxSupply) .. " RF/t")
  171.   gpu.set(34, 45, "Min Supply: " .. condenseInt(minSupply) .. " RF/t")
  172.  
  173.   local currentDemand = demandData[1]
  174.   local maxDemand, minDemand = maxAndMinFromArray(demandData)
  175.   gpu.set(103, 43, "Current Demand: " .. condenseInt(currentDemand) .. " RF/t")
  176.   gpu.set(103, 44, "Max Demand: " .. condenseInt(maxDemand) .. " RF/t")
  177.   gpu.set(103, 45, "Min Demand: " .. condenseInt(minDemand) .. " RF/t")
  178. end
  179.  
  180. updateScreen(currentRF, RFStorage, supplyData, demandData)
  181. event.pull("interrupted")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement