XDjackieXD

onoff.lua

Jul 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 net = component.modem
  9. local running = true
  10.  
  11. -- Colors
  12. local backcolor = 0x000000
  13. local forecolor = 0xFFFFFF
  14.  
  15. local buttonbg = 0x444444
  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. -- Gui
  29. local oldwidth, oldheight = gpu.getResolution()
  30. local oldbgcolor = gpu.getBackground()
  31. local oldfgcolor = gpu.getForeground()
  32. local width = 10
  33. local height = 20
  34. local buttons = {}
  35.  
  36. gpu.setResolution(width, height)
  37. gpu.setForeground(forecolor)
  38. gpu.setBackground(backcolor)
  39.  
  40. local function addButton(x, y, wi, he, name, callback, colorbg, colortext)
  41.   buttons[#buttons + 1] = {x, y, wi, he, name, callback, colorbg, colortext}
  42. end
  43.  
  44. local function drawButton(x, y, wi, he, text, buttonbgcolor, buttontextcolor)
  45.   gpu.setBackground(buttonbgcolor)
  46.   gpu.setForeground(buttontextcolor)
  47.  
  48.   gpu.fill(x, y, wi, he, " ")
  49.   gpu.set(x+(wi/2)-(string.len(text)/2) , y+(he/2), text)
  50.  
  51.   gpu.setBackground(backcolor)
  52.   gpu.setForeground(forecolor)
  53. end
  54.  
  55. local function drawScreen()
  56.   term.clear()
  57.   for i=1, #buttons do
  58.     drawButton(buttons[i][1], buttons[i][2], buttons[i][3], buttons[i][4], buttons[i][5], buttons[i][7], buttons[i][8])
  59.   end
  60. end
  61.  
  62. local function clickCallback(x, y, player)
  63.   for i=1, #buttons do
  64.     if x >= buttons[i][1] and x < buttons[i][1]+buttons[i][3] then
  65.       if y >= buttons[i][2] and y < buttons[i][2]+buttons[i][4] then
  66.         buttons[i][6](buttons[i], player)
  67.       end
  68.     end
  69.   end
  70. end
  71.  
  72.  
  73.  
  74. -- Callbacks
  75. local function touchCallback(name, address, x, y, button, player)
  76.   clickCallback(x, y, player)
  77. end
  78.  
  79. local function onCallback(button, player)
  80.   net.broadcast(123, "on")
  81.   drawButton(button[1], button[2], button[3], button[4], button[5], 0x9D0000, button[8])
  82.   os.sleep(0.5)
  83.   drawScreen()
  84. end
  85.  
  86. local function offCallback(button, player)
  87.   net.broadcast(123, "off")
  88.   drawButton(button[1], button[2], button[3], button[4], button[5], 0x9D0000, button[8])
  89.   os.sleep(0.5)
  90.   drawScreen()
  91. end
  92.  
  93.  
  94. -- Main Code
  95.  
  96. addButton(1, 1, 10, 9, "On", onCallback, buttonbg, buttontext)
  97. addButton(1, 12, 10, 9, "Off", offCallback, buttonbg, buttontext)
  98.  
  99. net.open(123)
  100.  
  101. event.listen("touch", touchCallback)
  102. drawScreen()
  103.  
  104. while running do
  105.   os.sleep(10)
  106. end
Add Comment
Please, Sign In to add comment