Advertisement
Guest User

Star

a guest
Aug 25th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.54 KB | None | 0 0
  1.  
  2. local filesystem = require("Filesystem")
  3. local image = require("Image")
  4. local screen = require("Screen")
  5. local GUI = require("GUI")
  6. local paths = require("Paths")
  7. local system = require("System")
  8.  
  9. if not component.isAvailable("stargate") then
  10.     GUI.alert("This program requires stargate from mod \"SGCraft\"")
  11.     return
  12. end
  13.  
  14. local stargate = component.get("stargate")
  15.  
  16. ---------------------------------------------------------------------------------------------
  17.  
  18. local resources = filesystem.path(system.getCurrentScript())
  19. local pathToContacts = paths.user.applicationData .. "Stargate/Contacts.cfg"
  20. local contacts = {}
  21. local Ch1Image = image.load(resources .. "Ch1.pic")
  22. local Ch2Image = image.load(resources .. "Ch2.pic")
  23.  
  24. local workspace = GUI.workspace()
  25.  
  26. ---------------------------------------------------------------------------------------------
  27.  
  28. local function loadContacts()
  29.     if filesystem.exists(pathToContacts) then
  30.         contacts = filesystem.readTable(pathToContacts)
  31.     end
  32. end
  33.  
  34. local function saveContacts()
  35.     filesystem.writeTable(pathToContacts, contacts)
  36. end
  37.  
  38. local function chevronDraw(object)
  39.     local inactiveColor, activeColor, fadeColor = 0x332400, 0xFFDB00, 0xCC6D00
  40.     -- screen.drawRectangle(object.x, object.y, object.width, object.height, object.isActivated and fadeColor or inactiveColor)
  41.     -- screen.drawRectangle(object.x + 1, object.y, 3, object.height, object.isActivated and activeColor or inactiveColor)
  42.     -- screen.drawText(object.x + 2, object.y + 1, object.isActivated and 0x0 or 0xFFFFFF, object.text)
  43.     screen.drawImage(object.x, object.y, object.isActivated and Ch1Image or Ch2Image)
  44.     return object
  45. end
  46.  
  47. local function newChevronObject(x, y)
  48.     local object = GUI.object(x, y, 5, 3)
  49.  
  50.     object.draw = chevronDraw
  51.     object.isActivated = false
  52.     object.text = " "
  53.  
  54.     return object
  55. end
  56.  
  57. local function addChevron(x, y)
  58.     table.insert(workspace.chevrons, workspace.chevronsContainer:addChild(newChevronObject(x, y)))
  59. end
  60.  
  61. local function updateChevrons(state)
  62.     for i = 1, #workspace.chevrons do
  63.         workspace.chevrons[i].isActivated = state
  64.         if not state then workspace.chevrons[i].text = " " end
  65.     end
  66. end
  67.  
  68. local function updateButtons()
  69.     workspace.removeContactButton.disabled = #contacts == 0
  70.     workspace.connectContactButton.disabled = #contacts == 0
  71. end
  72.  
  73. local function update()
  74.     local stargateState, irisState, imagePath = stargate.stargateState(), stargate.irisState()
  75.     workspace.irisButton.text = irisState == "Closed" and "Open Iris" or "Close Iris"
  76.     workspace.connectionButton.text = stargateState == "Connected" and "Disconnect" or "Connect"
  77.     workspace.connectedToLabel.text = stargateState == "Connected" and "(Connected to " .. stargate.remoteAddress() .. ")" or "(Not connected)"
  78.    
  79.     if stargateState == "Connected" then
  80.         workspace.connectContactButton.disabled = true
  81.         workspace.messageContactButton.disabled = false
  82.  
  83.         if irisState == "Closed" then
  84.             imagePath = "OnOn.pic"
  85.         else
  86.             imagePath = "OnOff.pic"
  87.         end
  88.     else
  89.         workspace.connectContactButton.disabled = false
  90.         workspace.messageContactButton.disabled = true
  91.  
  92.         if irisState == "Closed" then
  93.             imagePath = "OffOn.pic"
  94.         else
  95.             imagePath = "OffOff.pic"
  96.         end
  97.     end
  98.  
  99.     updateButtons()
  100.     workspace.SGImage.image = image.load(resources .. imagePath)
  101. end
  102.  
  103. local function updateContacts()
  104.     workspace.contactsComboBox:clear()
  105.     if #contacts == 0 then
  106.         workspace.contactsComboBox:addItem("No contacts found")
  107.     else
  108.         for i = 1, #contacts do
  109.             workspace.contactsComboBox:addItem(contacts[i].name)
  110.         end
  111.     end
  112. end
  113.  
  114. local function newThing(x, y, width, height)
  115.     local object = GUI.object(x, y, width, height)
  116.    
  117.     object.draw = function(object)
  118.         local x, y = object.x + object.width - 1, math.floor(object.y + object.height / 2)
  119.         for i = object.y, object.y + object.height - 1 do
  120.             screen.drawText(x, i, 0xEEEEEE, "│")
  121.         end
  122.         for i = object.x, object.x + width - 1 do
  123.             screen.drawText(i, y, 0xEEEEEE, "─")
  124.         end
  125.         screen.drawText(x, y, 0xEEEEEE, "┤")
  126.     end
  127.  
  128.     return object
  129. end
  130.  
  131. local function dial(address)
  132.     local success, reason = stargate.dial(address)
  133.     if success then
  134.         workspace.fuelProgressBar.value = math.ceil(stargate.energyToDial(address) / stargate.energyAvailable() * 100)
  135.         workspace:draw()
  136.     else
  137.         GUI.alert("Failed to dial: " .. tostring(reason))
  138.     end
  139. end
  140.  
  141. ---------------------------------------------------------------------------------------------
  142.  
  143. local width, height = 32, 37
  144. local x, y = workspace.width - width - 3, math.floor(workspace.height / 2 - height / 2)
  145.  
  146. workspace:addChild(GUI.panel(1, 1, workspace.width, workspace.height, 0x1E1E1E))
  147.  
  148. workspace.SGImage = workspace:addChild(GUI.image(1, 1, image.load(resources .. "OffOff.pic")))
  149. workspace.SGImage.localX, workspace.SGImage.localY = math.floor((x - 2) / 2 - image.getWidth(workspace.SGImage.image) / 2), workspace.height - image.getHeight(workspace.SGImage.image) + 1
  150.  
  151. workspace.chevronsContainer = workspace:addChild(GUI.container(workspace.SGImage.localX, workspace.SGImage.localY, workspace.SGImage.width, workspace.SGImage.height))
  152. workspace.chevrons = {}
  153. addChevron(13, 30)
  154. addChevron(8, 17)
  155. addChevron(21, 6)
  156. addChevron(45, 1)
  157. addChevron(72, 6)
  158. addChevron(83, 17)
  159. addChevron(79, 30)
  160.  
  161. workspace:addChild(newThing(workspace.SGImage.localX + workspace.SGImage.width, y, workspace.width - workspace.SGImage.localX - workspace.SGImage.width - width - 7,  height))
  162.  
  163. workspace:addChild(GUI.label(x, y, width, 1, 0xEEEEEE, "Stargate " .. stargate.localAddress())):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP); y = y + 1
  164. workspace.connectedToLabel = workspace:addChild(GUI.label(x, y, width, 1, 0x555555, "(Not connected)")):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP); y = y + 2
  165. workspace.connectionButton = workspace:addChild(GUI.framedButton(x, y, width, 3, 0xEEEEEE, 0xEEEEEE, 0xBBBBBB, 0xBBBBBB, "Connect")); y = y + 3
  166. -- workspace.connectionButton.animated = false
  167. workspace.connectionButton.onTouch = function()
  168.     if stargate.stargateState() == "Idle" then
  169.         local container = GUI.addBackgroundContainer(workspace, true, true, "Connect")
  170.         local input = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xEEEEEE, 0x666666, 0x666666, 0xEEEEEE, 0x262626, contacts.last, "Type address here"))
  171.         input.onInputFinished = function()
  172.             if input.text then
  173.                 dial(input.text)
  174.                 contacts.last = input.text
  175.                 saveContacts()
  176.                 container:remove()
  177.  
  178.                 workspace:draw()
  179.             end
  180.         end
  181.  
  182.         container.panel.eventHandler = function(workspace, object, e1)
  183.             if e1 == "touch" then
  184.                 input.onInputFinished()
  185.             end
  186.         end
  187.  
  188.         workspace:draw()
  189.     else
  190.         stargate.disconnect()
  191.     end
  192. end
  193.  
  194. workspace.irisButton = workspace:addChild(GUI.framedButton(x, y, width, 3, 0xEEEEEE, 0xEEEEEE, 0xBBBBBB, 0xBBBBBB, "Open Iris")); y = y + 3
  195. workspace.irisButton.onTouch = function()
  196.     if stargate.irisState() == "Open" then
  197.         stargate.closeIris()
  198.     else
  199.         stargate.openIris()
  200.     end
  201. end
  202.  
  203. workspace.messageContactButton = workspace:addChild(GUI.framedButton(x, y, width, 3, 0xEEEEEE, 0xEEEEEE, 0xBBBBBB, 0xBBBBBB, "Message")); y = y + 4
  204. workspace.messageContactButton.onTouch = function()
  205.     local container = GUI.addBackgroundContainer(workspace, true, true, "Message")
  206.     local input = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xEEEEEE, 0x666666, 0x666666, 0xEEEEEE, 0x262626, nil, "Type message text here"))
  207.     input.onInputFinished = function()
  208.         if input.text then
  209.             container:remove()
  210.             stargate.sendMessage(input.text)
  211.  
  212.             workspace:draw()
  213.         end
  214.     end
  215.  
  216.     container.panel.eventHandler = function(workspace, object, e1)
  217.         if e1 == "touch" then
  218.             input.onInputFinished()
  219.         end
  220.     end
  221.  
  222.     workspace:draw()
  223. end
  224.  
  225. workspace:addChild(GUI.label(x, y, width, 1, 0xEEEEEE, "Contacts")):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP); y = y + 2
  226. workspace.contactsComboBox = workspace:addChild(GUI.comboBox(x, y, width, 3, 0x3C3C3C, 0xBBBBBB, 0x555555, 0x888888)); y = y + 4
  227.  
  228. workspace.connectContactButton = workspace:addChild(GUI.framedButton(x, y, width, 3, 0xEEEEEE, 0xEEEEEE, 0xBBBBBB, 0xBBBBBB, "Connect")); y = y + 3
  229. workspace.connectContactButton.onTouch = function()
  230.     dial(contacts[workspace.contactsComboBox.selectedItem].address)
  231. end
  232.  
  233. workspace.addContactButton = workspace:addChild(GUI.framedButton(x, y, width, 3, 0xEEEEEE, 0xEEEEEE, 0xBBBBBB, 0xBBBBBB, "Add contact")); y = y + 3
  234. workspace.addContactButton.onTouch = function()
  235.     local container = GUI.addBackgroundContainer(workspace, true, true, "Add contact")
  236.     local input1 = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xEEEEEE, 0x666666, 0x666666, 0xEEEEEE, 0x262626, nil, "Name"))
  237.     local input2 = container.layout:addChild(GUI.input(1, 1, 36, 3, 0xEEEEEE, 0x666666, 0x666666, 0xEEEEEE, 0x262626, contacts.last, "Address"))
  238.  
  239.     container.panel.eventHandler = function(workspace, object, e1)
  240.         if e1 == "touch" then
  241.             if input1.text and input2.text then
  242.                 local exists = false
  243.                 for i = 1, #contacts do
  244.                     if contacts[i].address == input2.text then
  245.                         exists = true
  246.                         break
  247.                     end
  248.                 end
  249.                 if not exists then
  250.                     table.insert(contacts, {name = input1.text, address = input2.text})
  251.                     updateContacts()
  252.                     saveContacts() 
  253.                     updateButtons()
  254.                 end
  255.  
  256.                 container:remove()
  257.                 workspace:draw()
  258.             end
  259.         end
  260.     end
  261.  
  262.     workspace:draw()
  263. end
  264.  
  265. workspace.removeContactButton = workspace:addChild(GUI.framedButton(x, y, width, 3, 0xEEEEEE, 0xEEEEEE, 0xBBBBBB, 0xBBBBBB, "Remove contact")); y = y + 4
  266. workspace.removeContactButton.onTouch = function()
  267.     if #contacts > 0 then
  268.         table.remove(contacts, workspace.contactsComboBox.selectedItem)
  269.         updateContacts()
  270.         saveContacts()
  271.         updateButtons()
  272.  
  273.         workspace:draw()
  274.     end
  275. end
  276.  
  277. workspace:addChild(GUI.label(x, y, width, 1, 0xEEEEEE, "Energy to dial")):setAlignment(GUI.ALIGNMENT_HORIZONTAL_CENTER, GUI.ALIGNMENT_VERTICAL_TOP); y = y + 2
  278. workspace.fuelProgressBar = workspace:addChild(GUI.progressBar(x, y, width, 0xBBBBBB, 0x0, 0xEEEEEE, 100, true, true, "", "%")); y = y + 3
  279. workspace.exitButton = workspace:addChild(GUI.framedButton(x, y, width, 3, 0xEEEEEE, 0xEEEEEE, 0xBBBBBB, 0xBBBBBB, "Exit")); y = y + 4
  280. workspace.exitButton.onTouch = function()
  281.     workspace:stop()
  282. end
  283.  
  284. workspace.eventHandler = function(workspace, object, e1, e2, e3, e4)
  285.     if e1 == "sgIrisStateChange" then
  286.         update()
  287.         workspace:draw()
  288.     elseif e1 == "sgStargateStateChange" then
  289.         if e3 == "Idle" or e3 == "Connected" then
  290.             update()
  291.             updateChevrons(e3 == "Connected")
  292.             workspace:draw()
  293.         end
  294.     elseif e1 == "sgChevronEngaged" then
  295.         if workspace.chevrons[e3] then
  296.             workspace.chevrons[e3].isActivated = true
  297.             workspace.chevrons[e3].text = e4
  298.             workspace:draw()
  299.         end
  300.     elseif e1 == "sgMessageReceived" then
  301.         GUI.alert(e3)
  302.     end
  303. end
  304.  
  305. loadContacts()
  306. updateContacts()
  307. update()
  308. updateChevrons(stargate.stargateState() == "Connected")
  309.  
  310. workspace:draw()
  311. workspace:start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement