Guest User

button

a guest
Jan 25th, 2013
3,385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. local mon = peripheral.wrap("right")
  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.    setTable("OtherButton is COOL!", funcName, 5, 35, 10, 12)
  24. end    
  25.  
  26. function fill(text, color, bData)
  27.    mon.setBackgroundColor(color)
  28.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  29.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  30.    for j = bData["ymin"], bData["ymax"] do
  31.       mon.setCursorPos(bData["xmin"], j)
  32.       if j == yspot then
  33.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  34.             if k == xspot then
  35.                mon.write(text)
  36.             else
  37.                mon.write(" ")
  38.             end
  39.          end
  40.       else
  41.          for i = bData["xmin"], bData["xmax"] do
  42.             mon.write(" ")
  43.          end
  44.       end
  45.    end
  46.    mon.setBackgroundColor(colors.black)
  47. end
  48.      
  49. function screen()
  50.    local currColor
  51.    for name,data in pairs(button) do
  52.       local on = data["active"]
  53.       if on == true then currColor = colors.lime else currColor = colors.red end
  54.       fill(name, currColor, data)
  55.    end
  56. end
  57.      
  58. function checkxy(x, y)
  59.    for name, data in pairs(button) do
  60.       if y>=data["ymin"] and  y <= data["ymax"] then
  61.          if x>=data["xmin"] and x<= data["xmax"] then
  62.             data["func"]()
  63.             data["active"] = not data["active"]
  64.             print(name)
  65.          end
  66.       end
  67.    end
  68. end
  69.      
  70. function heading(text)
  71.    w, h = mon.getSize()
  72.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  73.    mon.write(text)
  74. end
  75.      
  76. fillTable()
  77. while true do
  78.    mon.clear()
  79.    heading("Heading Goes Here")
  80.    screen()
  81.    local e,side,x,y = os.pullEvent("monitor_touch")
  82.    print(x..":"..y)
  83.    checkxy(x,y)
  84.    sleep(.1)
  85. end
Add Comment
Please, Sign In to add comment