Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = component or require 'component'
- local unicode = unicode or require 'unicode'
- local computer = computer or require 'computer'
- local fs = require 'filesystem'
- local e = require 'event'
- local gpu, stargate = component.list("gpu")(), component.list("stargate")()
- local forceDialingAddress
- if not (gpu and stargate) then
- print("No stargate or gpu!")
- return 1
- end
- local path = os.getenv('PWD')..'/stargate/'
- do
- fs.makeDirectory(path)
- local file = io.open(path..'addresses')
- if not file then
- file = io.open(path..'addresses', 'w')
- end
- file:close()
- local file = io.open(path..'history')
- if not file then
- file = io.open(path..'history', 'w')
- end
- file:close()
- end
- local x = 71/2-9
- local y = 26/2-7
- gpu, stargate = component.proxy(gpu), component.proxy(stargate)
- local backcolor, forecolor, resX, resY = gpu.getBackground(), gpu.getForeground(), gpu.getResolution()
- local back = 0x2D2D2D
- gpu.setResolution(71, 26)
- --GUI
- local button_meta = {}
- local button = { -- Кнопка
- color = 0xFFDBC0;
- backcolor = back;
- textColor = 0;
- active = 0xFF9200;
- inactive = 0x4B4B4B;
- text = "";
- x = 1;
- y = 1;
- w = 3;
- h = 1;
- New = function(self, t)
- return setmetatable(t, button_meta)
- end;
- Draw = function(self, state)
- local x, y, w, h, text = self.x, self.y, self.w, self.h, self.text
- gpu.setForeground(self.textColor)
- if state == 0 then
- gpu.setBackground(self.color)
- elseif state == 1 then
- gpu.setBackground(self.active)
- elseif state == -1 then
- gpu.setBackground(self.inactive)
- end
- gpu.fill(x, y, w, h, ' ')
- gpu.set(x+(w-#text)/2, y+h/2, text)
- gpu.setForeground(self.backcolor)
- gpu.set(x, y, '▀')
- gpu.set(x+w-1, y, '▀')
- gpu.set(x, y+h-1, '▄')
- gpu.set(x+w-1, y+h-1, '▄')
- end;
- Detect = function(self, event, action)
- if event[1] == action
- and event[3] >= self.x and event[3] <= self.x+self.w-1
- and event[4] >= self.y and event[4] <= self.y+self.h-1 then
- self:Draw(1)
- os.sleep(0.2)
- self:Draw(0)
- return true
- else
- return false
- end
- end;
- }
- button_meta.__index = button
- local inputWindow, outputWindow, drawGUI
- do -- Диалоговое окно
- local function drawWindow(color, backcolor, name)
- gpu.setBackground(backcolor)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(71/2-15, 26/2-5, 31, 11, " ")
- gpu.set((71-unicode.len(name))/2+1, 26/2-4, name)
- gpu.setBackground(color)
- gpu.fill(71/2-14, 26/2-2, 29, 5, " ")
- end
- local function drawAddressBook(adrss, n)
- gpu.setBackground(0x664900)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(71/2+15, 26/2-5, 20, 11, ' ')
- gpu.set(71/2+19, 26/2-5, "Addresses")
- adrss:Draw(0)
- gpu.setForeground(0)
- for i = n, #adrss > 8+n and 8+n or #adrss do
- if (i-n)%2 == 1 then
- gpu.setBackground(0xCC9240)
- else
- gpu.setBackground(0xFFB680)
- end
- local string = (math.floor(i)..'. '..adrss[i][1]):sub(1, 18)
- string = string..(' '):rep(18-#string)
- gpu.set(71/2+16, 26/2-4+i-n, string)
- end
- end
- function inputWindow(name, color, backcolor, adrss)
- drawWindow(color, backcolor, name)
- local ok = button:New{ x = 25, y = 16, w = 8, h = 1, text = "OK", backcolor = backcolor, color = 0x009200 }
- local force = button:New{ x = 25, y = 18, w = 8, h = 1, text = "Force", backcolor = backcolor, color = 0xFFDB00 }
- local cancel = button:New{ x = 38, y = 17, w = 8, h = 1, text = "Cancel", backcolor = backcolor, color = 0xFF2400 }
- local n = 1
- if adrss then
- local file = io.open(path..'addresses')
- adrss = button:New{ x = 55, y = 18, w = 10, h = 1, text = "Add New", color = 0x009200, backcolor = 0x664900 }
- for line in file:lines() do
- adrss[#adrss+1] = { line:match('(.+)=(.+)') }
- end
- file:close()
- drawAddressBook(adrss, n)
- end
- ok:Draw(0)
- cancel:Draw(0)
- force:Draw(0)
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0)
- gpu.set(71/2-5, 26/2, '____-___-__')
- local b = ''
- while true do
- local event, adr, x, y, o, p = computer.pullSignal()
- if event == 'key_down' then
- if y == 14 then
- if #b > 0 then
- b = b:sub(0, #b-1)
- end
- elseif y == 28 then
- drawGUI()
- local a = table.concat({ b:match("(....)(...)(..)") }, '-')
- if #a == 0 then a = nil end
- return a or table.concat({ b:match("(....)(...)") }, '-')..stargate.localAddress():sub(8)
- elseif x < 127 and x > 47 and x ~= 61 then
- b = (b..string.char(x):upper()):sub(1, 9)
- end
- elseif event == 'touch' then
- if ok:Detect({event, adr, x, y}, 'touch') then
- drawGUI()
- local a = table.concat({ b:match("(....)(...)(..)") }, '-')
- if #a == 0 then a = nil end
- return a or table.concat({ b:match("(....)(...)") }, '-')..stargate.localAddress():sub(8)
- elseif cancel:Detect({event, adr, x, y}, 'touch') then
- drawGUI()
- return nil
- elseif adrss:Detect({event, adr, x, y}, 'touch') then
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0)
- elseif x >= 51 and x <= 69 and y >= 9 and y <= 9+#adrss-n then
- b = adrss[y-9+n][2]
- elseif force:Detect({event, adr, x, y}, 'touch') then
- drawGUI()
- local a = table.concat({ b:match("(....)(...)(..)") }, '-')
- if #a == 0 then a = nil end
- forceDialingAddress = a or table.concat({ b:match("(....)(...)") }, '-')..stargate.localAddress():sub(8)
- return forceDialingAddress
- end
- elseif event == 'scroll' and x >= 51 and x <= 69 and y >= 9 and y <= 18 then
- n = n + o
- if n < 1 then n = 1 end
- if n >= #adrss then n = #adrss end
- drawAddressBook(adrss, n)
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0)
- elseif event:sub(1, 2) == 'sg' then
- computer.pushSignal(event, adr, x, y, o, p)
- drawGUI()
- return nil
- end
- gpu.set(71/2-5, 26/2, b:sub(1, 4)..('_'):rep(4-#b:sub(1, 4)))
- gpu.set(71/2, 26/2, b:sub(5, 7)..('_'):rep(3-#b:sub(5, 7)))
- gpu.set(71/2+4, 26/2, b:sub(8, 9)..('_'):rep(2-#b:sub(8, 9)))
- end
- end
- function outputWindow(name, color, backcolor, text)
- local ok = button:New{ x = 71/2-5, y = 26/2+4, w = 10, h = 1, text = "Continue", backcolor = backcolor, color = 0x009200 }
- drawWindow(color, backcolor, name)
- --[[(71/2-14, 26/2-2, 29, 5)]]--
- gpu.setForeground(0)
- for i = 1, (unicode.len(text)/29+1) do
- gpu.set(71/2-14, 10+i, text:sub((i-1)*29+1, i*29))
- end
- ok:Draw(0)
- while true do
- local event = { computer.pullSignal() }
- if ok:Detect(event, 'touch') then
- break
- elseif event[1] == 'key_down' then
- break
- elseif event[1]:sub(1, 2) == 'sg' then
- computer.pushSignal(table.unpack(event))
- break
- end
- end
- drawGUI()
- end
- end
- --EndGUI
- local drawSG
- do
- local meta = {}
- function draw(x_y, color1, color2, text)
- local x = x_y[1]
- local y = x_y[2]
- if color2 then
- gpu.setBackground(color2)
- end
- if color1 then
- gpu.setForeground(color1)
- end
- gpu.set(x, y, text)
- return setmetatable({x+unicode.len(text), y}, meta)
- end
- meta.__index = {draw = draw}
- meta.__add = function(self, n)
- self[1] = self[1] + n
- return setmetatable(self, meta)
- end
- function drawSG(color, backcolor)
- gpu.setBackground(backcolor)
- gpu.setForeground(0x33B6C0)
- gpu.set(x, y, "▄▄█████▄▄")
- draw({x-4, y+1}, nil, nil, "▄▄██")
- :draw(nil, color, "▀▀▀▀▀▀▀▀▀")
- :draw(nil, backcolor, "██▄▄");
- draw({x-7, y+2}, nil, nil, "▄██")
- :draw(nil, color, "▀▀")
- :draw(0xC3C3C3, color, "▄▄█")
- :draw(nil, backcolor, "▀▀▀▀▀▀▀█")
- :draw(0xC3C3C3, color, "▄▄")
- :draw(0x33B6C0, color, "▀▀")
- :draw(nil, backcolor, "██▄");
- (draw({x-8, y+3}, nil, color, "██▀")
- :draw(0xC3C3C3, color, "▄█")
- :draw(nil, backcolor, "▀▀")+11)
- :draw(nil, nil, "▀▀█")
- :draw(nil, color, "▄")
- :draw(0x33B6C0, nil, "▀██");
- (draw({x-9, y+4}, nil, color, "██ ")
- :draw(0xC3C3C3, backcolor, "█▀")+17)
- :draw(nil, nil, "▀█")
- :draw(0x33B6C0, color, " ██");
- (draw({x-10, y+5}, nil, nil, "██ ")
- :draw(0xC3C3C3, backcolor, "█▀")+19)
- :draw(nil, nil, "▀█")
- :draw(0x33B6C0, color, " ██");
- (draw({x-11, y+6}, nil, nil, "██ ")
- :draw(0xC3C3C3, backcolor, "█▀")+21)
- :draw(nil, nil, "▀█")
- :draw(0x33B6C0, color, " ██");
- (draw({x-12, y+7}, nil, nil, "███ ")
- :draw(0xC3C3C3, backcolor, "█")+23)
- :draw(nil, nil, "█")
- :draw(0x33B6C0, color, " ███");
- (draw({x-12, y+8}, nil, nil, "███ ")
- :draw(0xC3C3C3, backcolor, "█")+23)
- :draw(nil, nil, "█")
- :draw(0x33B6C0, color, " ███");
- (draw({x-12, y+9}, nil, backcolor, "▀██")
- :draw(0xC3C3C3, color, " █")+23)
- :draw(nil, nil, "█ ")
- :draw(0x33B6C0, backcolor, "██▀");
- (draw({x-11, y+10}, nil, nil, "▀█")
- :draw(nil, color, "▄")
- :draw(0xC3C3C3, nil, "▀█")+21)
- :draw(nil, nil, "█▀")
- :draw(0x33B6C0, nil, "▄")
- :draw(nil, backcolor, "█▀");
- (draw({x-10, y+11}, nil, nil, "▀█")
- :draw(nil, color, "▄")
- :draw(0xC3C3C3, nil, "▀█")+19)
- :draw(nil, nil, "█▀")
- :draw(0x33B6C0, nil, "▄")
- :draw(nil, backcolor, "█▀");
- (draw({x-9, y+12}, nil, nil, "▀█")
- :draw(nil, color, "▄")
- :draw(0xC3C3C3, nil, "▀█")
- :draw(nil, backcolor, "▄")+15)
- :draw(nil, nil, "▄")
- :draw(nil, color, "█▀")
- :draw(0x33B6C0, nil, "▄")
- :draw(nil, backcolor, "█▀");
- (draw({x-8, y+13}, nil, nil, "▀██")
- :draw(nil, color, "▄")
- :draw(0xC3C3C3, nil, "▀▀█")
- :draw(nil, backcolor, "▄▄")+7)
- :draw(nil, nil, "▄▄")
- :draw(nil, color, "█▀▀")
- :draw(0x33B6C0, nil, "▄")
- :draw(nil, backcolor, "██▀");
- draw({x-6, y+14}, nil, nil, "▀▀██")
- :draw(nil, color, "▄▄")
- :draw(0xC3C3C3, nil, "▀▀▀▀▀▀▀▀▀")
- :draw(0x33B6C0, nil, "▄▄")
- :draw(nil, backcolor, "██▀▀");
- gpu.setBackground(backcolor)
- gpu.setForeground(0x33B6C0)
- gpu.set(x-2, y+15, "▀▀█████████▀▀")
- gpu.set(x+2, y+16, "▀▀▀▀▀")
- return x, y
- end
- end
- local function drawChevron(back, color, char, i)
- i=i/2
- local a = 2*math.pi/9*i
- local r = math.sin(a)
- local x, y = r*math.cos(a)*40+x+4, r*r*20+y-2
- gpu.setForeground(color)
- gpu.setBackground(back)
- gpu.set(x-2, y-1, "┌───┐")
- gpu.set(x-2, y, "| |")
- gpu.set(x-2, y+1, "└───┘")
- gpu.setForeground(0xFFFFFF)
- gpu.set(x, y, char)
- end
- local function setState(state)
- local string = "State: "..state
- gpu.setBackground(back)
- gpu.setForeground(0xFFFFFF)
- gpu.fill(x-7, y+8, 23, 1, ' ')
- gpu.set(x-#string/2+4, y+8, string)
- end
- local function drawEnergyCell()
- local amoutEU = 1001047
- local nowEU = stargate.energyAvailable()*20
- --if nowEU < amoutEU/3 then
- gpu.setBackground(0xFF0000)
- gpu.fill(1, 1, 3, 26*nowEU/amoutEU, ' ')
- --elseif nowEU < amoutEU/3 then
- -- gpu.setBackground(0xFF0000)
- -- gpu.fill(1, 1, 3, 26*nowEU/amoutEU, ' ')
- --end
- end
- local function updateHistory(address, type)
- address = address..type
- local file = io.open(path..'history')
- local history = {}
- local i = 0
- local n
- for line in file:lines() do
- i = i + 1
- history[i] = line
- if line == address then
- n = i
- end
- end
- file:close()
- if n then
- table.insert(history, 1, table.remove(history, n))
- else
- table.insert(history, 1, address)
- end
- file = io.open(path..'history', 'w')
- for i = 1, #history do
- file:write(history[i], '\n')
- end
- file:close()
- end
- local function drawAnotherGUI(list, b, n, a)
- gpu.setBackground(0xFFB680)
- gpu.fill(1, 1, 71, 26, ' ')
- for i = 1, #list do
- gpu.set()
- end
- end
- local Iris = button:New{ x = 55, y = 5, w = 17, h = 3, text = "Iris", color = stargate.irisState() == 'Open' and 0x009200 or 0xFF2400 }
- local Dial = button:New{ x = 55, y = 9, w = 17, h = 3, text = "Dial/Disconnect" }
- local History = button:New{ x = 55, y = 13, w = 17, h = 3, text = "History" }
- function drawGUI()
- gpu.setBackground(back)
- gpu.setForeground(0xA5A5A5)
- gpu.fill(1, 1, 71, 26, ' ')
- drawSG(0x004980, back)
- for i = 1, 9 do
- drawChevron(back, 0xFFFFFF, ' ', i)
- end
- setState(stargate.stargateState())
- gpu.set(x-1, y+7, table.concat({ stargate.localAddress():match("(....)(...)(..)") }, '—'))
- if stargate.irisState() == 'Offline' then
- Iris:Draw(-1)
- else
- Iris:Draw(0)
- end
- Dial:Draw(0)
- History:Draw(0)
- --drawEnergyCell()
- end
- drawGUI()
- local exit
- --stargate.dial("x95vfushr")
- while not exit do
- local event = { computer.pullSignal(0.3) }
- if event[1] == 'touch' then
- if event[3] == 71 and event[4] == 1 then
- exit = 1
- end
- local state = stargate.irisState()
- if state ~= 'Offline' and Iris:Detect(event, 'touch') then
- if state == 'Open' then
- Iris.color = 0x009200
- stargate.closeIris()
- Iris:Draw()
- elseif state == 'Closed' then
- Iris.color = 0xFF2400
- stargate.openIris()
- Iris:Draw()
- end
- elseif Dial:Detect(event, 'touch') then
- if stargate.stargateState() == 'Idle' then
- local string = inputWindow("Dial", 0x969696, 0x80, true)
- if string then
- local ok, err = pcall(stargate.dial, string)
- if not ok then
- outputWindow("Dial Error", 0x969696, 0x80, err)
- end
- end
- else
- stargate.disconnect()
- forceDialingAddress = nil
- end
- elseif History:Detect(event, 'touch') then
- drawAnotherGUI()
- end
- elseif event[1] == 'sgDialIn' then
- local address = stargate.remoteAddress()
- updateHistory(address, '+')
- drawSG(0xCC4900, back)
- gpu.setForeground(0xFFFFFF)
- gpu.set(x-1, y+9, table.concat({ address:match("(....)(...)(..)") }, '—'))
- gpu.set(x-5, y+19, 'Incoming Connection')
- elseif event[1] == 'sgDialOut' then
- local address = stargate.remoteAddress()
- updateHistory(address, '-')
- drawSG(0xCC4900, back)
- gpu.setForeground(0xFFFFFF)
- gpu.set(x-1, y+9, table.concat({ address:match("(....)(...)(..)") }, '—'))
- gpu.set(x-6, y+19, 'Outcoming Connection')
- elseif event[1] == 'sgStargateStateChange' then
- if event[3] == 'Closing' then
- for i = 1, 9 do
- drawChevron(back, 0xFF0092, '╳', i)
- end
- gpu.setForeground(0xFFFFFF)
- gpu.fill(x-1, y+9, 11, 1, ' ')
- gpu.fill(x-6, y+19, 20, 1, ' ')
- elseif event[3] == 'Idle' then
- drawSG(0x004980, back)
- for i = 1, 9 do
- drawChevron(back, 0xFFFFFF, ' ', i)
- end
- if forceDialingAddress then
- local ok, err = pcall(stargate.dial, forceDialingAddress)
- if not ok then
- forceDialingAddress = nil
- outputWindow("Force Connection Error", 0x969696, 0x80, err)
- end
- end
- end
- setState(event[3])
- elseif event[1] == 'sgChevronEngaged' then
- if event[3] > 7 then
- event[3] = event[3] - 4
- elseif event[3] > 3 then
- event[3] = event[3] + 2
- end
- drawChevron(back, 0xFF9200, event[4], event[3])
- elseif event[3] == 'sgMessageReceived' then
- end
- end
- return exit
Advertisement
Add Comment
Please, Sign In to add comment