Advertisement
Noobular

Button API (for Toggle)

Mar 30th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. os.loadAPI("config")
  2. local m2 = peripheral.wrap(config.Controller) -- Monitor with Controller (same as toggle)
  3. --------------------------------------------------------------------------------------------
  4. --------------------------------------------------------------------------------------------
  5. m2.setTextScale(1)
  6. m2.setTextColor(colors.white)
  7. local button={}
  8. m2.setBackgroundColor(colors.black)
  9. --------------------------------------------------------------------------------------------
  10. --------------------------------------------------------------------------------------------
  11. function clearTable()
  12.    button = {}
  13.    m2.clear()
  14. end
  15. --------------------------------------------------------------------------------------------
  16. --------------------------------------------------------------------------------------------              
  17. function setTable(name, func, xmin, xmax, ymin, ymax)
  18.    button[name] = {}
  19.    button[name]["func"] = func
  20.    button[name]["active"] = false
  21.    button[name]["xmin"] = xmin
  22.    button[name]["ymin"] = ymin
  23.    button[name]["xmax"] = xmax
  24.    button[name]["ymax"] = ymax
  25. end
  26. --------------------------------------------------------------------------------------------
  27. --------------------------------------------------------------------------------------------
  28. function fill(text, color, bData)
  29.    m2.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) +1
  32.    for j = bData["ymin"], bData["ymax"] do
  33.       m2.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.                m2.write(text)
  38.             else
  39.                m2.write(" ")
  40.             end
  41.          end
  42.       else
  43.          for i = bData["xmin"], bData["xmax"] do
  44.             m2.write(" ")
  45.          end
  46.       end
  47.    end
  48.    m2.setBackgroundColor(colors.black)
  49. end
  50. --------------------------------------------------------------------------------------------
  51. --------------------------------------------------------------------------------------------    
  52. function screen()
  53.    local currColor
  54.    for name,data in pairs(button) do
  55.       local on = data["active"]
  56.       if on == true then currColor = colors.lime else currColor = colors.cyan end
  57.       fill(name, currColor, data)
  58.    end
  59. end
  60. --------------------------------------------------------------------------------------------
  61. --------------------------------------------------------------------------------------------
  62. function toggleButton(name)
  63.    button[name]["active"] = not button[name]["active"]
  64.    screen()
  65. end    
  66. --------------------------------------------------------------------------------------------
  67. --------------------------------------------------------------------------------------------
  68. function flash(name)
  69.    toggleButton(name)
  70.    screen()
  71.    sleep(0.15)
  72.    toggleButton(name)
  73.    screen()
  74. end
  75. --------------------------------------------------------------------------------------------
  76. --------------------------------------------------------------------------------------------                                            
  77. function checkxy(x, y)
  78.    for name, data in pairs(button) do
  79.       if y>=data["ymin"] and  y <= data["ymax"] then
  80.          if x>=data["xmin"] and x<= data["xmax"] then
  81.             data["func"]()
  82.             return true
  83.             --data["active"] = not data["active"]
  84.             --print(name)
  85.          end
  86.       end
  87.    end
  88.    return false
  89. end
  90. --------------------------------------------------------------------------------------------
  91. --------------------------------------------------------------------------------------------    
  92. function heading(text)
  93.    w, h = m2.getSize()
  94.    m2.setCursorPos((w-string.len(text))/2+1, 1)
  95.    m2.write(text)
  96. end
  97. --------------------------------------------------------------------------------------------
  98. --------------------------------------------------------------------------------------------    
  99. function label(w, h, text)
  100.    m2.setCursorPos(w, h)
  101.    m2.write(text)
  102. end
  103. --------------------------------------------------------------------------------------------
  104. --------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement