1ng0

CC - Reactor Controler button - by lezchap

Dec 2nd, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. --slightly modified from the Button API from Direwolf20 (youtube.com/watch?v=l7ZwSFVYITU)
  2.  
  3. local mon = peripheral.find("monitor")
  4. mon.setTextScale(1)
  5. mon.setTextColor(colors.white)
  6. local button={}
  7. mon.setBackgroundColor(colors.black)
  8.  
  9. function clearTable()
  10.    button = {}
  11. end
  12.  
  13. function setButton(name, buttonOn)
  14.    print(name)
  15.    print(button[name]["active"])
  16.    button[name]["active"] = buttonOn
  17.    screen()
  18. end
  19.                                              
  20. function setTable(name, func, param, xmin, xmax, ymin, ymax)
  21.    button[name] = {}
  22.    button[name]["func"] = func
  23.    button[name]["active"] = false
  24.    button[name]["param"] = param
  25.    button[name]["xmin"] = xmin
  26.    button[name]["ymin"] = ymin
  27.    button[name]["xmax"] = xmax
  28.    button[name]["ymax"] = ymax
  29. end
  30.  
  31. function funcName()
  32.    print("You clicked buttonText")
  33. end
  34.        
  35. function fillTable()
  36.    setTable("ButtonText", funcName, 5, 25, 4, 8)
  37. end    
  38.  
  39. function fill(text, color, bData)
  40.    mon.setBackgroundColor(color)
  41.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  42.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  43.    for j = bData["ymin"], bData["ymax"] do
  44.       mon.setCursorPos(bData["xmin"], j)
  45.       if j == yspot then
  46.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  47.             if k == xspot then
  48.                mon.write(text)
  49.             else
  50.                mon.write(" ")
  51.             end
  52.          end
  53.       else
  54.          for i = bData["xmin"], bData["xmax"] do
  55.             mon.write(" ")
  56.          end
  57.       end
  58.    end
  59.    mon.setBackgroundColor(colors.black)
  60. end
  61.      
  62. function screen()
  63.    local currColor
  64.    for name,data in pairs(button) do
  65.       local on = data["active"]
  66.       if on == true then currColor = colors.lime else currColor = colors.red end
  67.       fill(name, currColor, data)
  68.    end
  69. end
  70.  
  71. function toggleButton(name)
  72.    button[name]["active"] = not button[name]["active"]
  73.    screen()
  74. end    
  75.  
  76. function flash(name)
  77.    toggleButton(name)
  78.    screen()
  79.    sleep(0.15)
  80.    toggleButton(name)
  81.    screen()
  82. end
  83.                                              
  84. function checkxy(x, y)
  85.    for name, data in pairs(button) do
  86.       if y>=data["ymin"] and  y <= data["ymax"] then
  87.          if x>=data["xmin"] and x<= data["xmax"] then
  88.             if data["param"] == "" then
  89.               data["func"]()
  90.             else
  91.               data["func"](data["param"])
  92.             end
  93.             return true
  94.             --data["active"] = not data["active"]
  95.             --print(name)
  96.          end
  97.       end
  98.    end
  99.    return false
  100. end
  101.      
  102. function heading(text)
  103.    w, h = mon.getSize()
  104.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  105.    mon.write(text)
  106. end
  107.      
  108. function label(w, h, text)
  109.    mon.setCursorPos(w, h)
  110.    mon.write(text)
  111. end
Add Comment
Please, Sign In to add comment