Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 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. screenSelection = 1
  117. screen = {
  118. title = "Dial Favorites",
  119. options = {
  120.  
  121. }
  122. }
  123.  
  124. for k, v in ipairs(favAddresses) do
  125. table.insert(screen["options"],
  126. {
  127. option = v["title"],
  128. func = dialFav
  129. })
  130. end
  131.  
  132. table.insert(screen["options"],
  133. {
  134. option = "Back",
  135. func = startup
  136. })
  137. end
  138.  
  139. function dialFav()
  140. local destination = tableAt(favAddresses, screenSelection - 1)
  141.  
  142. sg.dial(destination["address"])
  143. startup()
  144. end
  145.  
  146. function exitProgram()
  147. os.reboot()
  148. end
  149.  
  150. function awaitEvent()
  151. event, p1, p2, p3 = os.pullEvent()
  152. end
  153.  
  154. function isSelectionChanged()
  155. if event == "key" then
  156. if p1 == keys.up then
  157. if screenSelection == 1 then
  158. screenSelection = tablelength(screen["options"])
  159. else
  160. screenSelection = screenSelection - 1
  161. end
  162. elseif p1 == keys.down then
  163. if screenSelection == tablelength(screen["options"]) then
  164. screenSelection = 1
  165. else
  166. screenSelection = screenSelection + 1
  167. end
  168. end
  169. end
  170. end
  171.  
  172. function isSelctionSelected()
  173. if event == "key" then
  174. if p1 == keys.enter then
  175. tableAt(screen["options"], screenSelection - 1)["func"]()
  176. end
  177. end
  178. end
  179.  
  180. startup()
  181.  
  182. while true do
  183. awaitEvent()
  184. isSelectionChanged()
  185. isSelctionSelected()
  186. drawScreen(screen)
  187. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement