Advertisement
XDjackieXD

tpgui.lua

Jul 2nd, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. local unicode = require('unicode')
  2. local event = require('event')
  3. local term = require('term')
  4. local shell=require("shell")
  5. local fs = require('filesystem')
  6. local component = require('component')
  7. local gpu = component.gpu
  8. local debug = component.debug
  9. local running = true
  10.  
  11. -- Colors
  12. local backcolor = 0x444444
  13. local forecolor = 0xFFFFFF
  14.  
  15. local buttonbg = 0x000000
  16. local buttonselectedbg = 0x626262
  17. local buttontext = 0xC5C8C6
  18.  
  19. local selection = 0xF0544C
  20.  
  21. local infocolor = 0x0066FF
  22. local errorcolor = 0xFF0000
  23.  
  24.  
  25. -- Constants
  26. local myname = "XDjackieXD"
  27.  
  28. -- Quickdial
  29. local qdPlayers = {"Ellpeck", "canitzp", "_Inari", "unascribed", "Vexatos", "Chocohead", "Aesen"}
  30. local qdPos = {}
  31. qdPos["MyBooth"] = {458, 44, -317}
  32. qdPos["EllpeckBooth"] = {404, 44, -271}
  33.  
  34.  
  35. -- Gui
  36. local oldwidth, oldheight = gpu.getResolution()
  37. local oldbgcolor = gpu.getBackground()
  38. local oldfgcolor = gpu.getForeground()
  39. local width, height = gpu.maxResolution()
  40. local buttons = {}
  41.  
  42. gpu.setResolution(width, height)
  43. gpu.setForeground(forecolor)
  44. gpu.setBackground(backcolor)
  45.  
  46. local function addButton(x, y, wi, he, name, callback)
  47.   buttons[#buttons + 1] = {x, y, wi, he, name, callback}
  48. end
  49.  
  50. local function drawButton(x, y, wi, he, text)
  51.   gpu.setBackground(buttonbg)
  52.   gpu.setForeground(buttontext)
  53.  
  54.   gpu.fill(x, y, wi, he, " ")
  55.   gpu.set(x+(wi/2)-(string.len(text)/2) , y+(he/2), text)
  56.  
  57.   gpu.setBackground(backcolor)
  58.   gpu.setForeground(forecolor)
  59. end
  60.  
  61. local function drawScreen()
  62.   term.clear()
  63.   for i=1, #buttons do
  64.     drawButton(buttons[i][1], buttons[i][2], buttons[i][3], buttons[i][4], buttons[i][5])
  65.   end
  66. end
  67.  
  68. local function clickCallback(x, y, player)
  69.   for i=1, #buttons do
  70.     if x >= buttons[i][1] and x < buttons[i][1]+buttons[i][3] then
  71.       if y >= buttons[i][2] and y < buttons[i][2]+buttons[i][4] then
  72.         buttons[i][6](buttons[i][5], player)
  73.       end
  74.     end
  75.   end
  76. end
  77.  
  78.  
  79.  
  80. -- Callbacks
  81. local function touchCallback(name, address, x, y, button, player)
  82.   clickCallback(x, y, player)
  83. end
  84.  
  85. local function tabCompletionCallback(line, pos)
  86.   local players = debug.getPlayers()
  87.   local matches = {}
  88.   for i=1, #players do
  89.     if string.sub(players[i],1,string.len(line))==line then
  90.       table.insert(matches, players[i])
  91.     end
  92.   end
  93.   return matches
  94. end
  95.  
  96. local function exitCallback(name, player)
  97.   event.ignore("touch", touchCallback)
  98.   gpu.setBackground(oldbgcolor)
  99.   gpu.setForeground(oldfgcolor)
  100.   gpu.setResolution(oldwidth, oldheight)
  101.   term.clear()
  102.   running = false
  103.   os.exit()
  104. end
  105.  
  106. local function tpPlayerCallback(n2, n1)
  107.   if n1 ~= nil and n2 ~= nil then
  108.     local p1 = debug.getPlayer(n1)
  109.     local p2 = debug.getPlayer(n2)
  110.     local players = debug.getPlayers()
  111.     local f1, f2 = false, false
  112.  
  113.     for i, v in ipairs(players) do
  114.       if v == n1 then f1 = true end
  115.       if v == n2 then f2 = true end
  116.     end
  117.  
  118.     if f1 and f2 then
  119.       local posX, posY, posZ = p2.getPosition()
  120.       p1.setPosition(posX, posY, posZ)
  121.     else
  122.       gpu.setForeground(errorcolor)
  123.       local xpos, ypos = term.getCursor()
  124.       term.setCursor(width-18, height)
  125.       term.write("Player not found!")
  126.       term.setCursor(xpos, ypos)
  127.       gpu.setForeground(forecolor)
  128.     end
  129.   end
  130. end
  131.  
  132. local function tpPosCallback(name, player)
  133.   if qdPos[name] ~= nil then
  134.     local p1 = debug.getPlayer(player)
  135.     p1.setPosition(qdPos[name][1], qdPos[name][2], qdPos[name][3])
  136.   end
  137. end
  138.  
  139.  
  140. -- Main Code
  141.  
  142. local maxlength = 0
  143. for i=1, #qdPlayers do
  144.   if string.len(qdPlayers[i]) > maxlength then
  145.     maxlength = string.len(qdPlayers[i])
  146.   end
  147. end
  148. for k, v in pairs(qdPos) do
  149.   if string.len(k) > maxlength then
  150.     maxlength = string.len(k)
  151.   end
  152. end
  153. maxlength = maxlength+2
  154.  
  155. local row = 0
  156. local col = 0
  157. for i=1, #qdPlayers do
  158.   addButton(2+(col*(maxlength+1)), 2+(row*4), maxlength, 3, qdPlayers[i], tpPlayerCallback)
  159.  
  160.   col = col+1
  161.   if 2+(col*(maxlength+2)) > width then
  162.     col = 0
  163.     row = row+1
  164.   end
  165. end
  166. for k,v in pairs(qdPos) do
  167.   addButton(2+(col*(maxlength+1)), 2+(row*4), maxlength, 3, k, tpPosCallback)
  168.  
  169.   col = col+1
  170.   if 2+(col*(maxlength+2)) > width then
  171.     col = 0
  172.     row = row+1
  173.   end
  174. end
  175.  
  176. --addButton(2, 2, maxlength, 3, "Exit", exitCallback)
  177.  
  178. event.listen("touch", touchCallback)
  179.  
  180. while running do
  181.   drawScreen()
  182.   term.setCursor(1, height)
  183.   term.clearLine()
  184.   term.write(">")
  185.   player = string.sub(term.read({}, false, tabCompletionCallback), 1, -2)
  186.   if player == "exit" then
  187.     exitCallback("", "")
  188.   else
  189.     tpPlayerCallback(player, myname)
  190.   end
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement