Advertisement
Guest User

Untitled

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