Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 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 = dialManually
  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 dialManually()
  141. screenSelection = 1
  142. screen = {
  143. title = "Manual Dial",
  144. options = {
  145. {
  146. option = "Enter",
  147. func = function()
  148. term.setCursorPos(getCenterX("---------"), getCenterY() + 3)
  149. Print("ADDRESS:", getCenterX("ADDRESS:"), getCenterY() + 2)
  150. dial(read())
  151. startup()
  152. end
  153. },
  154. {
  155. option = "Back",
  156. func = startup
  157. }
  158. }
  159. }
  160. end
  161.  
  162. function dialFav()
  163. local destination = tableAt(favAddresses, screenSelection - 1)
  164. dial(destination["address"])
  165. startup()
  166. end
  167.  
  168. function dial(address)
  169. if sg.dial(address) == "targetBusy" then
  170. local _, y = term.getSize()
  171. Print("Stargate is in use", 1, y)
  172. sleep(2)
  173. end
  174. end
  175.  
  176. function exitProgram()
  177. os.reboot()
  178. end
  179.  
  180. function awaitEvent()
  181. event, p1, p2, p3 = os.pullEvent()
  182. end
  183.  
  184. function isSelectionChanged()
  185. if event == "key" then
  186. if p1 == keys.up then
  187. if screenSelection == 1 then
  188. screenSelection = tablelength(screen["options"])
  189. else
  190. screenSelection = screenSelection - 1
  191. end
  192. elseif p1 == keys.down then
  193. if screenSelection == tablelength(screen["options"]) then
  194. screenSelection = 1
  195. else
  196. screenSelection = screenSelection + 1
  197. end
  198. end
  199. end
  200. end
  201.  
  202. function isSelctionSelected()
  203. if event == "key" then
  204. if p1 == keys.enter then
  205. tableAt(screen["options"], screenSelection - 1)["func"]()
  206. end
  207. end
  208. end
  209.  
  210. startup()
  211.  
  212. while true do
  213. awaitEvent()
  214. isSelectionChanged()
  215. isSelctionSelected()
  216. drawScreen(screen)
  217. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement