sataninchen

button reactor

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