Advertisement
Guest User

button

a guest
Jul 28th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. local button={}
  5. --local button2={]
  6. mon.setBackgroundColor(colors.black)
  7.  
  8. function clearTable()
  9.    button = {}
  10.    mon.clear()
  11. end
  12. --function clearTable2()
  13. --  button2 = {}
  14. --  mon.clear()
  15.                
  16. function setTable(name, func, slot)
  17.    button[name] = {}
  18.    button[name]["func"] = func
  19.    button[name]["active"] = false
  20.    button[name]["xmin"] = xmin
  21.    button[name]["ymin"] = ymin
  22.    button[name]["xmax"] = xmax
  23.    button[name]["ymax"] = ymax
  24.    button[name]["slot"] = slot
  25. end
  26.  
  27. if
  28.  
  29. function setTable2(name, func, xmin, xmax, ymin, ymax)
  30.    button2[name] = {}
  31.    button2[name]["func"] = func
  32.    button2[name]["active"] = false
  33.    button2[name]["xmin"] = xmin
  34.    button2[name]["ymin"] = ymin
  35.    button2[name]["xmax"] = xmax
  36.    button2[name]["ymax"] = ymax
  37. end  
  38.  
  39. function funcName()
  40.    print("You clicked buttonText")
  41. end
  42.        
  43. function fillTable()
  44.    setTable("ButtonText", funcName, 5, 25, 4, 8)
  45. end    
  46.  
  47. function fillTable2()
  48.   setTable("ButtonText", funcName, 5, 25, 4, 8)
  49. end
  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.             data["func"]()
  100.             return true
  101.             --data["active"] = not data["active"]
  102.             --print(name)
  103.          end
  104.       end
  105.    end
  106.    return false
  107. end
  108.      
  109. function heading(text)
  110.    w, h = mon.getSize()
  111.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  112.    mon.write(text)
  113. end
  114.      
  115. function label(w, h, text)
  116.    mon.setCursorPos(w, h)
  117.    mon.write(text)
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement