Advertisement
SythsGod

Button API

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