Ktlo

Stargate Interface Alphfa 0.1

Mar 22nd, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.62 KB | None | 0 0
  1. local component = component or require 'component'
  2. local unicode = unicode or require 'unicode'
  3. local computer = computer or require 'computer'
  4. local fs = require 'filesystem'
  5. local e = require 'event'
  6. local gpu, stargate = component.list("gpu")(), component.list("stargate")()
  7. local forceDialingAddress
  8. if not (gpu and stargate) then
  9.     print("No stargate or gpu!")
  10.     return 1
  11. end
  12. local path = os.getenv('PWD')..'/stargate/'
  13. do
  14.     fs.makeDirectory(path)
  15.     local file = io.open(path..'addresses')
  16.     if not file then
  17.         file = io.open(path..'addresses', 'w')
  18.     end
  19.     file:close()
  20.     local file = io.open(path..'history')
  21.     if not file then
  22.         file = io.open(path..'history', 'w')
  23.     end
  24.     file:close()
  25. end
  26. local x = 71/2-9
  27. local y = 26/2-7
  28. gpu, stargate = component.proxy(gpu), component.proxy(stargate)
  29. local backcolor, forecolor, resX, resY = gpu.getBackground(), gpu.getForeground(), gpu.getResolution()
  30. local back = 0x2D2D2D
  31. gpu.setResolution(71, 26)
  32.  
  33. --GUI
  34. local button_meta = {}
  35. local button = { -- Кнопка
  36.     color = 0xFFDBC0;
  37.     backcolor = back;
  38.     textColor = 0;
  39.     active = 0xFF9200;
  40.     inactive = 0x4B4B4B;
  41.     text = "";
  42.     x = 1;
  43.     y = 1;
  44.     w = 3;
  45.     h = 1;
  46.     New = function(self, t)
  47.         return setmetatable(t, button_meta)
  48.     end;
  49.     Draw = function(self, state)
  50.         local x, y, w, h, text = self.x, self.y, self.w, self.h, self.text
  51.         gpu.setForeground(self.textColor)
  52.         if state == 0 then
  53.             gpu.setBackground(self.color)
  54.         elseif state == 1 then
  55.             gpu.setBackground(self.active)
  56.         elseif state == -1 then
  57.             gpu.setBackground(self.inactive)
  58.         end
  59.         gpu.fill(x, y, w, h, ' ')
  60.         gpu.set(x+(w-#text)/2, y+h/2, text)
  61.         gpu.setForeground(self.backcolor)
  62.         gpu.set(x, y, '▀')
  63.         gpu.set(x+w-1, y, '▀')
  64.         gpu.set(x, y+h-1, '▄')
  65.         gpu.set(x+w-1, y+h-1, '▄')
  66.     end;
  67.     Detect = function(self, event, action)
  68.         if event[1] == action
  69.         and event[3] >= self.x and event[3] <= self.x+self.w-1
  70.         and event[4] >= self.y and event[4] <= self.y+self.h-1 then
  71.             self:Draw(1)
  72.             os.sleep(0.2)
  73.             self:Draw(0)
  74.             return true
  75.         else
  76.             return false
  77.         end
  78.     end;
  79. }
  80. button_meta.__index = button
  81. local inputWindow, outputWindow, drawGUI
  82. do -- Диалоговое окно
  83. local function drawWindow(color, backcolor, name)
  84.     gpu.setBackground(backcolor)
  85.     gpu.setForeground(0xFFFFFF)
  86.     gpu.fill(71/2-15, 26/2-5, 31, 11, " ")
  87.     gpu.set((71-unicode.len(name))/2+1, 26/2-4, name)
  88.     gpu.setBackground(color)
  89.     gpu.fill(71/2-14, 26/2-2, 29, 5, " ")
  90. end
  91. local function drawAddressBook(adrss, n)
  92.     gpu.setBackground(0x664900)
  93.     gpu.setForeground(0xFFFFFF)
  94.     gpu.fill(71/2+15, 26/2-5, 20, 11, ' ')
  95.     gpu.set(71/2+19, 26/2-5, "Addresses")
  96.     adrss:Draw(0)
  97.     gpu.setForeground(0)
  98.     for i = n, #adrss > 8+n and 8+n or #adrss do
  99.         if (i-n)%2 == 1 then
  100.             gpu.setBackground(0xCC9240)
  101.         else
  102.             gpu.setBackground(0xFFB680)
  103.         end
  104.         local string = (math.floor(i)..'. '..adrss[i][1]):sub(1, 18)
  105.         string = string..(' '):rep(18-#string)
  106.         gpu.set(71/2+16, 26/2-4+i-n, string)
  107.     end
  108. end
  109. function inputWindow(name, color, backcolor, adrss)
  110.     drawWindow(color, backcolor, name)
  111.     local ok = button:New{ x = 25, y = 16, w = 8, h = 1, text = "OK", backcolor = backcolor, color = 0x009200 }
  112.     local force = button:New{ x = 25, y = 18, w = 8, h = 1, text = "Force", backcolor = backcolor, color = 0xFFDB00 }
  113.     local cancel = button:New{ x = 38, y = 17, w = 8, h = 1, text = "Cancel", backcolor = backcolor, color = 0xFF2400 }
  114.     local n = 1
  115.     if adrss then
  116.         local file = io.open(path..'addresses')
  117.         adrss = button:New{ x = 55, y = 18, w = 10, h = 1, text = "Add New", color = 0x009200, backcolor = 0x664900 }
  118.         for line in file:lines() do
  119.             adrss[#adrss+1] = { line:match('(.+)=(.+)') }
  120.         end
  121.         file:close()
  122.         drawAddressBook(adrss, n)
  123.     end
  124.     ok:Draw(0)
  125.     cancel:Draw(0)
  126.     force:Draw(0)
  127.     gpu.setForeground(0xFFFFFF)
  128.     gpu.setBackground(0)
  129.     gpu.set(71/2-5, 26/2, '____-___-__')
  130.     local b = ''
  131.     while true do
  132.         local event, adr, x, y, o, p = computer.pullSignal()
  133.         if event == 'key_down' then
  134.             if y == 14 then
  135.                 if #b > 0 then
  136.                     b = b:sub(0, #b-1)
  137.                 end
  138.             elseif y == 28 then
  139.                 drawGUI()
  140.                 local a = table.concat({ b:match("(....)(...)(..)") }, '-')
  141.                 if #a == 0 then a = nil end
  142.                 return a or table.concat({ b:match("(....)(...)") }, '-')..stargate.localAddress():sub(8)
  143.             elseif x < 127 and x > 47 and x ~= 61 then
  144.                 b = (b..string.char(x):upper()):sub(1, 9)
  145.             end
  146.         elseif event == 'touch' then
  147.             if ok:Detect({event, adr, x, y}, 'touch') then
  148.                 drawGUI()
  149.                 local a = table.concat({ b:match("(....)(...)(..)") }, '-')
  150.                 if #a == 0 then a = nil end
  151.                 return a or table.concat({ b:match("(....)(...)") }, '-')..stargate.localAddress():sub(8)
  152.             elseif cancel:Detect({event, adr, x, y}, 'touch') then
  153.                 drawGUI()
  154.                 return nil
  155.             elseif adrss:Detect({event, adr, x, y}, 'touch') then
  156.                 gpu.setForeground(0xFFFFFF)
  157.                 gpu.setBackground(0)
  158.                
  159.             elseif x >= 51 and x <= 69 and y >= 9 and y <= 9+#adrss-n then
  160.                 b = adrss[y-9+n][2]
  161.             elseif force:Detect({event, adr, x, y}, 'touch') then
  162.                 drawGUI()
  163.                 local a = table.concat({ b:match("(....)(...)(..)") }, '-')
  164.                 if #a == 0 then a = nil end
  165.                 forceDialingAddress = a or table.concat({ b:match("(....)(...)") }, '-')..stargate.localAddress():sub(8)
  166.                 return forceDialingAddress
  167.             end
  168.         elseif event == 'scroll' and  x >= 51 and x <= 69 and y >= 9 and y <= 18 then
  169.             n = n + o
  170.             if n < 1 then n = 1 end
  171.             if n >= #adrss then n = #adrss end
  172.             drawAddressBook(adrss, n)
  173.             gpu.setForeground(0xFFFFFF)
  174.             gpu.setBackground(0)
  175.         elseif event:sub(1, 2) == 'sg' then
  176.             computer.pushSignal(event, adr, x, y, o, p)
  177.             drawGUI()
  178.             return nil
  179.         end
  180.         gpu.set(71/2-5, 26/2, b:sub(1, 4)..('_'):rep(4-#b:sub(1, 4)))
  181.         gpu.set(71/2, 26/2, b:sub(5, 7)..('_'):rep(3-#b:sub(5, 7)))
  182.         gpu.set(71/2+4, 26/2, b:sub(8, 9)..('_'):rep(2-#b:sub(8, 9)))
  183.     end
  184. end
  185. function outputWindow(name, color, backcolor, text)
  186.     local ok = button:New{ x = 71/2-5, y = 26/2+4, w = 10, h = 1, text = "Continue", backcolor = backcolor, color = 0x009200 }
  187.     drawWindow(color, backcolor, name)
  188.     --[[(71/2-14, 26/2-2, 29, 5)]]--
  189.     gpu.setForeground(0)
  190.     for i = 1, (unicode.len(text)/29+1) do
  191.         gpu.set(71/2-14, 10+i, text:sub((i-1)*29+1, i*29))
  192.     end
  193.     ok:Draw(0)
  194.     while true do
  195.         local event = { computer.pullSignal() }
  196.         if ok:Detect(event, 'touch') then
  197.             break
  198.         elseif event[1] == 'key_down' then
  199.             break
  200.         elseif event[1]:sub(1, 2) == 'sg' then
  201.             computer.pushSignal(table.unpack(event))
  202.             break
  203.         end
  204.     end
  205.     drawGUI()
  206. end
  207. end
  208. --EndGUI
  209. local drawSG
  210. do
  211. local meta = {}
  212. function draw(x_y, color1, color2, text)
  213.     local x = x_y[1]
  214.     local y = x_y[2]
  215.     if color2 then
  216.         gpu.setBackground(color2)
  217.     end
  218.     if color1 then
  219.         gpu.setForeground(color1)
  220.     end
  221.     gpu.set(x, y, text)
  222.     return setmetatable({x+unicode.len(text), y}, meta)
  223. end
  224. meta.__index = {draw = draw}
  225. meta.__add = function(self, n)
  226.     self[1] = self[1] + n
  227.     return setmetatable(self, meta)
  228. end
  229.  
  230. function drawSG(color, backcolor)
  231.     gpu.setBackground(backcolor)
  232.     gpu.setForeground(0x33B6C0)
  233.     gpu.set(x, y, "▄▄█████▄▄")
  234.     draw({x-4, y+1}, nil, nil, "▄▄██")
  235.         :draw(nil, color, "▀▀▀▀▀▀▀▀▀")
  236.         :draw(nil, backcolor, "██▄▄");
  237.     draw({x-7, y+2}, nil, nil, "▄██")
  238.         :draw(nil, color, "▀▀")
  239.         :draw(0xC3C3C3, color, "▄▄█")
  240.         :draw(nil, backcolor, "▀▀▀▀▀▀▀█")
  241.         :draw(0xC3C3C3, color, "▄▄")
  242.         :draw(0x33B6C0, color, "▀▀")
  243.         :draw(nil, backcolor, "██▄");
  244.     (draw({x-8, y+3}, nil, color, "██▀")
  245.         :draw(0xC3C3C3, color, "▄█")
  246.         :draw(nil, backcolor, "▀▀")+11)
  247.         :draw(nil, nil, "▀▀█")
  248.         :draw(nil, color, "▄")
  249.         :draw(0x33B6C0, nil, "▀██");
  250.     (draw({x-9, y+4}, nil, color, "██ ")
  251.         :draw(0xC3C3C3, backcolor, "█▀")+17)
  252.         :draw(nil, nil, "▀█")
  253.         :draw(0x33B6C0, color, " ██");
  254.     (draw({x-10, y+5}, nil, nil, "██ ")
  255.         :draw(0xC3C3C3, backcolor, "█▀")+19)
  256.         :draw(nil, nil, "▀█")
  257.         :draw(0x33B6C0, color, " ██");
  258.     (draw({x-11, y+6}, nil, nil, "██ ")
  259.         :draw(0xC3C3C3, backcolor, "█▀")+21)
  260.         :draw(nil, nil, "▀█")
  261.         :draw(0x33B6C0, color, " ██");
  262.     (draw({x-12, y+7}, nil, nil, "███ ")
  263.         :draw(0xC3C3C3, backcolor, "█")+23)
  264.         :draw(nil, nil, "█")
  265.         :draw(0x33B6C0, color, " ███");
  266.     (draw({x-12, y+8}, nil, nil, "███ ")
  267.         :draw(0xC3C3C3, backcolor, "█")+23)
  268.         :draw(nil, nil, "█")
  269.         :draw(0x33B6C0, color, " ███");
  270.     (draw({x-12, y+9}, nil, backcolor, "▀██")
  271.         :draw(0xC3C3C3, color, " █")+23)
  272.         :draw(nil, nil, "█ ")
  273.         :draw(0x33B6C0, backcolor, "██▀");
  274.     (draw({x-11, y+10}, nil, nil, "▀█")
  275.         :draw(nil, color, "▄")
  276.         :draw(0xC3C3C3, nil, "▀█")+21)
  277.         :draw(nil, nil, "█▀")
  278.         :draw(0x33B6C0, nil, "▄")
  279.         :draw(nil, backcolor, "█▀");
  280.     (draw({x-10, y+11}, nil, nil, "▀█")
  281.         :draw(nil, color, "▄")
  282.         :draw(0xC3C3C3, nil, "▀█")+19)
  283.         :draw(nil, nil, "█▀")
  284.         :draw(0x33B6C0, nil, "▄")
  285.         :draw(nil, backcolor, "█▀");
  286.     (draw({x-9, y+12}, nil, nil, "▀█")
  287.         :draw(nil, color, "▄")
  288.         :draw(0xC3C3C3, nil, "▀█")
  289.         :draw(nil, backcolor, "▄")+15)
  290.         :draw(nil, nil, "▄")
  291.         :draw(nil, color, "█▀")
  292.         :draw(0x33B6C0, nil, "▄")
  293.         :draw(nil, backcolor, "█▀");
  294.     (draw({x-8, y+13}, nil, nil, "▀██")
  295.         :draw(nil, color, "▄")
  296.         :draw(0xC3C3C3, nil, "▀▀█")
  297.         :draw(nil, backcolor, "▄▄")+7)
  298.         :draw(nil, nil, "▄▄")
  299.         :draw(nil, color, "█▀▀")
  300.         :draw(0x33B6C0, nil, "▄")
  301.         :draw(nil, backcolor, "██▀");
  302.     draw({x-6, y+14}, nil, nil, "▀▀██")
  303.         :draw(nil, color, "▄▄")
  304.         :draw(0xC3C3C3, nil, "▀▀▀▀▀▀▀▀▀")
  305.         :draw(0x33B6C0, nil, "▄▄")
  306.         :draw(nil, backcolor, "██▀▀");
  307.     gpu.setBackground(backcolor)
  308.     gpu.setForeground(0x33B6C0)
  309.     gpu.set(x-2, y+15, "▀▀█████████▀▀")
  310.     gpu.set(x+2, y+16, "▀▀▀▀▀")
  311.     return x, y
  312. end
  313. end
  314.  
  315. local function drawChevron(back, color, char, i)
  316.     i=i/2
  317.     local a = 2*math.pi/9*i
  318.     local r = math.sin(a)
  319.     local x, y = r*math.cos(a)*40+x+4, r*r*20+y-2
  320.     gpu.setForeground(color)
  321.     gpu.setBackground(back)
  322.     gpu.set(x-2, y-1, "┌───┐")
  323.     gpu.set(x-2, y,   "|   |")
  324.     gpu.set(x-2, y+1, "└───┘")
  325.     gpu.setForeground(0xFFFFFF)
  326.     gpu.set(x, y, char)
  327. end
  328.  
  329. local function setState(state)
  330.     local string = "State: "..state
  331.     gpu.setBackground(back)
  332.     gpu.setForeground(0xFFFFFF)
  333.     gpu.fill(x-7, y+8, 23, 1, ' ')
  334.     gpu.set(x-#string/2+4, y+8, string)
  335. end
  336.  
  337. local function drawEnergyCell()
  338.     local amoutEU = 1001047
  339.     local nowEU = stargate.energyAvailable()*20
  340.     --if nowEU < amoutEU/3 then
  341.         gpu.setBackground(0xFF0000)
  342.         gpu.fill(1, 1, 3, 26*nowEU/amoutEU, ' ')
  343.     --elseif nowEU < amoutEU/3 then
  344.     --  gpu.setBackground(0xFF0000)
  345.     --  gpu.fill(1, 1, 3, 26*nowEU/amoutEU, ' ')
  346.     --end
  347. end
  348.  
  349. local function updateHistory(address, type)
  350.     address = address..type
  351.     local file = io.open(path..'history')
  352.     local history = {}
  353.     local i = 0
  354.     local n
  355.     for line in file:lines() do
  356.         i = i + 1
  357.         history[i] = line
  358.         if line == address then
  359.             n = i
  360.         end
  361.     end
  362.     file:close()
  363.     if n then
  364.         table.insert(history, 1, table.remove(history, n))
  365.     else
  366.         table.insert(history, 1, address)
  367.     end
  368.     file = io.open(path..'history', 'w')
  369.     for i = 1, #history do
  370.         file:write(history[i], '\n')
  371.     end
  372.     file:close()
  373. end
  374.  
  375. local function drawAnotherGUI(list, b, n, a)
  376.     gpu.setBackground(0xFFB680)
  377.     gpu.fill(1, 1, 71, 26, ' ')
  378.     for i = 1, #list do
  379.         gpu.set()
  380.     end
  381. end
  382.  
  383. local Iris = button:New{ x = 55, y = 5, w = 17, h = 3, text = "Iris", color = stargate.irisState() == 'Open' and 0x009200 or 0xFF2400 }
  384. local Dial = button:New{ x = 55, y = 9, w = 17, h = 3, text = "Dial/Disconnect" }
  385. local History = button:New{ x = 55, y = 13, w = 17, h = 3, text = "History" }
  386. function drawGUI()
  387.     gpu.setBackground(back)
  388.     gpu.setForeground(0xA5A5A5)
  389.     gpu.fill(1, 1, 71, 26, ' ')
  390.     drawSG(0x004980, back)
  391.     for i = 1, 9 do
  392.         drawChevron(back, 0xFFFFFF, ' ', i)
  393.     end
  394.     setState(stargate.stargateState())
  395.     gpu.set(x-1, y+7, table.concat({ stargate.localAddress():match("(....)(...)(..)") }, '—'))
  396.     if stargate.irisState() == 'Offline' then
  397.         Iris:Draw(-1)
  398.     else
  399.         Iris:Draw(0)
  400.     end
  401.     Dial:Draw(0)
  402.     History:Draw(0)
  403.     --drawEnergyCell()
  404. end
  405. drawGUI()
  406. local exit
  407. --stargate.dial("x95vfushr")
  408. while not exit do
  409.     local event = { computer.pullSignal(0.3) }
  410.     if event[1] == 'touch' then
  411.         if event[3] == 71 and event[4] == 1 then
  412.             exit = 1
  413.         end
  414.         local state = stargate.irisState()
  415.         if state ~= 'Offline' and Iris:Detect(event, 'touch') then
  416.             if state == 'Open' then
  417.                 Iris.color = 0x009200
  418.                 stargate.closeIris()
  419.                 Iris:Draw()
  420.             elseif state == 'Closed' then
  421.                 Iris.color = 0xFF2400
  422.                 stargate.openIris()
  423.                 Iris:Draw()
  424.             end
  425.         elseif Dial:Detect(event, 'touch') then
  426.             if stargate.stargateState() == 'Idle' then
  427.                 local string = inputWindow("Dial", 0x969696, 0x80, true)
  428.                 if string then
  429.                     local ok, err = pcall(stargate.dial, string)
  430.                     if not ok then
  431.                         outputWindow("Dial Error", 0x969696, 0x80, err)
  432.                     end
  433.                 end
  434.             else
  435.                 stargate.disconnect()
  436.                 forceDialingAddress = nil
  437.             end
  438.         elseif History:Detect(event, 'touch') then
  439.             drawAnotherGUI()
  440.         end
  441.     elseif event[1] == 'sgDialIn' then
  442.         local address = stargate.remoteAddress()
  443.         updateHistory(address, '+')
  444.         drawSG(0xCC4900, back)
  445.         gpu.setForeground(0xFFFFFF)
  446.         gpu.set(x-1, y+9, table.concat({ address:match("(....)(...)(..)") }, '—'))
  447.         gpu.set(x-5, y+19, 'Incoming Connection')
  448.     elseif event[1] == 'sgDialOut' then
  449.         local address = stargate.remoteAddress()
  450.         updateHistory(address, '-')
  451.         drawSG(0xCC4900, back)
  452.         gpu.setForeground(0xFFFFFF)
  453.         gpu.set(x-1, y+9, table.concat({ address:match("(....)(...)(..)") }, '—'))
  454.         gpu.set(x-6, y+19, 'Outcoming Connection')
  455.     elseif event[1] == 'sgStargateStateChange' then
  456.         if event[3] == 'Closing' then
  457.             for i = 1, 9 do
  458.                 drawChevron(back, 0xFF0092, '╳', i)
  459.             end
  460.             gpu.setForeground(0xFFFFFF)
  461.             gpu.fill(x-1, y+9, 11, 1, ' ')
  462.             gpu.fill(x-6, y+19, 20, 1, ' ')
  463.         elseif event[3] == 'Idle' then
  464.             drawSG(0x004980, back)
  465.             for i = 1, 9 do
  466.                 drawChevron(back, 0xFFFFFF, ' ', i)
  467.             end
  468.             if forceDialingAddress then
  469.                 local ok, err = pcall(stargate.dial, forceDialingAddress)
  470.                 if not ok then
  471.                     forceDialingAddress = nil
  472.                     outputWindow("Force Connection Error", 0x969696, 0x80, err)
  473.                 end
  474.             end
  475.         end
  476.         setState(event[3])
  477.     elseif event[1] == 'sgChevronEngaged' then
  478.         if event[3] > 7 then
  479.             event[3] = event[3] - 4
  480.         elseif event[3] > 3 then
  481.             event[3] = event[3] + 2
  482.         end
  483.         drawChevron(back, 0xFF9200, event[4], event[3])
  484.     elseif event[3] == 'sgMessageReceived' then
  485.        
  486.     end
  487. end
  488. return exit
Advertisement
Add Comment
Please, Sign In to add comment