Advertisement
Guest User

button

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