Guest User

Button

a guest
Dec 24th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 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 clearTable()
  8.    button = {}
  9.    mon.clear()
  10. end
  11.                
  12. function setTable(name, func, xmin, xmax, ymin, ymax)
  13.    button[name] = {}
  14.    button[name]["func"] = func
  15.    button[name]["active"] = false
  16.    button[name]["xmin"] = xmin
  17.    button[name]["ymin"] = ymin
  18.    button[name]["xmax"] = xmax
  19.    button[name]["ymax"] = ymax
  20. end
  21.  
  22. function LightsOn()
  23.    rs.setOutput("right", false)
  24. end
  25.  
  26. function LightsOff()
  27.    rs.setOutput("left", true)
  28. end
  29.        
  30. function fillTable()
  31.    setTable("LightsOn", LightsOn, 10, 30, 3, 8)
  32. end
  33.  
  34. function fillTable()
  35.    setTable("LightsOff", LightsOff, 40, 60, 3,8)  
  36. end
  37.  
  38. function fill(text, color, bData)
  39.    mon.setBackgroundColor(color)
  40.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  41.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  42.    for j = bData["ymin"], bData["ymax"] do
  43.       mon.setCursorPos(bData["xmin"], j)
  44.       if j == yspot then
  45.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  46.             if k == xspot then
  47.                mon.write(text)
  48.             else
  49.                mon.write(" ")
  50.             end
  51.          end
  52.       else
  53.          for i = bData["xmin"], bData["xmax"] do
  54.             mon.write(" ")
  55.          end
  56.       end
  57.    end
  58.    mon.setBackgroundColor(colors.black)
  59. end
  60.      
  61. function screen()
  62.    local currColor
  63.    for name,data in pairs(button) do
  64.       local on = data["active"]
  65.       if on == true then currColor = colors.lime else currColor = colors.red end
  66.       fill(name, currColor, data)
  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.             data["func"]()
  88.             return true
  89.             --data["active"] = not data["active"]
  90.             --print(name)
  91.          end
  92.       end
  93.    end
  94.    return false
  95. end
  96.      
  97. function heading(text)
  98.    w, h = mon.getSize()
  99.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  100.    mon.write(text)
  101. end
  102.      
  103. function label(w, h, text)
  104.    mon.setCursorPos(w, h)
  105.    mon.write(text)
  106. end
Advertisement
Add Comment
Please, Sign In to add comment