xavierlebel

button

Mar 3rd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local mon = peripheral.wrap("top")
  2. mon.setTextScale(1)
  3. mon.setTextColor(colors.white)
  4. local button={}
  5. mon.setBackgroundColor(colors.black)
  6.      
  7. function setTable(name, func, xmin, xmax, ymin, ymax)
  8.    button[name] = {}
  9.    button[name]["func"] = func
  10.    button[name]["active"] = false
  11.    button[name]["xmin"] = xmin
  12.    button[name]["ymin"] = ymin
  13.    button[name]["xmax"] = xmax
  14.    button[name]["ymax"] = ymax
  15. end
  16.  
  17. function funcName()
  18.    print("You clicked buttonText")
  19. end
  20.        
  21. function fillTable()
  22.    setTable("ButtonText", funcName, 5, 25, 4, 8)
  23. end    
  24.  
  25. function fill(text, color, bData)
  26.    mon.setBackgroundColor(color)
  27.    mon.write(text)
  28.    mon.setBackgroundColor(colors.black)
  29. end
  30.      
  31. function screen()
  32.    local currColor
  33.    for name,data in pairs(button) do
  34.       local on = data["active"]
  35.       if on == true then currColor = colors.lime else currColor = colors.gray end
  36.       fill(name, currColor, data)
  37.    end
  38. end
  39.  
  40. function toggleButton(name)
  41.    button[name]["active"] = not button[name]["active"]
  42.    screen()
  43. end    
  44.  
  45. function flash(name)
  46.    toggleButton(name)
  47.    screen()
  48.    sleep(0.15)
  49.    toggleButton(name)
  50.    screen()
  51. end
  52.                                              
  53. function checkxy(x, y)
  54.    for name, data in pairs(button) do
  55.       if y>=data["ymin"] and  y <= data["ymax"] then
  56.          if x>=data["xmin"] and x<= data["xmax"] then
  57.             data["func"]()
  58.             return true
  59.             --data["active"] = not data["active"]
  60.             --print(name)
  61.          end
  62.       end
  63.    end
  64.    return false
  65. end
  66.      
  67. function heading(text)
  68.    w, h = mon.getSize()
  69.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  70.    mon.write(text)
  71. end
  72.      
  73. function label(w, h, text)
  74.    mon.setCursorPos(w, h)
  75.    mon.write(text)
  76. end
Advertisement
Add Comment
Please, Sign In to add comment