Advertisement
Guest User

button

a guest
Aug 28th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.12 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. mon.setBackgroundColor(colors.black)
  5.  
  6. buttons={}
  7.  
  8.  
  9. function setButton(name, x,y,func)
  10. buttons[name]={}
  11. buttons[name]["x"]=x
  12. buttons[name]["y"]=y
  13. buttons[name]["func"]=func
  14. buttons[name]["active"]=false
  15. end
  16.  
  17. function drawButton(text, color, data)
  18.  mon.setBackgroundColor(color)
  19.  mon.setCursorPos(data["x"],data["y"])
  20.  mon.write(text)
  21.  mon.setBackgroundColor(colors.black)
  22. end
  23.  
  24.  
  25. function screen()
  26.  
  27.  local currColor
  28.  for name, data in pairs(buttons) do
  29.   local on=data["active"]
  30.  if on==true then currColor=colors.lime else currColor=colors.red end
  31.  drawButton(name, currColor, data)
  32. end  
  33. end
  34.  
  35.  
  36. function toggleButton(name)
  37.  buttons[name]["active"]= not buttons[name]["active"]
  38.  screen()
  39. end
  40.  
  41. function flash(name)
  42. toggleButton(name)
  43. screen()
  44. sleep(0.15)
  45. toggleButton(name)
  46. screen()
  47. end
  48.  
  49. function checkxy(x,y)
  50. for name,data in pairs(buttons) do
  51.  local xmax= data["x"]+string.len(name)
  52.  
  53.  if y==data["y"] then
  54.  if x>=data["x"] and x<xmax then
  55.  data["func"]()
  56.  return true
  57.  
  58. end
  59. end
  60. end
  61. return false
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement