Advertisement
Guest User

button

a guest
Sep 21st, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 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.    mon.clear()
  19.    mon.setCursorPos(1,1)
  20.    mon.write("Whale.")
  21.    sleep(5)
  22. end
  23.        
  24. function fillTable()
  25.    setTable("Emerald Kit", funcName, 5, 35, 10, 12)
  26. end    
  27.  
  28. function fill(text, color, bData)
  29.    mon.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.       mon.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.                mon.write(text)
  38.             else
  39.                mon.write(" ")
  40.             end
  41.          end
  42.       else
  43.          for i = bData["xmin"], bData["xmax"] do
  44.             mon.write(" ")
  45.          end
  46.       end
  47.    end
  48.    mon.setBackgroundColor(colors.black)
  49. end
  50.      
  51. function screen()
  52.    local currColor
  53.    for name,data in pairs(button) do
  54.       local on = data["active"]
  55.       if on == true then currColor = colors.lime else currColor = colors.red end
  56.       fill(name, currColor, data)
  57.    end
  58. end
  59.      
  60. function checkxy(x, y)
  61.    for name, data in pairs(button) do
  62.       if y>=data["ymin"] and  y <= data["ymax"] then
  63.          if x>=data["xmin"] and x<= data["xmax"] then
  64.             data["func"]()
  65.             data["active"] = not data["active"]
  66.             print(name)
  67.          end
  68.       end
  69.    end
  70. end
  71.      
  72. function heading(text)
  73.    w, h = mon.getSize()
  74.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  75.    mon.write(text)
  76. end
  77.      
  78. fillTable()
  79. while true do
  80.    mon.clear()
  81.    mon.setTextColour(32)
  82.    heading("Emerald Rank")
  83.    screen()
  84.    local e,side,x,y = os.pullEvent("monitor_touch")
  85.    print(x..":"..y)
  86.    checkxy(x,y)
  87.    sleep(.1)
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement