Advertisement
AlwayzPatty

Untitled

Nov 16th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. local screenSelection = 1
  2. local screen = nil
  3. local prevX, prevY = nil
  4. local maxX, maxY = term.getSize()
  5. os.loadAPI("DefAPI")
  6. os.loadAPI("TableAPI")
  7.  
  8. function drawScreen()
  9. isSelectionChanged()
  10. isSelctionSelected()
  11. resetScreen()
  12.  
  13. term.setBackgroundColor(colors.white)
  14. for i = 1, 3, 1 do
  15. term.setCursorPos(1, i)
  16. term.clearLine()
  17. end
  18. Print(screen["title"], getCenterX(screen["title"]), 2)
  19. term.setBackgroundColor(colors.gray)
  20.  
  21. --For each option of a screen
  22. if type(screen["static"]) == "table" then
  23. local line = 0
  24. if type(screen["options"]) == "table" then
  25. line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["static"])) / 4)
  26. else
  27. line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["static"])) / 2)
  28. end
  29.  
  30. if line < 1 or line > maxY then
  31. screenToSmall()
  32. else
  33. for k, v in ipairs(screen["static"]) do
  34. Print(label, 1, line + 3)
  35. end
  36. end
  37. end
  38.  
  39. if type(screen["options"]) == "table" then
  40. local line = 0
  41. if type(screen["static"]) == "table" then
  42. line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["options"])) / 4) + prevY + 1
  43. else
  44. line = (getCenterY() - 3 - math.floor(TableAPI.tablelength(screen["options"])) / 2)
  45. end
  46. if line < 1 or line > maxY then
  47. screenToSmall()
  48. else
  49. local curSel = 1
  50. for k, v in ipairs(screen["options"]) do
  51. local label = v["option"]
  52. if curSel == screenSelection then
  53. term.setTextColor(colors.white)
  54. Print("[ ", getCenterX("[ " .. label .. " ]"), line)
  55. term.setTextColor(colors.black)
  56. PrintInline(label, string.len("[ "), 0, true)
  57. term.setTextColor(colors.white)
  58. PrintInline(" ]", string.len(label), 0, true)
  59. term.setTextColor(colors.black)
  60. else
  61. Print(label, getCenterX(label), line)
  62. end
  63. line = line + 1
  64. curSel = curSel + 1
  65. end
  66. end
  67. end
  68. end
  69.  
  70. function setScreen(T)
  71. screen = T
  72. end
  73.  
  74. function getScreen()
  75. return screen
  76. end
  77.  
  78. function getCenterX(text)
  79. return (maxX - string.len(text)) / 2
  80. end
  81.  
  82. function screenToSmall()
  83. Print("SCREEN TO SMALL FOR OPTIONS", getCenterX("SCREEN TO SMALL FOR OPTIONS"), getCenterY())
  84. end
  85.  
  86. function getCenterY()
  87. return maxY / 2
  88. end
  89.  
  90. function resetScreen()
  91. term.setBackgroundColor(colors.gray)
  92. term.setTextColor(colors.black)
  93. term.clear()
  94. term.setCursorPos(1, 1)
  95. end
  96.  
  97. function PrintInline(text, x, y)
  98. term.setCursorPos(prevX + x, prevY + y)
  99. term.write(text)
  100. prevX = prevX + x
  101. prevY = prevY + y
  102. end
  103.  
  104. function Print(text, x, y)
  105. term.setCursorPos(x, y)
  106. term.clearLine()
  107. term.write(text)
  108. prevX = x
  109. prevY = y
  110. end
  111.  
  112. function isSelectionChanged()
  113. if event == "key" and type(screen["options"]) == "table" then
  114. if p1 == keys.up then
  115. if screenSelection == 1 then
  116. screenSelection = TableAPI.tablelength(screen["options"])
  117. else
  118. screenSelection = screenSelection - 1
  119. end
  120. elseif p1 == keys.down then
  121. if screenSelection == TableAPI.tablelength(screen["options"]) then
  122. screenSelection = 1
  123. else
  124. screenSelection = screenSelection + 1
  125. end
  126. end
  127. end
  128. end
  129.  
  130. function isSelctionSelected()
  131. if event == "key" and type(screen["options"]) == "table" then
  132. if p1 == keys.enter then
  133. TableAPI.tableAt(screen["options"], screenSelection - 1)["func"]()
  134. elseif p1 == keys.backspace then
  135. local back = TableAPI.tableAtKey(screen["options"], "option", "Back")
  136. if back then
  137. back["func"]()
  138. end
  139. end
  140. end
  141. end
  142.  
  143. function resetSelection()
  144. screenSelection = 1
  145. end
  146.  
  147. function GetDefAPI()
  148. return DefAPI
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement