Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. local sg = peripheral.find("stargate")
  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. local errorMessage = nil
  15.  
  16. function resetScreen()
  17. term.setBackgroundColor(colors.gray)
  18. term.setTextColor(colors.black)
  19. term.clear()
  20. term.setCursorPos(1, 1)
  21. end
  22.  
  23. function PrintInline(text, x, y)
  24. term.setCursorPos(prevX + x, prevY + y)
  25. term.write(text)
  26. prevX = prevX + x
  27. prevY = prevY + y
  28. end
  29.  
  30. function Print(text, x, y)
  31. term.setCursorPos(x, y)
  32. term.write(text)
  33. prevX = x
  34. prevY = y
  35. end
  36.  
  37. function drawScreen(screenSettings)
  38. resetScreen()
  39.  
  40. term.setBackgroundColor(colors.white)
  41. for i = 1, 3, 1 do
  42. term.setCursorPos(1, i)
  43. term.clearLine()
  44. end
  45. Print(screenSettings["title"], getCenterX(screenSettings["title"]), 2)
  46. term.setBackgroundColor(colors.gray)
  47.  
  48. --For each option of a screen
  49. local line = getCenterY() - math.floor(tablelength(screenSettings["options"]) / 2)
  50. local curSel = 1
  51. for k, v in ipairs(screenSettings["options"]) do
  52. local label = v["option"]
  53. if curSel == screenSelection then
  54. term.setTextColor(colors.white)
  55. Print("[ ", getCenterX("[ " .. label .. " ]"), line)
  56. term.setTextColor(colors.black)
  57. PrintInline(label, string.len("[ "), 0, true)
  58. term.setTextColor(colors.white)
  59. PrintInline(" ]", string.len(label), 0, true)
  60. term.setTextColor(colors.black)
  61. else
  62. Print(label, getCenterX(label), line)
  63. end
  64. line = line + 1
  65. curSel = curSel + 1
  66. end
  67. end
  68.  
  69. function getCenterX(text)
  70. local x,_ = term.getSize()
  71. return math.floor((x / 2) - (string.len(text) / 2))
  72. end
  73.  
  74. function getCenterY()
  75. local _,y = term.getSize()
  76. return math.floor(y / 2)
  77. end
  78.  
  79. function tablelength(T)
  80. local count = 0
  81. for _ in pairs(T) do count = count + 1 end
  82. return count
  83. end
  84.  
  85. function tableAt(T, index)
  86. local i = 0
  87. for _, v in pairs(T) do
  88. if i == index then
  89. return v
  90. end
  91. i = i + 1
  92. end
  93. end
  94.  
  95. function startup()
  96. screenSelection = 1
  97. screen = {
  98. title = "Stargate Control Program",
  99. options = {
  100. {
  101. option = "Dial Favorite",
  102. func = dialFavoriteScreen
  103. },
  104. {
  105. option = "Dial Manually",
  106. func = dialFavoriteScreen
  107. },
  108. {
  109. option = "Exit",
  110. func = exitProgram
  111. }
  112. }
  113. }
  114. end
  115.  
  116. function dialFavoriteScreen()
  117. screenSelection = 1
  118. screen = {
  119. title = "Dial Favorites",
  120. options = {
  121.  
  122. }
  123. }
  124.  
  125. for k, v in ipairs(favAddresses) do
  126. table.insert(screen["options"],
  127. {
  128. option = v["title"],
  129. func = dialFav
  130. })
  131. end
  132.  
  133. table.insert(screen["options"],
  134. {
  135. option = "Back",
  136. func = startup
  137. })
  138. end
  139.  
  140. function dialFav()
  141. local destination = tableAt(favAddresses, screenSelection - 1)
  142. dial(destination["address"])
  143. startup()
  144. end
  145.  
  146. function dial(address)
  147. if sg.dial(address) == "targetBusy" then
  148. local _, y = term.getSize()
  149. Print("Stargate is in use", 1, y)
  150. sleep(2)
  151. end
  152. end
  153.  
  154. function exitProgram()
  155. os.reboot()
  156. end
  157.  
  158. function awaitEvent()
  159. event, p1, p2, p3 = os.pullEvent()
  160. end
  161.  
  162. function isSelectionChanged()
  163. if event == "key" then
  164. if p1 == keys.up then
  165. if screenSelection == 1 then
  166. screenSelection = tablelength(screen["options"])
  167. else
  168. screenSelection = screenSelection - 1
  169. end
  170. elseif p1 == keys.down then
  171. if screenSelection == tablelength(screen["options"]) then
  172. screenSelection = 1
  173. else
  174. screenSelection = screenSelection + 1
  175. end
  176. end
  177. end
  178. end
  179.  
  180. function isSelctionSelected()
  181. if event == "key" then
  182. if p1 == keys.enter then
  183. tableAt(screen["options"], screenSelection - 1)["func"]()
  184. end
  185. end
  186. end
  187.  
  188. startup()
  189.  
  190. while true do
  191. awaitEvent()
  192. isSelectionChanged()
  193. isSelctionSelected()
  194. drawScreen(screen)
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement