Advertisement
Guest User

ui.lua

a guest
Jul 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. local ui = {}
  2.  
  3. local component = require("component")
  4. local gpu       = component.gpu
  5.  
  6. local screenWidthOuter, screenHeightOuter
  7. local screenWidthInner, screenHeightInner
  8. local screenTopInner, screenLeftInner
  9. local maxDepth
  10.  
  11. local function setScreenSettings()
  12.   screenWidthOuter, screenHeightOuter = gpu.getResolution()
  13.   screenWidthInner  = screenWidthOuter
  14.   screenHeightInner = screenHeightOuter
  15.   screenTopInner    = 1
  16.   screenLeftInner   = 1
  17.   maxDepth = gpu.maxDepth()  
  18. end
  19.  
  20. local function scaleValue(n1, n1Max, n2Max)
  21.   n1    = tonumber(n1)    or 0
  22.   n1Max = tonumber(n1Max) or 0
  23.   n2Max = tonumber(n2Max) or 0
  24.  
  25.   if n1Max > 0 then
  26.     return math.floor((n2Max*n1)/n1Max)
  27.   end
  28.   return 0
  29. end  
  30.  
  31. local function setBackground(color, isIndex, monoColor)
  32.   color     = color     or 0x000000
  33.   monoColor = monoColor or 0x000000
  34.   isIndex   = isIndex   or false
  35.   if maxDepth < 4 then
  36.     color   = monoColor
  37.     isIndex = false
  38.   end
  39.   local oldColor = gpu.setBackground(color, isIndex)
  40.   return oldColor
  41. end
  42.  
  43. local function setForeground(color, isIndex, monoColor)
  44.   color     = color     or 0xFFFFFF
  45.   monoColor = monoColor or 0xFFFFFF
  46.   isIndex   = isIndex   or false
  47.   if maxDepth < 4 then
  48.     color   = monoColor
  49.     isIndex = false
  50.   end
  51.   local oldColor = gpu.setForeground(color, isIndex)
  52.   return oldColor
  53. end
  54.  
  55. local function getXAlignRight(endX, val)
  56.   local len = string.len(val)
  57.   return endX-len
  58. end
  59.  
  60. local function getXAlignCenter(startX, width, val)
  61.   local len = string.len(val)
  62.   local mid = math.floor(width/2)
  63.   local midStr = math.floor(len/2)
  64.   return startX+mid-midStr
  65. end
  66.  
  67. local function drawText(x, y, text, fgColor, isFgIndex, bgColor, isBgIndex)
  68.   local oldFg = setForeground(fgColor, isFgIndex)
  69.   local oldBg = setBackground(bgColor, isBgIndex)
  70.  
  71.   gpu.set(x, y, text)
  72.  
  73.   setForeground(oldFg)
  74.   setBackground(oldBg)
  75. end
  76.  
  77. function ui.clear()
  78.   local w, h = gpu.getResolution()
  79.   gpu.fill(1, 1, w, h, " ")
  80. end
  81.  
  82. function ui.setScreenPaddingPercent(top, right, bottom, left)
  83.   local topOffset    = math.floor(screenHeightOuter*(top   /100))
  84.   local rightOffset  = math.floor(screenWidthOuter *(right /100))
  85.   local bottomOffset = math.floor(screenHeightOuter*(bottom/100))
  86.   local leftOffset   = math.floor(screenWidthOuter *(left  /100))
  87.  
  88.   screenTopInner    = topOffset+1
  89.   screenLeftInner   = leftOffset+1
  90.   screenWidthInner  = screenWidthOuter-rightOffset-leftOffset
  91.   screenHeightInner = screenHeightOuter-topOffset-bottomOffset
  92. end
  93.  
  94. function ui.getPanelDimensions(panelCount)
  95.   local panels = {}
  96.   local panelBottomPrevious = screenTopInner
  97.   local panelHeight = math.floor(screenHeightInner/panelCount)
  98.  
  99.   for i = 0, panelCount - 1 do
  100.     panels[i]           = {}
  101.     panels[i]["x"]      = screenLeftInner
  102.     panels[i]["y"]      = panelBottomPrevious
  103.     panels[i]["w"]      = screenWidthInner
  104.     panels[i]["h"]      = panelHeight
  105.     panels[i]["middle"] = panelBottomPrevious+math.floor(panelHeight/2)
  106.     panelBottomPrevious = panels[i]["y"] + panels[i]["h"]
  107.   end
  108.   return panels
  109. end
  110.  
  111. function ui.drawPanelBackground(panel, color, isIndex)
  112.   local oldColor = setBackground(color, isIndex)
  113.   gpu.fill(panel["x"], panel["y"], panel["w"], panel["h"], " ")
  114.   setBackground(oldColor)
  115. end
  116.  
  117. function ui.drawHorizontalPercentageBar(x, y, height, fgColor, isFgIndex, bgColor, isBgIndex, val, maxVal, width)
  118.   local innerWidth = scaleValue(val, maxVal, width)
  119.  
  120.   local oldColor = setBackground(bgColor, isBgIndex)
  121.   gpu.fill(x, y, width, height, " ")
  122.  
  123.   setBackground(fgColor, isFgIndex, 0xFFFFFF)
  124.   gpu.fill(x, y, innerWidth, height, " ")
  125.  
  126.   setBackground(oldColor)
  127. end
  128.  
  129. function ui.drawPanelText(panel, x, y, text, align, fgColor, isFgIndex, bgColor, isBgIndex)
  130.   if align == "left" then
  131.     x = panel["x"]
  132.   elseif align == "right" then
  133.     x = getXAlignRight(panel["x"]+panel["w"], text)
  134.   elseif align == "center" then
  135.     x = getXAlignCenter(panel["x"], panel["w"], text)
  136.   else
  137.     x = x or 1
  138.   end
  139.  
  140.   drawText(x, y, text, fgColor, isFgIndex, bgColor, isBgIndex)
  141. end
  142.  
  143. function ui.drawScreenText(x, y, text, align, fgColor, isFgIndex, bgColor, isBgIndex)
  144.   if align == "left" then
  145.     x = 1
  146.   elseif align == "right" then
  147.     x = getXAlignRight(1+screenWidthOuter, text)
  148.   elseif align == "center" then
  149.     x = getXAlignCenter(1, screenWidthOuter, text)
  150.   else
  151.     x = x or 1
  152.   end
  153.  
  154.   drawText(x, y, text, fgColor, isFgIndex, bgColor, isBgIndex)
  155. end
  156.  
  157. function ui.setResolution(w, h)
  158.   local oldW, oldH = gpu.getResolution()
  159.   if (oldW >= w) and (oldH >= h) then
  160.     gpu.setResolution(w, h)
  161.   end
  162.   setScreenSettings()
  163.   return oldW, oldH
  164. end
  165.  
  166. setScreenSettings()
  167.  
  168. return ui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement