Advertisement
Spiker985

Direwolf20 - (Edited) Button API

Jun 12th, 2013
11,854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. --[[Edited by Spiker985]]--
  2. --[[I have added a 2nd parameter to the setTable function be sure to follow the example]]--
  3. --[[If no 2nd parameter is needed leave blank quotation marks, ie ""]]--
  4. --[[edit]]--
  5. local mon = peripheral.find("monitor")
  6. --[[local mon = term]]-- If you want to use this for pocket computers
  7. mon.setTextColor(colors.white)
  8. mon.setBackgroundColor(colors.black)
  9. mon.setTextScale(1)
  10.  
  11. --[[Examples]]--
  12. --function funcName()
  13. --  print("You clicked buttonText")
  14. --end
  15. --function fillTable()
  16. -- setTable("ButtonText", funcName, "param1", "param2", 5, 25, 4, 8)
  17. --end
  18.  
  19. --[[Don't edit anything below this]]--
  20. local button={}
  21.  
  22. function clearTable()
  23.    button = {}
  24. end
  25.      
  26. function setTable(name, func, param1, param2, active, xmin, xmax, ymin, ymax)
  27.    button[name] = {}
  28.    button[name]["func"] = func
  29.    button[name]["active"] = active
  30.    if active == nil then
  31.      active = false
  32.    end
  33.    button[name]["param1"] = param1
  34.    button[name]["param2"] = param2
  35.    button[name]["xmin"] = xmin
  36.    button[name]["ymin"] = ymin
  37.    button[name]["xmax"] = xmax
  38.    button[name]["ymax"] = ymax
  39. end
  40.  
  41. function remTable(name)
  42.   button[name] = {}
  43.   button[name]["xmin"] = 0
  44.   button[name]["ymin"] = 0
  45.   button[name]["xmax"] = 0
  46.   button[name]["ymax"] = 0
  47. end
  48.    
  49.  
  50. function fill(text, color, bData)
  51.    mon.setBackgroundColor(color)
  52.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  53.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  54.    for j = bData["ymin"], bData["ymax"] do
  55.       mon.setCursorPos(bData["xmin"], j)
  56.       if j == yspot then
  57.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  58.             if k == xspot then
  59.                mon.write(text)
  60.             else
  61.                mon.write(" ")
  62.             end
  63.          end
  64.       else
  65.          for i = bData["xmin"], bData["xmax"] do
  66.             mon.write(" ")
  67.          end
  68.       end
  69.    end
  70.    mon.setBackgroundColor(colors.black)
  71. end
  72.      
  73. function screen()
  74.    local currColor
  75.    for name,data in pairs(button) do
  76.       local on = data["active"]
  77.       if on == true then currColor = colors.lime else currColor = colors.red end
  78.       fill(name, currColor, data)
  79.    end
  80. end
  81.  
  82. function toggleButton(name)
  83.    button[name]["active"] = not button[name]["active"]
  84.    screen()
  85. end    
  86.  
  87. function flash(name)
  88.    toggleButton(name)
  89.    screen()
  90.    sleep(0.15)
  91.    toggleButton(name)
  92.    screen()
  93. end
  94.                                              
  95. function checkxy(x, y)
  96.    for name, data in pairs(button) do
  97.       if y>=data["ymin"] and  y <= data["ymax"] then
  98.          if x>=data["xmin"] and x<= data["xmax"] then
  99.             if data["param1"] == "" then
  100.               data["func"]()
  101.             else
  102.               data["func"](data["param1"], data["param2"])
  103.             end
  104.         return true
  105.          end
  106.       end
  107.    end
  108.    return false
  109. end
  110.      
  111. function heading(text)
  112.    w, h = mon.getSize()
  113.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  114.    mon.write(text)
  115. end
  116.      
  117. function label(w, h, text)
  118.    mon.setCursorPos(w, h)
  119.    mon.write(text)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement