Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. local sg = peripheral.wrap("right")
  2.  
  3. local favAddresses = {
  4. {
  5. title = "",
  6. address = ""
  7. }
  8. }
  9.  
  10. local event, p1, p2, p3 = nil
  11. local screenSelection = 1
  12. local screen = nil
  13. local prevX, prevY = nil
  14.  
  15. function resetScreen()
  16. term.setBackgroundColor(colors.gray)
  17. term.setTextColor(colors.black)
  18. term.clear()
  19. term.setCursorPos(1, 1)
  20. end
  21.  
  22. function PrintInline(text, x, y)
  23. term.setCursorPos(prevX + x, prevY + y)
  24. term.write(text)
  25. prevX = prevX + x
  26. prevY = prevY + y
  27. end
  28.  
  29. function Print(text, x, y)
  30. term.setCursorPos(x, y)
  31. term.write(text)
  32. prevX = x
  33. prevY = y
  34. end
  35.  
  36. function drawScreen(screenSettings)
  37. resetScreen()
  38.  
  39. term.setBackgroundColor(colors.white)
  40. for i = 1, 3, 1 do
  41. term.setCursorPos(1, i)
  42. term.clearLine()
  43. end
  44. Print(screenSettings["title"], getCenterX(screenSettings["title"]), 2)
  45. term.setBackgroundColor(colors.gray)
  46.  
  47. --For each option of a screen
  48. local line = getCenterY() - math.floor(tablelength(screenSettings["options"]) / 2)
  49. local curSel = 1
  50. for k, v in ipairs(screenSettings["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.  
  68. function getCenterX(text)
  69. local x,_ = term.getSize()
  70. return math.floor((x / 2) - (string.len(text) / 2))
  71. end
  72.  
  73. function getCenterY()
  74. local _,y = term.getSize()
  75. return math.floor(y / 2)
  76. end
  77.  
  78. function tablelength(T)
  79. local count = 0
  80. for _ in pairs(T) do count = count + 1 end
  81. return count
  82. end
  83.  
  84. function tableAt(T, index)
  85. local i = 0
  86. for _, v in pairs(T) do
  87. if i == index then
  88. return v
  89. end
  90. i = i + 1
  91. end
  92. end
  93.  
  94. function startup()
  95. screenSelection = 1
  96. screen = {
  97. title = "Stargate Control Program",
  98. options = {
  99. {
  100. option = "Dial Favorite",
  101. func = dialFavoriteScreen
  102. },
  103. {
  104. option = "Dial Manually",
  105. func = dialFavoriteScreen
  106. },
  107. {
  108. option = "Exit",
  109. func = exitProgram
  110. }
  111. }
  112. }
  113. end
  114.  
  115. function dialFavoriteScreen()
  116.  
  117. end
  118.  
  119. function exitProgram()
  120. os.reboot()
  121. end
  122.  
  123. function awaitEvent()
  124. event, p1, p2, p3 = os.pullEvent()
  125. end
  126.  
  127. function isSelectionChanged()
  128. if event == "key" then
  129. if p1 == keys.up then
  130. if screenSelection == 1 then
  131. screenSelection = tablelength(screen["options"])
  132. else
  133. screenSelection = screenSelection - 1
  134. end
  135. elseif p1 == keys.down then
  136. if screenSelection == tablelength(screen["options"]) then
  137. screenSelection = 1
  138. else
  139. screenSelection = screenSelection + 1
  140. end
  141. end
  142. end
  143. end
  144.  
  145. function isSelctionSelected()
  146. if event == "key" then
  147. if p1 == keys.enter then
  148. tableAt(screen["options"], screenSelection - 1)["func"]()
  149. end
  150. end
  151. end
  152.  
  153. startup()
  154.  
  155. while true do
  156. awaitEvent()
  157. isSelectionChanged()
  158. isSelctionSelected()
  159. drawScreen(screen)
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement