Advertisement
p0rt23

ui.lua

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