Advertisement
Maxstripe

tear apart

Sep 25th, 2016
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local sg = component.stargate
  3. local gpu = component.gpu
  4. local keyboard = require("keyboard")
  5. local w, h = gpu.getResolution()
  6. local term = require("term")
  7. local event = require("event")
  8. local filesystem = require("filesystem")
  9.  
  10. local running = true
  11. local iris = false
  12.  
  13. --address tables
  14. characters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
  15. numbers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
  16.  
  17.  
  18. --[[
  19. function textField(x, y, lenx, leny)
  20.    local typing = true
  21.    local type = true
  22.    local character
  23.    local text = ""
  24.    local index = 0
  25.    while typing and index<=(lenx-1) do
  26.       gpu.set(x+index+1, y, "_")
  27.     local _,_,key,p1 = event.pull("key_down")
  28.     local char = keyboard.keys[p1]
  29.       index = index + 1
  30.   if (index==lenx) or (key==13) then
  31.       gpu.set(x+index, y, " ")
  32.       type = false
  33.     typing = false
  34.       gpu.set(x+index+1, y, " ")
  35.       for i=1,index-1 do
  36.         character = gpu.get(x+i, y)
  37.         text = text..character
  38.       end
  39.     end
  40.     if (key == 8) and (index > 1) then --backspace
  41.       index = index - 1
  42.       --text = string.sub(text, 1, index+1)
  43.       gpu.set(x+index+1, y, " ")
  44.       gpu.set(x+index, y, "_")
  45.       index=index-1
  46.     elseif (index >= 1) and (key ~= 13) then
  47.       if char == "lshift" or char == "rshift" or char == "back" or char == "enter" then
  48.       index=index-1
  49.     else
  50.       if keyboard.isShiftDown() then
  51.           character = string.upper(char)
  52.         else
  53.       character = char
  54.     end
  55.     if char == "space" then
  56.       character = " "
  57.     elseif char == "numpad0" then
  58.         character = "0"
  59.       elseif char == "numpad1" then
  60.         character = "1"
  61.       elseif char == "numpad2" then
  62.         character = "2"
  63.       elseif char == "numpad3" then
  64.         character = "3"
  65.       elseif char == "numpad4" then
  66.         character = "4"
  67.       elseif char == "numpad5" then
  68.         character = "5"
  69.       elseif char == "numpad6" then
  70.         character = "6"
  71.       elseif char == "numpad7" then
  72.         character = "7"
  73.       elseif char == "numpad8" then
  74.         character = "8"
  75.       elseif char == "numpad9" then
  76.         character = "9"
  77.     elseif char == "numpadsub" or char == "minus" then
  78.         character = "-"
  79.     elseif char == "period" then
  80.         character = "."
  81.    
  82.     end
  83.     if type then
  84.           gpu.set(x+index, y, character)
  85.         end
  86.       end
  87.     end
  88.   end
  89.   return text
  90. end
  91. ]]
  92. function removeLines(filename, startingLine, numLines)
  93.     local fp = io.open(filename, "r")
  94.     if fp == nil then return nil end
  95.  
  96.     content = {}
  97.     i = 1;
  98.     for line in fp:lines() do
  99.       if i < startingLine or i >= startingLine + numLines then
  100.       content[#content+1] = line
  101.     end
  102.     i = i + 1
  103.     end
  104.  
  105.     if i > startingLine and i < startingLine + numLines then return nil end
  106.  
  107.     fp:close()
  108.     fp = io.open(filename, "w")
  109.  
  110.     for i = 1, #content do
  111.     fp:write(string.format("%s\n", content[i]))
  112.     end
  113.  
  114.     fp:close()
  115. end
  116.  
  117. function wrap(str, limit)
  118.   if (str == nil) or (str == " ") then return nil end
  119.   local Lines, here, limit = {}, 1, limit or 72
  120.   Lines[1] = string.sub(str,1,str:find("(%s+)()(%S+)()")-1)
  121.  
  122.   str:gsub("(%s+)()(%S+)()",
  123.         function(sp, st, word, fi)
  124.           if fi-here > limit then
  125.                 here = st
  126.                 Lines[#Lines+1] = word
  127.           else Lines[#Lines] = Lines[#Lines].." "..word end
  128.         end)
  129.  
  130.   return Lines
  131. end
  132.  
  133. function tablelength(T)
  134.   local count = 0
  135.   for _ in pairs(T) do count = count + 1 end
  136.   return count
  137. end
  138.  
  139.   local currentPage = 1
  140.   local index = 0
  141.   local locations = {}
  142.   local addresses = {}
  143.   --local numLines = 0
  144.  
  145.   for location in string.gmatch(sg.getLocations(), '([^,]+)') do
  146.       table.insert(locations, location)
  147.   end
  148.  
  149.   local addressBookRunning = true
  150.   while addressBookRunning do
  151.    
  152.     --print(tablelength(locations))
  153.    
  154.     for iter = 1+((currentPage-1)*5), tablelength(locations) do
  155.       --numLines = numLines + 1
  156.       --print(iter..tablelength(locations))
  157.      
  158.       if (sg.getAddressForLocation(locations[iter]) ~= "None") then
  159.         index = index + 1
  160.        
  161.      
  162.         local coords = sg.getCoordinatesForLocation(locations[iter])
  163.         local atmosphere = sg.getAtmosphereForLocation(locations[iter])
  164.         local galaxy = sg.getGalaxyNameForLocation(locations[iter])
  165.         local address = sg.getAddressForLocation(locations[iter])
  166.        
  167.         gpu.set(110, 4+index*7, "# "..index+((currentPage-1)*5))
  168.         gpu.set(120, 4+index*7, locations[iter])
  169.         gpu.set(110, 5+index*7, "Additional symbols: ")
  170.         gpu.set(110, 6+index*7, "Atmosphere: "..atmosphere)
  171.         gpu.set(110, 7+index*7, "Galaxy: "..galaxy)
  172.        
  173.         table.insert(addresses, address)
  174.        
  175.         local j = 0
  176.         for i = 1,10 do
  177.           local symbol = address:sub(i,i)
  178.           if i<8 then
  179.             j = j + 1
  180.             gpu.set(10+(j-1)*15, 5+index*7, symbol)
  181.           elseif i==8 then
  182.             gpu.set(130, 5+index*7, symbol)
  183.           elseif i==9 then
  184.             gpu.set(132, 5+index*7, symbol)
  185.           end
  186.         end
  187.        
  188.         if index > 4 then
  189.           --currentPage = currentPage + 1
  190.           break
  191.         end
  192.        
  193.       end
  194.     end
  195.  
  196.     if override then return nil end
  197.  
  198.   --click handling
  199.     local clicking = true
  200.     while clicking do
  201.       local _,_,x,y = event.pull(1, "touch")
  202.       if x then
  203.         --exit
  204.         if ((x<w) and (x>w-5)) and ((y<2) and (y>0)) then
  205.           addressBookRunning = false
  206.           clicking = false
  207.           break
  208.         end
  209.       --next page
  210.         if ((x<153) and (x>149)) and ((y<h-2) and (y>h-4)) and (currentPage < 20) then
  211.           gpu.setBackground(0x000000)
  212.           gpu.fill(4, 10, w-10, 35, " ")
  213.           gpu.fill(70, h-2, 74, 1, " ")
  214.           gpu.setBackground(0x339D8F)
  215.           gpu.fill(70+(currentPage*4), h-2, 2, 1, " ")
  216.           currentPage = currentPage+1
  217.           index = 0
  218.           clicking = false
  219.         end
  220.       --prev. page
  221.         if ((x<68) and (x>64)) and ((y<h-2) and (y>h-4)) and (currentPage > 1) then
  222.           gpu.setBackground(0x000000)
  223.           gpu.fill(4, 10, w-10, 35, " ")
  224.           gpu.fill(70, h-2, 74, 1, " ")
  225.           currentPage = currentPage-1
  226.           gpu.setBackground(0x339D8F)
  227.           gpu.fill(70+((currentPage-1)*4), h-2, 2, 1, " ")
  228.           index = 0
  229.           clicking = false
  230.         end
  231.         for i=0,4 do
  232.           --dial
  233.           if ((x<153) and (x>143)) and ((y<(14+i*7)) and (y>(12+i*7))) then
  234.             addressBookRunning = false
  235.             clicking = false
  236.             ok, result = pcall(sg.dial, addresses[(i+1)+((currentPage-1)*5)])
  237.           end
  238.         end
  239.       end
  240.     end
  241.   end
  242.    
  243.  
  244.   resetGUI()
  245. end
  246.  
  247.  
  248. resetGUI()
  249.  
  250. --Main loop
  251. while running do
  252.   --Information
  253.   local state, engaged, direction = sg.stargateState()
  254.   local iris = sg.irisState()
  255.  
  256.  
  257.   --drawButtons()
  258.   gpu.setBackground(0x000000)
  259.   gpu.setForeground(0xFFFFFF)
  260.   --iris
  261.   gpu.set(14, 4, iris.."   ")
  262.   --energy available
  263.   local energy = sg.energyAvailable()
  264.   --if energy > 5000000 then
  265.   --  energy = 5000000
  266.   --end
  267.   energy = string.format("%d", energy/1000)
  268.   --gpu.set(8, 20, energy.."/5000 kSU   ")
  269.   gpu.set(8, 20, energy.." kSU        ")
  270.   --energy required
  271.   if (not (address == nil)) and (string.len(address) > 6) then
  272.     --gpu.set(8, 24, sg.energyToDial(address))
  273.   end
  274.  
  275.   --click handling
  276.   local _,_,x,y = event.pull(1, "touch")
  277.   if x then
  278.    
  279.     --address book
  280.     if ((x<30) and (x>4)) and ((y<29) and (y>27)) then
  281.       addressBook()
  282.     end
  283.  
  284.     --dialing
  285.   if ((x<30) and (x>4)) and ((y<11) and (y>9)) then
  286.       gpu.fill(5, 10, 26, 1, " ")
  287.       local typing = true
  288.       local type = true
  289.       local symbol
  290.     address = ""
  291.       local index = 0
  292.       while typing and index<=11 do
  293.         gpu.set(11+index+1, 10, "_")
  294.         local _,_,key = event.pull("key_down")
  295.         index = index + 1
  296.         if (key == 8) and (index > 1) then --backspace
  297.           index = index - 1
  298.           address = string.sub(address, 1, index+1)
  299.           gpu.set(11+index+1, 10, " ")
  300.           gpu.set(11+index, 10, "_")
  301.           index=index-1
  302.         elseif index >= 1 then
  303.           if ((key>47) and (key<58)) then
  304.             symbol = numbers[key-47]
  305.           end
  306.             if ((key>96) and (key<123)) then
  307.               symbol = characters[key-96]
  308.             end
  309.             if type then
  310.               if symbol ~= null then
  311.                 gpu.set(11+index, 10, symbol)
  312.               end
  313.             end
  314.             if index==4 then
  315.               index = index + 1
  316.               gpu.set(11+index, 10, "-")
  317.             end
  318.             if index==8 then
  319.               index = index + 1
  320.               gpu.set(11+index, 10, "-")
  321.             end
  322.             if index==11 then
  323.               type = false
  324.               gpu.set(11+index+1, 10, "_")
  325.               for i=1,11 do
  326.                 symbol = gpu.get(11+i, 10)
  327.                 if symbol == "-" then
  328.           address = address.."-"
  329.                 else
  330.                   address = address..symbol
  331.                 end
  332.               end
  333.               while true do
  334.                 local _,_,key = event.pull("key_down")
  335.                 if key == 13 then
  336.                   gpu.set(11+index+1, 10, " ")
  337.                   typing = false
  338.                   break
  339.          
  340.  
  341. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement