Guest User

button1 API

a guest
Jun 14th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. local button={}
  5. mon.setBackgroundColor(colors.black)
  6.  
  7. function clearTable()
  8.     button = {}
  9. end
  10.  
  11. function setTable(name, func, param, xmin, xmax, ymin, ymax)
  12.     button[name] = {}
  13.     button[name]["func"] = func
  14.     button[name]["param"] = param
  15.     button[name]["active"] = false
  16.     button[name]["xmin"] = xmin
  17.     button[name]["ymin"] = ymin
  18.     button[name]["xmax"] = xmax
  19.     button[name]["ymax"] = ymax
  20. end
  21. function funcName()
  22.     print("You clicked buttonText")
  23. end
  24.  
  25. function fillTable()
  26.     setTable("ButtonText", funcName, 5, 25, 4, 8)
  27. end    
  28. function fill(text, color, bData)
  29.     mon.setBackgroundColor(color)
  30.     local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  31.     local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2)
  32.     for j = bData["ymin"], bData["ymax"] do
  33.         mon.setCursorPos(bData["xmin"], j)
  34.         if j == yspot then
  35.             for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  36.                 if k == xspot then
  37.                     mon.write(text)
  38.                     else
  39.                     mon.write(" ")
  40.                 end
  41.             end
  42.             else
  43.             for i = bData["xmin"], bData["xmax"] do
  44.                 mon.write(" ")
  45.             end
  46.         end
  47.     end
  48.     mon.setBackgroundColor(colors.black)
  49. end
  50.  
  51. function screen()
  52.     local currColor
  53.     for name,data in pairs(button) do
  54.         local on = data["active"]
  55.         if on == true then currColor = colors.lime else currColor = colors.red end
  56.         fill(name, currColor, data)
  57.     end
  58. end
  59. function toggleButton(name)
  60.     button[name]["active"] = not button[name]["active"]
  61.     screen()
  62. end    
  63. function flash(name)
  64.     toggleButton(name)
  65.     screen()
  66.     sleep(0.15)
  67.     toggleButton(name)
  68.     screen()
  69. end
  70.  
  71. function checkxy(x, y)
  72.     for name, data in pairs(button) do
  73.         if y>=data["ymin"] and  y <= data["ymax"] then
  74.             if x>=data["xmin"] and x<= data["xmax"] then
  75.                 data["func"](data["param"])
  76.                 return true
  77.                 --data["active"] = not data["active"]
  78.                 --print(name)
  79.             end
  80.         end
  81.     end
  82.     return false
  83. end
  84.  
  85. function heading(text)
  86.     w, h = mon.getSize()
  87.     mon.setCursorPos((w-string.len(text))/2+1, 1)
  88.     mon.write(text)
  89. end
  90.  
  91. function label(w, h, text)
  92.     mon.setCursorPos(w, h)
  93.     mon.write(text)
  94. end
Advertisement
Add Comment
Please, Sign In to add comment