Advertisement
Plazter

no idea RFTOOL TP

Mar 11th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. component = require("component")
  2. dial = component.rftools_dialing_device
  3. event = require("event")
  4. gpu = component.gpu
  5. term = require("term")
  6. keyboard = require("keyboard")
  7.  
  8. colors = { black = 0x000000, white = 0xf8f8ff, blue = 0x0000ff, lightGray = 0xd9d9d9, red = 0xff0000, purple = 0x9b30ff, carrot = 0xffa500, magenta = 0xcd00cd, lightBlue = 0x87cefa, yellow = 0xffff00, lime = 0x32cd32, pink = 0xffc0cb, gray = 0x696969, brown = 0x8b4500, green = 0x006400, cyan = 0x008b8b, olive = 0x6b8e23, gold = 0x8b6914, orangered = 0xdb4e02, diamond = 0x0fa7c7, crimson = 0xaf002a, fuchsia = 0xfd3f92, folly = 0xff004f, frenchBlue = 0x0072bb, lilac = 0x86608e, flax = 0xeedc82, darkGray = 0x563c5c, englishGreen = 0x1b4d3e, eggplant = 0x614051, deepPink  = 0xff1493, ruby = 0x843f5b, orange = 0xf5c71a, lemon = 0xffd300, darkBlue = 0x002e63, bitterLime = 0xbfff00 }
  9.  
  10. blackListedDestinations = {"Mine"}
  11.  
  12. destinations = {}
  13. origin = nil
  14.  
  15. function init()
  16.     if not component.isAvailable("gpu") then io.stderr:write("Program needs a GPU to run"); os.exit() end
  17.     gpu.setResolution(gpu.maxResolution())
  18.     maxWidth, maxHeight = gpu.getResolution()
  19.     term.clear()
  20.     getMachines()
  21.     component.screen.setTouchModeInverted(true)
  22. end
  23.  
  24. function getMachines()
  25.     destinations = checkReceivers()
  26.     origin = getHere()
  27. end
  28.  
  29. function checkReceivers()
  30.     local receivers = dial.getReceivers()
  31.     for k,v in pairs(receivers) do
  32.         if type(receivers[k]) ~= "table" then
  33.             receivers[k] = nil
  34.         end
  35.     end
  36.     for i = #receivers, 1, -1 do
  37.         for j = 1, #blackListedDestinations do
  38.             if receivers[i].name == blackListedDestinations[j] then
  39.                 table.remove(receivers, i)
  40.                 break
  41.             end
  42.         end
  43.     end
  44.     return receivers
  45. end
  46.  
  47. function getHere()
  48.     return dial.getTransmitters()[1].position
  49. end
  50.  
  51. function display(destinationList)
  52.     local headerText = "Possible destinations:\n"
  53.     do --get optimal resolution
  54.         widestWidth = string.len(headerText)
  55.         for k,v in pairs(destinationList) do
  56.             if string.len(v.name) > widestWidth then widestWidth = string.len(v.name) end
  57.         end
  58.         width = widestWidth + 2
  59.         height = math.min(#destinationList + 2, maxHeight)
  60.         gpu.setResolution(width, height)
  61.     end
  62.     term.clear()
  63.     term.setCursor(1,1)
  64.     io.stderr:write(headerText)
  65.     for k,v in ipairs(destinationList) do
  66.         io.stdout:write(v.name .. "\n")
  67.         if k > height - 2 then break end
  68.     end
  69.     term.setCursor(1, height)
  70.     term.write("[[Refresh]]")
  71. end
  72.  
  73. function selectDestination(destinationList, number)
  74.     dial.dial(origin, destinationList[number].position, 0, true)
  75.     os.sleep(5)
  76.     dial.interrupt(origin)
  77. end
  78.  
  79. function debug(text)
  80.     print(text); os.exit()
  81. end
  82.  
  83. function checkClick(destinationList)
  84.     local e, _, x, y, _, _ = event.pull(5, "touch")
  85.     if e == nil then return end
  86.     do --we pushed a screen
  87.         local function hasPushedLine(line)
  88.             if line == 0 then return false end
  89.             return true
  90.         end
  91.        
  92.         if hasPushedLine(y) then --we have pushed something ~= header
  93.             if y == height then
  94.                 getMachines()
  95.                 display(destinations)
  96.             else
  97.                 selectDestination(destinationList, y - 1)
  98.             end
  99.         end
  100.     end
  101. end
  102.  
  103. init()
  104. display(destinations)
  105. repeat
  106.     checkClick(destinations)
  107. until keyboard.isControlDown()
  108. term.clear()
  109. gpu.setResolution(gpu.maxResolution())
  110. component.screen.setTouchModeInverted(false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement